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 | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 79 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 80 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 81 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 82 | #include <linux/io_uring.h> |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 83 | #include <linux/blk-cgroup.h> |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 84 | #include <linux/audit.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 86 | #define CREATE_TRACE_POINTS |
| 87 | #include <trace/events/io_uring.h> |
| 88 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | #include <uapi/linux/io_uring.h> |
| 90 | |
| 91 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 92 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 93 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 94 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 99 | */ |
| 100 | #define IORING_FILE_TABLE_SHIFT 9 |
| 101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 105 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 106 | |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 107 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 109 | IOSQE_BUFFER_SELECT) |
| 110 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 111 | struct io_uring { |
| 112 | u32 head ____cacheline_aligned_in_smp; |
| 113 | u32 tail ____cacheline_aligned_in_smp; |
| 114 | }; |
| 115 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 116 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 117 | * This data is shared with the application through the mmap at offsets |
| 118 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 119 | * |
| 120 | * The offsets to the member fields are published through struct |
| 121 | * io_sqring_offsets when calling io_uring_setup. |
| 122 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | /* |
| 125 | * Head and tail offsets into the ring; the offsets need to be |
| 126 | * masked to get valid indices. |
| 127 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 129 | * and the application controls tail of the sq ring and the head of the |
| 130 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 132 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 133 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 134 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 135 | * ring_entries - 1) |
| 136 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 137 | u32 sq_ring_mask, cq_ring_mask; |
| 138 | /* Ring sizes (constant, power of 2) */ |
| 139 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Number of invalid entries dropped by the kernel due to |
| 142 | * invalid index stored in array |
| 143 | * |
| 144 | * Written by the kernel, shouldn't be modified by the |
| 145 | * application (i.e. get number of "new events" by comparing to |
| 146 | * cached value). |
| 147 | * |
| 148 | * After a new SQ head value was read by the application this |
| 149 | * counter includes all submissions that were dropped reaching |
| 150 | * the new SQ head (and possibly more). |
| 151 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 152 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 154 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 155 | * |
| 156 | * Written by the kernel, shouldn't be modified by the |
| 157 | * application. |
| 158 | * |
| 159 | * The application needs a full memory barrier before checking |
| 160 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 161 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 162 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 163 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 164 | * Runtime CQ flags |
| 165 | * |
| 166 | * Written by the application, shouldn't be modified by the |
| 167 | * kernel. |
| 168 | */ |
| 169 | u32 cq_flags; |
| 170 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 171 | * Number of completion events lost because the queue was full; |
| 172 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 173 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 174 | * the completion queue. |
| 175 | * |
| 176 | * Written by the kernel, shouldn't be modified by the |
| 177 | * application (i.e. get number of "new events" by comparing to |
| 178 | * cached value). |
| 179 | * |
| 180 | * As completion events come in out of order this counter is not |
| 181 | * ordered with any other data. |
| 182 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 183 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 184 | /* |
| 185 | * Ring buffer of completion events. |
| 186 | * |
| 187 | * The kernel writes completion events fresh every time they are |
| 188 | * produced, so the application is allowed to modify pending |
| 189 | * entries. |
| 190 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 191 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 194 | enum io_uring_cmd_flags { |
| 195 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 196 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 199 | struct io_mapped_ubuf { |
| 200 | u64 ubuf; |
| 201 | size_t len; |
| 202 | struct bio_vec *bvec; |
| 203 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 204 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 207 | struct io_ring_ctx; |
| 208 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 209 | struct io_rsrc_put { |
| 210 | struct list_head list; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 211 | union { |
| 212 | void *rsrc; |
| 213 | struct file *file; |
| 214 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | struct fixed_rsrc_table { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 218 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 221 | struct fixed_rsrc_ref_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 222 | struct percpu_ref refs; |
| 223 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 224 | struct list_head rsrc_list; |
| 225 | struct fixed_rsrc_data *rsrc_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 226 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 227 | struct io_rsrc_put *prsrc); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 228 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 229 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 230 | }; |
| 231 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 232 | struct fixed_rsrc_data { |
| 233 | struct fixed_rsrc_table *table; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 234 | struct io_ring_ctx *ctx; |
| 235 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 236 | struct fixed_rsrc_ref_node *node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 237 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 238 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 239 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 242 | struct io_buffer { |
| 243 | struct list_head list; |
| 244 | __u64 addr; |
| 245 | __s32 len; |
| 246 | __u16 bid; |
| 247 | }; |
| 248 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 249 | struct io_restriction { |
| 250 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 251 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 252 | u8 sqe_flags_allowed; |
| 253 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 254 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 255 | }; |
| 256 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 257 | struct io_sq_data { |
| 258 | refcount_t refs; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 259 | struct mutex lock; |
| 260 | |
| 261 | /* ctx's that are using this sqd */ |
| 262 | struct list_head ctx_list; |
| 263 | struct list_head ctx_new_list; |
| 264 | struct mutex ctx_lock; |
| 265 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 266 | struct task_struct *thread; |
| 267 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 268 | |
| 269 | unsigned sq_thread_idle; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 270 | }; |
| 271 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 272 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 273 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 274 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 275 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 276 | |
| 277 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 278 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 279 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 280 | unsigned int locked_free_nr; |
| 281 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 282 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 283 | /* IRQ completion list, under ->completion_lock */ |
| 284 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 285 | }; |
| 286 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 287 | struct io_submit_link { |
| 288 | struct io_kiocb *head; |
| 289 | struct io_kiocb *last; |
| 290 | }; |
| 291 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 292 | struct io_submit_state { |
| 293 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 294 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * io_kiocb alloc cache |
| 298 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 299 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 300 | unsigned int free_reqs; |
| 301 | |
| 302 | bool plug_started; |
| 303 | |
| 304 | /* |
| 305 | * Batch completion logic |
| 306 | */ |
| 307 | struct io_comp_state comp; |
| 308 | |
| 309 | /* |
| 310 | * File reference cache |
| 311 | */ |
| 312 | struct file *file; |
| 313 | unsigned int fd; |
| 314 | unsigned int file_refs; |
| 315 | unsigned int ios_left; |
| 316 | }; |
| 317 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 318 | struct io_ring_ctx { |
| 319 | struct { |
| 320 | struct percpu_ref refs; |
| 321 | } ____cacheline_aligned_in_smp; |
| 322 | |
| 323 | struct { |
| 324 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 325 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 326 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 327 | unsigned int cq_overflow_flushed: 1; |
| 328 | unsigned int drain_next: 1; |
| 329 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 330 | unsigned int restricted: 1; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 331 | unsigned int sqo_dead: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 332 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 333 | /* |
| 334 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 335 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 336 | * |
| 337 | * This indirection could e.g. be used to assign fixed |
| 338 | * io_uring_sqe entries to operations and only submit them to |
| 339 | * the queue when needed. |
| 340 | * |
| 341 | * The kernel modifies neither the indices array nor the entries |
| 342 | * array. |
| 343 | */ |
| 344 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 345 | unsigned cached_sq_head; |
| 346 | unsigned sq_entries; |
| 347 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 348 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 349 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 350 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 351 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 352 | |
| 353 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 354 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 355 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 356 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 357 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 358 | } ____cacheline_aligned_in_smp; |
| 359 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 360 | struct { |
| 361 | struct mutex uring_lock; |
| 362 | wait_queue_head_t wait; |
| 363 | } ____cacheline_aligned_in_smp; |
| 364 | |
| 365 | struct io_submit_state submit_state; |
| 366 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 367 | struct io_rings *rings; |
| 368 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 369 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 370 | struct io_wq *io_wq; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * For SQPOLL usage - we hold a reference to the parent task, so we |
| 374 | * have access to the ->files |
| 375 | */ |
| 376 | struct task_struct *sqo_task; |
| 377 | |
| 378 | /* Only used for accounting purposes */ |
| 379 | struct mm_struct *mm_account; |
| 380 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 381 | #ifdef CONFIG_BLK_CGROUP |
| 382 | struct cgroup_subsys_state *sqo_blkcg_css; |
| 383 | #endif |
| 384 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 385 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 386 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 387 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 388 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 389 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 390 | /* |
| 391 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 392 | * readers must ensure that ->refs is alive as long as the file* is |
| 393 | * used. Only updated through io_uring_register(2). |
| 394 | */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 395 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 396 | unsigned nr_user_files; |
| 397 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 398 | /* if used, fixed mapped user buffers */ |
| 399 | unsigned nr_user_bufs; |
| 400 | struct io_mapped_ubuf *user_bufs; |
| 401 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 402 | struct user_struct *user; |
| 403 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 404 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 405 | |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 406 | #ifdef CONFIG_AUDIT |
| 407 | kuid_t loginuid; |
| 408 | unsigned int sessionid; |
| 409 | #endif |
| 410 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 411 | struct completion ref_comp; |
| 412 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 413 | |
| 414 | #if defined(CONFIG_UNIX) |
| 415 | struct socket *ring_sock; |
| 416 | #endif |
| 417 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 418 | struct idr io_buffer_idr; |
| 419 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 420 | struct idr personality_idr; |
| 421 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 422 | struct { |
| 423 | unsigned cached_cq_tail; |
| 424 | unsigned cq_entries; |
| 425 | unsigned cq_mask; |
| 426 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 427 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 428 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 429 | struct wait_queue_head cq_wait; |
| 430 | struct fasync_struct *cq_fasync; |
| 431 | struct eventfd_ctx *cq_ev_fd; |
| 432 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 433 | |
| 434 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 435 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 436 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 437 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 438 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 439 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 440 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 441 | * manipulate the list, hence no extra locking is needed there. |
| 442 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 443 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 444 | struct hlist_head *cancel_hash; |
| 445 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 446 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 447 | |
| 448 | spinlock_t inflight_lock; |
| 449 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 450 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 451 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 452 | struct delayed_work rsrc_put_work; |
| 453 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 454 | struct list_head rsrc_ref_list; |
| 455 | spinlock_t rsrc_ref_lock; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 456 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 457 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 458 | |
| 459 | /* Keep this last, we don't need it for the fast path */ |
| 460 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 463 | /* |
| 464 | * First field must be the file pointer in all the |
| 465 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 466 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 467 | struct io_poll_iocb { |
| 468 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 469 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 470 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 471 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 472 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 473 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 474 | }; |
| 475 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 476 | struct io_poll_remove { |
| 477 | struct file *file; |
| 478 | u64 addr; |
| 479 | }; |
| 480 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 481 | struct io_close { |
| 482 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 483 | int fd; |
| 484 | }; |
| 485 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 486 | struct io_timeout_data { |
| 487 | struct io_kiocb *req; |
| 488 | struct hrtimer timer; |
| 489 | struct timespec64 ts; |
| 490 | enum hrtimer_mode mode; |
| 491 | }; |
| 492 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 493 | struct io_accept { |
| 494 | struct file *file; |
| 495 | struct sockaddr __user *addr; |
| 496 | int __user *addr_len; |
| 497 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 498 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | struct io_sync { |
| 502 | struct file *file; |
| 503 | loff_t len; |
| 504 | loff_t off; |
| 505 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 506 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 509 | struct io_cancel { |
| 510 | struct file *file; |
| 511 | u64 addr; |
| 512 | }; |
| 513 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 514 | struct io_timeout { |
| 515 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 516 | u32 off; |
| 517 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 518 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 519 | /* head of the link, used by linked timeouts only */ |
| 520 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 521 | }; |
| 522 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 523 | struct io_timeout_rem { |
| 524 | struct file *file; |
| 525 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 526 | |
| 527 | /* timeout update */ |
| 528 | struct timespec64 ts; |
| 529 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 530 | }; |
| 531 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 532 | struct io_rw { |
| 533 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 534 | struct kiocb kiocb; |
| 535 | u64 addr; |
| 536 | u64 len; |
| 537 | }; |
| 538 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 539 | struct io_connect { |
| 540 | struct file *file; |
| 541 | struct sockaddr __user *addr; |
| 542 | int addr_len; |
| 543 | }; |
| 544 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 545 | struct io_sr_msg { |
| 546 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 547 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 548 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 549 | void __user *buf; |
| 550 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 551 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 552 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 553 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 554 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 555 | }; |
| 556 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 557 | struct io_open { |
| 558 | struct file *file; |
| 559 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 560 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 561 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 562 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 563 | }; |
| 564 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 565 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 566 | struct file *file; |
| 567 | u64 arg; |
| 568 | u32 nr_args; |
| 569 | u32 offset; |
| 570 | }; |
| 571 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 572 | struct io_fadvise { |
| 573 | struct file *file; |
| 574 | u64 offset; |
| 575 | u32 len; |
| 576 | u32 advice; |
| 577 | }; |
| 578 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 579 | struct io_madvise { |
| 580 | struct file *file; |
| 581 | u64 addr; |
| 582 | u32 len; |
| 583 | u32 advice; |
| 584 | }; |
| 585 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 586 | struct io_epoll { |
| 587 | struct file *file; |
| 588 | int epfd; |
| 589 | int op; |
| 590 | int fd; |
| 591 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 594 | struct io_splice { |
| 595 | struct file *file_out; |
| 596 | struct file *file_in; |
| 597 | loff_t off_out; |
| 598 | loff_t off_in; |
| 599 | u64 len; |
| 600 | unsigned int flags; |
| 601 | }; |
| 602 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 603 | struct io_provide_buf { |
| 604 | struct file *file; |
| 605 | __u64 addr; |
| 606 | __s32 len; |
| 607 | __u32 bgid; |
| 608 | __u16 nbufs; |
| 609 | __u16 bid; |
| 610 | }; |
| 611 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 612 | struct io_statx { |
| 613 | struct file *file; |
| 614 | int dfd; |
| 615 | unsigned int mask; |
| 616 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 617 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 618 | struct statx __user *buffer; |
| 619 | }; |
| 620 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 621 | struct io_shutdown { |
| 622 | struct file *file; |
| 623 | int how; |
| 624 | }; |
| 625 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 626 | struct io_rename { |
| 627 | struct file *file; |
| 628 | int old_dfd; |
| 629 | int new_dfd; |
| 630 | struct filename *oldpath; |
| 631 | struct filename *newpath; |
| 632 | int flags; |
| 633 | }; |
| 634 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 635 | struct io_unlink { |
| 636 | struct file *file; |
| 637 | int dfd; |
| 638 | int flags; |
| 639 | struct filename *filename; |
| 640 | }; |
| 641 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 642 | struct io_completion { |
| 643 | struct file *file; |
| 644 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 645 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 646 | }; |
| 647 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 648 | struct io_async_connect { |
| 649 | struct sockaddr_storage address; |
| 650 | }; |
| 651 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 652 | struct io_async_msghdr { |
| 653 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 654 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 655 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 656 | struct sockaddr __user *uaddr; |
| 657 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 658 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 661 | struct io_async_rw { |
| 662 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 663 | const struct iovec *free_iovec; |
| 664 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 665 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 666 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 667 | }; |
| 668 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 669 | enum { |
| 670 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 671 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 672 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 673 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 674 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 675 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 676 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 677 | REQ_F_FAIL_LINK_BIT, |
| 678 | REQ_F_INFLIGHT_BIT, |
| 679 | REQ_F_CUR_POS_BIT, |
| 680 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 681 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 682 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 683 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 684 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 685 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 686 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 687 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 688 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 689 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 690 | |
| 691 | /* not a real bit, just to check we're not overflowing the space */ |
| 692 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 693 | }; |
| 694 | |
| 695 | enum { |
| 696 | /* ctx owns file */ |
| 697 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 698 | /* drain existing IO first */ |
| 699 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 700 | /* linked sqes */ |
| 701 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 702 | /* doesn't sever on completion < 0 */ |
| 703 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 704 | /* IOSQE_ASYNC */ |
| 705 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 706 | /* IOSQE_BUFFER_SELECT */ |
| 707 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 708 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 709 | /* fail rest of links */ |
| 710 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 711 | /* on inflight list */ |
| 712 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 713 | /* read/write uses file position */ |
| 714 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 715 | /* must not punt to workers */ |
| 716 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 717 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 718 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 719 | /* regular file */ |
| 720 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 721 | /* needs cleanup */ |
| 722 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 723 | /* already went through poll handler */ |
| 724 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 725 | /* buffer already selected */ |
| 726 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 727 | /* doesn't need file table for this request */ |
| 728 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 729 | /* io_wq_work is initialized */ |
| 730 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 731 | /* linked timeout is active, i.e. prepared by link's head */ |
| 732 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 733 | /* completion is deferred through io_comp_state */ |
| 734 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | struct async_poll { |
| 738 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 739 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 740 | }; |
| 741 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 742 | struct io_task_work { |
| 743 | struct io_wq_work_node node; |
| 744 | task_work_func_t func; |
| 745 | }; |
| 746 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 747 | /* |
| 748 | * NOTE! Each of the iocb union members has the file pointer |
| 749 | * as the first entry in their struct definition. So you can |
| 750 | * access the file pointer through any of the sub-structs, |
| 751 | * or directly as just 'ki_filp' in this struct. |
| 752 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 753 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 754 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 755 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 756 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 757 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 758 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 759 | struct io_accept accept; |
| 760 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 761 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 762 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 763 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 764 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 765 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 766 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 767 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 768 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 769 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 770 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 771 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 772 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 773 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 774 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 775 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 776 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 777 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 778 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 779 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 780 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 781 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 782 | /* opcode allocated if it needs to store data for async defer */ |
| 783 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 784 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 785 | /* polled IO has completed */ |
| 786 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 787 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 788 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 789 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 790 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 791 | struct io_ring_ctx *ctx; |
| 792 | unsigned int flags; |
| 793 | refcount_t refs; |
| 794 | struct task_struct *task; |
| 795 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 796 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 797 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 798 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 799 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 800 | /* |
| 801 | * 1. used with ctx->iopoll_list with reads/writes |
| 802 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 803 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 804 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 805 | union { |
| 806 | struct io_task_work io_task_work; |
| 807 | struct callback_head task_work; |
| 808 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 809 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 810 | struct hlist_node hash_node; |
| 811 | struct async_poll *apoll; |
| 812 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 813 | }; |
| 814 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 815 | struct io_defer_entry { |
| 816 | struct list_head list; |
| 817 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 818 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 819 | }; |
| 820 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 821 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | /* needs req->file assigned */ |
| 823 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 824 | /* hash wq insertion if file is a regular file */ |
| 825 | unsigned hash_reg_file : 1; |
| 826 | /* unbound wq insertion if file is a non-regular file */ |
| 827 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 828 | /* opcode is not supported by this kernel */ |
| 829 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 830 | /* set if opcode supports polled "wait" */ |
| 831 | unsigned pollin : 1; |
| 832 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 833 | /* op supports buffer selection */ |
| 834 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 835 | /* must always have async data allocated */ |
| 836 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 837 | /* should block plug */ |
| 838 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 839 | /* size of async data needed, if any */ |
| 840 | unsigned short async_size; |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 841 | unsigned work_flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 842 | }; |
| 843 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 844 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 845 | [IORING_OP_NOP] = {}, |
| 846 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 847 | .needs_file = 1, |
| 848 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 849 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 850 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 851 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 852 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 853 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 854 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 855 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 856 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 857 | .needs_file = 1, |
| 858 | .hash_reg_file = 1, |
| 859 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 860 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 861 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 862 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 863 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 864 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 865 | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 866 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 867 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 868 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 869 | .work_flags = IO_WQ_WORK_BLKCG, |
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_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 872 | .needs_file = 1, |
| 873 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 874 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 875 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 876 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 877 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
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_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 880 | .needs_file = 1, |
| 881 | .hash_reg_file = 1, |
| 882 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 883 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 884 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 885 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 886 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
| 887 | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 889 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 890 | .needs_file = 1, |
| 891 | .unbound_nonreg_file = 1, |
| 892 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 893 | [IORING_OP_POLL_REMOVE] = {}, |
| 894 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 896 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 898 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 899 | .needs_file = 1, |
| 900 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 901 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 902 | .needs_async_data = 1, |
| 903 | .async_size = sizeof(struct io_async_msghdr), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 904 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 905 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 906 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 907 | .needs_file = 1, |
| 908 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 909 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 910 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 911 | .needs_async_data = 1, |
| 912 | .async_size = sizeof(struct io_async_msghdr), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 913 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 914 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 915 | [IORING_OP_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 | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 918 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 919 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 920 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 921 | /* used by timeout updates' prep() */ |
| 922 | .work_flags = IO_WQ_WORK_MM, |
| 923 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 924 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 925 | .needs_file = 1, |
| 926 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 927 | .pollin = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 928 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 929 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 930 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 931 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 932 | .needs_async_data = 1, |
| 933 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 934 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 935 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 936 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 937 | .needs_file = 1, |
| 938 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 939 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 940 | .needs_async_data = 1, |
| 941 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 942 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 943 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 944 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 945 | .needs_file = 1, |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 946 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 947 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 948 | [IORING_OP_OPENAT] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 949 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 950 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 951 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 952 | [IORING_OP_CLOSE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 953 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 954 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 955 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 956 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 957 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 958 | [IORING_OP_STATX] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 959 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
| 960 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 961 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 962 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 963 | .needs_file = 1, |
| 964 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 965 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 966 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 967 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 968 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 969 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 970 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 971 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 972 | .needs_file = 1, |
| 973 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 974 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 975 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 976 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 977 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 978 | IO_WQ_WORK_FSIZE, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 979 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 980 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 981 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 982 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 983 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 984 | [IORING_OP_MADVISE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 985 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 986 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 987 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 988 | .needs_file = 1, |
| 989 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 990 | .pollout = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 991 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 992 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 993 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 994 | .needs_file = 1, |
| 995 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 996 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 997 | .buffer_select = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 998 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 999 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1000 | [IORING_OP_OPENAT2] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 1001 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 1002 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1003 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1004 | [IORING_OP_EPOLL_CTL] = { |
| 1005 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 1006 | .work_flags = IO_WQ_WORK_FILES, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1007 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1008 | [IORING_OP_SPLICE] = { |
| 1009 | .needs_file = 1, |
| 1010 | .hash_reg_file = 1, |
| 1011 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 1012 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1013 | }, |
| 1014 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1015 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1016 | [IORING_OP_TEE] = { |
| 1017 | .needs_file = 1, |
| 1018 | .hash_reg_file = 1, |
| 1019 | .unbound_nonreg_file = 1, |
| 1020 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1021 | [IORING_OP_SHUTDOWN] = { |
| 1022 | .needs_file = 1, |
| 1023 | }, |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 1024 | [IORING_OP_RENAMEAT] = { |
| 1025 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1026 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1027 | }, |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 1028 | [IORING_OP_UNLINKAT] = { |
| 1029 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1030 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1031 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1032 | }; |
| 1033 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1034 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1035 | struct task_struct *task, |
| 1036 | struct files_struct *files); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1037 | 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] | 1038 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1039 | struct io_ring_ctx *ctx); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 1040 | 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] | 1041 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 1042 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1043 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1044 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1045 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1046 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1047 | static void io_dismantle_req(struct io_kiocb *req); |
| 1048 | static void io_put_task(struct task_struct *task, int nr); |
| 1049 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1050 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1051 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1052 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1053 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1054 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1055 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1056 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1057 | static struct file *io_file_get(struct io_submit_state *state, |
| 1058 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1059 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1060 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1061 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1062 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1063 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1064 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1065 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1066 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1067 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1068 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1069 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1070 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1071 | static struct kmem_cache *req_cachep; |
| 1072 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1073 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1074 | |
| 1075 | struct sock *io_uring_get_socket(struct file *file) |
| 1076 | { |
| 1077 | #if defined(CONFIG_UNIX) |
| 1078 | if (file->f_op == &io_uring_fops) { |
| 1079 | struct io_ring_ctx *ctx = file->private_data; |
| 1080 | |
| 1081 | return ctx->ring_sock->sk; |
| 1082 | } |
| 1083 | #endif |
| 1084 | return NULL; |
| 1085 | } |
| 1086 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1087 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1088 | #define io_for_each_link(pos, head) \ |
| 1089 | for (pos = (head); pos; pos = pos->link) |
| 1090 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1091 | static inline void io_clean_op(struct io_kiocb *req) |
| 1092 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1093 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1094 | __io_clean_op(req); |
| 1095 | } |
| 1096 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1097 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1098 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1099 | struct io_ring_ctx *ctx = req->ctx; |
| 1100 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1101 | if (!req->fixed_rsrc_refs) { |
| 1102 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1103 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 1107 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1108 | { |
| 1109 | if (!percpu_ref_tryget(ref)) { |
| 1110 | /* already at zero, wait for ->release() */ |
| 1111 | if (!try_wait_for_completion(compl)) |
| 1112 | synchronize_rcu(); |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | percpu_ref_resurrect(ref); |
| 1117 | reinit_completion(compl); |
| 1118 | percpu_ref_put(ref); |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1122 | static bool io_match_task(struct io_kiocb *head, |
| 1123 | struct task_struct *task, |
| 1124 | struct files_struct *files) |
| 1125 | { |
| 1126 | struct io_kiocb *req; |
| 1127 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1128 | if (task && head->task != task) { |
| 1129 | /* in terms of cancelation, always match if req task is dead */ |
| 1130 | if (head->task->flags & PF_EXITING) |
| 1131 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1132 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1133 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1134 | if (!files) |
| 1135 | return true; |
| 1136 | |
| 1137 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1138 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1139 | continue; |
| 1140 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1141 | return true; |
| 1142 | if ((req->work.flags & IO_WQ_WORK_FILES) && |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1143 | req->work.identity->files == files) |
| 1144 | return true; |
| 1145 | } |
| 1146 | return false; |
| 1147 | } |
| 1148 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1149 | static void io_sq_thread_drop_mm_files(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1150 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1151 | struct files_struct *files = current->files; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1152 | struct mm_struct *mm = current->mm; |
| 1153 | |
| 1154 | if (mm) { |
| 1155 | kthread_unuse_mm(mm); |
| 1156 | mmput(mm); |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1157 | current->mm = NULL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1158 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1159 | if (files) { |
| 1160 | struct nsproxy *nsproxy = current->nsproxy; |
| 1161 | |
| 1162 | task_lock(current); |
| 1163 | current->files = NULL; |
| 1164 | current->nsproxy = NULL; |
| 1165 | task_unlock(current); |
| 1166 | put_files_struct(files); |
| 1167 | put_nsproxy(nsproxy); |
| 1168 | } |
| 1169 | } |
| 1170 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1171 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1172 | { |
| 1173 | if (!current->files) { |
| 1174 | struct files_struct *files; |
| 1175 | struct nsproxy *nsproxy; |
| 1176 | |
| 1177 | task_lock(ctx->sqo_task); |
| 1178 | files = ctx->sqo_task->files; |
| 1179 | if (!files) { |
| 1180 | task_unlock(ctx->sqo_task); |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1181 | return -EOWNERDEAD; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1182 | } |
| 1183 | atomic_inc(&files->count); |
| 1184 | get_nsproxy(ctx->sqo_task->nsproxy); |
| 1185 | nsproxy = ctx->sqo_task->nsproxy; |
| 1186 | task_unlock(ctx->sqo_task); |
| 1187 | |
| 1188 | task_lock(current); |
| 1189 | current->files = files; |
| 1190 | current->nsproxy = nsproxy; |
| 1191 | task_unlock(current); |
| 1192 | } |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1193 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 1197 | { |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1198 | struct mm_struct *mm; |
| 1199 | |
| 1200 | if (current->mm) |
| 1201 | return 0; |
| 1202 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1203 | task_lock(ctx->sqo_task); |
| 1204 | mm = ctx->sqo_task->mm; |
| 1205 | if (unlikely(!mm || !mmget_not_zero(mm))) |
| 1206 | mm = NULL; |
| 1207 | task_unlock(ctx->sqo_task); |
| 1208 | |
| 1209 | if (mm) { |
| 1210 | kthread_use_mm(mm); |
| 1211 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1212 | } |
| 1213 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1214 | return -EFAULT; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1215 | } |
| 1216 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1217 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1218 | struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1219 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1220 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1221 | int ret; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1222 | |
| 1223 | if (def->work_flags & IO_WQ_WORK_MM) { |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1224 | ret = __io_sq_thread_acquire_mm(ctx); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1225 | if (unlikely(ret)) |
| 1226 | return ret; |
| 1227 | } |
| 1228 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1229 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
| 1230 | ret = __io_sq_thread_acquire_files(ctx); |
| 1231 | if (unlikely(ret)) |
| 1232 | return ret; |
| 1233 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1234 | |
| 1235 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1236 | } |
| 1237 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1238 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1239 | struct io_kiocb *req) |
| 1240 | { |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1241 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
| 1242 | return 0; |
| 1243 | return __io_sq_thread_acquire_mm_files(ctx, req); |
| 1244 | } |
| 1245 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1246 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| 1247 | struct cgroup_subsys_state **cur_css) |
| 1248 | |
| 1249 | { |
| 1250 | #ifdef CONFIG_BLK_CGROUP |
| 1251 | /* puts the old one when swapping */ |
| 1252 | if (*cur_css != ctx->sqo_blkcg_css) { |
| 1253 | kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| 1254 | *cur_css = ctx->sqo_blkcg_css; |
| 1255 | } |
| 1256 | #endif |
| 1257 | } |
| 1258 | |
| 1259 | static void io_sq_thread_unassociate_blkcg(void) |
| 1260 | { |
| 1261 | #ifdef CONFIG_BLK_CGROUP |
| 1262 | kthread_associate_blkcg(NULL); |
| 1263 | #endif |
| 1264 | } |
| 1265 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1266 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1267 | { |
| 1268 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1269 | req->flags |= REQ_F_FAIL_LINK; |
| 1270 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1271 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1272 | /* |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1273 | * None of these are dereferenced, they are simply used to check if any of |
| 1274 | * them have changed. If we're under current and check they are still the |
| 1275 | * same, we're fine to grab references to them for actual out-of-line use. |
| 1276 | */ |
| 1277 | static void io_init_identity(struct io_identity *id) |
| 1278 | { |
| 1279 | id->files = current->files; |
| 1280 | id->mm = current->mm; |
| 1281 | #ifdef CONFIG_BLK_CGROUP |
| 1282 | rcu_read_lock(); |
| 1283 | id->blkcg_css = blkcg_css(); |
| 1284 | rcu_read_unlock(); |
| 1285 | #endif |
| 1286 | id->creds = current_cred(); |
| 1287 | id->nsproxy = current->nsproxy; |
| 1288 | id->fs = current->fs; |
| 1289 | id->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1290 | #ifdef CONFIG_AUDIT |
| 1291 | id->loginuid = current->loginuid; |
| 1292 | id->sessionid = current->sessionid; |
| 1293 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1294 | refcount_set(&id->count, 1); |
| 1295 | } |
| 1296 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1297 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1298 | { |
| 1299 | memset(&req->work, 0, sizeof(req->work)); |
| 1300 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1301 | } |
| 1302 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1303 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1304 | * Note: must call io_req_init_async() for the first time you |
| 1305 | * touch any members of io_wq_work. |
| 1306 | */ |
| 1307 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1308 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1309 | struct io_uring_task *tctx = current->io_uring; |
| 1310 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1311 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1312 | return; |
| 1313 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1314 | __io_req_init_async(req); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1315 | |
| 1316 | /* Grab a ref if this isn't our static identity */ |
| 1317 | req->work.identity = tctx->identity; |
| 1318 | if (tctx->identity != &tctx->__identity) |
| 1319 | refcount_inc(&req->work.identity->count); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1322 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1323 | { |
| 1324 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1325 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1326 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1329 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1330 | { |
| 1331 | return !req->timeout.off; |
| 1332 | } |
| 1333 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1334 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1335 | { |
| 1336 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1337 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1338 | |
| 1339 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1340 | if (!ctx) |
| 1341 | return NULL; |
| 1342 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1343 | /* |
| 1344 | * Use 5 bits less than the max cq entries, that should give us around |
| 1345 | * 32 entries per hash list if totally full and uniformly spread. |
| 1346 | */ |
| 1347 | hash_bits = ilog2(p->cq_entries); |
| 1348 | hash_bits -= 5; |
| 1349 | if (hash_bits <= 0) |
| 1350 | hash_bits = 1; |
| 1351 | ctx->cancel_hash_bits = hash_bits; |
| 1352 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1353 | GFP_KERNEL); |
| 1354 | if (!ctx->cancel_hash) |
| 1355 | goto err; |
| 1356 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1357 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1358 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1359 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1360 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1361 | |
| 1362 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1363 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1364 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1365 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1366 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1367 | init_completion(&ctx->ref_comp); |
| 1368 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1369 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1370 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1371 | mutex_init(&ctx->uring_lock); |
| 1372 | init_waitqueue_head(&ctx->wait); |
| 1373 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1374 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1375 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1376 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1377 | spin_lock_init(&ctx->inflight_lock); |
| 1378 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1379 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1380 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1381 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1382 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1383 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1384 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1385 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1386 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1387 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1388 | kfree(ctx); |
| 1389 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1392 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1393 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1394 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1395 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1396 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1397 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1398 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1399 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1400 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1401 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1402 | } |
| 1403 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1404 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1405 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1406 | if (req->work.identity == &tctx->__identity) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1407 | return; |
| 1408 | if (refcount_dec_and_test(&req->work.identity->count)) |
| 1409 | kfree(req->work.identity); |
| 1410 | } |
| 1411 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1412 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1413 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1414 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1415 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1416 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1417 | if (req->work.flags & IO_WQ_WORK_MM) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1418 | mmdrop(req->work.identity->mm); |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1419 | #ifdef CONFIG_BLK_CGROUP |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1420 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1421 | css_put(req->work.identity->blkcg_css); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1422 | #endif |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1423 | if (req->work.flags & IO_WQ_WORK_CREDS) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1424 | put_cred(req->work.identity->creds); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1425 | if (req->work.flags & IO_WQ_WORK_FS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1426 | struct fs_struct *fs = req->work.identity->fs; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1427 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1428 | spin_lock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1429 | if (--fs->users) |
| 1430 | fs = NULL; |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1431 | spin_unlock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1432 | if (fs) |
| 1433 | free_fs_struct(fs); |
| 1434 | } |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1435 | if (req->work.flags & IO_WQ_WORK_FILES) { |
| 1436 | put_files_struct(req->work.identity->files); |
| 1437 | put_nsproxy(req->work.identity->nsproxy); |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1438 | } |
| 1439 | if (req->flags & REQ_F_INFLIGHT) { |
| 1440 | struct io_ring_ctx *ctx = req->ctx; |
| 1441 | struct io_uring_task *tctx = req->task->io_uring; |
| 1442 | unsigned long flags; |
| 1443 | |
| 1444 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1445 | list_del(&req->inflight_entry); |
| 1446 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1447 | req->flags &= ~REQ_F_INFLIGHT; |
| 1448 | if (atomic_read(&tctx->in_idle)) |
| 1449 | wake_up(&tctx->wait); |
| 1450 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1451 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1452 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
| 1453 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | |
| 1454 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1455 | io_put_identity(req->task->io_uring, req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Create a private copy of io_identity, since some fields don't match |
| 1460 | * the current context. |
| 1461 | */ |
| 1462 | static bool io_identity_cow(struct io_kiocb *req) |
| 1463 | { |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1464 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1465 | const struct cred *creds = NULL; |
| 1466 | struct io_identity *id; |
| 1467 | |
| 1468 | if (req->work.flags & IO_WQ_WORK_CREDS) |
| 1469 | creds = req->work.identity->creds; |
| 1470 | |
| 1471 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| 1472 | if (unlikely(!id)) { |
| 1473 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * We can safely just re-init the creds we copied Either the field |
| 1479 | * matches the current one, or we haven't grabbed it yet. The only |
| 1480 | * exception is ->creds, through registered personalities, so handle |
| 1481 | * that one separately. |
| 1482 | */ |
| 1483 | io_init_identity(id); |
| 1484 | if (creds) |
Pavel Begunkov | e8c954d | 2020-12-06 22:22:46 +0000 | [diff] [blame] | 1485 | id->creds = creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1486 | |
| 1487 | /* add one for this request */ |
| 1488 | refcount_inc(&id->count); |
| 1489 | |
Jens Axboe | cb8a8ae | 2020-11-03 12:19:07 -0700 | [diff] [blame] | 1490 | /* drop tctx and req identity references, if needed */ |
| 1491 | if (tctx->identity != &tctx->__identity && |
| 1492 | refcount_dec_and_test(&tctx->identity->count)) |
| 1493 | kfree(tctx->identity); |
| 1494 | if (req->work.identity != &tctx->__identity && |
| 1495 | refcount_dec_and_test(&req->work.identity->count)) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1496 | kfree(req->work.identity); |
| 1497 | |
| 1498 | req->work.identity = id; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1499 | tctx->identity = id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1500 | return true; |
| 1501 | } |
| 1502 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1503 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1504 | { |
| 1505 | struct io_ring_ctx *ctx = req->ctx; |
| 1506 | |
| 1507 | if (!(req->flags & REQ_F_INFLIGHT)) { |
| 1508 | io_req_init_async(req); |
| 1509 | req->flags |= REQ_F_INFLIGHT; |
| 1510 | |
| 1511 | spin_lock_irq(&ctx->inflight_lock); |
| 1512 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1513 | spin_unlock_irq(&ctx->inflight_lock); |
| 1514 | } |
| 1515 | } |
| 1516 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1517 | static bool io_grab_identity(struct io_kiocb *req) |
| 1518 | { |
| 1519 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1520 | struct io_identity *id = req->work.identity; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1521 | |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 1522 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| 1523 | if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| 1524 | return false; |
| 1525 | req->work.flags |= IO_WQ_WORK_FSIZE; |
| 1526 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1527 | #ifdef CONFIG_BLK_CGROUP |
| 1528 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| 1529 | (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| 1530 | rcu_read_lock(); |
| 1531 | if (id->blkcg_css != blkcg_css()) { |
| 1532 | rcu_read_unlock(); |
| 1533 | return false; |
| 1534 | } |
| 1535 | /* |
| 1536 | * This should be rare, either the cgroup is dying or the task |
| 1537 | * is moving cgroups. Just punt to root for the handful of ios. |
| 1538 | */ |
| 1539 | if (css_tryget_online(id->blkcg_css)) |
| 1540 | req->work.flags |= IO_WQ_WORK_BLKCG; |
| 1541 | rcu_read_unlock(); |
| 1542 | } |
| 1543 | #endif |
| 1544 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| 1545 | if (id->creds != current_cred()) |
| 1546 | return false; |
| 1547 | get_cred(id->creds); |
| 1548 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 1549 | } |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1550 | #ifdef CONFIG_AUDIT |
| 1551 | if (!uid_eq(current->loginuid, id->loginuid) || |
| 1552 | current->sessionid != id->sessionid) |
| 1553 | return false; |
| 1554 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1555 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
| 1556 | (def->work_flags & IO_WQ_WORK_FS)) { |
| 1557 | if (current->fs != id->fs) |
| 1558 | return false; |
| 1559 | spin_lock(&id->fs->lock); |
| 1560 | if (!id->fs->in_exec) { |
| 1561 | id->fs->users++; |
| 1562 | req->work.flags |= IO_WQ_WORK_FS; |
| 1563 | } else { |
| 1564 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1565 | } |
| 1566 | spin_unlock(¤t->fs->lock); |
| 1567 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1568 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| 1569 | (def->work_flags & IO_WQ_WORK_FILES) && |
| 1570 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| 1571 | if (id->files != current->files || |
| 1572 | id->nsproxy != current->nsproxy) |
| 1573 | return false; |
| 1574 | atomic_inc(&id->files->count); |
| 1575 | get_nsproxy(id->nsproxy); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1576 | req->work.flags |= IO_WQ_WORK_FILES; |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1577 | io_req_track_inflight(req); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1578 | } |
Jens Axboe | 7778877 | 2020-12-29 10:50:46 -0700 | [diff] [blame] | 1579 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
| 1580 | (def->work_flags & IO_WQ_WORK_MM)) { |
| 1581 | if (id->mm != current->mm) |
| 1582 | return false; |
| 1583 | mmgrab(id->mm); |
| 1584 | req->work.flags |= IO_WQ_WORK_MM; |
| 1585 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1586 | |
| 1587 | return true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1588 | } |
| 1589 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1590 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1591 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1592 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1593 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1594 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1595 | io_req_init_async(req); |
| 1596 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1597 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1598 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1599 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1600 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1601 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1602 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1603 | } else { |
| 1604 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1605 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1606 | } |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1607 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1608 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| 1609 | if (io_grab_identity(req)) |
| 1610 | return; |
| 1611 | |
| 1612 | if (!io_identity_cow(req)) |
| 1613 | return; |
| 1614 | |
| 1615 | /* can't fail at this point */ |
| 1616 | if (!io_grab_identity(req)) |
| 1617 | WARN_ON(1); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1618 | } |
| 1619 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1620 | static void io_prep_async_link(struct io_kiocb *req) |
| 1621 | { |
| 1622 | struct io_kiocb *cur; |
| 1623 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1624 | io_for_each_link(cur, req) |
| 1625 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1626 | } |
| 1627 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1628 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1629 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1630 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1631 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1632 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1633 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1634 | &req->work, req->flags); |
| 1635 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1636 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1637 | } |
| 1638 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1639 | static void io_queue_async_work(struct io_kiocb *req) |
| 1640 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1641 | struct io_kiocb *link; |
| 1642 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1643 | /* init ->work of the whole link before punting */ |
| 1644 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1645 | link = __io_queue_async_work(req); |
| 1646 | |
| 1647 | if (link) |
| 1648 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1649 | } |
| 1650 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1651 | static void io_kill_timeout(struct io_kiocb *req) |
| 1652 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1653 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1654 | int ret; |
| 1655 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1656 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1657 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1658 | atomic_set(&req->ctx->cq_timeouts, |
| 1659 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1660 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1661 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1662 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1666 | /* |
| 1667 | * Returns true if we found and killed one or more timeouts |
| 1668 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1669 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1670 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1671 | { |
| 1672 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1673 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1674 | |
| 1675 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1676 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1677 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1678 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1679 | canceled++; |
| 1680 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1681 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1682 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1683 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1684 | } |
| 1685 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1686 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1687 | { |
| 1688 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1689 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1690 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1691 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1692 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1693 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1694 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1695 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1696 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1697 | } while (!list_empty(&ctx->defer_list)); |
| 1698 | } |
| 1699 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1700 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1701 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1702 | u32 seq; |
| 1703 | |
| 1704 | if (list_empty(&ctx->timeout_list)) |
| 1705 | return; |
| 1706 | |
| 1707 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1708 | |
| 1709 | do { |
| 1710 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1711 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1712 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1713 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1714 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1715 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1716 | |
| 1717 | /* |
| 1718 | * Since seq can easily wrap around over time, subtract |
| 1719 | * the last seq at which timeouts were flushed before comparing. |
| 1720 | * Assuming not more than 2^31-1 events have happened since, |
| 1721 | * these subtractions won't have wrapped, so we can check if |
| 1722 | * target is in [last_seq, current_seq] by comparing the two. |
| 1723 | */ |
| 1724 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1725 | events_got = seq - ctx->cq_last_tm_flush; |
| 1726 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1727 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1728 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1729 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1730 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1731 | } while (!list_empty(&ctx->timeout_list)); |
| 1732 | |
| 1733 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1734 | } |
| 1735 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1736 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1737 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1738 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1739 | |
| 1740 | /* order cqe stores with ring update */ |
| 1741 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1742 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1743 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1744 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1745 | } |
| 1746 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1747 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1748 | { |
| 1749 | struct io_rings *r = ctx->rings; |
| 1750 | |
| 1751 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1752 | } |
| 1753 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1754 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1755 | { |
| 1756 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1757 | } |
| 1758 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1759 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1760 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1761 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1762 | unsigned tail; |
| 1763 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1764 | /* |
| 1765 | * writes to the cq entry need to come after reading head; the |
| 1766 | * control dependency is enough as we're using WRITE_ONCE to |
| 1767 | * fill the cq entry |
| 1768 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1769 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1770 | return NULL; |
| 1771 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1772 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1773 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1776 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1777 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1778 | if (!ctx->cq_ev_fd) |
| 1779 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1780 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1781 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1782 | if (!ctx->eventfd_async) |
| 1783 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1784 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1787 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1788 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1789 | /* see waitqueue_active() comment */ |
| 1790 | smp_mb(); |
| 1791 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1792 | if (waitqueue_active(&ctx->wait)) |
| 1793 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1794 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1795 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1796 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1797 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1798 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1799 | wake_up_interruptible(&ctx->cq_wait); |
| 1800 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1801 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1802 | } |
| 1803 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1804 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1805 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1806 | /* see waitqueue_active() comment */ |
| 1807 | smp_mb(); |
| 1808 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1809 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1810 | if (waitqueue_active(&ctx->wait)) |
| 1811 | wake_up(&ctx->wait); |
| 1812 | } |
| 1813 | if (io_should_trigger_evfd(ctx)) |
| 1814 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1815 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1816 | wake_up_interruptible(&ctx->cq_wait); |
| 1817 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1818 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1821 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1822 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1823 | struct task_struct *tsk, |
| 1824 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1825 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1826 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1827 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1828 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1829 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1830 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1831 | LIST_HEAD(list); |
| 1832 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1833 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1834 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1835 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1836 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1837 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1838 | 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] | 1839 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1840 | continue; |
| 1841 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1842 | cqe = io_get_cqring(ctx); |
| 1843 | if (!cqe && !force) |
| 1844 | break; |
| 1845 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1846 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1847 | if (cqe) { |
| 1848 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1849 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1850 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1851 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1852 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1853 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1854 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1855 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1856 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1859 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1860 | if (all_flushed) { |
| 1861 | clear_bit(0, &ctx->sq_check_overflow); |
| 1862 | clear_bit(0, &ctx->cq_check_overflow); |
| 1863 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1864 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1865 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1866 | if (posted) |
| 1867 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1868 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1869 | if (posted) |
| 1870 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1871 | |
| 1872 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1873 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1874 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1875 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1876 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1877 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1878 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1881 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1882 | struct task_struct *tsk, |
| 1883 | struct files_struct *files) |
| 1884 | { |
| 1885 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1886 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1887 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1888 | mutex_lock(&ctx->uring_lock); |
| 1889 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1890 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1891 | mutex_unlock(&ctx->uring_lock); |
| 1892 | } |
| 1893 | } |
| 1894 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1895 | 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] | 1896 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1897 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1898 | struct io_uring_cqe *cqe; |
| 1899 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1900 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1901 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1902 | /* |
| 1903 | * If we can't get a cq entry, userspace overflowed the |
| 1904 | * submission (by quite a lot). Increment the overflow count in |
| 1905 | * the ring. |
| 1906 | */ |
| 1907 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1908 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1909 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1910 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1911 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1912 | } else if (ctx->cq_overflow_flushed || |
| 1913 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1914 | /* |
| 1915 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1916 | * then we cannot store the request for later flushing, we need |
| 1917 | * to drop it on the floor. |
| 1918 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1919 | ctx->cached_cq_overflow++; |
| 1920 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1921 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1922 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1923 | set_bit(0, &ctx->sq_check_overflow); |
| 1924 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1925 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1926 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1927 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1928 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1929 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1930 | refcount_inc(&req->refs); |
| 1931 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1935 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1936 | { |
| 1937 | __io_cqring_fill_event(req, res, 0); |
| 1938 | } |
| 1939 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1940 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1941 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1942 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1943 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1944 | unsigned long flags; |
| 1945 | |
| 1946 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1947 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1948 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1949 | /* |
| 1950 | * If we're the last reference to this request, add to our locked |
| 1951 | * free_list cache. |
| 1952 | */ |
| 1953 | if (refcount_dec_and_test(&req->refs)) { |
| 1954 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1955 | |
| 1956 | io_dismantle_req(req); |
| 1957 | io_put_task(req->task, 1); |
| 1958 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1959 | cs->locked_free_nr++; |
| 1960 | } else |
| 1961 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1962 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1963 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1964 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1965 | if (req) { |
| 1966 | io_queue_next(req); |
| 1967 | percpu_ref_put(&ctx->refs); |
| 1968 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1971 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1972 | unsigned int cflags) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1973 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1974 | io_clean_op(req); |
| 1975 | req->result = res; |
| 1976 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1977 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1978 | } |
| 1979 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1980 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1981 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1982 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1983 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1984 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1985 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1986 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1990 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1991 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1994 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1995 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1996 | struct io_submit_state *state = &ctx->submit_state; |
| 1997 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1998 | struct io_kiocb *req = NULL; |
| 1999 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2000 | /* |
| 2001 | * If we have more than a batch's worth of requests in our IRQ side |
| 2002 | * locked cache, grab the lock and move them over to our submission |
| 2003 | * side cache. |
| 2004 | */ |
| 2005 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 2006 | spin_lock_irq(&ctx->completion_lock); |
| 2007 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 2008 | cs->locked_free_nr = 0; |
| 2009 | spin_unlock_irq(&ctx->completion_lock); |
| 2010 | } |
| 2011 | |
| 2012 | while (!list_empty(&cs->free_list)) { |
| 2013 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2014 | compl.list); |
| 2015 | list_del(&req->compl.list); |
| 2016 | state->reqs[state->free_reqs++] = req; |
| 2017 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 2018 | break; |
| 2019 | } |
| 2020 | |
| 2021 | return req != NULL; |
| 2022 | } |
| 2023 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2024 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2025 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2026 | struct io_submit_state *state = &ctx->submit_state; |
| 2027 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2028 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
| 2029 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 2030 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2031 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 2032 | int ret; |
| 2033 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2034 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2035 | goto got_req; |
| 2036 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2037 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 2038 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2039 | |
| 2040 | /* |
| 2041 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2042 | * retry single alloc to be on the safe side. |
| 2043 | */ |
| 2044 | if (unlikely(ret <= 0)) { |
| 2045 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2046 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2047 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2048 | ret = 1; |
| 2049 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2050 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2051 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2052 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2053 | state->free_reqs--; |
| 2054 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2057 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 2058 | bool fixed) |
| 2059 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2060 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2061 | fput(file); |
| 2062 | } |
| 2063 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2064 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2065 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 2066 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2067 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2068 | if (req->async_data) |
| 2069 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2070 | if (req->file) |
| 2071 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 2072 | if (req->fixed_rsrc_refs) |
| 2073 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2074 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2075 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2076 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2077 | static inline void io_put_task(struct task_struct *task, int nr) |
| 2078 | { |
| 2079 | struct io_uring_task *tctx = task->io_uring; |
| 2080 | |
| 2081 | percpu_counter_sub(&tctx->inflight, nr); |
| 2082 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 2083 | wake_up(&tctx->wait); |
| 2084 | put_task_struct_many(task, nr); |
| 2085 | } |
| 2086 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2087 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2088 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2089 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2090 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2091 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2092 | io_put_task(req->task, 1); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2093 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2094 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2095 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2096 | } |
| 2097 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2098 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2099 | { |
| 2100 | struct io_kiocb *nxt = req->link; |
| 2101 | |
| 2102 | req->link = nxt->link; |
| 2103 | nxt->link = NULL; |
| 2104 | } |
| 2105 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2106 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2107 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2108 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2109 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2110 | bool cancelled = false; |
| 2111 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2112 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2113 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2114 | link = req->link; |
| 2115 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 2116 | /* |
| 2117 | * Can happen if a linked timeout fired and link had been like |
| 2118 | * req -> link t-out -> link t-out [-> ...] |
| 2119 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2120 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 2121 | struct io_timeout_data *io = link->async_data; |
| 2122 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2123 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2124 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2125 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2126 | ret = hrtimer_try_to_cancel(&io->timer); |
| 2127 | if (ret != -1) { |
| 2128 | io_cqring_fill_event(link, -ECANCELED); |
| 2129 | io_commit_cqring(ctx); |
| 2130 | cancelled = true; |
| 2131 | } |
| 2132 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2133 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2134 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 2135 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2136 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2137 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2138 | io_put_req(link); |
| 2139 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2140 | } |
| 2141 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2142 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2143 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2144 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2145 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2146 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2147 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2148 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2149 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2150 | link = req->link; |
| 2151 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2152 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2153 | while (link) { |
| 2154 | nxt = link->link; |
| 2155 | link->link = NULL; |
| 2156 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2157 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2158 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2159 | |
| 2160 | /* |
| 2161 | * It's ok to free under spinlock as they're not linked anymore, |
| 2162 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 2163 | * work.fs->lock. |
| 2164 | */ |
| 2165 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 2166 | io_put_req_deferred(link, 2); |
| 2167 | else |
| 2168 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2169 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2170 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2171 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2172 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2173 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2174 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2175 | } |
| 2176 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2177 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2178 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2179 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 2180 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2181 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2182 | /* |
| 2183 | * If LINK is set, we have dependent requests in this chain. If we |
| 2184 | * didn't fail this request, queue the first one up, moving any other |
| 2185 | * dependencies to the next request. In case of failure, fail the rest |
| 2186 | * of the chain. |
| 2187 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2188 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 2189 | struct io_kiocb *nxt = req->link; |
| 2190 | |
| 2191 | req->link = NULL; |
| 2192 | return nxt; |
| 2193 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2194 | io_fail_links(req); |
| 2195 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2196 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2197 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2198 | 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] | 2199 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 2200 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2201 | return NULL; |
| 2202 | return __io_req_find_next(req); |
| 2203 | } |
| 2204 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2205 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 2206 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2207 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2208 | struct io_wq_work_list list; |
| 2209 | struct io_wq_work_node *node; |
| 2210 | |
| 2211 | if (wq_list_empty(&tctx->task_list)) |
| 2212 | return false; |
| 2213 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2214 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2215 | list = tctx->task_list; |
| 2216 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2217 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2218 | |
| 2219 | node = list.first; |
| 2220 | while (node) { |
| 2221 | struct io_wq_work_node *next = node->next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2222 | struct io_ring_ctx *this_ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2223 | struct io_kiocb *req; |
| 2224 | |
| 2225 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2226 | this_ctx = req->ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2227 | req->task_work.func(&req->task_work); |
| 2228 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2229 | |
| 2230 | if (!ctx) { |
| 2231 | ctx = this_ctx; |
| 2232 | } else if (ctx != this_ctx) { |
| 2233 | mutex_lock(&ctx->uring_lock); |
| 2234 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2235 | mutex_unlock(&ctx->uring_lock); |
| 2236 | ctx = this_ctx; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | if (ctx && ctx->submit_state.comp.nr) { |
| 2241 | mutex_lock(&ctx->uring_lock); |
| 2242 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2243 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | return list.first != NULL; |
| 2247 | } |
| 2248 | |
| 2249 | static void tctx_task_work(struct callback_head *cb) |
| 2250 | { |
| 2251 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 2252 | |
| 2253 | while (__tctx_task_work(tctx)) |
| 2254 | cond_resched(); |
| 2255 | |
| 2256 | clear_bit(0, &tctx->task_state); |
| 2257 | } |
| 2258 | |
| 2259 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 2260 | enum task_work_notify_mode notify) |
| 2261 | { |
| 2262 | struct io_uring_task *tctx = tsk->io_uring; |
| 2263 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2264 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2265 | int ret; |
| 2266 | |
| 2267 | WARN_ON_ONCE(!tctx); |
| 2268 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2269 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2270 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2271 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2272 | |
| 2273 | /* task_work already pending, we're done */ |
| 2274 | if (test_bit(0, &tctx->task_state) || |
| 2275 | test_and_set_bit(0, &tctx->task_state)) |
| 2276 | return 0; |
| 2277 | |
| 2278 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 2279 | return 0; |
| 2280 | |
| 2281 | /* |
| 2282 | * Slow path - we failed, find and delete work. if the work is not |
| 2283 | * in the list, it got run and we're fine. |
| 2284 | */ |
| 2285 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2286 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2287 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 2288 | if (&req->io_task_work.node == node) { |
| 2289 | wq_list_del(&tctx->task_list, node, prev); |
| 2290 | ret = 1; |
| 2291 | break; |
| 2292 | } |
| 2293 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2294 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2295 | clear_bit(0, &tctx->task_state); |
| 2296 | return ret; |
| 2297 | } |
| 2298 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2299 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2300 | { |
| 2301 | struct task_struct *tsk = req->task; |
| 2302 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2303 | enum task_work_notify_mode notify; |
| 2304 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2305 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2306 | if (tsk->flags & PF_EXITING) |
| 2307 | return -ESRCH; |
| 2308 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2309 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2310 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2311 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2312 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2313 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2314 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2315 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2316 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2317 | notify = TWA_SIGNAL; |
| 2318 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2319 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2320 | if (!ret) |
| 2321 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2322 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2323 | return ret; |
| 2324 | } |
| 2325 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2326 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2327 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2328 | { |
| 2329 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); |
| 2330 | |
| 2331 | init_task_work(&req->task_work, cb); |
| 2332 | task_work_add(tsk, &req->task_work, TWA_NONE); |
| 2333 | wake_up_process(tsk); |
| 2334 | } |
| 2335 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2336 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2337 | { |
| 2338 | struct io_ring_ctx *ctx = req->ctx; |
| 2339 | |
| 2340 | spin_lock_irq(&ctx->completion_lock); |
| 2341 | io_cqring_fill_event(req, error); |
| 2342 | io_commit_cqring(ctx); |
| 2343 | spin_unlock_irq(&ctx->completion_lock); |
| 2344 | |
| 2345 | io_cqring_ev_posted(ctx); |
| 2346 | req_set_fail_links(req); |
| 2347 | io_double_put_req(req); |
| 2348 | } |
| 2349 | |
| 2350 | static void io_req_task_cancel(struct callback_head *cb) |
| 2351 | { |
| 2352 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2353 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2354 | |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2355 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2356 | __io_req_task_cancel(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2357 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2358 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2362 | { |
| 2363 | struct io_ring_ctx *ctx = req->ctx; |
| 2364 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2365 | /* 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] | 2366 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | dc0eced | 2021-02-12 18:41:15 +0000 | [diff] [blame] | 2367 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
| 2368 | !io_sq_thread_acquire_mm_files(ctx, req)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2369 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2370 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2371 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2372 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2373 | } |
| 2374 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2375 | static void io_req_task_submit(struct callback_head *cb) |
| 2376 | { |
| 2377 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2378 | |
| 2379 | __io_req_task_submit(req); |
| 2380 | } |
| 2381 | |
| 2382 | static void io_req_task_queue(struct io_kiocb *req) |
| 2383 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2384 | int ret; |
| 2385 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2386 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2387 | ret = io_req_task_work_add(req); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2388 | if (unlikely(ret)) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2389 | req->result = -ECANCELED; |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2390 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2391 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2392 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2393 | } |
| 2394 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2395 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2396 | { |
| 2397 | percpu_ref_get(&req->ctx->refs); |
| 2398 | req->result = ret; |
| 2399 | req->task_work.func = io_req_task_cancel; |
| 2400 | |
| 2401 | if (unlikely(io_req_task_work_add(req))) |
| 2402 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2403 | } |
| 2404 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2405 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2406 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2407 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2408 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2409 | if (nxt) |
| 2410 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2413 | static void io_free_req(struct io_kiocb *req) |
| 2414 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2415 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2416 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2417 | } |
| 2418 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2419 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2420 | struct task_struct *task; |
| 2421 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2422 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2423 | }; |
| 2424 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2425 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2426 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2427 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2428 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2429 | rb->task = NULL; |
| 2430 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2431 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2432 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2433 | struct req_batch *rb) |
| 2434 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2435 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2436 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2437 | if (rb->ctx_refs) |
| 2438 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2439 | } |
| 2440 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2441 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2442 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2443 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2444 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2445 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2446 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2447 | if (rb->task) |
| 2448 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2449 | rb->task = req->task; |
| 2450 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2451 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2452 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2453 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2454 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2455 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2456 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2457 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2458 | else |
| 2459 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2460 | } |
| 2461 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2462 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2463 | struct io_ring_ctx *ctx) |
| 2464 | { |
| 2465 | int i, nr = cs->nr; |
| 2466 | struct io_kiocb *req; |
| 2467 | struct req_batch rb; |
| 2468 | |
| 2469 | io_init_req_batch(&rb); |
| 2470 | spin_lock_irq(&ctx->completion_lock); |
| 2471 | for (i = 0; i < nr; i++) { |
| 2472 | req = cs->reqs[i]; |
| 2473 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2474 | } |
| 2475 | io_commit_cqring(ctx); |
| 2476 | spin_unlock_irq(&ctx->completion_lock); |
| 2477 | |
| 2478 | io_cqring_ev_posted(ctx); |
| 2479 | for (i = 0; i < nr; i++) { |
| 2480 | req = cs->reqs[i]; |
| 2481 | |
| 2482 | /* submission and completion refs */ |
| 2483 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2484 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | io_req_free_batch_finish(ctx, &rb); |
| 2488 | cs->nr = 0; |
| 2489 | } |
| 2490 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2491 | /* |
| 2492 | * Drop reference to request, return next in chain (if there is one) if this |
| 2493 | * was the last reference to this request. |
| 2494 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2495 | 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] | 2496 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2497 | struct io_kiocb *nxt = NULL; |
| 2498 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2499 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2500 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2501 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2502 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2503 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2506 | static void io_put_req(struct io_kiocb *req) |
| 2507 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | if (refcount_dec_and_test(&req->refs)) |
| 2509 | io_free_req(req); |
| 2510 | } |
| 2511 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2512 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2513 | { |
| 2514 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2515 | |
| 2516 | io_free_req(req); |
| 2517 | } |
| 2518 | |
| 2519 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2520 | { |
| 2521 | int ret; |
| 2522 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2523 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2524 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2525 | if (unlikely(ret)) |
| 2526 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2530 | { |
| 2531 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2532 | io_free_req_deferred(req); |
| 2533 | } |
| 2534 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2535 | static void io_double_put_req(struct io_kiocb *req) |
| 2536 | { |
| 2537 | /* drop both submit and complete references */ |
| 2538 | if (refcount_sub_and_test(2, &req->refs)) |
| 2539 | io_free_req(req); |
| 2540 | } |
| 2541 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2542 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2543 | { |
| 2544 | /* See comment at the top of this file */ |
| 2545 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2546 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2547 | } |
| 2548 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2549 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2550 | { |
| 2551 | struct io_rings *rings = ctx->rings; |
| 2552 | |
| 2553 | /* make sure SQ entry isn't read before tail */ |
| 2554 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2555 | } |
| 2556 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2557 | 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] | 2558 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2559 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2560 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2561 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2562 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2563 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2564 | kfree(kbuf); |
| 2565 | return cflags; |
| 2566 | } |
| 2567 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2568 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2569 | { |
| 2570 | struct io_buffer *kbuf; |
| 2571 | |
| 2572 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2573 | return io_put_kbuf(req, kbuf); |
| 2574 | } |
| 2575 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2576 | static inline bool io_run_task_work(void) |
| 2577 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2578 | /* |
| 2579 | * Not safe to run on exiting task, and the task_work handling will |
| 2580 | * not add work to such a task. |
| 2581 | */ |
| 2582 | if (unlikely(current->flags & PF_EXITING)) |
| 2583 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2584 | if (current->task_works) { |
| 2585 | __set_current_state(TASK_RUNNING); |
| 2586 | task_work_run(); |
| 2587 | return true; |
| 2588 | } |
| 2589 | |
| 2590 | return false; |
| 2591 | } |
| 2592 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2593 | /* |
| 2594 | * Find and free completed poll iocbs |
| 2595 | */ |
| 2596 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2597 | struct list_head *done) |
| 2598 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2599 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2600 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2601 | |
| 2602 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2603 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2604 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2605 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2606 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2607 | int cflags = 0; |
| 2608 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2609 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2610 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2611 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2612 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2613 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2614 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2615 | continue; |
| 2616 | } |
| 2617 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2618 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2619 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2620 | |
| 2621 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2622 | (*nr_events)++; |
| 2623 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2624 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2625 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2626 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2627 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2628 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2629 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2630 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2633 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2634 | long min) |
| 2635 | { |
| 2636 | struct io_kiocb *req, *tmp; |
| 2637 | LIST_HEAD(done); |
| 2638 | bool spin; |
| 2639 | int ret; |
| 2640 | |
| 2641 | /* |
| 2642 | * Only spin for completions if we don't have multiple devices hanging |
| 2643 | * off our complete list, and we're under the requested amount. |
| 2644 | */ |
| 2645 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2646 | |
| 2647 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2648 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2649 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2650 | |
| 2651 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2652 | * Move completed and retryable entries to our local lists. |
| 2653 | * If we find a request that requires polling, break out |
| 2654 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2655 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2656 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2657 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2658 | continue; |
| 2659 | } |
| 2660 | if (!list_empty(&done)) |
| 2661 | break; |
| 2662 | |
| 2663 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2664 | if (ret < 0) |
| 2665 | break; |
| 2666 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2667 | /* iopoll may have completed current req */ |
| 2668 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2669 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2670 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2671 | if (ret && spin) |
| 2672 | spin = false; |
| 2673 | ret = 0; |
| 2674 | } |
| 2675 | |
| 2676 | if (!list_empty(&done)) |
| 2677 | io_iopoll_complete(ctx, nr_events, &done); |
| 2678 | |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
| 2682 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2683 | * 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] | 2684 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2685 | * as a non-spinning completion check. |
| 2686 | */ |
| 2687 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2688 | long min) |
| 2689 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2690 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2691 | int ret; |
| 2692 | |
| 2693 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2694 | if (ret < 0) |
| 2695 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2696 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * We can't just wait for polled events to come to us, we have to actively |
| 2705 | * find and complete them. |
| 2706 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2707 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2708 | { |
| 2709 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2710 | return; |
| 2711 | |
| 2712 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2713 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2714 | unsigned int nr_events = 0; |
| 2715 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2716 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2717 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2718 | /* let it sleep and repeat later if can't complete a request */ |
| 2719 | if (nr_events == 0) |
| 2720 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2721 | /* |
| 2722 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2723 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2724 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2725 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2726 | if (need_resched()) { |
| 2727 | mutex_unlock(&ctx->uring_lock); |
| 2728 | cond_resched(); |
| 2729 | mutex_lock(&ctx->uring_lock); |
| 2730 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2731 | } |
| 2732 | mutex_unlock(&ctx->uring_lock); |
| 2733 | } |
| 2734 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2735 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2736 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2737 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2738 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2739 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2740 | /* |
| 2741 | * We disallow the app entering submit/complete with polling, but we |
| 2742 | * still need to lock the ring to prevent racing with polled issue |
| 2743 | * that got punted to a workqueue. |
| 2744 | */ |
| 2745 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2746 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2747 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2748 | * Don't enter poll loop if we already have events pending. |
| 2749 | * If we do, we can potentially be spinning for commands that |
| 2750 | * already triggered a CQE (eg in error). |
| 2751 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2752 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2753 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2754 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2755 | break; |
| 2756 | |
| 2757 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2758 | * If a submit got punted to a workqueue, we can have the |
| 2759 | * application entering polling for a command before it gets |
| 2760 | * issued. That app will hold the uring_lock for the duration |
| 2761 | * of the poll right here, so we need to take a breather every |
| 2762 | * now and then to ensure that the issue has a chance to add |
| 2763 | * the poll to the issued list. Otherwise we can spin here |
| 2764 | * forever, while the workqueue is stuck trying to acquire the |
| 2765 | * very same mutex. |
| 2766 | */ |
| 2767 | if (!(++iters & 7)) { |
| 2768 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2769 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2770 | mutex_lock(&ctx->uring_lock); |
| 2771 | } |
| 2772 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2773 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2774 | if (ret <= 0) |
| 2775 | break; |
| 2776 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2777 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2778 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2779 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2780 | return ret; |
| 2781 | } |
| 2782 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2783 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2784 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2785 | /* |
| 2786 | * Tell lockdep we inherited freeze protection from submission |
| 2787 | * thread. |
| 2788 | */ |
| 2789 | if (req->flags & REQ_F_ISREG) { |
| 2790 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2791 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2792 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2793 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2794 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2797 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2798 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2799 | { |
| 2800 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2801 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2802 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2803 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2804 | /* already prepared */ |
| 2805 | if (req->async_data) |
| 2806 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2807 | |
| 2808 | switch (req->opcode) { |
| 2809 | case IORING_OP_READV: |
| 2810 | case IORING_OP_READ_FIXED: |
| 2811 | case IORING_OP_READ: |
| 2812 | rw = READ; |
| 2813 | break; |
| 2814 | case IORING_OP_WRITEV: |
| 2815 | case IORING_OP_WRITE_FIXED: |
| 2816 | case IORING_OP_WRITE: |
| 2817 | rw = WRITE; |
| 2818 | break; |
| 2819 | default: |
| 2820 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2821 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2822 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2823 | } |
| 2824 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2825 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2826 | if (ret < 0) |
| 2827 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2828 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2829 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2830 | #endif |
| 2831 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2832 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2833 | { |
| 2834 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2835 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2836 | int ret; |
| 2837 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2838 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2839 | return false; |
| 2840 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2841 | return false; |
| 2842 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2843 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2844 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2845 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2846 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2847 | if (!ret && io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2848 | refcount_inc(&req->refs); |
| 2849 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2850 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2851 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2852 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2853 | #endif |
| 2854 | return false; |
| 2855 | } |
| 2856 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2857 | 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] | 2858 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2859 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2860 | int cflags = 0; |
| 2861 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2862 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2863 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2864 | if (res != req->result) |
| 2865 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2866 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2867 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2868 | kiocb_end_write(req); |
| 2869 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2870 | cflags = io_put_rw_kbuf(req); |
| 2871 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2875 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2876 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2877 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2878 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2881 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2882 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2883 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2884 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2885 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2886 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2887 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2888 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2889 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2890 | |
| 2891 | WRITE_ONCE(req->result, res); |
| 2892 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2893 | smp_wmb(); |
| 2894 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | /* |
| 2898 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2899 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2900 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2901 | * accessing the kiocb cookie. |
| 2902 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2903 | 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] | 2904 | { |
| 2905 | struct io_ring_ctx *ctx = req->ctx; |
| 2906 | |
| 2907 | /* |
| 2908 | * Track whether we have multiple files in our lists. This will impact |
| 2909 | * how we do polling eventually, not spinning if we're on potentially |
| 2910 | * different devices. |
| 2911 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2912 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2913 | ctx->poll_multi_file = false; |
| 2914 | } else if (!ctx->poll_multi_file) { |
| 2915 | struct io_kiocb *list_req; |
| 2916 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2917 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2918 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2919 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2920 | ctx->poll_multi_file = true; |
| 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * For fast devices, IO may have already completed. If it has, add |
| 2925 | * it to the front so we find it first. |
| 2926 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2927 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2928 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2929 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2930 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2931 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2932 | /* |
| 2933 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2934 | * task context or in io worker task context. If current task context is |
| 2935 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2936 | */ |
| 2937 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2938 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2939 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2940 | } |
| 2941 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2942 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2943 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2944 | if (state->file_refs) { |
| 2945 | fput_many(state->file, state->file_refs); |
| 2946 | state->file_refs = 0; |
| 2947 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | /* |
| 2951 | * Get as many references to a file as we have IOs left in this submission, |
| 2952 | * assuming most submissions are for one file, or at least that each file |
| 2953 | * has more than one submission. |
| 2954 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2955 | 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] | 2956 | { |
| 2957 | if (!state) |
| 2958 | return fget(fd); |
| 2959 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2960 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2961 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2962 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2963 | return state->file; |
| 2964 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2965 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2966 | } |
| 2967 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2968 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2969 | return NULL; |
| 2970 | |
| 2971 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2972 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2973 | return state->file; |
| 2974 | } |
| 2975 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2976 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2977 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2978 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2979 | } |
| 2980 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2981 | /* |
| 2982 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2983 | * any file. For now, just ensure that anything potentially problematic is done |
| 2984 | * inline. |
| 2985 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2986 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2987 | { |
| 2988 | umode_t mode = file_inode(file)->i_mode; |
| 2989 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2990 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2991 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2992 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2993 | return true; |
| 2994 | return false; |
| 2995 | } |
| 2996 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2997 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2998 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2999 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 3000 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 3001 | file->f_op != &io_uring_fops) |
| 3002 | return true; |
| 3003 | return false; |
| 3004 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3005 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 3006 | /* any ->read/write should understand O_NONBLOCK */ |
| 3007 | if (file->f_flags & O_NONBLOCK) |
| 3008 | return true; |
| 3009 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3010 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 3011 | return false; |
| 3012 | |
| 3013 | if (rw == READ) |
| 3014 | return file->f_op->read_iter != NULL; |
| 3015 | |
| 3016 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3017 | } |
| 3018 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3019 | 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] | 3020 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3021 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3022 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3023 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3024 | unsigned ioprio; |
| 3025 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3026 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3027 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 3028 | req->flags |= REQ_F_ISREG; |
| 3029 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3030 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3031 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3032 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3033 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3034 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3035 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 3036 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 3037 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 3038 | if (unlikely(ret)) |
| 3039 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3040 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3041 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 3042 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 3043 | req->flags |= REQ_F_NOWAIT; |
| 3044 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3045 | ioprio = READ_ONCE(sqe->ioprio); |
| 3046 | if (ioprio) { |
| 3047 | ret = ioprio_check_cap(ioprio); |
| 3048 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3049 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3050 | |
| 3051 | kiocb->ki_ioprio = ioprio; |
| 3052 | } else |
| 3053 | kiocb->ki_ioprio = get_current_ioprio(); |
| 3054 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3055 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3056 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 3057 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3058 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3059 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3060 | kiocb->ki_flags |= IOCB_HIPRI; |
| 3061 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3062 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3063 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3064 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3065 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3066 | kiocb->ki_complete = io_complete_rw; |
| 3067 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3068 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3069 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3070 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3071 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3072 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3076 | { |
| 3077 | switch (ret) { |
| 3078 | case -EIOCBQUEUED: |
| 3079 | break; |
| 3080 | case -ERESTARTSYS: |
| 3081 | case -ERESTARTNOINTR: |
| 3082 | case -ERESTARTNOHAND: |
| 3083 | case -ERESTART_RESTARTBLOCK: |
| 3084 | /* |
| 3085 | * We can't just restart the syscall, since previously |
| 3086 | * submitted sqes may already be in progress. Just fail this |
| 3087 | * IO with EINTR. |
| 3088 | */ |
| 3089 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3090 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3091 | default: |
| 3092 | kiocb->ki_complete(kiocb, ret, 0); |
| 3093 | } |
| 3094 | } |
| 3095 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3096 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3097 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3098 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3099 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3100 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3101 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3102 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3103 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3104 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3105 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3106 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3107 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3108 | } |
| 3109 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3110 | if (req->flags & REQ_F_CUR_POS) |
| 3111 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 3112 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3113 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3114 | else |
| 3115 | io_rw_done(kiocb, ret); |
| 3116 | } |
| 3117 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3118 | 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] | 3119 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3120 | struct io_ring_ctx *ctx = req->ctx; |
| 3121 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3122 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 3123 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3124 | size_t offset; |
| 3125 | u64 buf_addr; |
| 3126 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3127 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3128 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3129 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3130 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3131 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3132 | |
| 3133 | /* overflow */ |
| 3134 | if (buf_addr + len < buf_addr) |
| 3135 | return -EFAULT; |
| 3136 | /* not inside the mapped region */ |
| 3137 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3138 | return -EFAULT; |
| 3139 | |
| 3140 | /* |
| 3141 | * May not be a start of buffer, set size appropriately |
| 3142 | * and advance us to the beginning. |
| 3143 | */ |
| 3144 | offset = buf_addr - imu->ubuf; |
| 3145 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3146 | |
| 3147 | if (offset) { |
| 3148 | /* |
| 3149 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3150 | * using the latter parts of a big fixed buffer - it iterates |
| 3151 | * over each segment manually. We can cheat a bit here, because |
| 3152 | * we know that: |
| 3153 | * |
| 3154 | * 1) it's a BVEC iter, we set it up |
| 3155 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3156 | * first and last bvec |
| 3157 | * |
| 3158 | * So just find our index, and adjust the iterator afterwards. |
| 3159 | * If the offset is within the first bvec (or the whole first |
| 3160 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3161 | * since we can just skip the first segment, which may not |
| 3162 | * be PAGE_SIZE aligned. |
| 3163 | */ |
| 3164 | const struct bio_vec *bvec = imu->bvec; |
| 3165 | |
| 3166 | if (offset <= bvec->bv_len) { |
| 3167 | iov_iter_advance(iter, offset); |
| 3168 | } else { |
| 3169 | unsigned long seg_skip; |
| 3170 | |
| 3171 | /* skip first vec */ |
| 3172 | offset -= bvec->bv_len; |
| 3173 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3174 | |
| 3175 | iter->bvec = bvec + seg_skip; |
| 3176 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3177 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3178 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3182 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3185 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3186 | { |
| 3187 | if (needs_lock) |
| 3188 | mutex_unlock(&ctx->uring_lock); |
| 3189 | } |
| 3190 | |
| 3191 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3192 | { |
| 3193 | /* |
| 3194 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3195 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3196 | * The only exception is when we've detached the request and issue it |
| 3197 | * from an async worker thread, grab the lock for that case. |
| 3198 | */ |
| 3199 | if (needs_lock) |
| 3200 | mutex_lock(&ctx->uring_lock); |
| 3201 | } |
| 3202 | |
| 3203 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3204 | int bgid, struct io_buffer *kbuf, |
| 3205 | bool needs_lock) |
| 3206 | { |
| 3207 | struct io_buffer *head; |
| 3208 | |
| 3209 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3210 | return kbuf; |
| 3211 | |
| 3212 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3213 | |
| 3214 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3215 | |
| 3216 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3217 | if (head) { |
| 3218 | if (!list_empty(&head->list)) { |
| 3219 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3220 | list); |
| 3221 | list_del(&kbuf->list); |
| 3222 | } else { |
| 3223 | kbuf = head; |
| 3224 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3225 | } |
| 3226 | if (*len > kbuf->len) |
| 3227 | *len = kbuf->len; |
| 3228 | } else { |
| 3229 | kbuf = ERR_PTR(-ENOBUFS); |
| 3230 | } |
| 3231 | |
| 3232 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3233 | |
| 3234 | return kbuf; |
| 3235 | } |
| 3236 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3237 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3238 | bool needs_lock) |
| 3239 | { |
| 3240 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3241 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3242 | |
| 3243 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3244 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3245 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3246 | if (IS_ERR(kbuf)) |
| 3247 | return kbuf; |
| 3248 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3249 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3250 | return u64_to_user_ptr(kbuf->addr); |
| 3251 | } |
| 3252 | |
| 3253 | #ifdef CONFIG_COMPAT |
| 3254 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3255 | bool needs_lock) |
| 3256 | { |
| 3257 | struct compat_iovec __user *uiov; |
| 3258 | compat_ssize_t clen; |
| 3259 | void __user *buf; |
| 3260 | ssize_t len; |
| 3261 | |
| 3262 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3263 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3264 | return -EFAULT; |
| 3265 | if (__get_user(clen, &uiov->iov_len)) |
| 3266 | return -EFAULT; |
| 3267 | if (clen < 0) |
| 3268 | return -EINVAL; |
| 3269 | |
| 3270 | len = clen; |
| 3271 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3272 | if (IS_ERR(buf)) |
| 3273 | return PTR_ERR(buf); |
| 3274 | iov[0].iov_base = buf; |
| 3275 | iov[0].iov_len = (compat_size_t) len; |
| 3276 | return 0; |
| 3277 | } |
| 3278 | #endif |
| 3279 | |
| 3280 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3281 | bool needs_lock) |
| 3282 | { |
| 3283 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3284 | void __user *buf; |
| 3285 | ssize_t len; |
| 3286 | |
| 3287 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3288 | return -EFAULT; |
| 3289 | |
| 3290 | len = iov[0].iov_len; |
| 3291 | if (len < 0) |
| 3292 | return -EINVAL; |
| 3293 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3294 | if (IS_ERR(buf)) |
| 3295 | return PTR_ERR(buf); |
| 3296 | iov[0].iov_base = buf; |
| 3297 | iov[0].iov_len = len; |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
| 3301 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3302 | bool needs_lock) |
| 3303 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3304 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3305 | struct io_buffer *kbuf; |
| 3306 | |
| 3307 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3308 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3309 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3310 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3311 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3312 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3313 | return -EINVAL; |
| 3314 | |
| 3315 | #ifdef CONFIG_COMPAT |
| 3316 | if (req->ctx->compat) |
| 3317 | return io_compat_import(req, iov, needs_lock); |
| 3318 | #endif |
| 3319 | |
| 3320 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3321 | } |
| 3322 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3323 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3324 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3325 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3326 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3327 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3328 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3329 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3330 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3331 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3332 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3333 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3334 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3335 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3336 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3337 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3338 | return -EINVAL; |
| 3339 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3340 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3341 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3342 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3343 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3344 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3345 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3348 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3349 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3350 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3351 | } |
| 3352 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3353 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3354 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3355 | if (!ret) |
| 3356 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3357 | *iovec = NULL; |
| 3358 | return ret; |
| 3359 | } |
| 3360 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3361 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3362 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3363 | } |
| 3364 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3365 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3366 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3367 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3368 | } |
| 3369 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3370 | /* |
| 3371 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3372 | * by looping over ->read() or ->write() manually. |
| 3373 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3374 | 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] | 3375 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3376 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3377 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3378 | ssize_t ret = 0; |
| 3379 | |
| 3380 | /* |
| 3381 | * Don't support polled IO through this interface, and we can't |
| 3382 | * support non-blocking either. For the latter, this just causes |
| 3383 | * the kiocb to be handled from an async context. |
| 3384 | */ |
| 3385 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3386 | return -EOPNOTSUPP; |
| 3387 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3388 | return -EAGAIN; |
| 3389 | |
| 3390 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3391 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3392 | ssize_t nr; |
| 3393 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3394 | if (!iov_iter_is_bvec(iter)) { |
| 3395 | iovec = iov_iter_iovec(iter); |
| 3396 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3397 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3398 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3399 | } |
| 3400 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3401 | if (rw == READ) { |
| 3402 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3403 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3404 | } else { |
| 3405 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3406 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | if (nr < 0) { |
| 3410 | if (!ret) |
| 3411 | ret = nr; |
| 3412 | break; |
| 3413 | } |
| 3414 | ret += nr; |
| 3415 | if (nr != iovec.iov_len) |
| 3416 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3417 | req->rw.len -= nr; |
| 3418 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3419 | iov_iter_advance(iter, nr); |
| 3420 | } |
| 3421 | |
| 3422 | return ret; |
| 3423 | } |
| 3424 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3425 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3426 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3427 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3428 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3429 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3430 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3431 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3432 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3433 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3434 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3435 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3436 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3437 | unsigned iov_off = 0; |
| 3438 | |
| 3439 | rw->iter.iov = rw->fast_iov; |
| 3440 | if (iter->iov != fast_iov) { |
| 3441 | iov_off = iter->iov - fast_iov; |
| 3442 | rw->iter.iov += iov_off; |
| 3443 | } |
| 3444 | if (rw->fast_iov != fast_iov) |
| 3445 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3446 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3447 | } else { |
| 3448 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3452 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3453 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3454 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3455 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3456 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3457 | } |
| 3458 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3459 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3460 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3461 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3462 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3463 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3464 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3467 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3468 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3469 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3470 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3471 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3472 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3473 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3474 | if (__io_alloc_async_data(req)) { |
| 3475 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3476 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3477 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3478 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3479 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3480 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3481 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3484 | 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] | 3485 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3486 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3487 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3488 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3489 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3490 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3491 | if (unlikely(ret < 0)) |
| 3492 | return ret; |
| 3493 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3494 | iorw->bytes_done = 0; |
| 3495 | iorw->free_iovec = iov; |
| 3496 | if (iov) |
| 3497 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3498 | return 0; |
| 3499 | } |
| 3500 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3501 | 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] | 3502 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3503 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3504 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3505 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3508 | /* |
| 3509 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3510 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3511 | * This gets called when the page is unlocked, and we generally expect that to |
| 3512 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3513 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3514 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3515 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3516 | * slow path. |
| 3517 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3518 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3519 | int sync, void *arg) |
| 3520 | { |
| 3521 | struct wait_page_queue *wpq; |
| 3522 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3523 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3524 | |
| 3525 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3526 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3527 | if (!wake_page_match(wpq, key)) |
| 3528 | return 0; |
| 3529 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3530 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3531 | list_del_init(&wait->entry); |
| 3532 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3533 | /* submit ref gets dropped, acquire a new one */ |
| 3534 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3535 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3536 | return 1; |
| 3537 | } |
| 3538 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3539 | /* |
| 3540 | * This controls whether a given IO request should be armed for async page |
| 3541 | * based retry. If we return false here, the request is handed to the async |
| 3542 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3543 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3544 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3545 | * will register a callback when the page is unlocked at IO completion. Through |
| 3546 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3547 | * That retry will attempt the buffered read again. The retry will generally |
| 3548 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3549 | * async worker threads for a blocking retry. |
| 3550 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3551 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3552 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3553 | struct io_async_rw *rw = req->async_data; |
| 3554 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3555 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3556 | |
| 3557 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3558 | if (req->flags & REQ_F_NOWAIT) |
| 3559 | return false; |
| 3560 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3561 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3562 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3563 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3564 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3565 | /* |
| 3566 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3567 | * support callback based unlocks |
| 3568 | */ |
| 3569 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3570 | return false; |
| 3571 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3572 | wait->wait.func = io_async_buf_func; |
| 3573 | wait->wait.private = req; |
| 3574 | wait->wait.flags = 0; |
| 3575 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3576 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3577 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3578 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3579 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3583 | { |
| 3584 | if (req->file->f_op->read_iter) |
| 3585 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3586 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3587 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3588 | else |
| 3589 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3590 | } |
| 3591 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3592 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3593 | { |
| 3594 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3595 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3596 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3597 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3598 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3599 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3600 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3601 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3602 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3603 | iovec = NULL; |
| 3604 | } else { |
| 3605 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3606 | if (ret < 0) |
| 3607 | return ret; |
| 3608 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3609 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3610 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3611 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3612 | /* Ensure we clear previously set non-block flag */ |
| 3613 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3614 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3615 | else |
| 3616 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3617 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3618 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3619 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3620 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3621 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3622 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3623 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3624 | 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] | 3625 | if (unlikely(ret)) { |
| 3626 | kfree(iovec); |
| 3627 | return ret; |
| 3628 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3629 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3630 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3631 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3632 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3633 | goto out_free; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3634 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3635 | /* IOPOLL retry should happen for io-wq threads */ |
| 3636 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3637 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3638 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3639 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3640 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3641 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3642 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3643 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3644 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3645 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3646 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3647 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3648 | } |
| 3649 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3650 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3651 | if (ret2) |
| 3652 | return ret2; |
| 3653 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3654 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3655 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3656 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3657 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3658 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3659 | do { |
| 3660 | io_size -= ret; |
| 3661 | rw->bytes_done += ret; |
| 3662 | /* if we can retry, do so with the callbacks armed */ |
| 3663 | if (!io_rw_should_retry(req)) { |
| 3664 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3665 | return -EAGAIN; |
| 3666 | } |
| 3667 | |
| 3668 | /* |
| 3669 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3670 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3671 | * desired page gets unlocked. We can also get a partial read |
| 3672 | * here, and if we do, then just retry at the new offset. |
| 3673 | */ |
| 3674 | ret = io_iter_do_read(req, iter); |
| 3675 | if (ret == -EIOCBQUEUED) |
| 3676 | return 0; |
| 3677 | /* we got some bytes, but not all. retry. */ |
| 3678 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3679 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3680 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3681 | out_free: |
| 3682 | /* it's faster to check here then delegate to kfree */ |
| 3683 | if (iovec) |
| 3684 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3685 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3686 | } |
| 3687 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3688 | 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] | 3689 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3690 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3691 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3692 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3695 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3696 | { |
| 3697 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3698 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3699 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3700 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3701 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3702 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3703 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3704 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3705 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3706 | iovec = NULL; |
| 3707 | } else { |
| 3708 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3709 | if (ret < 0) |
| 3710 | return ret; |
| 3711 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3712 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3713 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3714 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3715 | /* Ensure we clear previously set non-block flag */ |
| 3716 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3717 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3718 | else |
| 3719 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3720 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3721 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3722 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3723 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3724 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3725 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3726 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3727 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3728 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3729 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3730 | 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] | 3731 | if (unlikely(ret)) |
| 3732 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3733 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3734 | /* |
| 3735 | * Open-code file_start_write here to grab freeze protection, |
| 3736 | * which will be released by another thread in |
| 3737 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3738 | * released so that it doesn't complain about the held lock when |
| 3739 | * we return to userspace. |
| 3740 | */ |
| 3741 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3742 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3743 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3744 | SB_FREEZE_WRITE); |
| 3745 | } |
| 3746 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3747 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3748 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3749 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3750 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3751 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3752 | else |
| 3753 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3754 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3755 | /* |
| 3756 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3757 | * retry them without IOCB_NOWAIT. |
| 3758 | */ |
| 3759 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3760 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3761 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3762 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3763 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3764 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3765 | /* IOPOLL retry should happen for io-wq threads */ |
| 3766 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3767 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3768 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3769 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3770 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3771 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3772 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3773 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3774 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3775 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3776 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3777 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3778 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3779 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3780 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3781 | return ret; |
| 3782 | } |
| 3783 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3784 | static int io_renameat_prep(struct io_kiocb *req, |
| 3785 | const struct io_uring_sqe *sqe) |
| 3786 | { |
| 3787 | struct io_rename *ren = &req->rename; |
| 3788 | const char __user *oldf, *newf; |
| 3789 | |
| 3790 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3791 | return -EBADF; |
| 3792 | |
| 3793 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3794 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3795 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3796 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3797 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3798 | |
| 3799 | ren->oldpath = getname(oldf); |
| 3800 | if (IS_ERR(ren->oldpath)) |
| 3801 | return PTR_ERR(ren->oldpath); |
| 3802 | |
| 3803 | ren->newpath = getname(newf); |
| 3804 | if (IS_ERR(ren->newpath)) { |
| 3805 | putname(ren->oldpath); |
| 3806 | return PTR_ERR(ren->newpath); |
| 3807 | } |
| 3808 | |
| 3809 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3813 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3814 | { |
| 3815 | struct io_rename *ren = &req->rename; |
| 3816 | int ret; |
| 3817 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3818 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3819 | return -EAGAIN; |
| 3820 | |
| 3821 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3822 | ren->newpath, ren->flags); |
| 3823 | |
| 3824 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3825 | if (ret < 0) |
| 3826 | req_set_fail_links(req); |
| 3827 | io_req_complete(req, ret); |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3831 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3832 | const struct io_uring_sqe *sqe) |
| 3833 | { |
| 3834 | struct io_unlink *un = &req->unlink; |
| 3835 | const char __user *fname; |
| 3836 | |
| 3837 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3838 | return -EBADF; |
| 3839 | |
| 3840 | un->dfd = READ_ONCE(sqe->fd); |
| 3841 | |
| 3842 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3843 | if (un->flags & ~AT_REMOVEDIR) |
| 3844 | return -EINVAL; |
| 3845 | |
| 3846 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3847 | un->filename = getname(fname); |
| 3848 | if (IS_ERR(un->filename)) |
| 3849 | return PTR_ERR(un->filename); |
| 3850 | |
| 3851 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3852 | return 0; |
| 3853 | } |
| 3854 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3855 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3856 | { |
| 3857 | struct io_unlink *un = &req->unlink; |
| 3858 | int ret; |
| 3859 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3860 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3861 | return -EAGAIN; |
| 3862 | |
| 3863 | if (un->flags & AT_REMOVEDIR) |
| 3864 | ret = do_rmdir(un->dfd, un->filename); |
| 3865 | else |
| 3866 | ret = do_unlinkat(un->dfd, un->filename); |
| 3867 | |
| 3868 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3869 | if (ret < 0) |
| 3870 | req_set_fail_links(req); |
| 3871 | io_req_complete(req, ret); |
| 3872 | return 0; |
| 3873 | } |
| 3874 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3875 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3876 | const struct io_uring_sqe *sqe) |
| 3877 | { |
| 3878 | #if defined(CONFIG_NET) |
| 3879 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3880 | return -EINVAL; |
| 3881 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3882 | sqe->buf_index) |
| 3883 | return -EINVAL; |
| 3884 | |
| 3885 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3886 | return 0; |
| 3887 | #else |
| 3888 | return -EOPNOTSUPP; |
| 3889 | #endif |
| 3890 | } |
| 3891 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3892 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3893 | { |
| 3894 | #if defined(CONFIG_NET) |
| 3895 | struct socket *sock; |
| 3896 | int ret; |
| 3897 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3898 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3899 | return -EAGAIN; |
| 3900 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3901 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3902 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3903 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3904 | |
| 3905 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3906 | if (ret < 0) |
| 3907 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3908 | io_req_complete(req, ret); |
| 3909 | return 0; |
| 3910 | #else |
| 3911 | return -EOPNOTSUPP; |
| 3912 | #endif |
| 3913 | } |
| 3914 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3915 | static int __io_splice_prep(struct io_kiocb *req, |
| 3916 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3917 | { |
| 3918 | struct io_splice* sp = &req->splice; |
| 3919 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3920 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3921 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3922 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3923 | |
| 3924 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3925 | sp->len = READ_ONCE(sqe->len); |
| 3926 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3927 | |
| 3928 | if (unlikely(sp->flags & ~valid_flags)) |
| 3929 | return -EINVAL; |
| 3930 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3931 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3932 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3933 | if (!sp->file_in) |
| 3934 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3935 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3936 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3937 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3938 | /* |
| 3939 | * Splice operation will be punted aync, and here need to |
| 3940 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3941 | */ |
| 3942 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3943 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3944 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3945 | |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3949 | static int io_tee_prep(struct io_kiocb *req, |
| 3950 | const struct io_uring_sqe *sqe) |
| 3951 | { |
| 3952 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3953 | return -EINVAL; |
| 3954 | return __io_splice_prep(req, sqe); |
| 3955 | } |
| 3956 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3957 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3958 | { |
| 3959 | struct io_splice *sp = &req->splice; |
| 3960 | struct file *in = sp->file_in; |
| 3961 | struct file *out = sp->file_out; |
| 3962 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3963 | long ret = 0; |
| 3964 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3965 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3966 | return -EAGAIN; |
| 3967 | if (sp->len) |
| 3968 | ret = do_tee(in, out, sp->len, flags); |
| 3969 | |
| 3970 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3971 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3972 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3973 | if (ret != sp->len) |
| 3974 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3975 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3976 | return 0; |
| 3977 | } |
| 3978 | |
| 3979 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3980 | { |
| 3981 | struct io_splice* sp = &req->splice; |
| 3982 | |
| 3983 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3984 | sp->off_out = READ_ONCE(sqe->off); |
| 3985 | return __io_splice_prep(req, sqe); |
| 3986 | } |
| 3987 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3988 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3989 | { |
| 3990 | struct io_splice *sp = &req->splice; |
| 3991 | struct file *in = sp->file_in; |
| 3992 | struct file *out = sp->file_out; |
| 3993 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3994 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3995 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3996 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3997 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3998 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3999 | |
| 4000 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4001 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4002 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4003 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4004 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4005 | |
| 4006 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 4007 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4008 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4009 | if (ret != sp->len) |
| 4010 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4011 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4012 | return 0; |
| 4013 | } |
| 4014 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4015 | /* |
| 4016 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4017 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4018 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4019 | { |
| 4020 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4021 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4022 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4023 | return -EINVAL; |
| 4024 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4025 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4026 | return 0; |
| 4027 | } |
| 4028 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4029 | 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] | 4030 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4031 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4032 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4033 | if (!req->file) |
| 4034 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4035 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4036 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4037 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4038 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4039 | return -EINVAL; |
| 4040 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4041 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4042 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4043 | return -EINVAL; |
| 4044 | |
| 4045 | req->sync.off = READ_ONCE(sqe->off); |
| 4046 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4047 | return 0; |
| 4048 | } |
| 4049 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4050 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4051 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4052 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4053 | int ret; |
| 4054 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4055 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4056 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4057 | return -EAGAIN; |
| 4058 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4059 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4060 | end > 0 ? end : LLONG_MAX, |
| 4061 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4062 | if (ret < 0) |
| 4063 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4064 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4068 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4069 | const struct io_uring_sqe *sqe) |
| 4070 | { |
| 4071 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 4072 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4073 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4074 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4075 | |
| 4076 | req->sync.off = READ_ONCE(sqe->off); |
| 4077 | req->sync.len = READ_ONCE(sqe->addr); |
| 4078 | req->sync.mode = READ_ONCE(sqe->len); |
| 4079 | return 0; |
| 4080 | } |
| 4081 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4082 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4083 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4084 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4085 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4086 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4087 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4088 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4089 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4090 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4091 | if (ret < 0) |
| 4092 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4093 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4094 | return 0; |
| 4095 | } |
| 4096 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4097 | 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] | 4098 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4099 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4100 | int ret; |
| 4101 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4102 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4103 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4104 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4105 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4106 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4107 | /* open.how should be already initialised */ |
| 4108 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4109 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4110 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4111 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4112 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4113 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4114 | if (IS_ERR(req->open.filename)) { |
| 4115 | ret = PTR_ERR(req->open.filename); |
| 4116 | req->open.filename = NULL; |
| 4117 | return ret; |
| 4118 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4119 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4120 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4121 | return 0; |
| 4122 | } |
| 4123 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4124 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4125 | { |
| 4126 | u64 flags, mode; |
| 4127 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4128 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4129 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4130 | mode = READ_ONCE(sqe->len); |
| 4131 | flags = READ_ONCE(sqe->open_flags); |
| 4132 | req->open.how = build_open_how(flags, mode); |
| 4133 | return __io_openat_prep(req, sqe); |
| 4134 | } |
| 4135 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4136 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4137 | { |
| 4138 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4139 | size_t len; |
| 4140 | int ret; |
| 4141 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4142 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4143 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4144 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4145 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4146 | if (len < OPEN_HOW_SIZE_VER0) |
| 4147 | return -EINVAL; |
| 4148 | |
| 4149 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4150 | len); |
| 4151 | if (ret) |
| 4152 | return ret; |
| 4153 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4154 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4155 | } |
| 4156 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4157 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4158 | { |
| 4159 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4160 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4161 | bool nonblock_set; |
| 4162 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4163 | int ret; |
| 4164 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4165 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4166 | if (ret) |
| 4167 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4168 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4169 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4170 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4171 | /* |
| 4172 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4173 | * it'll always -EAGAIN |
| 4174 | */ |
| 4175 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4176 | return -EAGAIN; |
| 4177 | op.lookup_flags |= LOOKUP_CACHED; |
| 4178 | op.open_flag |= O_NONBLOCK; |
| 4179 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4180 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4181 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4182 | if (ret < 0) |
| 4183 | goto err; |
| 4184 | |
| 4185 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4186 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4187 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 4188 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4189 | /* |
| 4190 | * We could hang on to this 'fd', but seems like marginal |
| 4191 | * gain for something that is now known to be a slower path. |
| 4192 | * So just put it, and we'll get a new one when we retry. |
| 4193 | */ |
| 4194 | put_unused_fd(ret); |
| 4195 | return -EAGAIN; |
| 4196 | } |
| 4197 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4198 | if (IS_ERR(file)) { |
| 4199 | put_unused_fd(ret); |
| 4200 | ret = PTR_ERR(file); |
| 4201 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4202 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4203 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4204 | fsnotify_open(file); |
| 4205 | fd_install(ret, file); |
| 4206 | } |
| 4207 | err: |
| 4208 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4209 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4210 | if (ret < 0) |
| 4211 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4212 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4213 | return 0; |
| 4214 | } |
| 4215 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4216 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4217 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4218 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4221 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4222 | const struct io_uring_sqe *sqe) |
| 4223 | { |
| 4224 | struct io_provide_buf *p = &req->pbuf; |
| 4225 | u64 tmp; |
| 4226 | |
| 4227 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4228 | return -EINVAL; |
| 4229 | |
| 4230 | tmp = READ_ONCE(sqe->fd); |
| 4231 | if (!tmp || tmp > USHRT_MAX) |
| 4232 | return -EINVAL; |
| 4233 | |
| 4234 | memset(p, 0, sizeof(*p)); |
| 4235 | p->nbufs = tmp; |
| 4236 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4237 | return 0; |
| 4238 | } |
| 4239 | |
| 4240 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4241 | int bgid, unsigned nbufs) |
| 4242 | { |
| 4243 | unsigned i = 0; |
| 4244 | |
| 4245 | /* shouldn't happen */ |
| 4246 | if (!nbufs) |
| 4247 | return 0; |
| 4248 | |
| 4249 | /* the head kbuf is the list itself */ |
| 4250 | while (!list_empty(&buf->list)) { |
| 4251 | struct io_buffer *nxt; |
| 4252 | |
| 4253 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4254 | list_del(&nxt->list); |
| 4255 | kfree(nxt); |
| 4256 | if (++i == nbufs) |
| 4257 | return i; |
| 4258 | } |
| 4259 | i++; |
| 4260 | kfree(buf); |
| 4261 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4262 | |
| 4263 | return i; |
| 4264 | } |
| 4265 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4266 | 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] | 4267 | { |
| 4268 | struct io_provide_buf *p = &req->pbuf; |
| 4269 | struct io_ring_ctx *ctx = req->ctx; |
| 4270 | struct io_buffer *head; |
| 4271 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4272 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4273 | |
| 4274 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4275 | |
| 4276 | lockdep_assert_held(&ctx->uring_lock); |
| 4277 | |
| 4278 | ret = -ENOENT; |
| 4279 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4280 | if (head) |
| 4281 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4282 | if (ret < 0) |
| 4283 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4284 | |
| 4285 | /* need to hold the lock to complete IOPOLL requests */ |
| 4286 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4287 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4288 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4289 | } else { |
| 4290 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4291 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4292 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4293 | return 0; |
| 4294 | } |
| 4295 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4296 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4297 | const struct io_uring_sqe *sqe) |
| 4298 | { |
| 4299 | struct io_provide_buf *p = &req->pbuf; |
| 4300 | u64 tmp; |
| 4301 | |
| 4302 | if (sqe->ioprio || sqe->rw_flags) |
| 4303 | return -EINVAL; |
| 4304 | |
| 4305 | tmp = READ_ONCE(sqe->fd); |
| 4306 | if (!tmp || tmp > USHRT_MAX) |
| 4307 | return -E2BIG; |
| 4308 | p->nbufs = tmp; |
| 4309 | p->addr = READ_ONCE(sqe->addr); |
| 4310 | p->len = READ_ONCE(sqe->len); |
| 4311 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4312 | 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] | 4313 | return -EFAULT; |
| 4314 | |
| 4315 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4316 | tmp = READ_ONCE(sqe->off); |
| 4317 | if (tmp > USHRT_MAX) |
| 4318 | return -E2BIG; |
| 4319 | p->bid = tmp; |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
| 4323 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4324 | { |
| 4325 | struct io_buffer *buf; |
| 4326 | u64 addr = pbuf->addr; |
| 4327 | int i, bid = pbuf->bid; |
| 4328 | |
| 4329 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4330 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4331 | if (!buf) |
| 4332 | break; |
| 4333 | |
| 4334 | buf->addr = addr; |
| 4335 | buf->len = pbuf->len; |
| 4336 | buf->bid = bid; |
| 4337 | addr += pbuf->len; |
| 4338 | bid++; |
| 4339 | if (!*head) { |
| 4340 | INIT_LIST_HEAD(&buf->list); |
| 4341 | *head = buf; |
| 4342 | } else { |
| 4343 | list_add_tail(&buf->list, &(*head)->list); |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | return i ? i : -ENOMEM; |
| 4348 | } |
| 4349 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4350 | 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] | 4351 | { |
| 4352 | struct io_provide_buf *p = &req->pbuf; |
| 4353 | struct io_ring_ctx *ctx = req->ctx; |
| 4354 | struct io_buffer *head, *list; |
| 4355 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4356 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4357 | |
| 4358 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4359 | |
| 4360 | lockdep_assert_held(&ctx->uring_lock); |
| 4361 | |
| 4362 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4363 | |
| 4364 | ret = io_add_buffers(p, &head); |
| 4365 | if (ret < 0) |
| 4366 | goto out; |
| 4367 | |
| 4368 | if (!list) { |
| 4369 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4370 | GFP_KERNEL); |
| 4371 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4372 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4373 | goto out; |
| 4374 | } |
| 4375 | } |
| 4376 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4377 | if (ret < 0) |
| 4378 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4379 | |
| 4380 | /* need to hold the lock to complete IOPOLL requests */ |
| 4381 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4382 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4383 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4384 | } else { |
| 4385 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4386 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4387 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4388 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4391 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4392 | const struct io_uring_sqe *sqe) |
| 4393 | { |
| 4394 | #if defined(CONFIG_EPOLL) |
| 4395 | if (sqe->ioprio || sqe->buf_index) |
| 4396 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4397 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4398 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4399 | |
| 4400 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4401 | req->epoll.op = READ_ONCE(sqe->len); |
| 4402 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4403 | |
| 4404 | if (ep_op_has_event(req->epoll.op)) { |
| 4405 | struct epoll_event __user *ev; |
| 4406 | |
| 4407 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4408 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4409 | return -EFAULT; |
| 4410 | } |
| 4411 | |
| 4412 | return 0; |
| 4413 | #else |
| 4414 | return -EOPNOTSUPP; |
| 4415 | #endif |
| 4416 | } |
| 4417 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4418 | 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] | 4419 | { |
| 4420 | #if defined(CONFIG_EPOLL) |
| 4421 | struct io_epoll *ie = &req->epoll; |
| 4422 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4423 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4424 | |
| 4425 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4426 | if (force_nonblock && ret == -EAGAIN) |
| 4427 | return -EAGAIN; |
| 4428 | |
| 4429 | if (ret < 0) |
| 4430 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4431 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4432 | return 0; |
| 4433 | #else |
| 4434 | return -EOPNOTSUPP; |
| 4435 | #endif |
| 4436 | } |
| 4437 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4438 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4439 | { |
| 4440 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4441 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4442 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4443 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4444 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4445 | |
| 4446 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4447 | req->madvise.len = READ_ONCE(sqe->len); |
| 4448 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4449 | return 0; |
| 4450 | #else |
| 4451 | return -EOPNOTSUPP; |
| 4452 | #endif |
| 4453 | } |
| 4454 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4455 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4456 | { |
| 4457 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4458 | struct io_madvise *ma = &req->madvise; |
| 4459 | int ret; |
| 4460 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4461 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4462 | return -EAGAIN; |
| 4463 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4464 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4465 | if (ret < 0) |
| 4466 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4467 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4468 | return 0; |
| 4469 | #else |
| 4470 | return -EOPNOTSUPP; |
| 4471 | #endif |
| 4472 | } |
| 4473 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4474 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4475 | { |
| 4476 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4477 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4478 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4479 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4480 | |
| 4481 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4482 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4483 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4487 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4488 | { |
| 4489 | struct io_fadvise *fa = &req->fadvise; |
| 4490 | int ret; |
| 4491 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4492 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4493 | switch (fa->advice) { |
| 4494 | case POSIX_FADV_NORMAL: |
| 4495 | case POSIX_FADV_RANDOM: |
| 4496 | case POSIX_FADV_SEQUENTIAL: |
| 4497 | break; |
| 4498 | default: |
| 4499 | return -EAGAIN; |
| 4500 | } |
| 4501 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4502 | |
| 4503 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4504 | if (ret < 0) |
| 4505 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4506 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4507 | return 0; |
| 4508 | } |
| 4509 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4510 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4511 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4512 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4513 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4514 | if (sqe->ioprio || sqe->buf_index) |
| 4515 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4516 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4517 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4518 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4519 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4520 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4521 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4522 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4523 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4524 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4525 | return 0; |
| 4526 | } |
| 4527 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4528 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4529 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4530 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4531 | int ret; |
| 4532 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4533 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4534 | /* only need file table for an actual valid fd */ |
| 4535 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4536 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4537 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4538 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4539 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4540 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4541 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4542 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4543 | if (ret < 0) |
| 4544 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4545 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | } |
| 4548 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4549 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4550 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4551 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4552 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4553 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4554 | sqe->rw_flags || sqe->buf_index) |
| 4555 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4556 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4557 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4558 | |
| 4559 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4560 | return 0; |
| 4561 | } |
| 4562 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4563 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4564 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4565 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4566 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4567 | struct fdtable *fdt; |
| 4568 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4569 | int ret; |
| 4570 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4571 | file = NULL; |
| 4572 | ret = -EBADF; |
| 4573 | spin_lock(&files->file_lock); |
| 4574 | fdt = files_fdtable(files); |
| 4575 | if (close->fd >= fdt->max_fds) { |
| 4576 | spin_unlock(&files->file_lock); |
| 4577 | goto err; |
| 4578 | } |
| 4579 | file = fdt->fd[close->fd]; |
| 4580 | if (!file) { |
| 4581 | spin_unlock(&files->file_lock); |
| 4582 | goto err; |
| 4583 | } |
| 4584 | |
| 4585 | if (file->f_op == &io_uring_fops) { |
| 4586 | spin_unlock(&files->file_lock); |
| 4587 | file = NULL; |
| 4588 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4589 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4590 | |
| 4591 | /* 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] | 4592 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4593 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4594 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4595 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4596 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4597 | ret = __close_fd_get_file(close->fd, &file); |
| 4598 | spin_unlock(&files->file_lock); |
| 4599 | if (ret < 0) { |
| 4600 | if (ret == -ENOENT) |
| 4601 | ret = -EBADF; |
| 4602 | goto err; |
| 4603 | } |
| 4604 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4605 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4606 | ret = filp_close(file, current->files); |
| 4607 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4608 | if (ret < 0) |
| 4609 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4610 | if (file) |
| 4611 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4612 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4613 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4616 | 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] | 4617 | { |
| 4618 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4619 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4620 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4621 | return -EINVAL; |
| 4622 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4623 | return -EINVAL; |
| 4624 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4625 | req->sync.off = READ_ONCE(sqe->off); |
| 4626 | req->sync.len = READ_ONCE(sqe->len); |
| 4627 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4628 | return 0; |
| 4629 | } |
| 4630 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4631 | 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] | 4632 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4633 | int ret; |
| 4634 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4635 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4636 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4637 | return -EAGAIN; |
| 4638 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4639 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4640 | req->sync.flags); |
| 4641 | if (ret < 0) |
| 4642 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4643 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4644 | return 0; |
| 4645 | } |
| 4646 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4647 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4648 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4649 | struct io_async_msghdr *kmsg) |
| 4650 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4651 | struct io_async_msghdr *async_msg = req->async_data; |
| 4652 | |
| 4653 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4654 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4655 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4656 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4657 | return -ENOMEM; |
| 4658 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4659 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4660 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4661 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4662 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4663 | /* if were using fast_iov, set it to the new one */ |
| 4664 | if (!async_msg->free_iov) |
| 4665 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4666 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4667 | return -EAGAIN; |
| 4668 | } |
| 4669 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4670 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4671 | struct io_async_msghdr *iomsg) |
| 4672 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4673 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4674 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4675 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4676 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4677 | } |
| 4678 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4679 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4680 | { |
| 4681 | int ret; |
| 4682 | |
| 4683 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4684 | return 0; |
| 4685 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4686 | if (!ret) |
| 4687 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4688 | return ret; |
| 4689 | } |
| 4690 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4691 | 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] | 4692 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4693 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4694 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4695 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4696 | return -EINVAL; |
| 4697 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4698 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4699 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4700 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4701 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4702 | #ifdef CONFIG_COMPAT |
| 4703 | if (req->ctx->compat) |
| 4704 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4705 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4706 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4709 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4710 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4711 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4712 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4713 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4714 | int ret; |
| 4715 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4716 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4717 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4718 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4719 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4720 | kmsg = req->async_data; |
| 4721 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4722 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4723 | if (ret) |
| 4724 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4725 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4728 | flags = req->sr_msg.msg_flags; |
| 4729 | if (flags & MSG_DONTWAIT) |
| 4730 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4731 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4732 | flags |= MSG_DONTWAIT; |
| 4733 | |
| 4734 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4735 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4736 | return io_setup_async_msg(req, kmsg); |
| 4737 | if (ret == -ERESTARTSYS) |
| 4738 | ret = -EINTR; |
| 4739 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4740 | /* fast path, check for non-NULL to avoid function call */ |
| 4741 | if (kmsg->free_iov) |
| 4742 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4743 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4744 | if (ret < 0) |
| 4745 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4746 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4747 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4748 | } |
| 4749 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4750 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4751 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4752 | struct io_sr_msg *sr = &req->sr_msg; |
| 4753 | struct msghdr msg; |
| 4754 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4755 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4756 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4757 | int ret; |
| 4758 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4759 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4760 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4761 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4762 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4763 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4764 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4765 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4766 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4767 | msg.msg_name = NULL; |
| 4768 | msg.msg_control = NULL; |
| 4769 | msg.msg_controllen = 0; |
| 4770 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4771 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4772 | flags = req->sr_msg.msg_flags; |
| 4773 | if (flags & MSG_DONTWAIT) |
| 4774 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4775 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4776 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4777 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4778 | msg.msg_flags = flags; |
| 4779 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4780 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4781 | return -EAGAIN; |
| 4782 | if (ret == -ERESTARTSYS) |
| 4783 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4784 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4785 | if (ret < 0) |
| 4786 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4787 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4788 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4789 | } |
| 4790 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4791 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4792 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4793 | { |
| 4794 | struct io_sr_msg *sr = &req->sr_msg; |
| 4795 | struct iovec __user *uiov; |
| 4796 | size_t iov_len; |
| 4797 | int ret; |
| 4798 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4799 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4800 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4801 | if (ret) |
| 4802 | return ret; |
| 4803 | |
| 4804 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4805 | if (iov_len > 1) |
| 4806 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4807 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4808 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4809 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4810 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4811 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4812 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4813 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4814 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4815 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4816 | if (ret > 0) |
| 4817 | ret = 0; |
| 4818 | } |
| 4819 | |
| 4820 | return ret; |
| 4821 | } |
| 4822 | |
| 4823 | #ifdef CONFIG_COMPAT |
| 4824 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4825 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4826 | { |
| 4827 | struct compat_msghdr __user *msg_compat; |
| 4828 | struct io_sr_msg *sr = &req->sr_msg; |
| 4829 | struct compat_iovec __user *uiov; |
| 4830 | compat_uptr_t ptr; |
| 4831 | compat_size_t len; |
| 4832 | int ret; |
| 4833 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4834 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4835 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4836 | &ptr, &len); |
| 4837 | if (ret) |
| 4838 | return ret; |
| 4839 | |
| 4840 | uiov = compat_ptr(ptr); |
| 4841 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4842 | compat_ssize_t clen; |
| 4843 | |
| 4844 | if (len > 1) |
| 4845 | return -EINVAL; |
| 4846 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4847 | return -EFAULT; |
| 4848 | if (__get_user(clen, &uiov->iov_len)) |
| 4849 | return -EFAULT; |
| 4850 | if (clen < 0) |
| 4851 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4852 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4853 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4854 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4855 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4856 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4857 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4858 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4859 | if (ret < 0) |
| 4860 | return ret; |
| 4861 | } |
| 4862 | |
| 4863 | return 0; |
| 4864 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4865 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4866 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4867 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4868 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4869 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4870 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4871 | |
| 4872 | #ifdef CONFIG_COMPAT |
| 4873 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4874 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4875 | #endif |
| 4876 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4877 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4878 | } |
| 4879 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4880 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4881 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4882 | { |
| 4883 | struct io_sr_msg *sr = &req->sr_msg; |
| 4884 | struct io_buffer *kbuf; |
| 4885 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4886 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4887 | if (IS_ERR(kbuf)) |
| 4888 | return kbuf; |
| 4889 | |
| 4890 | sr->kbuf = kbuf; |
| 4891 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4892 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4895 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4896 | { |
| 4897 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4898 | } |
| 4899 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4900 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4901 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4902 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4903 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4904 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4905 | return 0; |
| 4906 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4907 | if (!ret) |
| 4908 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4909 | return ret; |
| 4910 | } |
| 4911 | |
| 4912 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4913 | { |
| 4914 | struct io_sr_msg *sr = &req->sr_msg; |
| 4915 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4916 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4917 | return -EINVAL; |
| 4918 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4919 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4920 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4921 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4922 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4923 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4924 | #ifdef CONFIG_COMPAT |
| 4925 | if (req->ctx->compat) |
| 4926 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4927 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4928 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4929 | } |
| 4930 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4931 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4932 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4933 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4934 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4935 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4936 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4937 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4938 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4939 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4940 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4941 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4942 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4943 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4944 | kmsg = req->async_data; |
| 4945 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4946 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4947 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4948 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4949 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4950 | } |
| 4951 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4952 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4953 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4954 | if (IS_ERR(kbuf)) |
| 4955 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4956 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4957 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4958 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4959 | 1, req->sr_msg.len); |
| 4960 | } |
| 4961 | |
| 4962 | flags = req->sr_msg.msg_flags; |
| 4963 | if (flags & MSG_DONTWAIT) |
| 4964 | req->flags |= REQ_F_NOWAIT; |
| 4965 | else if (force_nonblock) |
| 4966 | flags |= MSG_DONTWAIT; |
| 4967 | |
| 4968 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4969 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4970 | if (force_nonblock && ret == -EAGAIN) |
| 4971 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4972 | if (ret == -ERESTARTSYS) |
| 4973 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4974 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4975 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4976 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4977 | /* fast path, check for non-NULL to avoid function call */ |
| 4978 | if (kmsg->free_iov) |
| 4979 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4980 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4981 | if (ret < 0) |
| 4982 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4983 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4984 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4985 | } |
| 4986 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4987 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4988 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4989 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4990 | struct io_sr_msg *sr = &req->sr_msg; |
| 4991 | struct msghdr msg; |
| 4992 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4993 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4994 | struct iovec iov; |
| 4995 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4996 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4997 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4998 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4999 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5000 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5001 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5002 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5003 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5004 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5005 | if (IS_ERR(kbuf)) |
| 5006 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5007 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5010 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5011 | if (unlikely(ret)) |
| 5012 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5013 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5014 | msg.msg_name = NULL; |
| 5015 | msg.msg_control = NULL; |
| 5016 | msg.msg_controllen = 0; |
| 5017 | msg.msg_namelen = 0; |
| 5018 | msg.msg_iocb = NULL; |
| 5019 | msg.msg_flags = 0; |
| 5020 | |
| 5021 | flags = req->sr_msg.msg_flags; |
| 5022 | if (flags & MSG_DONTWAIT) |
| 5023 | req->flags |= REQ_F_NOWAIT; |
| 5024 | else if (force_nonblock) |
| 5025 | flags |= MSG_DONTWAIT; |
| 5026 | |
| 5027 | ret = sock_recvmsg(sock, &msg, flags); |
| 5028 | if (force_nonblock && ret == -EAGAIN) |
| 5029 | return -EAGAIN; |
| 5030 | if (ret == -ERESTARTSYS) |
| 5031 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5032 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5033 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5034 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5035 | if (ret < 0) |
| 5036 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5037 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5038 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5039 | } |
| 5040 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5041 | 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] | 5042 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5043 | struct io_accept *accept = &req->accept; |
| 5044 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5045 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5046 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 5047 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5048 | return -EINVAL; |
| 5049 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5050 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5051 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5052 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5053 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5054 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5055 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5056 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5057 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5058 | { |
| 5059 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5060 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5061 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5062 | int ret; |
| 5063 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5064 | if (req->file->f_flags & O_NONBLOCK) |
| 5065 | req->flags |= REQ_F_NOWAIT; |
| 5066 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5067 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5068 | accept->addr_len, accept->flags, |
| 5069 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5070 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5071 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5072 | if (ret < 0) { |
| 5073 | if (ret == -ERESTARTSYS) |
| 5074 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5075 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5076 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5077 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5078 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5081 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5082 | { |
| 5083 | struct io_async_connect *io = req->async_data; |
| 5084 | struct io_connect *conn = &req->connect; |
| 5085 | |
| 5086 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5087 | } |
| 5088 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5089 | 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] | 5090 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5091 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5092 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5093 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5094 | return -EINVAL; |
| 5095 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5096 | return -EINVAL; |
| 5097 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5098 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5099 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5100 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5103 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5104 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5105 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5106 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5107 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5108 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5109 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5110 | if (req->async_data) { |
| 5111 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5112 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5113 | ret = move_addr_to_kernel(req->connect.addr, |
| 5114 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5115 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5116 | if (ret) |
| 5117 | goto out; |
| 5118 | io = &__io; |
| 5119 | } |
| 5120 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5121 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5122 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5123 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5124 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5125 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5126 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5127 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5128 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5129 | ret = -ENOMEM; |
| 5130 | goto out; |
| 5131 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5132 | io = req->async_data; |
| 5133 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5134 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5135 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5136 | if (ret == -ERESTARTSYS) |
| 5137 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5138 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5139 | if (ret < 0) |
| 5140 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5141 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5142 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5143 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5144 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5145 | #define IO_NETOP_FN(op) \ |
| 5146 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5147 | { \ |
| 5148 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5149 | } |
| 5150 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5151 | #define IO_NETOP_PREP(op) \ |
| 5152 | IO_NETOP_FN(op) \ |
| 5153 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5154 | { \ |
| 5155 | return -EOPNOTSUPP; \ |
| 5156 | } \ |
| 5157 | |
| 5158 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5159 | IO_NETOP_PREP(op) \ |
| 5160 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5161 | { \ |
| 5162 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5163 | } |
| 5164 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5165 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5166 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5167 | IO_NETOP_PREP_ASYNC(connect); |
| 5168 | IO_NETOP_PREP(accept); |
| 5169 | IO_NETOP_FN(send); |
| 5170 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5171 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5172 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5173 | struct io_poll_table { |
| 5174 | struct poll_table_struct pt; |
| 5175 | struct io_kiocb *req; |
| 5176 | int error; |
| 5177 | }; |
| 5178 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5179 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5180 | __poll_t mask, task_work_func_t func) |
| 5181 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5182 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5183 | |
| 5184 | /* for instances that support it check for an event match first: */ |
| 5185 | if (mask && !(mask & poll->events)) |
| 5186 | return 0; |
| 5187 | |
| 5188 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5189 | |
| 5190 | list_del_init(&poll->wait.entry); |
| 5191 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5192 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 5193 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5194 | percpu_ref_get(&req->ctx->refs); |
| 5195 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5196 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5197 | * If this fails, then the task is exiting. When a task exits, the |
| 5198 | * work gets canceled, so just cancel this request as well instead |
| 5199 | * of executing it. We can't safely execute it anyway, as we may not |
| 5200 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5201 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5202 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5203 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5204 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 5205 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5206 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5207 | return 1; |
| 5208 | } |
| 5209 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5210 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5211 | __acquires(&req->ctx->completion_lock) |
| 5212 | { |
| 5213 | struct io_ring_ctx *ctx = req->ctx; |
| 5214 | |
| 5215 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5216 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5217 | |
| 5218 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5219 | } |
| 5220 | |
| 5221 | spin_lock_irq(&ctx->completion_lock); |
| 5222 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5223 | add_wait_queue(poll->head, &poll->wait); |
| 5224 | return true; |
| 5225 | } |
| 5226 | |
| 5227 | return false; |
| 5228 | } |
| 5229 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5230 | 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] | 5231 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5232 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5233 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5234 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5235 | return req->apoll->double_poll; |
| 5236 | } |
| 5237 | |
| 5238 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5239 | { |
| 5240 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5241 | return &req->poll; |
| 5242 | return &req->apoll->poll; |
| 5243 | } |
| 5244 | |
| 5245 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5246 | { |
| 5247 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5248 | |
| 5249 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5250 | |
| 5251 | if (poll && poll->head) { |
| 5252 | struct wait_queue_head *head = poll->head; |
| 5253 | |
| 5254 | spin_lock(&head->lock); |
| 5255 | list_del_init(&poll->wait.entry); |
| 5256 | if (poll->wait.private) |
| 5257 | refcount_dec(&req->refs); |
| 5258 | poll->head = NULL; |
| 5259 | spin_unlock(&head->lock); |
| 5260 | } |
| 5261 | } |
| 5262 | |
| 5263 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5264 | { |
| 5265 | struct io_ring_ctx *ctx = req->ctx; |
| 5266 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5267 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5268 | req->poll.done = true; |
| 5269 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5270 | io_commit_cqring(ctx); |
| 5271 | } |
| 5272 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5273 | static void io_poll_task_func(struct callback_head *cb) |
| 5274 | { |
| 5275 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5276 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5277 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5278 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5279 | if (io_poll_rewait(req, &req->poll)) { |
| 5280 | spin_unlock_irq(&ctx->completion_lock); |
| 5281 | } else { |
| 5282 | hash_del(&req->hash_node); |
| 5283 | io_poll_complete(req, req->result, 0); |
| 5284 | spin_unlock_irq(&ctx->completion_lock); |
| 5285 | |
| 5286 | nxt = io_put_req_find_next(req); |
| 5287 | io_cqring_ev_posted(ctx); |
| 5288 | if (nxt) |
| 5289 | __io_req_task_submit(nxt); |
| 5290 | } |
| 5291 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5292 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5293 | } |
| 5294 | |
| 5295 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5296 | int sync, void *key) |
| 5297 | { |
| 5298 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5299 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5300 | __poll_t mask = key_to_poll(key); |
| 5301 | |
| 5302 | /* for instances that support it check for an event match first: */ |
| 5303 | if (mask && !(mask & poll->events)) |
| 5304 | return 0; |
| 5305 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5306 | list_del_init(&wait->entry); |
| 5307 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5308 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5309 | bool done; |
| 5310 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5311 | spin_lock(&poll->head->lock); |
| 5312 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5313 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5314 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5315 | /* make sure double remove sees this as being gone */ |
| 5316 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5317 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5318 | if (!done) { |
| 5319 | /* use wait func handler, so it matches the rq type */ |
| 5320 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5321 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5322 | } |
| 5323 | refcount_dec(&req->refs); |
| 5324 | return 1; |
| 5325 | } |
| 5326 | |
| 5327 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5328 | wait_queue_func_t wake_func) |
| 5329 | { |
| 5330 | poll->head = NULL; |
| 5331 | poll->done = false; |
| 5332 | poll->canceled = false; |
| 5333 | poll->events = events; |
| 5334 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5335 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5336 | } |
| 5337 | |
| 5338 | 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] | 5339 | struct wait_queue_head *head, |
| 5340 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5341 | { |
| 5342 | struct io_kiocb *req = pt->req; |
| 5343 | |
| 5344 | /* |
| 5345 | * If poll->head is already set, it's because the file being polled |
| 5346 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5347 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5348 | */ |
| 5349 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5350 | struct io_poll_iocb *poll_one = poll; |
| 5351 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5352 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5353 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5354 | pt->error = -EINVAL; |
| 5355 | return; |
| 5356 | } |
| 5357 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5358 | if (!poll) { |
| 5359 | pt->error = -ENOMEM; |
| 5360 | return; |
| 5361 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5362 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5363 | refcount_inc(&req->refs); |
| 5364 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5365 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | pt->error = 0; |
| 5369 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5370 | |
| 5371 | if (poll->events & EPOLLEXCLUSIVE) |
| 5372 | add_wait_queue_exclusive(head, &poll->wait); |
| 5373 | else |
| 5374 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5378 | struct poll_table_struct *p) |
| 5379 | { |
| 5380 | 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] | 5381 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5382 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5383 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5384 | } |
| 5385 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5386 | static void io_async_task_func(struct callback_head *cb) |
| 5387 | { |
| 5388 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5389 | struct async_poll *apoll = req->apoll; |
| 5390 | struct io_ring_ctx *ctx = req->ctx; |
| 5391 | |
| 5392 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5393 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5394 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5395 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5396 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5397 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5398 | } |
| 5399 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5400 | /* 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] | 5401 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5402 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5403 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5404 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5405 | spin_unlock_irq(&ctx->completion_lock); |
| 5406 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5407 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5408 | __io_req_task_submit(req); |
| 5409 | else |
| 5410 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5411 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5412 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5413 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5414 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5418 | void *key) |
| 5419 | { |
| 5420 | struct io_kiocb *req = wait->private; |
| 5421 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5422 | |
| 5423 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5424 | key_to_poll(key)); |
| 5425 | |
| 5426 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5427 | } |
| 5428 | |
| 5429 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5430 | { |
| 5431 | struct io_ring_ctx *ctx = req->ctx; |
| 5432 | struct hlist_head *list; |
| 5433 | |
| 5434 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5435 | hlist_add_head(&req->hash_node, list); |
| 5436 | } |
| 5437 | |
| 5438 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5439 | struct io_poll_iocb *poll, |
| 5440 | struct io_poll_table *ipt, __poll_t mask, |
| 5441 | wait_queue_func_t wake_func) |
| 5442 | __acquires(&ctx->completion_lock) |
| 5443 | { |
| 5444 | struct io_ring_ctx *ctx = req->ctx; |
| 5445 | bool cancel = false; |
| 5446 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5447 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5448 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5449 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5450 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5451 | |
| 5452 | ipt->pt._key = mask; |
| 5453 | ipt->req = req; |
| 5454 | ipt->error = -EINVAL; |
| 5455 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5456 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5457 | |
| 5458 | spin_lock_irq(&ctx->completion_lock); |
| 5459 | if (likely(poll->head)) { |
| 5460 | spin_lock(&poll->head->lock); |
| 5461 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5462 | if (ipt->error) |
| 5463 | cancel = true; |
| 5464 | ipt->error = 0; |
| 5465 | mask = 0; |
| 5466 | } |
| 5467 | if (mask || ipt->error) |
| 5468 | list_del_init(&poll->wait.entry); |
| 5469 | else if (cancel) |
| 5470 | WRITE_ONCE(poll->canceled, true); |
| 5471 | else if (!poll->done) /* actually waiting for an event */ |
| 5472 | io_poll_req_insert(req); |
| 5473 | spin_unlock(&poll->head->lock); |
| 5474 | } |
| 5475 | |
| 5476 | return mask; |
| 5477 | } |
| 5478 | |
| 5479 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5480 | { |
| 5481 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5482 | struct io_ring_ctx *ctx = req->ctx; |
| 5483 | struct async_poll *apoll; |
| 5484 | struct io_poll_table ipt; |
| 5485 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5486 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5487 | |
| 5488 | if (!req->file || !file_can_poll(req->file)) |
| 5489 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5490 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5491 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5492 | if (def->pollin) |
| 5493 | rw = READ; |
| 5494 | else if (def->pollout) |
| 5495 | rw = WRITE; |
| 5496 | else |
| 5497 | return false; |
| 5498 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5499 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5500 | return false; |
| 5501 | |
| 5502 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5503 | if (unlikely(!apoll)) |
| 5504 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5505 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5506 | |
| 5507 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5508 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5509 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5510 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5511 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5512 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5513 | if (def->pollout) |
| 5514 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5515 | |
| 5516 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5517 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5518 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5519 | mask &= ~POLLIN; |
| 5520 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5521 | mask |= POLLERR | POLLPRI; |
| 5522 | |
| 5523 | ipt.pt._qproc = io_async_queue_proc; |
| 5524 | |
| 5525 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5526 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5527 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5528 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5529 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5530 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5531 | kfree(apoll); |
| 5532 | return false; |
| 5533 | } |
| 5534 | spin_unlock_irq(&ctx->completion_lock); |
| 5535 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5536 | apoll->poll.events); |
| 5537 | return true; |
| 5538 | } |
| 5539 | |
| 5540 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5541 | struct io_poll_iocb *poll) |
| 5542 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5543 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5544 | |
| 5545 | spin_lock(&poll->head->lock); |
| 5546 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5547 | if (!list_empty(&poll->wait.entry)) { |
| 5548 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5549 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5550 | } |
| 5551 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5552 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5553 | return do_complete; |
| 5554 | } |
| 5555 | |
| 5556 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5557 | { |
| 5558 | bool do_complete; |
| 5559 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5560 | io_poll_remove_double(req); |
| 5561 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5562 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5563 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5564 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5565 | struct async_poll *apoll = req->apoll; |
| 5566 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5567 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5568 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5569 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5570 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5571 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5572 | kfree(apoll); |
| 5573 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5574 | } |
| 5575 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5576 | if (do_complete) { |
| 5577 | io_cqring_fill_event(req, -ECANCELED); |
| 5578 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5579 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5580 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5586 | /* |
| 5587 | * Returns true if we found and killed one or more poll requests |
| 5588 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5589 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5590 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5591 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5592 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5593 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5594 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5595 | |
| 5596 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5597 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5598 | struct hlist_head *list; |
| 5599 | |
| 5600 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5601 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5602 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5603 | posted += io_poll_remove_one(req); |
| 5604 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5605 | } |
| 5606 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5607 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5608 | if (posted) |
| 5609 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5610 | |
| 5611 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5612 | } |
| 5613 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5614 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5615 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5616 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5617 | struct io_kiocb *req; |
| 5618 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5619 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5620 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5621 | if (sqe_addr != req->user_data) |
| 5622 | continue; |
| 5623 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5624 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5625 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | return -ENOENT; |
| 5629 | } |
| 5630 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5631 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5632 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5633 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5634 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5635 | return -EINVAL; |
| 5636 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5637 | sqe->poll_events) |
| 5638 | return -EINVAL; |
| 5639 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5640 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5641 | return 0; |
| 5642 | } |
| 5643 | |
| 5644 | /* |
| 5645 | * Find a running poll command that matches one specified in sqe->addr, |
| 5646 | * and remove it if found. |
| 5647 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5648 | 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] | 5649 | { |
| 5650 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5651 | int ret; |
| 5652 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5653 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5654 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5655 | spin_unlock_irq(&ctx->completion_lock); |
| 5656 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5657 | if (ret < 0) |
| 5658 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5659 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5660 | return 0; |
| 5661 | } |
| 5662 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5663 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5664 | void *key) |
| 5665 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5666 | struct io_kiocb *req = wait->private; |
| 5667 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5668 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5669 | 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] | 5670 | } |
| 5671 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5672 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5673 | struct poll_table_struct *p) |
| 5674 | { |
| 5675 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5676 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5677 | __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] | 5678 | } |
| 5679 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5680 | 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] | 5681 | { |
| 5682 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5683 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5684 | |
| 5685 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5686 | return -EINVAL; |
| 5687 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5688 | return -EINVAL; |
| 5689 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5690 | events = READ_ONCE(sqe->poll32_events); |
| 5691 | #ifdef __BIG_ENDIAN |
| 5692 | events = swahw32(events); |
| 5693 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5694 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5695 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5696 | return 0; |
| 5697 | } |
| 5698 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5699 | 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] | 5700 | { |
| 5701 | struct io_poll_iocb *poll = &req->poll; |
| 5702 | struct io_ring_ctx *ctx = req->ctx; |
| 5703 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5704 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5705 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5706 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5707 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5708 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5709 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5710 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5711 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5712 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5713 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5714 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5715 | spin_unlock_irq(&ctx->completion_lock); |
| 5716 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5717 | if (mask) { |
| 5718 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5719 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5720 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5721 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5722 | } |
| 5723 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5724 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5725 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5726 | struct io_timeout_data *data = container_of(timer, |
| 5727 | struct io_timeout_data, timer); |
| 5728 | struct io_kiocb *req = data->req; |
| 5729 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5730 | unsigned long flags; |
| 5731 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5732 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5733 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5734 | atomic_set(&req->ctx->cq_timeouts, |
| 5735 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5736 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5737 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5738 | io_commit_cqring(ctx); |
| 5739 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5740 | |
| 5741 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5742 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5743 | io_put_req(req); |
| 5744 | return HRTIMER_NORESTART; |
| 5745 | } |
| 5746 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5747 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5748 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5749 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5750 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5751 | struct io_kiocb *req; |
| 5752 | int ret = -ENOENT; |
| 5753 | |
| 5754 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5755 | if (user_data == req->user_data) { |
| 5756 | ret = 0; |
| 5757 | break; |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5762 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5763 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5764 | io = req->async_data; |
| 5765 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5766 | if (ret == -1) |
| 5767 | return ERR_PTR(-EALREADY); |
| 5768 | list_del_init(&req->timeout.list); |
| 5769 | return req; |
| 5770 | } |
| 5771 | |
| 5772 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5773 | { |
| 5774 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5775 | |
| 5776 | if (IS_ERR(req)) |
| 5777 | return PTR_ERR(req); |
| 5778 | |
| 5779 | req_set_fail_links(req); |
| 5780 | io_cqring_fill_event(req, -ECANCELED); |
| 5781 | io_put_req_deferred(req, 1); |
| 5782 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5783 | } |
| 5784 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5785 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5786 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5787 | { |
| 5788 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5789 | struct io_timeout_data *data; |
| 5790 | |
| 5791 | if (IS_ERR(req)) |
| 5792 | return PTR_ERR(req); |
| 5793 | |
| 5794 | req->timeout.off = 0; /* noseq */ |
| 5795 | data = req->async_data; |
| 5796 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5797 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5798 | data->timer.function = io_timeout_fn; |
| 5799 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5800 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5801 | } |
| 5802 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5803 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5804 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5805 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5806 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5807 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5808 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5809 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5810 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5811 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5812 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5813 | return -EINVAL; |
| 5814 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5815 | tr->addr = READ_ONCE(sqe->addr); |
| 5816 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5817 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5818 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5819 | return -EINVAL; |
| 5820 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5821 | return -EFAULT; |
| 5822 | } else if (tr->flags) { |
| 5823 | /* timeout removal doesn't support flags */ |
| 5824 | return -EINVAL; |
| 5825 | } |
| 5826 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5827 | return 0; |
| 5828 | } |
| 5829 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5830 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5831 | { |
| 5832 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5833 | : HRTIMER_MODE_REL; |
| 5834 | } |
| 5835 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5836 | /* |
| 5837 | * Remove or update an existing timeout command |
| 5838 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5839 | 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] | 5840 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5841 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5842 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5843 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5844 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5845 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5846 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5847 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5848 | else |
| 5849 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5850 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5851 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5852 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5853 | io_commit_cqring(ctx); |
| 5854 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5855 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5856 | if (ret < 0) |
| 5857 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5858 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5859 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5860 | } |
| 5861 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5862 | 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] | 5863 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5864 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5865 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5866 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5867 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5868 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5869 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5870 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5871 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5872 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5873 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5874 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5875 | flags = READ_ONCE(sqe->timeout_flags); |
| 5876 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5877 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5878 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5879 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5880 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5881 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5882 | return -ENOMEM; |
| 5883 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5884 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5885 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5886 | |
| 5887 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5888 | return -EFAULT; |
| 5889 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5890 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5891 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5892 | return 0; |
| 5893 | } |
| 5894 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5895 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5896 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5897 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5898 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5899 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5900 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5901 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5902 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5903 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5904 | /* |
| 5905 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5906 | * timeout event to be satisfied. If it isn't set, then this is |
| 5907 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5908 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5909 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5910 | entry = ctx->timeout_list.prev; |
| 5911 | goto add; |
| 5912 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5913 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5914 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5915 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5916 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5917 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5918 | * This is safe because ->completion_lock is held, and submissions |
| 5919 | * and completions are never mixed in the same ->completion_lock section. |
| 5920 | */ |
| 5921 | ctx->cq_last_tm_flush = tail; |
| 5922 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5923 | /* |
| 5924 | * Insertion sort, ensuring the first entry in the list is always |
| 5925 | * the one we need first. |
| 5926 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5927 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5928 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5929 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5930 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5931 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5932 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5933 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5934 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5935 | break; |
| 5936 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5937 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5938 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5939 | data->timer.function = io_timeout_fn; |
| 5940 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5941 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5942 | return 0; |
| 5943 | } |
| 5944 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5945 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5946 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5947 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5948 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5949 | return req->user_data == (unsigned long) data; |
| 5950 | } |
| 5951 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5952 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5953 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5954 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5955 | int ret = 0; |
| 5956 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5957 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5958 | switch (cancel_ret) { |
| 5959 | case IO_WQ_CANCEL_OK: |
| 5960 | ret = 0; |
| 5961 | break; |
| 5962 | case IO_WQ_CANCEL_RUNNING: |
| 5963 | ret = -EALREADY; |
| 5964 | break; |
| 5965 | case IO_WQ_CANCEL_NOTFOUND: |
| 5966 | ret = -ENOENT; |
| 5967 | break; |
| 5968 | } |
| 5969 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5970 | return ret; |
| 5971 | } |
| 5972 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5973 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5974 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5975 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5976 | { |
| 5977 | unsigned long flags; |
| 5978 | int ret; |
| 5979 | |
| 5980 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5981 | if (ret != -ENOENT) { |
| 5982 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5983 | goto done; |
| 5984 | } |
| 5985 | |
| 5986 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5987 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5988 | if (ret != -ENOENT) |
| 5989 | goto done; |
| 5990 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5991 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5992 | if (!ret) |
| 5993 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5994 | io_cqring_fill_event(req, ret); |
| 5995 | io_commit_cqring(ctx); |
| 5996 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5997 | io_cqring_ev_posted(ctx); |
| 5998 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5999 | if (ret < 0) |
| 6000 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6001 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6004 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6005 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6006 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6007 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6008 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6009 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6010 | return -EINVAL; |
| 6011 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6012 | return -EINVAL; |
| 6013 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6014 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6015 | return 0; |
| 6016 | } |
| 6017 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6018 | 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] | 6019 | { |
| 6020 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6021 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6022 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6023 | return 0; |
| 6024 | } |
| 6025 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6026 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6027 | const struct io_uring_sqe *sqe) |
| 6028 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 6029 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 6030 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6031 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6032 | return -EINVAL; |
| 6033 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6034 | return -EINVAL; |
| 6035 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6036 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6037 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6038 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6039 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6040 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6041 | return 0; |
| 6042 | } |
| 6043 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6044 | 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] | 6045 | { |
| 6046 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6047 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6048 | int ret; |
| 6049 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6050 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6051 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6052 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6053 | up.offset = req->rsrc_update.offset; |
| 6054 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6055 | |
| 6056 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6057 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6058 | mutex_unlock(&ctx->uring_lock); |
| 6059 | |
| 6060 | if (ret < 0) |
| 6061 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6062 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6063 | return 0; |
| 6064 | } |
| 6065 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6066 | 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] | 6067 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6068 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6069 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6070 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6071 | case IORING_OP_READV: |
| 6072 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6073 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6074 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6075 | case IORING_OP_WRITEV: |
| 6076 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6077 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6078 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6079 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6080 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6081 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6082 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6083 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6084 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6085 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6086 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6087 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6088 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6089 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6090 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6091 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6092 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6093 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6094 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6095 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6096 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6097 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6098 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6099 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6100 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6101 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6102 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6103 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6104 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6105 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6106 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6107 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6108 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6109 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6110 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6111 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6112 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6113 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6114 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6115 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6116 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6117 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6118 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6119 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6120 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6121 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6122 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6123 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6124 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6125 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6126 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6127 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6128 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6129 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6130 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6131 | case IORING_OP_SHUTDOWN: |
| 6132 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6133 | case IORING_OP_RENAMEAT: |
| 6134 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6135 | case IORING_OP_UNLINKAT: |
| 6136 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6137 | } |
| 6138 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6139 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6140 | req->opcode); |
| 6141 | return-EINVAL; |
| 6142 | } |
| 6143 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6144 | static int io_req_prep_async(struct io_kiocb *req) |
| 6145 | { |
| 6146 | switch (req->opcode) { |
| 6147 | case IORING_OP_READV: |
| 6148 | case IORING_OP_READ_FIXED: |
| 6149 | case IORING_OP_READ: |
| 6150 | return io_rw_prep_async(req, READ); |
| 6151 | case IORING_OP_WRITEV: |
| 6152 | case IORING_OP_WRITE_FIXED: |
| 6153 | case IORING_OP_WRITE: |
| 6154 | return io_rw_prep_async(req, WRITE); |
| 6155 | case IORING_OP_SENDMSG: |
| 6156 | case IORING_OP_SEND: |
| 6157 | return io_sendmsg_prep_async(req); |
| 6158 | case IORING_OP_RECVMSG: |
| 6159 | case IORING_OP_RECV: |
| 6160 | return io_recvmsg_prep_async(req); |
| 6161 | case IORING_OP_CONNECT: |
| 6162 | return io_connect_prep_async(req); |
| 6163 | } |
| 6164 | return 0; |
| 6165 | } |
| 6166 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6167 | static int io_req_defer_prep(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6168 | { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6169 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6170 | return 0; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6171 | /* some opcodes init it during the inital prep */ |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6172 | if (req->async_data) |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6173 | return 0; |
| 6174 | if (__io_alloc_async_data(req)) |
| 6175 | return -EAGAIN; |
| 6176 | return io_req_prep_async(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6177 | } |
| 6178 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6179 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6180 | { |
| 6181 | struct io_kiocb *pos; |
| 6182 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6183 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6184 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6185 | io_for_each_link(pos, req) |
| 6186 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6187 | |
| 6188 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6189 | return total_submitted - nr_reqs; |
| 6190 | } |
| 6191 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6192 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6193 | { |
| 6194 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6195 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6196 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6197 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6198 | |
| 6199 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6200 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6201 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6202 | return 0; |
| 6203 | |
| 6204 | seq = io_get_sequence(req); |
| 6205 | /* Still a chance to pass the sequence check */ |
| 6206 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6207 | return 0; |
| 6208 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6209 | ret = io_req_defer_prep(req); |
| 6210 | if (ret) |
| 6211 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6212 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6213 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6214 | if (!de) |
| 6215 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6216 | |
| 6217 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6218 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6219 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6220 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6221 | io_queue_async_work(req); |
| 6222 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6223 | } |
| 6224 | |
| 6225 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6226 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6227 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6228 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6229 | spin_unlock_irq(&ctx->completion_lock); |
| 6230 | return -EIOCBQUEUED; |
| 6231 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6232 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6233 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6234 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6235 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6236 | switch (req->opcode) { |
| 6237 | case IORING_OP_READV: |
| 6238 | case IORING_OP_READ_FIXED: |
| 6239 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6240 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6241 | break; |
| 6242 | case IORING_OP_RECVMSG: |
| 6243 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6244 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6245 | break; |
| 6246 | } |
| 6247 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6248 | } |
| 6249 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6250 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6251 | switch (req->opcode) { |
| 6252 | case IORING_OP_READV: |
| 6253 | case IORING_OP_READ_FIXED: |
| 6254 | case IORING_OP_READ: |
| 6255 | case IORING_OP_WRITEV: |
| 6256 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6257 | case IORING_OP_WRITE: { |
| 6258 | struct io_async_rw *io = req->async_data; |
| 6259 | if (io->free_iovec) |
| 6260 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6261 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6262 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6263 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6264 | case IORING_OP_SENDMSG: { |
| 6265 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6266 | |
| 6267 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6268 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6269 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6270 | case IORING_OP_SPLICE: |
| 6271 | case IORING_OP_TEE: |
| 6272 | io_put_file(req, req->splice.file_in, |
| 6273 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6274 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6275 | case IORING_OP_OPENAT: |
| 6276 | case IORING_OP_OPENAT2: |
| 6277 | if (req->open.filename) |
| 6278 | putname(req->open.filename); |
| 6279 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6280 | case IORING_OP_RENAMEAT: |
| 6281 | putname(req->rename.oldpath); |
| 6282 | putname(req->rename.newpath); |
| 6283 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6284 | case IORING_OP_UNLINKAT: |
| 6285 | putname(req->unlink.filename); |
| 6286 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6287 | } |
| 6288 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6289 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6290 | } |
| 6291 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6292 | 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] | 6293 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6294 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6295 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6296 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6297 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6298 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6299 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6300 | break; |
| 6301 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6302 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6303 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6304 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6305 | break; |
| 6306 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6307 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6308 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6309 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6310 | break; |
| 6311 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6312 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6313 | break; |
| 6314 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6315 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6316 | break; |
| 6317 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6318 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6319 | break; |
| 6320 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6321 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6322 | break; |
| 6323 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6324 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6325 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6326 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6327 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6328 | break; |
| 6329 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6330 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6331 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6332 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6333 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6334 | break; |
| 6335 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6336 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6337 | break; |
| 6338 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6339 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6340 | break; |
| 6341 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6342 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6343 | break; |
| 6344 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6345 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6346 | break; |
| 6347 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6348 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6349 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6350 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6351 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6352 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6353 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6354 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6355 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6356 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6357 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6358 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6359 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6360 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6361 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6362 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6363 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6364 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6365 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6366 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6367 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6368 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6369 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6370 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6371 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6372 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6373 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6374 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6375 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6376 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6377 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6378 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6379 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6380 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6381 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6382 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6383 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6384 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6385 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6386 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6387 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6388 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6389 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6390 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6391 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6392 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6393 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6394 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6395 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6396 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6397 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6398 | default: |
| 6399 | ret = -EINVAL; |
| 6400 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6404 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6405 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6406 | /* If the op doesn't have a file, we're not polling for it */ |
| 6407 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6408 | const bool in_async = io_wq_current_is_worker(); |
| 6409 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6410 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6411 | if (in_async) |
| 6412 | mutex_lock(&ctx->uring_lock); |
| 6413 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6414 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6415 | |
| 6416 | if (in_async) |
| 6417 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | return 0; |
| 6421 | } |
| 6422 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6423 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6424 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6425 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6426 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6427 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6428 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6429 | timeout = io_prep_linked_timeout(req); |
| 6430 | if (timeout) |
| 6431 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6432 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6433 | if (work->flags & IO_WQ_WORK_CANCEL) |
| 6434 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6435 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6436 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6437 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6438 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6439 | /* |
| 6440 | * We can get EAGAIN for polled IO even though we're |
| 6441 | * forcing a sync submission from here, since we can't |
| 6442 | * wait for request slots on the block side. |
| 6443 | */ |
| 6444 | if (ret != -EAGAIN) |
| 6445 | break; |
| 6446 | cond_resched(); |
| 6447 | } while (1); |
| 6448 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6449 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6450 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6451 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6452 | /* io-wq is going to take one down */ |
| 6453 | refcount_inc(&req->refs); |
| 6454 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6455 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6456 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6457 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6458 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6459 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6460 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6461 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6462 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6463 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6464 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6465 | } |
| 6466 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6467 | static struct file *io_file_get(struct io_submit_state *state, |
| 6468 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6469 | { |
| 6470 | struct io_ring_ctx *ctx = req->ctx; |
| 6471 | struct file *file; |
| 6472 | |
| 6473 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6474 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6475 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6476 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6477 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6478 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6479 | } else { |
| 6480 | trace_io_uring_file_get(ctx, fd); |
| 6481 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6482 | } |
| 6483 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6484 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6485 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6486 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6487 | } |
| 6488 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6489 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6490 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6491 | struct io_timeout_data *data = container_of(timer, |
| 6492 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6493 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6494 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6495 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6496 | |
| 6497 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6498 | prev = req->timeout.head; |
| 6499 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6500 | |
| 6501 | /* |
| 6502 | * We don't expect the list to be empty, that will only happen if we |
| 6503 | * race with the completion of the linked work. |
| 6504 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6505 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6506 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6507 | else |
| 6508 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6509 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6510 | |
| 6511 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6512 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6513 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6514 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6515 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6516 | io_req_complete_post(req, -ETIME, 0); |
| 6517 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6518 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6519 | return HRTIMER_NORESTART; |
| 6520 | } |
| 6521 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6522 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6523 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6524 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6525 | * If the back reference is NULL, then our linked request finished |
| 6526 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6527 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6528 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6529 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6530 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6531 | data->timer.function = io_link_timeout_fn; |
| 6532 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6533 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6534 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6538 | { |
| 6539 | struct io_ring_ctx *ctx = req->ctx; |
| 6540 | |
| 6541 | spin_lock_irq(&ctx->completion_lock); |
| 6542 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6543 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6544 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6545 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6546 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6547 | } |
| 6548 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6549 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6550 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6551 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6552 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6553 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6554 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6555 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6556 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6557 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6558 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6559 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6560 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6561 | } |
| 6562 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6563 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6564 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6565 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6566 | const struct cred *old_creds = NULL; |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6567 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6568 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6569 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6570 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6571 | req->work.identity->creds != current_cred()) |
| 6572 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6573 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6574 | 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] | 6575 | |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6576 | if (old_creds) |
| 6577 | revert_creds(old_creds); |
| 6578 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6579 | /* |
| 6580 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6581 | * doesn't support non-blocking read/write attempts |
| 6582 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6583 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6584 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6585 | /* |
| 6586 | * Queued up for async execution, worker will release |
| 6587 | * submit reference when the iocb is actually submitted. |
| 6588 | */ |
| 6589 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6590 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6591 | } else if (likely(!ret)) { |
| 6592 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6593 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6594 | struct io_ring_ctx *ctx = req->ctx; |
| 6595 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 6596 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6597 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6598 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6599 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6600 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6601 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6602 | } |
| 6603 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6604 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6605 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6606 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6607 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6608 | if (linked_timeout) |
| 6609 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6610 | } |
| 6611 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6612 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6613 | { |
| 6614 | int ret; |
| 6615 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6616 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6617 | if (ret) { |
| 6618 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6619 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6620 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6621 | io_put_req(req); |
| 6622 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6623 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6624 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6625 | ret = io_req_defer_prep(req); |
| 6626 | if (unlikely(ret)) |
| 6627 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6628 | io_queue_async_work(req); |
| 6629 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6630 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6631 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6632 | } |
| 6633 | |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 6634 | /* |
| 6635 | * Check SQE restrictions (opcode and flags). |
| 6636 | * |
| 6637 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6638 | */ |
| 6639 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6640 | struct io_kiocb *req, |
| 6641 | unsigned int sqe_flags) |
| 6642 | { |
| 6643 | if (!ctx->restricted) |
| 6644 | return true; |
| 6645 | |
| 6646 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6647 | return false; |
| 6648 | |
| 6649 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6650 | ctx->restrictions.sqe_flags_required) |
| 6651 | return false; |
| 6652 | |
| 6653 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6654 | ctx->restrictions.sqe_flags_required)) |
| 6655 | return false; |
| 6656 | |
| 6657 | return true; |
| 6658 | } |
| 6659 | |
| 6660 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6661 | const struct io_uring_sqe *sqe) |
| 6662 | { |
| 6663 | struct io_submit_state *state; |
| 6664 | unsigned int sqe_flags; |
| 6665 | int id, ret = 0; |
| 6666 | |
| 6667 | req->opcode = READ_ONCE(sqe->opcode); |
| 6668 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6669 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
| 6670 | req->user_data = READ_ONCE(sqe->user_data); |
| 6671 | req->async_data = NULL; |
| 6672 | req->file = NULL; |
| 6673 | req->ctx = ctx; |
| 6674 | req->link = NULL; |
| 6675 | req->fixed_rsrc_refs = NULL; |
| 6676 | /* one is dropped after submission, the other at completion */ |
| 6677 | refcount_set(&req->refs, 2); |
| 6678 | req->task = current; |
| 6679 | req->result = 0; |
| 6680 | |
| 6681 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame^] | 6682 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6683 | req->flags = 0; |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 6684 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame^] | 6685 | } |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 6686 | |
| 6687 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6688 | return -EINVAL; |
| 6689 | |
| 6690 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
| 6691 | return -EFAULT; |
| 6692 | |
| 6693 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6694 | return -EACCES; |
| 6695 | |
| 6696 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6697 | !io_op_defs[req->opcode].buffer_select) |
| 6698 | return -EOPNOTSUPP; |
| 6699 | |
| 6700 | id = READ_ONCE(sqe->personality); |
| 6701 | if (id) { |
| 6702 | struct io_identity *iod; |
| 6703 | |
| 6704 | iod = idr_find(&ctx->personality_idr, id); |
| 6705 | if (unlikely(!iod)) |
| 6706 | return -EINVAL; |
| 6707 | refcount_inc(&iod->count); |
| 6708 | |
| 6709 | __io_req_init_async(req); |
| 6710 | get_cred(iod->creds); |
| 6711 | req->work.identity = iod; |
| 6712 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 6713 | } |
| 6714 | |
| 6715 | state = &ctx->submit_state; |
| 6716 | |
| 6717 | /* |
| 6718 | * Plug now if we have more than 1 IO left after this, and the target |
| 6719 | * is potentially a read/write to block based storage. |
| 6720 | */ |
| 6721 | if (!state->plug_started && state->ios_left > 1 && |
| 6722 | io_op_defs[req->opcode].plug) { |
| 6723 | blk_start_plug(&state->plug); |
| 6724 | state->plug_started = true; |
| 6725 | } |
| 6726 | |
| 6727 | if (io_op_defs[req->opcode].needs_file) { |
| 6728 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
| 6729 | |
| 6730 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
| 6731 | if (unlikely(!req->file)) |
| 6732 | ret = -EBADF; |
| 6733 | } |
| 6734 | |
| 6735 | state->ios_left--; |
| 6736 | return ret; |
| 6737 | } |
| 6738 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6739 | 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] | 6740 | const struct io_uring_sqe *sqe) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6741 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6742 | struct io_submit_link *link = &ctx->submit_state.link; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6743 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6744 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6745 | ret = io_init_req(ctx, req, sqe); |
| 6746 | if (unlikely(ret)) { |
| 6747 | fail_req: |
| 6748 | io_put_req(req); |
| 6749 | io_req_complete(req, ret); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6750 | if (link->head) { |
| 6751 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6752 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6753 | io_put_req(link->head); |
| 6754 | io_req_complete(link->head, -ECANCELED); |
| 6755 | link->head = NULL; |
| 6756 | } |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6757 | return ret; |
| 6758 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6759 | ret = io_req_prep(req, sqe); |
| 6760 | if (unlikely(ret)) |
| 6761 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6762 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6763 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6764 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6765 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6766 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6767 | /* |
| 6768 | * If we already have a head request, queue this one for async |
| 6769 | * submittal once the head completes. If we don't have a head but |
| 6770 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6771 | * submitted sync once the chain is complete. If none of those |
| 6772 | * conditions are true (normal request), then just queue it. |
| 6773 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6774 | if (link->head) { |
| 6775 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6776 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6777 | /* |
| 6778 | * Taking sequential execution of a link, draining both sides |
| 6779 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6780 | * requests in the link. So, it drains the head and the |
| 6781 | * next after the link request. The last one is done via |
| 6782 | * drain_next flag to persist the effect across calls. |
| 6783 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6784 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6785 | head->flags |= REQ_F_IO_DRAIN; |
| 6786 | ctx->drain_next = 1; |
| 6787 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6788 | ret = io_req_defer_prep(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6789 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6790 | goto fail_req; |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6791 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6792 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6793 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6794 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6795 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6796 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6797 | io_queue_sqe(head); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6798 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6799 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6800 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6801 | if (unlikely(ctx->drain_next)) { |
| 6802 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6803 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6804 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6805 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6806 | link->head = req; |
| 6807 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6808 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6809 | io_queue_sqe(req); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6810 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6811 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6812 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6813 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6814 | } |
| 6815 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6816 | /* |
| 6817 | * Batched submission is done, ensure local IO is flushed out. |
| 6818 | */ |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6819 | static void io_submit_state_end(struct io_submit_state *state, |
| 6820 | struct io_ring_ctx *ctx) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6821 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6822 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6823 | io_queue_sqe(state->link.head); |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6824 | if (state->comp.nr) |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6825 | io_submit_flush_completions(&state->comp, ctx); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6826 | if (state->plug_started) |
| 6827 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6828 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | /* |
| 6832 | * Start submission side cache. |
| 6833 | */ |
| 6834 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6835 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6836 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6837 | state->plug_started = false; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6838 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6839 | /* set only head, no need to init link_last in advance */ |
| 6840 | state->link.head = NULL; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6841 | } |
| 6842 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6843 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6844 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6845 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6846 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6847 | /* |
| 6848 | * Ensure any loads from the SQEs are done at this point, |
| 6849 | * since once we write the new head, the application could |
| 6850 | * write new data to them. |
| 6851 | */ |
| 6852 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6856 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6857 | * that is mapped by userspace. This means that care needs to be taken to |
| 6858 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6859 | * being a good citizen. If members of the sqe are validated and then later |
| 6860 | * used, it's important that those reads are done through READ_ONCE() to |
| 6861 | * prevent a re-load down the line. |
| 6862 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6863 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6864 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6865 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6866 | unsigned head; |
| 6867 | |
| 6868 | /* |
| 6869 | * The cached sq head (or cq tail) serves two purposes: |
| 6870 | * |
| 6871 | * 1) allows us to batch the cost of updating the user visible |
| 6872 | * head updates. |
| 6873 | * 2) allows the kernel side to track the head on its own, even |
| 6874 | * though the application is the one updating it. |
| 6875 | */ |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6876 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6877 | if (likely(head < ctx->sq_entries)) |
| 6878 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6879 | |
| 6880 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6881 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6882 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6883 | return NULL; |
| 6884 | } |
| 6885 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6886 | 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] | 6887 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6888 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6889 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6890 | /* 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] | 6891 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6892 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6893 | return -EBUSY; |
| 6894 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6895 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6896 | /* make sure SQ entry isn't read before tail */ |
| 6897 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6898 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6899 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6900 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6901 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6902 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6903 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6904 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6905 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6906 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6907 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6908 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6909 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6910 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6911 | if (unlikely(!req)) { |
| 6912 | if (!submitted) |
| 6913 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6914 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6915 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6916 | sqe = io_get_sqe(ctx); |
| 6917 | if (unlikely(!sqe)) { |
| 6918 | kmem_cache_free(req_cachep, req); |
| 6919 | break; |
| 6920 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6921 | /* will complete beyond this point, count as submitted */ |
| 6922 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6923 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6924 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6925 | } |
| 6926 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6927 | if (unlikely(submitted != nr)) { |
| 6928 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6929 | struct io_uring_task *tctx = current->io_uring; |
| 6930 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6931 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6932 | percpu_ref_put_many(&ctx->refs, unused); |
| 6933 | percpu_counter_sub(&tctx->inflight, unused); |
| 6934 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6935 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6936 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6937 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6938 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6939 | io_commit_sqring(ctx); |
| 6940 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6941 | return submitted; |
| 6942 | } |
| 6943 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6944 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6945 | { |
| 6946 | /* Tell userspace we may need a wakeup call */ |
| 6947 | spin_lock_irq(&ctx->completion_lock); |
| 6948 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6949 | spin_unlock_irq(&ctx->completion_lock); |
| 6950 | } |
| 6951 | |
| 6952 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6953 | { |
| 6954 | spin_lock_irq(&ctx->completion_lock); |
| 6955 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6956 | spin_unlock_irq(&ctx->completion_lock); |
| 6957 | } |
| 6958 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6959 | 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] | 6960 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6961 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6962 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6963 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6964 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6965 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6966 | if (cap_entries && to_submit > 8) |
| 6967 | to_submit = 8; |
| 6968 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6969 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6970 | unsigned nr_events = 0; |
| 6971 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6972 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6973 | if (!list_empty(&ctx->iopoll_list)) |
| 6974 | io_do_iopoll(ctx, &nr_events, 0); |
| 6975 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6976 | if (to_submit && !ctx->sqo_dead && |
| 6977 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6978 | ret = io_submit_sqes(ctx, to_submit); |
| 6979 | mutex_unlock(&ctx->uring_lock); |
| 6980 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6981 | |
| 6982 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6983 | wake_up(&ctx->sqo_sq_wait); |
| 6984 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6985 | return ret; |
| 6986 | } |
| 6987 | |
| 6988 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6989 | { |
| 6990 | struct io_ring_ctx *ctx; |
| 6991 | unsigned sq_thread_idle = 0; |
| 6992 | |
| 6993 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6994 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 6995 | sq_thread_idle = ctx->sq_thread_idle; |
| 6996 | } |
| 6997 | |
| 6998 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6999 | } |
| 7000 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7001 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7002 | { |
| 7003 | struct io_ring_ctx *ctx; |
| 7004 | |
| 7005 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7006 | 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] | 7007 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7008 | complete(&ctx->sq_thread_comp); |
| 7009 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7010 | |
| 7011 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7012 | } |
| 7013 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7014 | static int io_sq_thread(void *data) |
| 7015 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7016 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7017 | struct files_struct *old_files = current->files; |
| 7018 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7019 | const struct cred *old_cred = NULL; |
| 7020 | struct io_sq_data *sqd = data; |
| 7021 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7022 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7023 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7024 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7025 | task_lock(current); |
| 7026 | current->files = NULL; |
| 7027 | current->nsproxy = NULL; |
| 7028 | task_unlock(current); |
| 7029 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7030 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7031 | int ret; |
| 7032 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7033 | |
| 7034 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7035 | * Any changes to the sqd lists are synchronized through the |
| 7036 | * kthread parking. This synchronizes the thread vs users, |
| 7037 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7038 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7039 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7040 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7041 | /* |
| 7042 | * When sq thread is unparked, in case the previous park operation |
| 7043 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7044 | * to be stopped, so here needs to have a check. |
| 7045 | */ |
| 7046 | if (kthread_should_stop()) |
| 7047 | break; |
| 7048 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7049 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7050 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7051 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7052 | timeout = jiffies + sqd->sq_thread_idle; |
| 7053 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7054 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7055 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7056 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7057 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7058 | if (current->cred != ctx->creds) { |
| 7059 | if (old_cred) |
| 7060 | revert_creds(old_cred); |
| 7061 | old_cred = override_creds(ctx->creds); |
| 7062 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7063 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7064 | #ifdef CONFIG_AUDIT |
| 7065 | current->loginuid = ctx->loginuid; |
| 7066 | current->sessionid = ctx->sessionid; |
| 7067 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7068 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7069 | ret = __io_sq_thread(ctx, cap_entries); |
| 7070 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7071 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7072 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7073 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7074 | } |
| 7075 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7076 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7077 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7078 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7079 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7080 | if (sqt_spin) |
| 7081 | timeout = jiffies + sqd->sq_thread_idle; |
| 7082 | continue; |
| 7083 | } |
| 7084 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7085 | needs_sched = true; |
| 7086 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7087 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7088 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7089 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7090 | needs_sched = false; |
| 7091 | break; |
| 7092 | } |
| 7093 | if (io_sqring_entries(ctx)) { |
| 7094 | needs_sched = false; |
| 7095 | break; |
| 7096 | } |
| 7097 | } |
| 7098 | |
Hao Xu | 8b28fdf | 2021-01-31 22:39:04 +0800 | [diff] [blame] | 7099 | if (needs_sched && !kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7100 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7101 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7102 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7103 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7104 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7105 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7106 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7107 | |
| 7108 | finish_wait(&sqd->wait, &wait); |
| 7109 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7110 | } |
| 7111 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7112 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7113 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7114 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7115 | if (cur_css) |
| 7116 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7117 | if (old_cred) |
| 7118 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7119 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7120 | task_lock(current); |
| 7121 | current->files = old_files; |
| 7122 | current->nsproxy = old_nsproxy; |
| 7123 | task_unlock(current); |
| 7124 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7125 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7126 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7127 | return 0; |
| 7128 | } |
| 7129 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7130 | struct io_wait_queue { |
| 7131 | struct wait_queue_entry wq; |
| 7132 | struct io_ring_ctx *ctx; |
| 7133 | unsigned to_wait; |
| 7134 | unsigned nr_timeouts; |
| 7135 | }; |
| 7136 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7137 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7138 | { |
| 7139 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7140 | |
| 7141 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7142 | * 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] | 7143 | * started waiting. For timeouts, we always want to return to userspace, |
| 7144 | * regardless of event count. |
| 7145 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7146 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7147 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7148 | } |
| 7149 | |
| 7150 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7151 | int wake_flags, void *key) |
| 7152 | { |
| 7153 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7154 | wq); |
| 7155 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7156 | /* |
| 7157 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7158 | * the task, and the next invocation will do it. |
| 7159 | */ |
| 7160 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7161 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7162 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7163 | } |
| 7164 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7165 | static int io_run_task_work_sig(void) |
| 7166 | { |
| 7167 | if (io_run_task_work()) |
| 7168 | return 1; |
| 7169 | if (!signal_pending(current)) |
| 7170 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7171 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7172 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7173 | return -EINTR; |
| 7174 | } |
| 7175 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7176 | /* when returns >0, the caller should retry */ |
| 7177 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7178 | struct io_wait_queue *iowq, |
| 7179 | signed long *timeout) |
| 7180 | { |
| 7181 | int ret; |
| 7182 | |
| 7183 | /* make sure we run task_work before checking for signals */ |
| 7184 | ret = io_run_task_work_sig(); |
| 7185 | if (ret || io_should_wake(iowq)) |
| 7186 | return ret; |
| 7187 | /* let the caller flush overflows, retry */ |
| 7188 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7189 | return 1; |
| 7190 | |
| 7191 | *timeout = schedule_timeout(*timeout); |
| 7192 | return !*timeout ? -ETIME : 1; |
| 7193 | } |
| 7194 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7195 | /* |
| 7196 | * Wait until events become available, if we don't already have some. The |
| 7197 | * application must reap them itself, as they reside on the shared cq ring. |
| 7198 | */ |
| 7199 | 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] | 7200 | const sigset_t __user *sig, size_t sigsz, |
| 7201 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7202 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7203 | struct io_wait_queue iowq = { |
| 7204 | .wq = { |
| 7205 | .private = current, |
| 7206 | .func = io_wake_function, |
| 7207 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7208 | }, |
| 7209 | .ctx = ctx, |
| 7210 | .to_wait = min_events, |
| 7211 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7212 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7213 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7214 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7215 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7216 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7217 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7218 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7219 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7220 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7221 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7222 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7223 | |
| 7224 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7225 | #ifdef CONFIG_COMPAT |
| 7226 | if (in_compat_syscall()) |
| 7227 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7228 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7229 | else |
| 7230 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7231 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7232 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7233 | if (ret) |
| 7234 | return ret; |
| 7235 | } |
| 7236 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7237 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7238 | struct timespec64 ts; |
| 7239 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7240 | if (get_timespec64(&ts, uts)) |
| 7241 | return -EFAULT; |
| 7242 | timeout = timespec64_to_jiffies(&ts); |
| 7243 | } |
| 7244 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7245 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7246 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7247 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7248 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7249 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7250 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7251 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7252 | finish_wait(&ctx->wait, &iowq.wq); |
| 7253 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7254 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7255 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7256 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7257 | 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] | 7258 | } |
| 7259 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7260 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7261 | { |
| 7262 | #if defined(CONFIG_UNIX) |
| 7263 | if (ctx->ring_sock) { |
| 7264 | struct sock *sock = ctx->ring_sock->sk; |
| 7265 | struct sk_buff *skb; |
| 7266 | |
| 7267 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7268 | kfree_skb(skb); |
| 7269 | } |
| 7270 | #else |
| 7271 | int i; |
| 7272 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7273 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7274 | struct file *file; |
| 7275 | |
| 7276 | file = io_file_from_index(ctx, i); |
| 7277 | if (file) |
| 7278 | fput(file); |
| 7279 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7280 | #endif |
| 7281 | } |
| 7282 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7283 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7284 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7285 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7286 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7287 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7288 | complete(&data->done); |
| 7289 | } |
| 7290 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7291 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
| 7292 | { |
| 7293 | spin_lock_bh(&ctx->rsrc_ref_lock); |
| 7294 | } |
| 7295 | |
| 7296 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
| 7297 | { |
| 7298 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7299 | } |
| 7300 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7301 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 7302 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7303 | struct fixed_rsrc_ref_node *ref_node) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7304 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7305 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7306 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7307 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7308 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7309 | percpu_ref_get(&rsrc_data->refs); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7312 | 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] | 7313 | { |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7314 | struct fixed_rsrc_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7315 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7316 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7317 | ref_node = data->node; |
Pavel Begunkov | e6cb007 | 2021-02-20 18:03:47 +0000 | [diff] [blame] | 7318 | data->node = NULL; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7319 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7320 | if (ref_node) |
| 7321 | percpu_ref_kill(&ref_node->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7322 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7323 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7324 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 7325 | struct io_ring_ctx *ctx, |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7326 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 7327 | struct io_rsrc_put *prsrc)) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7328 | { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7329 | struct fixed_rsrc_ref_node *backup_node; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7330 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7331 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7332 | if (data->quiesce) |
| 7333 | return -ENXIO; |
| 7334 | |
| 7335 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7336 | do { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7337 | ret = -ENOMEM; |
| 7338 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 7339 | if (!backup_node) |
| 7340 | break; |
| 7341 | backup_node->rsrc_data = data; |
| 7342 | backup_node->rsrc_put = rsrc_put; |
| 7343 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7344 | io_sqe_rsrc_kill_node(ctx, data); |
| 7345 | percpu_ref_kill(&data->refs); |
| 7346 | flush_delayed_work(&ctx->rsrc_put_work); |
| 7347 | |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7348 | ret = wait_for_completion_interruptible(&data->done); |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 7349 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7350 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7351 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7352 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
| 7353 | backup_node = NULL; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7354 | mutex_unlock(&ctx->uring_lock); |
| 7355 | ret = io_run_task_work_sig(); |
| 7356 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7357 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7358 | data->quiesce = false; |
| 7359 | |
| 7360 | if (backup_node) |
| 7361 | destroy_fixed_rsrc_ref_node(backup_node); |
| 7362 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7365 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7366 | { |
| 7367 | struct fixed_rsrc_data *data; |
| 7368 | |
| 7369 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7370 | if (!data) |
| 7371 | return NULL; |
| 7372 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7373 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7374 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7375 | kfree(data); |
| 7376 | return NULL; |
| 7377 | } |
| 7378 | data->ctx = ctx; |
| 7379 | init_completion(&data->done); |
| 7380 | return data; |
| 7381 | } |
| 7382 | |
| 7383 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7384 | { |
| 7385 | percpu_ref_exit(&data->refs); |
| 7386 | kfree(data->table); |
| 7387 | kfree(data); |
| 7388 | } |
| 7389 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7390 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7391 | { |
| 7392 | struct fixed_rsrc_data *data = ctx->file_data; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7393 | unsigned nr_tables, i; |
| 7394 | int ret; |
| 7395 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7396 | /* |
| 7397 | * percpu_ref_is_dying() is to stop parallel files unregister |
| 7398 | * Since we possibly drop uring lock later in this function to |
| 7399 | * run task work. |
| 7400 | */ |
| 7401 | if (!data || percpu_ref_is_dying(&data->refs)) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7402 | return -ENXIO; |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7403 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7404 | if (ret) |
| 7405 | return ret; |
| 7406 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7407 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7408 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7409 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7410 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7411 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7412 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7413 | ctx->nr_user_files = 0; |
| 7414 | return 0; |
| 7415 | } |
| 7416 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7417 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7418 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7419 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7420 | /* |
| 7421 | * The park is a bit of a work-around, without it we get |
| 7422 | * warning spews on shutdown with SQPOLL set and affinity |
| 7423 | * set to a single CPU. |
| 7424 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7425 | if (sqd->thread) { |
| 7426 | kthread_park(sqd->thread); |
| 7427 | kthread_stop(sqd->thread); |
| 7428 | } |
| 7429 | |
| 7430 | kfree(sqd); |
| 7431 | } |
| 7432 | } |
| 7433 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7434 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7435 | { |
| 7436 | struct io_ring_ctx *ctx_attach; |
| 7437 | struct io_sq_data *sqd; |
| 7438 | struct fd f; |
| 7439 | |
| 7440 | f = fdget(p->wq_fd); |
| 7441 | if (!f.file) |
| 7442 | return ERR_PTR(-ENXIO); |
| 7443 | if (f.file->f_op != &io_uring_fops) { |
| 7444 | fdput(f); |
| 7445 | return ERR_PTR(-EINVAL); |
| 7446 | } |
| 7447 | |
| 7448 | ctx_attach = f.file->private_data; |
| 7449 | sqd = ctx_attach->sq_data; |
| 7450 | if (!sqd) { |
| 7451 | fdput(f); |
| 7452 | return ERR_PTR(-EINVAL); |
| 7453 | } |
| 7454 | |
| 7455 | refcount_inc(&sqd->refs); |
| 7456 | fdput(f); |
| 7457 | return sqd; |
| 7458 | } |
| 7459 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7460 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7461 | { |
| 7462 | struct io_sq_data *sqd; |
| 7463 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7464 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7465 | return io_attach_sq_data(p); |
| 7466 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7467 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7468 | if (!sqd) |
| 7469 | return ERR_PTR(-ENOMEM); |
| 7470 | |
| 7471 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7472 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7473 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7474 | mutex_init(&sqd->ctx_lock); |
| 7475 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7476 | init_waitqueue_head(&sqd->wait); |
| 7477 | return sqd; |
| 7478 | } |
| 7479 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7480 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7481 | __releases(&sqd->lock) |
| 7482 | { |
| 7483 | if (!sqd->thread) |
| 7484 | return; |
| 7485 | kthread_unpark(sqd->thread); |
| 7486 | mutex_unlock(&sqd->lock); |
| 7487 | } |
| 7488 | |
| 7489 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7490 | __acquires(&sqd->lock) |
| 7491 | { |
| 7492 | if (!sqd->thread) |
| 7493 | return; |
| 7494 | mutex_lock(&sqd->lock); |
| 7495 | kthread_park(sqd->thread); |
| 7496 | } |
| 7497 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7498 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7499 | { |
| 7500 | struct io_sq_data *sqd = ctx->sq_data; |
| 7501 | |
| 7502 | if (sqd) { |
| 7503 | if (sqd->thread) { |
| 7504 | /* |
| 7505 | * We may arrive here from the error branch in |
| 7506 | * io_sq_offload_create() where the kthread is created |
| 7507 | * without being waked up, thus wake it up now to make |
| 7508 | * sure the wait will complete. |
| 7509 | */ |
| 7510 | wake_up_process(sqd->thread); |
| 7511 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7512 | |
| 7513 | io_sq_thread_park(sqd); |
| 7514 | } |
| 7515 | |
| 7516 | mutex_lock(&sqd->ctx_lock); |
| 7517 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7518 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7519 | mutex_unlock(&sqd->ctx_lock); |
| 7520 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7521 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7522 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7523 | |
| 7524 | io_put_sq_data(sqd); |
| 7525 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7526 | } |
| 7527 | } |
| 7528 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7529 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7530 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7531 | io_sq_thread_stop(ctx); |
| 7532 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7533 | if (ctx->io_wq) { |
| 7534 | io_wq_destroy(ctx->io_wq); |
| 7535 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7536 | } |
| 7537 | } |
| 7538 | |
| 7539 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7540 | /* |
| 7541 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7542 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7543 | * loops in the file referencing. |
| 7544 | */ |
| 7545 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7546 | { |
| 7547 | struct sock *sk = ctx->ring_sock->sk; |
| 7548 | struct scm_fp_list *fpl; |
| 7549 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7550 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7551 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7552 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7553 | if (!fpl) |
| 7554 | return -ENOMEM; |
| 7555 | |
| 7556 | skb = alloc_skb(0, GFP_KERNEL); |
| 7557 | if (!skb) { |
| 7558 | kfree(fpl); |
| 7559 | return -ENOMEM; |
| 7560 | } |
| 7561 | |
| 7562 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7563 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7564 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7565 | fpl->user = get_uid(ctx->user); |
| 7566 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7567 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7568 | |
| 7569 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7570 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7571 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7572 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7573 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7574 | } |
| 7575 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7576 | if (nr_files) { |
| 7577 | fpl->max = SCM_MAX_FD; |
| 7578 | fpl->count = nr_files; |
| 7579 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7580 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7581 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7582 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7583 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7584 | for (i = 0; i < nr_files; i++) |
| 7585 | fput(fpl->fp[i]); |
| 7586 | } else { |
| 7587 | kfree_skb(skb); |
| 7588 | kfree(fpl); |
| 7589 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7590 | |
| 7591 | return 0; |
| 7592 | } |
| 7593 | |
| 7594 | /* |
| 7595 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7596 | * causes regular reference counting to break down. We rely on the UNIX |
| 7597 | * garbage collection to take care of this problem for us. |
| 7598 | */ |
| 7599 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7600 | { |
| 7601 | unsigned left, total; |
| 7602 | int ret = 0; |
| 7603 | |
| 7604 | total = 0; |
| 7605 | left = ctx->nr_user_files; |
| 7606 | while (left) { |
| 7607 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7608 | |
| 7609 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7610 | if (ret) |
| 7611 | break; |
| 7612 | left -= this_files; |
| 7613 | total += this_files; |
| 7614 | } |
| 7615 | |
| 7616 | if (!ret) |
| 7617 | return 0; |
| 7618 | |
| 7619 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7620 | struct file *file = io_file_from_index(ctx, total); |
| 7621 | |
| 7622 | if (file) |
| 7623 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7624 | total++; |
| 7625 | } |
| 7626 | |
| 7627 | return ret; |
| 7628 | } |
| 7629 | #else |
| 7630 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7631 | { |
| 7632 | return 0; |
| 7633 | } |
| 7634 | #endif |
| 7635 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7636 | 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] | 7637 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7638 | { |
| 7639 | int i; |
| 7640 | |
| 7641 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7642 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7643 | unsigned this_files; |
| 7644 | |
| 7645 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7646 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7647 | GFP_KERNEL); |
| 7648 | if (!table->files) |
| 7649 | break; |
| 7650 | nr_files -= this_files; |
| 7651 | } |
| 7652 | |
| 7653 | if (i == nr_tables) |
| 7654 | return 0; |
| 7655 | |
| 7656 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7657 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7658 | kfree(table->files); |
| 7659 | } |
| 7660 | return 1; |
| 7661 | } |
| 7662 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7663 | 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] | 7664 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7665 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7666 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7667 | struct sock *sock = ctx->ring_sock->sk; |
| 7668 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7669 | struct sk_buff *skb; |
| 7670 | int i; |
| 7671 | |
| 7672 | __skb_queue_head_init(&list); |
| 7673 | |
| 7674 | /* |
| 7675 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7676 | * remove this entry and rearrange the file array. |
| 7677 | */ |
| 7678 | skb = skb_dequeue(head); |
| 7679 | while (skb) { |
| 7680 | struct scm_fp_list *fp; |
| 7681 | |
| 7682 | fp = UNIXCB(skb).fp; |
| 7683 | for (i = 0; i < fp->count; i++) { |
| 7684 | int left; |
| 7685 | |
| 7686 | if (fp->fp[i] != file) |
| 7687 | continue; |
| 7688 | |
| 7689 | unix_notinflight(fp->user, fp->fp[i]); |
| 7690 | left = fp->count - 1 - i; |
| 7691 | if (left) { |
| 7692 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7693 | left * sizeof(struct file *)); |
| 7694 | } |
| 7695 | fp->count--; |
| 7696 | if (!fp->count) { |
| 7697 | kfree_skb(skb); |
| 7698 | skb = NULL; |
| 7699 | } else { |
| 7700 | __skb_queue_tail(&list, skb); |
| 7701 | } |
| 7702 | fput(file); |
| 7703 | file = NULL; |
| 7704 | break; |
| 7705 | } |
| 7706 | |
| 7707 | if (!file) |
| 7708 | break; |
| 7709 | |
| 7710 | __skb_queue_tail(&list, skb); |
| 7711 | |
| 7712 | skb = skb_dequeue(head); |
| 7713 | } |
| 7714 | |
| 7715 | if (skb_peek(&list)) { |
| 7716 | spin_lock_irq(&head->lock); |
| 7717 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7718 | __skb_queue_tail(head, skb); |
| 7719 | spin_unlock_irq(&head->lock); |
| 7720 | } |
| 7721 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7722 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7723 | #endif |
| 7724 | } |
| 7725 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7726 | 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] | 7727 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7728 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7729 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7730 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7731 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7732 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7733 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7734 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7735 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7736 | } |
| 7737 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7738 | percpu_ref_exit(&ref_node->refs); |
| 7739 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7740 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7741 | } |
| 7742 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7743 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7744 | { |
| 7745 | struct io_ring_ctx *ctx; |
| 7746 | struct llist_node *node; |
| 7747 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7748 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7749 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7750 | |
| 7751 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7752 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7753 | struct llist_node *next = node->next; |
| 7754 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7755 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7756 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7757 | node = next; |
| 7758 | } |
| 7759 | } |
| 7760 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7761 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7762 | unsigned i) |
| 7763 | { |
| 7764 | struct fixed_rsrc_table *table; |
| 7765 | |
| 7766 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7767 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7768 | } |
| 7769 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7770 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7771 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7772 | struct fixed_rsrc_ref_node *ref_node; |
| 7773 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7774 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7775 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7776 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7777 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7778 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7779 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7780 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7781 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7782 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7783 | ref_node->done = true; |
| 7784 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7785 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7786 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7787 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7788 | /* recycle ref nodes in order */ |
| 7789 | if (!ref_node->done) |
| 7790 | break; |
| 7791 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7792 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7793 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7794 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7795 | |
| 7796 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7797 | delay = 0; |
| 7798 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7799 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7800 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7801 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7802 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7803 | } |
| 7804 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7805 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7806 | struct io_ring_ctx *ctx) |
| 7807 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7808 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7809 | |
| 7810 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7811 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7812 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7813 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7814 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7815 | 0, GFP_KERNEL)) { |
| 7816 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7817 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7818 | } |
| 7819 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7820 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7821 | ref_node->done = false; |
| 7822 | return ref_node; |
| 7823 | } |
| 7824 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7825 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7826 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7827 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7828 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7829 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7830 | } |
| 7831 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7832 | 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] | 7833 | { |
| 7834 | percpu_ref_exit(&ref_node->refs); |
| 7835 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7836 | } |
| 7837 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7838 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7839 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7840 | unsigned nr_args) |
| 7841 | { |
| 7842 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7843 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7844 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7845 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7846 | struct fixed_rsrc_ref_node *ref_node; |
| 7847 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7848 | |
| 7849 | if (ctx->file_data) |
| 7850 | return -EBUSY; |
| 7851 | if (!nr_args) |
| 7852 | return -EINVAL; |
| 7853 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7854 | return -EMFILE; |
| 7855 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7856 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7857 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7858 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7859 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7860 | |
| 7861 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7862 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7863 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7864 | if (!file_data->table) |
| 7865 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7866 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7867 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7868 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7869 | |
| 7870 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7871 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7872 | ret = -EFAULT; |
| 7873 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7874 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7875 | /* allow sparse sets */ |
| 7876 | if (fd == -1) |
| 7877 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7878 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7879 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7880 | ret = -EBADF; |
| 7881 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7882 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7883 | |
| 7884 | /* |
| 7885 | * Don't allow io_uring instances to be registered. If UNIX |
| 7886 | * isn't enabled, then this causes a reference cycle and this |
| 7887 | * instance can never get freed. If UNIX is enabled we'll |
| 7888 | * handle it just fine, but there's still no point in allowing |
| 7889 | * a ring fd as it doesn't support regular read/write anyway. |
| 7890 | */ |
| 7891 | if (file->f_op == &io_uring_fops) { |
| 7892 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7893 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7894 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7895 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7896 | } |
| 7897 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7898 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7899 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7900 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7901 | return ret; |
| 7902 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7903 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7904 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7905 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7906 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7907 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7908 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7909 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7910 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7911 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7912 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7913 | out_fput: |
| 7914 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7915 | file = io_file_from_index(ctx, i); |
| 7916 | if (file) |
| 7917 | fput(file); |
| 7918 | } |
| 7919 | for (i = 0; i < nr_tables; i++) |
| 7920 | kfree(file_data->table[i].files); |
| 7921 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7922 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7923 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7924 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7925 | return ret; |
| 7926 | } |
| 7927 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7928 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7929 | int index) |
| 7930 | { |
| 7931 | #if defined(CONFIG_UNIX) |
| 7932 | struct sock *sock = ctx->ring_sock->sk; |
| 7933 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7934 | struct sk_buff *skb; |
| 7935 | |
| 7936 | /* |
| 7937 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7938 | * file set. If there's no room, fall back to allocating a new skb |
| 7939 | * and filling it in. |
| 7940 | */ |
| 7941 | spin_lock_irq(&head->lock); |
| 7942 | skb = skb_peek(head); |
| 7943 | if (skb) { |
| 7944 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7945 | |
| 7946 | if (fpl->count < SCM_MAX_FD) { |
| 7947 | __skb_unlink(skb, head); |
| 7948 | spin_unlock_irq(&head->lock); |
| 7949 | fpl->fp[fpl->count] = get_file(file); |
| 7950 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7951 | fpl->count++; |
| 7952 | spin_lock_irq(&head->lock); |
| 7953 | __skb_queue_head(head, skb); |
| 7954 | } else { |
| 7955 | skb = NULL; |
| 7956 | } |
| 7957 | } |
| 7958 | spin_unlock_irq(&head->lock); |
| 7959 | |
| 7960 | if (skb) { |
| 7961 | fput(file); |
| 7962 | return 0; |
| 7963 | } |
| 7964 | |
| 7965 | return __io_sqe_files_scm(ctx, 1, index); |
| 7966 | #else |
| 7967 | return 0; |
| 7968 | #endif |
| 7969 | } |
| 7970 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7971 | 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] | 7972 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7973 | struct io_rsrc_put *prsrc; |
| 7974 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7975 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7976 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7977 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7978 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7979 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7980 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7981 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7982 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7983 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7984 | } |
| 7985 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7986 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7987 | struct file *file) |
| 7988 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7989 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7992 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7993 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7994 | unsigned nr_args) |
| 7995 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7996 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7997 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7998 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7999 | __s32 __user *fds; |
| 8000 | int fd, i, err; |
| 8001 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8002 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8003 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8004 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8005 | return -EOVERFLOW; |
| 8006 | if (done > ctx->nr_user_files) |
| 8007 | return -EINVAL; |
| 8008 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8009 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 8010 | if (!ref_node) |
| 8011 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8012 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8013 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8014 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8015 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8016 | err = 0; |
| 8017 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 8018 | err = -EFAULT; |
| 8019 | break; |
| 8020 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8021 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8022 | continue; |
| 8023 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8024 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8025 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 8026 | |
| 8027 | if (*file_slot) { |
| 8028 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8029 | if (err) |
| 8030 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8031 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8032 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8033 | } |
| 8034 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8035 | file = fget(fd); |
| 8036 | if (!file) { |
| 8037 | err = -EBADF; |
| 8038 | break; |
| 8039 | } |
| 8040 | /* |
| 8041 | * Don't allow io_uring instances to be registered. If |
| 8042 | * UNIX isn't enabled, then this causes a reference |
| 8043 | * cycle and this instance can never get freed. If UNIX |
| 8044 | * is enabled we'll handle it just fine, but there's |
| 8045 | * still no point in allowing a ring fd as it doesn't |
| 8046 | * support regular read/write anyway. |
| 8047 | */ |
| 8048 | if (file->f_op == &io_uring_fops) { |
| 8049 | fput(file); |
| 8050 | err = -EBADF; |
| 8051 | break; |
| 8052 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8053 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8054 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8055 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8056 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8057 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8058 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8059 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8060 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8061 | } |
| 8062 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8063 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8064 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 8065 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8066 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8067 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8068 | |
| 8069 | return done ? done : err; |
| 8070 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8071 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8072 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8073 | unsigned nr_args) |
| 8074 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8075 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8076 | |
| 8077 | if (!ctx->file_data) |
| 8078 | return -ENXIO; |
| 8079 | if (!nr_args) |
| 8080 | return -EINVAL; |
| 8081 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8082 | return -EFAULT; |
| 8083 | if (up.resv) |
| 8084 | return -EINVAL; |
| 8085 | |
| 8086 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8087 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8088 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8089 | 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] | 8090 | { |
| 8091 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8092 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8093 | req = io_put_req_find_next(req); |
| 8094 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8095 | } |
| 8096 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8097 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8098 | struct io_uring_params *p) |
| 8099 | { |
| 8100 | struct io_wq_data data; |
| 8101 | struct fd f; |
| 8102 | struct io_ring_ctx *ctx_attach; |
| 8103 | unsigned int concurrency; |
| 8104 | int ret = 0; |
| 8105 | |
| 8106 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8107 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8108 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8109 | |
| 8110 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8111 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8112 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8113 | |
| 8114 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8115 | if (IS_ERR(ctx->io_wq)) { |
| 8116 | ret = PTR_ERR(ctx->io_wq); |
| 8117 | ctx->io_wq = NULL; |
| 8118 | } |
| 8119 | return ret; |
| 8120 | } |
| 8121 | |
| 8122 | f = fdget(p->wq_fd); |
| 8123 | if (!f.file) |
| 8124 | return -EBADF; |
| 8125 | |
| 8126 | if (f.file->f_op != &io_uring_fops) { |
| 8127 | ret = -EINVAL; |
| 8128 | goto out_fput; |
| 8129 | } |
| 8130 | |
| 8131 | ctx_attach = f.file->private_data; |
| 8132 | /* @io_wq is protected by holding the fd */ |
| 8133 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8134 | ret = -EINVAL; |
| 8135 | goto out_fput; |
| 8136 | } |
| 8137 | |
| 8138 | ctx->io_wq = ctx_attach->io_wq; |
| 8139 | out_fput: |
| 8140 | fdput(f); |
| 8141 | return ret; |
| 8142 | } |
| 8143 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8144 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8145 | { |
| 8146 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8147 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8148 | |
| 8149 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8150 | if (unlikely(!tctx)) |
| 8151 | return -ENOMEM; |
| 8152 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8153 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8154 | if (unlikely(ret)) { |
| 8155 | kfree(tctx); |
| 8156 | return ret; |
| 8157 | } |
| 8158 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8159 | xa_init(&tctx->xa); |
| 8160 | init_waitqueue_head(&tctx->wait); |
| 8161 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8162 | atomic_set(&tctx->in_idle, 0); |
| 8163 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8164 | io_init_identity(&tctx->__identity); |
| 8165 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8166 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8167 | spin_lock_init(&tctx->task_lock); |
| 8168 | INIT_WQ_LIST(&tctx->task_list); |
| 8169 | tctx->task_state = 0; |
| 8170 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8171 | return 0; |
| 8172 | } |
| 8173 | |
| 8174 | void __io_uring_free(struct task_struct *tsk) |
| 8175 | { |
| 8176 | struct io_uring_task *tctx = tsk->io_uring; |
| 8177 | |
| 8178 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8179 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8180 | if (tctx->identity != &tctx->__identity) |
| 8181 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8182 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8183 | kfree(tctx); |
| 8184 | tsk->io_uring = NULL; |
| 8185 | } |
| 8186 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8187 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8188 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8189 | { |
| 8190 | int ret; |
| 8191 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8192 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8193 | struct io_sq_data *sqd; |
| 8194 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8195 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8196 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8197 | goto err; |
| 8198 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8199 | sqd = io_get_sq_data(p); |
| 8200 | if (IS_ERR(sqd)) { |
| 8201 | ret = PTR_ERR(sqd); |
| 8202 | goto err; |
| 8203 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8204 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8205 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8206 | io_sq_thread_park(sqd); |
| 8207 | mutex_lock(&sqd->ctx_lock); |
| 8208 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8209 | mutex_unlock(&sqd->ctx_lock); |
| 8210 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8211 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8212 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8213 | if (!ctx->sq_thread_idle) |
| 8214 | ctx->sq_thread_idle = HZ; |
| 8215 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8216 | if (sqd->thread) |
| 8217 | goto done; |
| 8218 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8219 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8220 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8221 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8222 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8223 | if (cpu >= nr_cpu_ids) |
| 8224 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8225 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8226 | goto err; |
| 8227 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8228 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8229 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8230 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8231 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8232 | "io_uring-sq"); |
| 8233 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8234 | if (IS_ERR(sqd->thread)) { |
| 8235 | ret = PTR_ERR(sqd->thread); |
| 8236 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8237 | goto err; |
| 8238 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8239 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8240 | if (ret) |
| 8241 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8242 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8243 | /* Can't have SQ_AFF without SQPOLL */ |
| 8244 | ret = -EINVAL; |
| 8245 | goto err; |
| 8246 | } |
| 8247 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8248 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8249 | ret = io_init_wq_offload(ctx, p); |
| 8250 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8251 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8252 | |
| 8253 | return 0; |
| 8254 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8255 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8256 | return ret; |
| 8257 | } |
| 8258 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8259 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8260 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8261 | struct io_sq_data *sqd = ctx->sq_data; |
| 8262 | |
| 8263 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8264 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8265 | } |
| 8266 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8267 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8268 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8269 | { |
| 8270 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8271 | } |
| 8272 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8273 | static inline int __io_account_mem(struct user_struct *user, |
| 8274 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8275 | { |
| 8276 | unsigned long page_limit, cur_pages, new_pages; |
| 8277 | |
| 8278 | /* Don't allow more pages than we can safely lock */ |
| 8279 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8280 | |
| 8281 | do { |
| 8282 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8283 | new_pages = cur_pages + nr_pages; |
| 8284 | if (new_pages > page_limit) |
| 8285 | return -ENOMEM; |
| 8286 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8287 | new_pages) != cur_pages); |
| 8288 | |
| 8289 | return 0; |
| 8290 | } |
| 8291 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8292 | 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] | 8293 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8294 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8295 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8296 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8297 | if (ctx->mm_account) |
| 8298 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8299 | } |
| 8300 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8301 | 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] | 8302 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8303 | int ret; |
| 8304 | |
| 8305 | if (ctx->limit_mem) { |
| 8306 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8307 | if (ret) |
| 8308 | return ret; |
| 8309 | } |
| 8310 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8311 | if (ctx->mm_account) |
| 8312 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8313 | |
| 8314 | return 0; |
| 8315 | } |
| 8316 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8317 | static void io_mem_free(void *ptr) |
| 8318 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8319 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8320 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8321 | if (!ptr) |
| 8322 | return; |
| 8323 | |
| 8324 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8325 | if (put_page_testzero(page)) |
| 8326 | free_compound_page(page); |
| 8327 | } |
| 8328 | |
| 8329 | static void *io_mem_alloc(size_t size) |
| 8330 | { |
| 8331 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8332 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8333 | |
| 8334 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8335 | } |
| 8336 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8337 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8338 | size_t *sq_offset) |
| 8339 | { |
| 8340 | struct io_rings *rings; |
| 8341 | size_t off, sq_array_size; |
| 8342 | |
| 8343 | off = struct_size(rings, cqes, cq_entries); |
| 8344 | if (off == SIZE_MAX) |
| 8345 | return SIZE_MAX; |
| 8346 | |
| 8347 | #ifdef CONFIG_SMP |
| 8348 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8349 | if (off == 0) |
| 8350 | return SIZE_MAX; |
| 8351 | #endif |
| 8352 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8353 | if (sq_offset) |
| 8354 | *sq_offset = off; |
| 8355 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8356 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8357 | if (sq_array_size == SIZE_MAX) |
| 8358 | return SIZE_MAX; |
| 8359 | |
| 8360 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8361 | return SIZE_MAX; |
| 8362 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8363 | return off; |
| 8364 | } |
| 8365 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8366 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8367 | { |
| 8368 | int i, j; |
| 8369 | |
| 8370 | if (!ctx->user_bufs) |
| 8371 | return -ENXIO; |
| 8372 | |
| 8373 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8374 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8375 | |
| 8376 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8377 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8378 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8379 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8380 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8381 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8382 | imu->nr_bvecs = 0; |
| 8383 | } |
| 8384 | |
| 8385 | kfree(ctx->user_bufs); |
| 8386 | ctx->user_bufs = NULL; |
| 8387 | ctx->nr_user_bufs = 0; |
| 8388 | return 0; |
| 8389 | } |
| 8390 | |
| 8391 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8392 | void __user *arg, unsigned index) |
| 8393 | { |
| 8394 | struct iovec __user *src; |
| 8395 | |
| 8396 | #ifdef CONFIG_COMPAT |
| 8397 | if (ctx->compat) { |
| 8398 | struct compat_iovec __user *ciovs; |
| 8399 | struct compat_iovec ciov; |
| 8400 | |
| 8401 | ciovs = (struct compat_iovec __user *) arg; |
| 8402 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8403 | return -EFAULT; |
| 8404 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8405 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8406 | dst->iov_len = ciov.iov_len; |
| 8407 | return 0; |
| 8408 | } |
| 8409 | #endif |
| 8410 | src = (struct iovec __user *) arg; |
| 8411 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8412 | return -EFAULT; |
| 8413 | return 0; |
| 8414 | } |
| 8415 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8416 | /* |
| 8417 | * Not super efficient, but this is just a registration time. And we do cache |
| 8418 | * the last compound head, so generally we'll only do a full search if we don't |
| 8419 | * match that one. |
| 8420 | * |
| 8421 | * We check if the given compound head page has already been accounted, to |
| 8422 | * avoid double accounting it. This allows us to account the full size of the |
| 8423 | * page, not just the constituent pages of a huge page. |
| 8424 | */ |
| 8425 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8426 | int nr_pages, struct page *hpage) |
| 8427 | { |
| 8428 | int i, j; |
| 8429 | |
| 8430 | /* check current page array */ |
| 8431 | for (i = 0; i < nr_pages; i++) { |
| 8432 | if (!PageCompound(pages[i])) |
| 8433 | continue; |
| 8434 | if (compound_head(pages[i]) == hpage) |
| 8435 | return true; |
| 8436 | } |
| 8437 | |
| 8438 | /* check previously registered pages */ |
| 8439 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8440 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8441 | |
| 8442 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8443 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8444 | continue; |
| 8445 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8446 | return true; |
| 8447 | } |
| 8448 | } |
| 8449 | |
| 8450 | return false; |
| 8451 | } |
| 8452 | |
| 8453 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8454 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8455 | struct page **last_hpage) |
| 8456 | { |
| 8457 | int i, ret; |
| 8458 | |
| 8459 | for (i = 0; i < nr_pages; i++) { |
| 8460 | if (!PageCompound(pages[i])) { |
| 8461 | imu->acct_pages++; |
| 8462 | } else { |
| 8463 | struct page *hpage; |
| 8464 | |
| 8465 | hpage = compound_head(pages[i]); |
| 8466 | if (hpage == *last_hpage) |
| 8467 | continue; |
| 8468 | *last_hpage = hpage; |
| 8469 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8470 | continue; |
| 8471 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8472 | } |
| 8473 | } |
| 8474 | |
| 8475 | if (!imu->acct_pages) |
| 8476 | return 0; |
| 8477 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8478 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8479 | if (ret) |
| 8480 | imu->acct_pages = 0; |
| 8481 | return ret; |
| 8482 | } |
| 8483 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8484 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8485 | struct io_mapped_ubuf *imu, |
| 8486 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8487 | { |
| 8488 | struct vm_area_struct **vmas = NULL; |
| 8489 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8490 | unsigned long off, start, end, ubuf; |
| 8491 | size_t size; |
| 8492 | int ret, pret, nr_pages, i; |
| 8493 | |
| 8494 | ubuf = (unsigned long) iov->iov_base; |
| 8495 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8496 | start = ubuf >> PAGE_SHIFT; |
| 8497 | nr_pages = end - start; |
| 8498 | |
| 8499 | ret = -ENOMEM; |
| 8500 | |
| 8501 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8502 | if (!pages) |
| 8503 | goto done; |
| 8504 | |
| 8505 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8506 | GFP_KERNEL); |
| 8507 | if (!vmas) |
| 8508 | goto done; |
| 8509 | |
| 8510 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8511 | GFP_KERNEL); |
| 8512 | if (!imu->bvec) |
| 8513 | goto done; |
| 8514 | |
| 8515 | ret = 0; |
| 8516 | mmap_read_lock(current->mm); |
| 8517 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8518 | pages, vmas); |
| 8519 | if (pret == nr_pages) { |
| 8520 | /* don't support file backed memory */ |
| 8521 | for (i = 0; i < nr_pages; i++) { |
| 8522 | struct vm_area_struct *vma = vmas[i]; |
| 8523 | |
| 8524 | if (vma->vm_file && |
| 8525 | !is_file_hugepages(vma->vm_file)) { |
| 8526 | ret = -EOPNOTSUPP; |
| 8527 | break; |
| 8528 | } |
| 8529 | } |
| 8530 | } else { |
| 8531 | ret = pret < 0 ? pret : -EFAULT; |
| 8532 | } |
| 8533 | mmap_read_unlock(current->mm); |
| 8534 | if (ret) { |
| 8535 | /* |
| 8536 | * if we did partial map, or found file backed vmas, |
| 8537 | * release any pages we did get |
| 8538 | */ |
| 8539 | if (pret > 0) |
| 8540 | unpin_user_pages(pages, pret); |
| 8541 | kvfree(imu->bvec); |
| 8542 | goto done; |
| 8543 | } |
| 8544 | |
| 8545 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8546 | if (ret) { |
| 8547 | unpin_user_pages(pages, pret); |
| 8548 | kvfree(imu->bvec); |
| 8549 | goto done; |
| 8550 | } |
| 8551 | |
| 8552 | off = ubuf & ~PAGE_MASK; |
| 8553 | size = iov->iov_len; |
| 8554 | for (i = 0; i < nr_pages; i++) { |
| 8555 | size_t vec_len; |
| 8556 | |
| 8557 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8558 | imu->bvec[i].bv_page = pages[i]; |
| 8559 | imu->bvec[i].bv_len = vec_len; |
| 8560 | imu->bvec[i].bv_offset = off; |
| 8561 | off = 0; |
| 8562 | size -= vec_len; |
| 8563 | } |
| 8564 | /* store original address for later verification */ |
| 8565 | imu->ubuf = ubuf; |
| 8566 | imu->len = iov->iov_len; |
| 8567 | imu->nr_bvecs = nr_pages; |
| 8568 | ret = 0; |
| 8569 | done: |
| 8570 | kvfree(pages); |
| 8571 | kvfree(vmas); |
| 8572 | return ret; |
| 8573 | } |
| 8574 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8575 | 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] | 8576 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8577 | if (ctx->user_bufs) |
| 8578 | return -EBUSY; |
| 8579 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8580 | return -EINVAL; |
| 8581 | |
| 8582 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8583 | GFP_KERNEL); |
| 8584 | if (!ctx->user_bufs) |
| 8585 | return -ENOMEM; |
| 8586 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8587 | return 0; |
| 8588 | } |
| 8589 | |
| 8590 | static int io_buffer_validate(struct iovec *iov) |
| 8591 | { |
| 8592 | /* |
| 8593 | * Don't impose further limits on the size and buffer |
| 8594 | * constraints here, we'll -EINVAL later when IO is |
| 8595 | * submitted if they are wrong. |
| 8596 | */ |
| 8597 | if (!iov->iov_base || !iov->iov_len) |
| 8598 | return -EFAULT; |
| 8599 | |
| 8600 | /* arbitrary limit, but we need something */ |
| 8601 | if (iov->iov_len > SZ_1G) |
| 8602 | return -EFAULT; |
| 8603 | |
| 8604 | return 0; |
| 8605 | } |
| 8606 | |
| 8607 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8608 | unsigned int nr_args) |
| 8609 | { |
| 8610 | int i, ret; |
| 8611 | struct iovec iov; |
| 8612 | struct page *last_hpage = NULL; |
| 8613 | |
| 8614 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8615 | if (ret) |
| 8616 | return ret; |
| 8617 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8618 | for (i = 0; i < nr_args; i++) { |
| 8619 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8620 | |
| 8621 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8622 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8623 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8624 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8625 | ret = io_buffer_validate(&iov); |
| 8626 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8627 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8628 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8629 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8630 | if (ret) |
| 8631 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8632 | |
| 8633 | ctx->nr_user_bufs++; |
| 8634 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8635 | |
| 8636 | if (ret) |
| 8637 | io_sqe_buffers_unregister(ctx); |
| 8638 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8639 | return ret; |
| 8640 | } |
| 8641 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8642 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8643 | { |
| 8644 | __s32 __user *fds = arg; |
| 8645 | int fd; |
| 8646 | |
| 8647 | if (ctx->cq_ev_fd) |
| 8648 | return -EBUSY; |
| 8649 | |
| 8650 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8651 | return -EFAULT; |
| 8652 | |
| 8653 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8654 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8655 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8656 | ctx->cq_ev_fd = NULL; |
| 8657 | return ret; |
| 8658 | } |
| 8659 | |
| 8660 | return 0; |
| 8661 | } |
| 8662 | |
| 8663 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8664 | { |
| 8665 | if (ctx->cq_ev_fd) { |
| 8666 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8667 | ctx->cq_ev_fd = NULL; |
| 8668 | return 0; |
| 8669 | } |
| 8670 | |
| 8671 | return -ENXIO; |
| 8672 | } |
| 8673 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8674 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8675 | { |
| 8676 | struct io_ring_ctx *ctx = data; |
| 8677 | struct io_buffer *buf = p; |
| 8678 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8679 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8680 | return 0; |
| 8681 | } |
| 8682 | |
| 8683 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8684 | { |
| 8685 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8686 | idr_destroy(&ctx->io_buffer_idr); |
| 8687 | } |
| 8688 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8689 | 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] | 8690 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8691 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8692 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8693 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8694 | if (tsk && req->task != tsk) |
| 8695 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8696 | list_del(&req->compl.list); |
| 8697 | kmem_cache_free(req_cachep, req); |
| 8698 | } |
| 8699 | } |
| 8700 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8701 | 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] | 8702 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8703 | struct io_submit_state *submit_state = &ctx->submit_state; |
| 8704 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8705 | mutex_lock(&ctx->uring_lock); |
| 8706 | |
| 8707 | if (submit_state->free_reqs) |
| 8708 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8709 | submit_state->reqs); |
| 8710 | |
| 8711 | io_req_cache_free(&submit_state->comp.free_list, NULL); |
| 8712 | |
| 8713 | spin_lock_irq(&ctx->completion_lock); |
| 8714 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); |
| 8715 | spin_unlock_irq(&ctx->completion_lock); |
| 8716 | |
| 8717 | mutex_unlock(&ctx->uring_lock); |
| 8718 | } |
| 8719 | |
| 8720 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8721 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8722 | /* |
| 8723 | * Some may use context even when all refs and requests have been put, |
| 8724 | * and they are free to do so while still holding uring_lock, see |
| 8725 | * __io_req_task_submit(). Wait for them to finish. |
| 8726 | */ |
| 8727 | mutex_lock(&ctx->uring_lock); |
| 8728 | mutex_unlock(&ctx->uring_lock); |
| 8729 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8730 | io_finish_async(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8731 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8732 | |
| 8733 | if (ctx->sqo_task) { |
| 8734 | put_task_struct(ctx->sqo_task); |
| 8735 | ctx->sqo_task = NULL; |
| 8736 | mmdrop(ctx->mm_account); |
| 8737 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8738 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8739 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8740 | #ifdef CONFIG_BLK_CGROUP |
| 8741 | if (ctx->sqo_blkcg_css) |
| 8742 | css_put(ctx->sqo_blkcg_css); |
| 8743 | #endif |
| 8744 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8745 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8746 | io_sqe_files_unregister(ctx); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8747 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8748 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8749 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8750 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8751 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8752 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8753 | if (ctx->ring_sock) { |
| 8754 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8755 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8756 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8757 | #endif |
| 8758 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8759 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8760 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8761 | |
| 8762 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8763 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8764 | put_cred(ctx->creds); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8765 | io_req_caches_free(ctx, NULL); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8766 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8767 | kfree(ctx); |
| 8768 | } |
| 8769 | |
| 8770 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8771 | { |
| 8772 | struct io_ring_ctx *ctx = file->private_data; |
| 8773 | __poll_t mask = 0; |
| 8774 | |
| 8775 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8776 | /* |
| 8777 | * synchronizes with barrier from wq_has_sleeper call in |
| 8778 | * io_commit_cqring |
| 8779 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8780 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8781 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8782 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8783 | |
| 8784 | /* |
| 8785 | * Don't flush cqring overflow list here, just do a simple check. |
| 8786 | * Otherwise there could possible be ABBA deadlock: |
| 8787 | * CPU0 CPU1 |
| 8788 | * ---- ---- |
| 8789 | * lock(&ctx->uring_lock); |
| 8790 | * lock(&ep->mtx); |
| 8791 | * lock(&ctx->uring_lock); |
| 8792 | * lock(&ep->mtx); |
| 8793 | * |
| 8794 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8795 | * pushs them to do the flush. |
| 8796 | */ |
| 8797 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8798 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8799 | |
| 8800 | return mask; |
| 8801 | } |
| 8802 | |
| 8803 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8804 | { |
| 8805 | struct io_ring_ctx *ctx = file->private_data; |
| 8806 | |
| 8807 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8808 | } |
| 8809 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8810 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8811 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8812 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8813 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8814 | iod = idr_remove(&ctx->personality_idr, id); |
| 8815 | if (iod) { |
| 8816 | put_cred(iod->creds); |
| 8817 | if (refcount_dec_and_test(&iod->count)) |
| 8818 | kfree(iod); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8819 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8820 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8821 | |
| 8822 | return -EINVAL; |
| 8823 | } |
| 8824 | |
| 8825 | static int io_remove_personalities(int id, void *p, void *data) |
| 8826 | { |
| 8827 | struct io_ring_ctx *ctx = data; |
| 8828 | |
| 8829 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8830 | return 0; |
| 8831 | } |
| 8832 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8833 | static void io_ring_exit_work(struct work_struct *work) |
| 8834 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8835 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8836 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8837 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8838 | /* |
| 8839 | * If we're doing polled IO and end up having requests being |
| 8840 | * submitted async (out-of-line), then completions can come in while |
| 8841 | * we're waiting for refs to drop. We need to reap these manually, |
| 8842 | * as nobody else will be looking for them. |
| 8843 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8844 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8845 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8846 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8847 | io_ring_ctx_free(ctx); |
| 8848 | } |
| 8849 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8850 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8851 | { |
| 8852 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8853 | |
| 8854 | return req->ctx == data; |
| 8855 | } |
| 8856 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8857 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8858 | { |
| 8859 | mutex_lock(&ctx->uring_lock); |
| 8860 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8861 | |
| 8862 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8863 | ctx->sqo_dead = 1; |
| 8864 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8865 | /* if force is set, the ring is going away. always drop after that */ |
| 8866 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8867 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8868 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8869 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8870 | mutex_unlock(&ctx->uring_lock); |
| 8871 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8872 | io_kill_timeouts(ctx, NULL, NULL); |
| 8873 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8874 | |
| 8875 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8876 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8877 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8878 | /* 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] | 8879 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8880 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8881 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8882 | /* |
| 8883 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8884 | * if we're exiting a ton of rings at the same time. It just adds |
| 8885 | * noise and overhead, there's no discernable change in runtime |
| 8886 | * over using system_wq. |
| 8887 | */ |
| 8888 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8889 | } |
| 8890 | |
| 8891 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8892 | { |
| 8893 | struct io_ring_ctx *ctx = file->private_data; |
| 8894 | |
| 8895 | file->private_data = NULL; |
| 8896 | io_ring_ctx_wait_and_kill(ctx); |
| 8897 | return 0; |
| 8898 | } |
| 8899 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8900 | struct io_task_cancel { |
| 8901 | struct task_struct *task; |
| 8902 | struct files_struct *files; |
| 8903 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8904 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8905 | 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] | 8906 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8907 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8908 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8909 | bool ret; |
| 8910 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8911 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8912 | unsigned long flags; |
| 8913 | struct io_ring_ctx *ctx = req->ctx; |
| 8914 | |
| 8915 | /* protect against races with linked timeouts */ |
| 8916 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8917 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8918 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8919 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8920 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8921 | } |
| 8922 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8923 | } |
| 8924 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8925 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8926 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8927 | struct files_struct *files) |
| 8928 | { |
| 8929 | struct io_defer_entry *de = NULL; |
| 8930 | LIST_HEAD(list); |
| 8931 | |
| 8932 | spin_lock_irq(&ctx->completion_lock); |
| 8933 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8934 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8935 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8936 | break; |
| 8937 | } |
| 8938 | } |
| 8939 | spin_unlock_irq(&ctx->completion_lock); |
| 8940 | |
| 8941 | while (!list_empty(&list)) { |
| 8942 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8943 | list_del_init(&de->list); |
| 8944 | req_set_fail_links(de->req); |
| 8945 | io_put_req(de->req); |
| 8946 | io_req_complete(de->req, -ECANCELED); |
| 8947 | kfree(de); |
| 8948 | } |
| 8949 | } |
| 8950 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8951 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8952 | struct task_struct *task, |
| 8953 | struct files_struct *files) |
| 8954 | { |
| 8955 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
| 8956 | |
| 8957 | while (1) { |
| 8958 | enum io_wq_cancel cret; |
| 8959 | bool ret = false; |
| 8960 | |
| 8961 | if (ctx->io_wq) { |
| 8962 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8963 | &cancel, true); |
| 8964 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8965 | } |
| 8966 | |
| 8967 | /* SQPOLL thread does its own polling */ |
| 8968 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8969 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8970 | io_iopoll_try_reap_events(ctx); |
| 8971 | ret = true; |
| 8972 | } |
| 8973 | } |
| 8974 | |
| 8975 | ret |= io_poll_remove_all(ctx, task, files); |
| 8976 | ret |= io_kill_timeouts(ctx, task, files); |
| 8977 | ret |= io_run_task_work(); |
| 8978 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8979 | if (!ret) |
| 8980 | break; |
| 8981 | cond_resched(); |
| 8982 | } |
| 8983 | } |
| 8984 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8985 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8986 | struct task_struct *task, |
| 8987 | struct files_struct *files) |
| 8988 | { |
| 8989 | struct io_kiocb *req; |
| 8990 | int cnt = 0; |
| 8991 | |
| 8992 | spin_lock_irq(&ctx->inflight_lock); |
| 8993 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8994 | cnt += io_match_task(req, task, files); |
| 8995 | spin_unlock_irq(&ctx->inflight_lock); |
| 8996 | return cnt; |
| 8997 | } |
| 8998 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8999 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 9000 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9001 | struct files_struct *files) |
| 9002 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9003 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 9004 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9005 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9006 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9007 | inflight = io_uring_count_inflight(ctx, task, files); |
| 9008 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9009 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9010 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9011 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9012 | |
| 9013 | if (ctx->sq_data) |
| 9014 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9015 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 9016 | TASK_UNINTERRUPTIBLE); |
| 9017 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 9018 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 9019 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9020 | if (ctx->sq_data) |
| 9021 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9022 | } |
| 9023 | } |
| 9024 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9025 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 9026 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9027 | mutex_lock(&ctx->uring_lock); |
| 9028 | ctx->sqo_dead = 1; |
| 9029 | mutex_unlock(&ctx->uring_lock); |
| 9030 | |
| 9031 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 9032 | if (ctx->rings) |
| 9033 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9036 | /* |
| 9037 | * We need to iteratively cancel requests, in case a request has dependent |
| 9038 | * hard links. These persist even for failure of cancelations, hence keep |
| 9039 | * looping until none are found. |
| 9040 | */ |
| 9041 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 9042 | struct files_struct *files) |
| 9043 | { |
| 9044 | struct task_struct *task = current; |
| 9045 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9046 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9047 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9048 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9049 | atomic_inc(&task->io_uring->in_idle); |
| 9050 | io_sq_thread_park(ctx->sq_data); |
| 9051 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9052 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 9053 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9054 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 9055 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 9056 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9057 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9058 | |
| 9059 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 9060 | atomic_dec(&task->io_uring->in_idle); |
| 9061 | /* |
| 9062 | * If the files that are going away are the ones in the thread |
| 9063 | * identity, clear them out. |
| 9064 | */ |
| 9065 | if (task->io_uring->identity->files == files) |
| 9066 | task->io_uring->identity->files = NULL; |
| 9067 | io_sq_thread_unpark(ctx->sq_data); |
| 9068 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9069 | } |
| 9070 | |
| 9071 | /* |
| 9072 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9073 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9074 | 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] | 9075 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9076 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9077 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9078 | |
| 9079 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9080 | ret = io_uring_alloc_task_context(current); |
| 9081 | if (unlikely(ret)) |
| 9082 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9083 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9084 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9085 | if (tctx->last != file) { |
| 9086 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9087 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9088 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9089 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9090 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 9091 | file, GFP_KERNEL)); |
| 9092 | if (ret) { |
| 9093 | fput(file); |
| 9094 | return ret; |
| 9095 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 9096 | |
| 9097 | /* one and only SQPOLL file note, held by sqo_task */ |
| 9098 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 9099 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9100 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9101 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9102 | } |
| 9103 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9104 | /* |
| 9105 | * This is race safe in that the task itself is doing this, hence it |
| 9106 | * cannot be going through the exit/cancel paths at the same time. |
| 9107 | * This cannot be modified while exit/cancel is running. |
| 9108 | */ |
| 9109 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9110 | tctx->sqpoll = true; |
| 9111 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9112 | return 0; |
| 9113 | } |
| 9114 | |
| 9115 | /* |
| 9116 | * Remove this io_uring_file -> task mapping. |
| 9117 | */ |
| 9118 | static void io_uring_del_task_file(struct file *file) |
| 9119 | { |
| 9120 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9121 | |
| 9122 | if (tctx->last == file) |
| 9123 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9124 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9125 | if (file) |
| 9126 | fput(file); |
| 9127 | } |
| 9128 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9129 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9130 | { |
| 9131 | struct file *file; |
| 9132 | unsigned long index; |
| 9133 | |
| 9134 | xa_for_each(&tctx->xa, index, file) |
| 9135 | io_uring_del_task_file(file); |
| 9136 | } |
| 9137 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9138 | void __io_uring_files_cancel(struct files_struct *files) |
| 9139 | { |
| 9140 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9141 | struct file *file; |
| 9142 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9143 | |
| 9144 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9145 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9146 | xa_for_each(&tctx->xa, index, file) |
| 9147 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9148 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9149 | |
| 9150 | if (files) |
| 9151 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9152 | } |
| 9153 | |
| 9154 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9155 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9156 | return percpu_counter_sum(&tctx->inflight); |
| 9157 | } |
| 9158 | |
| 9159 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 9160 | { |
| 9161 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9162 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9163 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9164 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9165 | if (!ctx->sq_data) |
| 9166 | return; |
| 9167 | tctx = ctx->sq_data->thread->io_uring; |
| 9168 | io_disable_sqo_submit(ctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9169 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9170 | atomic_inc(&tctx->in_idle); |
| 9171 | do { |
| 9172 | /* read completions before cancelations */ |
| 9173 | inflight = tctx_inflight(tctx); |
| 9174 | if (!inflight) |
| 9175 | break; |
| 9176 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9177 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9178 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9179 | /* |
| 9180 | * If we've seen completions, retry without waiting. This |
| 9181 | * avoids a race where a completion comes in before we did |
| 9182 | * prepare_to_wait(). |
| 9183 | */ |
| 9184 | if (inflight == tctx_inflight(tctx)) |
| 9185 | schedule(); |
| 9186 | finish_wait(&tctx->wait, &wait); |
| 9187 | } while (1); |
| 9188 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9189 | } |
| 9190 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9191 | /* |
| 9192 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9193 | * requests. |
| 9194 | */ |
| 9195 | void __io_uring_task_cancel(void) |
| 9196 | { |
| 9197 | struct io_uring_task *tctx = current->io_uring; |
| 9198 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9199 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9200 | |
| 9201 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9202 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9203 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9204 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9205 | if (tctx->sqpoll) { |
| 9206 | struct file *file; |
| 9207 | unsigned long index; |
| 9208 | |
| 9209 | xa_for_each(&tctx->xa, index, file) |
| 9210 | io_uring_cancel_sqpoll(file->private_data); |
| 9211 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9212 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9213 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9214 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9215 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9216 | if (!inflight) |
| 9217 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9218 | __io_uring_files_cancel(NULL); |
| 9219 | |
| 9220 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9221 | |
| 9222 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9223 | * If we've seen completions, retry without waiting. This |
| 9224 | * avoids a race where a completion comes in before we did |
| 9225 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9226 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9227 | if (inflight == tctx_inflight(tctx)) |
| 9228 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9229 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9230 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9231 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9232 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9233 | |
| 9234 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9235 | } |
| 9236 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9237 | static int io_uring_flush(struct file *file, void *data) |
| 9238 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9239 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9240 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9241 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9242 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9243 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9244 | io_req_caches_free(ctx, current); |
| 9245 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9246 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9247 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9248 | return 0; |
| 9249 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9250 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9251 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9252 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9253 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9254 | /* |
| 9255 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9256 | * task file note. If the task is exiting, drop regardless of count. |
| 9257 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9258 | if (atomic_long_read(&file->f_count) != 2) |
| 9259 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9260 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9261 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9262 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 9263 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9264 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9265 | /* sqo_dead check is for when this happens after cancellation */ |
| 9266 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9267 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9268 | |
| 9269 | io_disable_sqo_submit(ctx); |
| 9270 | } |
| 9271 | |
| 9272 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9273 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9274 | return 0; |
| 9275 | } |
| 9276 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9277 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9278 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9279 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9280 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9281 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9282 | struct page *page; |
| 9283 | void *ptr; |
| 9284 | |
| 9285 | switch (offset) { |
| 9286 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9287 | case IORING_OFF_CQ_RING: |
| 9288 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9289 | break; |
| 9290 | case IORING_OFF_SQES: |
| 9291 | ptr = ctx->sq_sqes; |
| 9292 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9293 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9294 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9295 | } |
| 9296 | |
| 9297 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9298 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9299 | return ERR_PTR(-EINVAL); |
| 9300 | |
| 9301 | return ptr; |
| 9302 | } |
| 9303 | |
| 9304 | #ifdef CONFIG_MMU |
| 9305 | |
| 9306 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9307 | { |
| 9308 | size_t sz = vma->vm_end - vma->vm_start; |
| 9309 | unsigned long pfn; |
| 9310 | void *ptr; |
| 9311 | |
| 9312 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9313 | if (IS_ERR(ptr)) |
| 9314 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9315 | |
| 9316 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9317 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9318 | } |
| 9319 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9320 | #else /* !CONFIG_MMU */ |
| 9321 | |
| 9322 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9323 | { |
| 9324 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9325 | } |
| 9326 | |
| 9327 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9328 | { |
| 9329 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9330 | } |
| 9331 | |
| 9332 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9333 | unsigned long addr, unsigned long len, |
| 9334 | unsigned long pgoff, unsigned long flags) |
| 9335 | { |
| 9336 | void *ptr; |
| 9337 | |
| 9338 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9339 | if (IS_ERR(ptr)) |
| 9340 | return PTR_ERR(ptr); |
| 9341 | |
| 9342 | return (unsigned long) ptr; |
| 9343 | } |
| 9344 | |
| 9345 | #endif /* !CONFIG_MMU */ |
| 9346 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9347 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9348 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9349 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9350 | DEFINE_WAIT(wait); |
| 9351 | |
| 9352 | do { |
| 9353 | if (!io_sqring_full(ctx)) |
| 9354 | break; |
| 9355 | |
| 9356 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9357 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9358 | if (unlikely(ctx->sqo_dead)) { |
| 9359 | ret = -EOWNERDEAD; |
| 9360 | goto out; |
| 9361 | } |
| 9362 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9363 | if (!io_sqring_full(ctx)) |
| 9364 | break; |
| 9365 | |
| 9366 | schedule(); |
| 9367 | } while (!signal_pending(current)); |
| 9368 | |
| 9369 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9370 | out: |
| 9371 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9372 | } |
| 9373 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9374 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9375 | struct __kernel_timespec __user **ts, |
| 9376 | const sigset_t __user **sig) |
| 9377 | { |
| 9378 | struct io_uring_getevents_arg arg; |
| 9379 | |
| 9380 | /* |
| 9381 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9382 | * is just a pointer to the sigset_t. |
| 9383 | */ |
| 9384 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9385 | *sig = (const sigset_t __user *) argp; |
| 9386 | *ts = NULL; |
| 9387 | return 0; |
| 9388 | } |
| 9389 | |
| 9390 | /* |
| 9391 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9392 | * timespec and sigset_t pointers if good. |
| 9393 | */ |
| 9394 | if (*argsz != sizeof(arg)) |
| 9395 | return -EINVAL; |
| 9396 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9397 | return -EFAULT; |
| 9398 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9399 | *argsz = arg.sigmask_sz; |
| 9400 | *ts = u64_to_user_ptr(arg.ts); |
| 9401 | return 0; |
| 9402 | } |
| 9403 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9404 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9405 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9406 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9407 | { |
| 9408 | struct io_ring_ctx *ctx; |
| 9409 | long ret = -EBADF; |
| 9410 | int submitted = 0; |
| 9411 | struct fd f; |
| 9412 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9413 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9414 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9415 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9416 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9417 | return -EINVAL; |
| 9418 | |
| 9419 | f = fdget(fd); |
| 9420 | if (!f.file) |
| 9421 | return -EBADF; |
| 9422 | |
| 9423 | ret = -EOPNOTSUPP; |
| 9424 | if (f.file->f_op != &io_uring_fops) |
| 9425 | goto out_fput; |
| 9426 | |
| 9427 | ret = -ENXIO; |
| 9428 | ctx = f.file->private_data; |
| 9429 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9430 | goto out_fput; |
| 9431 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9432 | ret = -EBADFD; |
| 9433 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9434 | goto out; |
| 9435 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9436 | /* |
| 9437 | * For SQ polling, the thread will do all submissions and completions. |
| 9438 | * Just return the requested submit count, and wake the thread if |
| 9439 | * we were asked to. |
| 9440 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9441 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9442 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9443 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9444 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9445 | ret = -EOWNERDEAD; |
| 9446 | if (unlikely(ctx->sqo_dead)) |
| 9447 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9448 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9449 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9450 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9451 | ret = io_sqpoll_wait_sq(ctx); |
| 9452 | if (ret) |
| 9453 | goto out; |
| 9454 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9455 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9456 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9457 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9458 | if (unlikely(ret)) |
| 9459 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9460 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9461 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9462 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9463 | |
| 9464 | if (submitted != to_submit) |
| 9465 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9466 | } |
| 9467 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9468 | const sigset_t __user *sig; |
| 9469 | struct __kernel_timespec __user *ts; |
| 9470 | |
| 9471 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9472 | if (unlikely(ret)) |
| 9473 | goto out; |
| 9474 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9475 | min_complete = min(min_complete, ctx->cq_entries); |
| 9476 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9477 | /* |
| 9478 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9479 | * space applications don't need to do io completion events |
| 9480 | * polling again, they can rely on io_sq_thread to do polling |
| 9481 | * work, which can reduce cpu usage and uring_lock contention. |
| 9482 | */ |
| 9483 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9484 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9485 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9486 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9487 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9488 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9489 | } |
| 9490 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9491 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9492 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9493 | out_fput: |
| 9494 | fdput(f); |
| 9495 | return submitted ? submitted : ret; |
| 9496 | } |
| 9497 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9498 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9499 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9500 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9501 | struct io_identity *iod = p; |
| 9502 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9503 | struct seq_file *m = data; |
| 9504 | struct user_namespace *uns = seq_user_ns(m); |
| 9505 | struct group_info *gi; |
| 9506 | kernel_cap_t cap; |
| 9507 | unsigned __capi; |
| 9508 | int g; |
| 9509 | |
| 9510 | seq_printf(m, "%5d\n", id); |
| 9511 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9512 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9513 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9514 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9515 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9516 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9517 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9518 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9519 | seq_puts(m, "\n\tGroups:\t"); |
| 9520 | gi = cred->group_info; |
| 9521 | for (g = 0; g < gi->ngroups; g++) { |
| 9522 | seq_put_decimal_ull(m, g ? " " : "", |
| 9523 | from_kgid_munged(uns, gi->gid[g])); |
| 9524 | } |
| 9525 | seq_puts(m, "\n\tCapEff:\t"); |
| 9526 | cap = cred->cap_effective; |
| 9527 | CAP_FOR_EACH_U32(__capi) |
| 9528 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9529 | seq_putc(m, '\n'); |
| 9530 | return 0; |
| 9531 | } |
| 9532 | |
| 9533 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9534 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9535 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9536 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9537 | int i; |
| 9538 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9539 | /* |
| 9540 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9541 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9542 | * cases. If we fail to get the lock, we just don't iterate any |
| 9543 | * structures that could be going away outside the io_uring mutex. |
| 9544 | */ |
| 9545 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9546 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9547 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9548 | sq = ctx->sq_data; |
| 9549 | |
| 9550 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9551 | 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] | 9552 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9553 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9554 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9555 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9556 | if (f) |
| 9557 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9558 | else |
| 9559 | seq_printf(m, "%5u: <none>\n", i); |
| 9560 | } |
| 9561 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9562 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9563 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9564 | |
| 9565 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9566 | (unsigned int) buf->len); |
| 9567 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9568 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9569 | seq_printf(m, "Personalities:\n"); |
| 9570 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9571 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9572 | seq_printf(m, "PollList:\n"); |
| 9573 | spin_lock_irq(&ctx->completion_lock); |
| 9574 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9575 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9576 | struct io_kiocb *req; |
| 9577 | |
| 9578 | hlist_for_each_entry(req, list, hash_node) |
| 9579 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9580 | req->task->task_works != NULL); |
| 9581 | } |
| 9582 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9583 | if (has_lock) |
| 9584 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9585 | } |
| 9586 | |
| 9587 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9588 | { |
| 9589 | struct io_ring_ctx *ctx = f->private_data; |
| 9590 | |
| 9591 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9592 | __io_uring_show_fdinfo(ctx, m); |
| 9593 | percpu_ref_put(&ctx->refs); |
| 9594 | } |
| 9595 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9596 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9597 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9598 | static const struct file_operations io_uring_fops = { |
| 9599 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9600 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9601 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9602 | #ifndef CONFIG_MMU |
| 9603 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9604 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9605 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9606 | .poll = io_uring_poll, |
| 9607 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9608 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9609 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9610 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9611 | }; |
| 9612 | |
| 9613 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9614 | struct io_uring_params *p) |
| 9615 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9616 | struct io_rings *rings; |
| 9617 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9618 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9619 | /* make sure these are sane, as we already accounted them */ |
| 9620 | ctx->sq_entries = p->sq_entries; |
| 9621 | ctx->cq_entries = p->cq_entries; |
| 9622 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9623 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9624 | if (size == SIZE_MAX) |
| 9625 | return -EOVERFLOW; |
| 9626 | |
| 9627 | rings = io_mem_alloc(size); |
| 9628 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9629 | return -ENOMEM; |
| 9630 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9631 | ctx->rings = rings; |
| 9632 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9633 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9634 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9635 | rings->sq_ring_entries = p->sq_entries; |
| 9636 | rings->cq_ring_entries = p->cq_entries; |
| 9637 | ctx->sq_mask = rings->sq_ring_mask; |
| 9638 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9639 | |
| 9640 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9641 | if (size == SIZE_MAX) { |
| 9642 | io_mem_free(ctx->rings); |
| 9643 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9644 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9645 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9646 | |
| 9647 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9648 | if (!ctx->sq_sqes) { |
| 9649 | io_mem_free(ctx->rings); |
| 9650 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9651 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9652 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9653 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9654 | return 0; |
| 9655 | } |
| 9656 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9657 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9658 | { |
| 9659 | int ret, fd; |
| 9660 | |
| 9661 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9662 | if (fd < 0) |
| 9663 | return fd; |
| 9664 | |
| 9665 | ret = io_uring_add_task_file(ctx, file); |
| 9666 | if (ret) { |
| 9667 | put_unused_fd(fd); |
| 9668 | return ret; |
| 9669 | } |
| 9670 | fd_install(fd, file); |
| 9671 | return fd; |
| 9672 | } |
| 9673 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9674 | /* |
| 9675 | * Allocate an anonymous fd, this is what constitutes the application |
| 9676 | * visible backing of an io_uring instance. The application mmaps this |
| 9677 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9678 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9679 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9680 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9681 | { |
| 9682 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9683 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9684 | int ret; |
| 9685 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9686 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9687 | &ctx->ring_sock); |
| 9688 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9689 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9690 | #endif |
| 9691 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9692 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9693 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9694 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9695 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9696 | sock_release(ctx->ring_sock); |
| 9697 | ctx->ring_sock = NULL; |
| 9698 | } else { |
| 9699 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9700 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9701 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9702 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9703 | } |
| 9704 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9705 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9706 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9707 | { |
| 9708 | struct user_struct *user = NULL; |
| 9709 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9710 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9711 | int ret; |
| 9712 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9713 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9714 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9715 | if (entries > IORING_MAX_ENTRIES) { |
| 9716 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9717 | return -EINVAL; |
| 9718 | entries = IORING_MAX_ENTRIES; |
| 9719 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9720 | |
| 9721 | /* |
| 9722 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9723 | * application to drive a higher depth than the size of the SQ ring, |
| 9724 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9725 | * some flexibility in overcommitting a bit. If the application has |
| 9726 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9727 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9728 | */ |
| 9729 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9730 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9731 | /* |
| 9732 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9733 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9734 | * any cq vs sq ring sizing. |
| 9735 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9736 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9737 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9738 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9739 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9740 | return -EINVAL; |
| 9741 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9742 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9743 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9744 | if (p->cq_entries < p->sq_entries) |
| 9745 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9746 | } else { |
| 9747 | p->cq_entries = 2 * p->sq_entries; |
| 9748 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9749 | |
| 9750 | user = get_uid(current_user()); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9751 | |
| 9752 | ctx = io_ring_ctx_alloc(p); |
| 9753 | if (!ctx) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9754 | free_uid(user); |
| 9755 | return -ENOMEM; |
| 9756 | } |
| 9757 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9758 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9759 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9760 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9761 | #ifdef CONFIG_AUDIT |
| 9762 | ctx->loginuid = current->loginuid; |
| 9763 | ctx->sessionid = current->sessionid; |
| 9764 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9765 | ctx->sqo_task = get_task_struct(current); |
| 9766 | |
| 9767 | /* |
| 9768 | * This is just grabbed for accounting purposes. When a process exits, |
| 9769 | * the mm is exited and dropped before the files, hence we need to hang |
| 9770 | * on to this mm purely for the purposes of being able to unaccount |
| 9771 | * memory (locked/pinned vm). It's not used for anything else. |
| 9772 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9773 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9774 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9775 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9776 | #ifdef CONFIG_BLK_CGROUP |
| 9777 | /* |
| 9778 | * The sq thread will belong to the original cgroup it was inited in. |
| 9779 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9780 | * issued bios will be associated with the closest cgroup later in the |
| 9781 | * block layer. |
| 9782 | */ |
| 9783 | rcu_read_lock(); |
| 9784 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9785 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9786 | rcu_read_unlock(); |
| 9787 | if (!ret) { |
| 9788 | /* don't init against a dying cgroup, have the user try again */ |
| 9789 | ctx->sqo_blkcg_css = NULL; |
| 9790 | ret = -ENODEV; |
| 9791 | goto err; |
| 9792 | } |
| 9793 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9794 | ret = io_allocate_scq_urings(ctx, p); |
| 9795 | if (ret) |
| 9796 | goto err; |
| 9797 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9798 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9799 | if (ret) |
| 9800 | goto err; |
| 9801 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9802 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9803 | io_sq_offload_start(ctx); |
| 9804 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9805 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9806 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9807 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9808 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9809 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9810 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9811 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9812 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9813 | |
| 9814 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9815 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9816 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9817 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9818 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9819 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9820 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9821 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9822 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9823 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9824 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9825 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9826 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9827 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9828 | |
| 9829 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9830 | ret = -EFAULT; |
| 9831 | goto err; |
| 9832 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9833 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9834 | file = io_uring_get_file(ctx); |
| 9835 | if (IS_ERR(file)) { |
| 9836 | ret = PTR_ERR(file); |
| 9837 | goto err; |
| 9838 | } |
| 9839 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9840 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9841 | * Install ring fd as the very last thing, so we don't risk someone |
| 9842 | * having closed it before we finish setup |
| 9843 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9844 | ret = io_uring_install_fd(ctx, file); |
| 9845 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9846 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9847 | /* fput will clean it up */ |
| 9848 | fput(file); |
| 9849 | return ret; |
| 9850 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9851 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9852 | 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] | 9853 | return ret; |
| 9854 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9855 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9856 | io_ring_ctx_wait_and_kill(ctx); |
| 9857 | return ret; |
| 9858 | } |
| 9859 | |
| 9860 | /* |
| 9861 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9862 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9863 | * params structure passed in. |
| 9864 | */ |
| 9865 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9866 | { |
| 9867 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9868 | int i; |
| 9869 | |
| 9870 | if (copy_from_user(&p, params, sizeof(p))) |
| 9871 | return -EFAULT; |
| 9872 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9873 | if (p.resv[i]) |
| 9874 | return -EINVAL; |
| 9875 | } |
| 9876 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9877 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9878 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9879 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9880 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9881 | return -EINVAL; |
| 9882 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9883 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9884 | } |
| 9885 | |
| 9886 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9887 | struct io_uring_params __user *, params) |
| 9888 | { |
| 9889 | return io_uring_setup(entries, params); |
| 9890 | } |
| 9891 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9892 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9893 | { |
| 9894 | struct io_uring_probe *p; |
| 9895 | size_t size; |
| 9896 | int i, ret; |
| 9897 | |
| 9898 | size = struct_size(p, ops, nr_args); |
| 9899 | if (size == SIZE_MAX) |
| 9900 | return -EOVERFLOW; |
| 9901 | p = kzalloc(size, GFP_KERNEL); |
| 9902 | if (!p) |
| 9903 | return -ENOMEM; |
| 9904 | |
| 9905 | ret = -EFAULT; |
| 9906 | if (copy_from_user(p, arg, size)) |
| 9907 | goto out; |
| 9908 | ret = -EINVAL; |
| 9909 | if (memchr_inv(p, 0, size)) |
| 9910 | goto out; |
| 9911 | |
| 9912 | p->last_op = IORING_OP_LAST - 1; |
| 9913 | if (nr_args > IORING_OP_LAST) |
| 9914 | nr_args = IORING_OP_LAST; |
| 9915 | |
| 9916 | for (i = 0; i < nr_args; i++) { |
| 9917 | p->ops[i].op = i; |
| 9918 | if (!io_op_defs[i].not_supported) |
| 9919 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9920 | } |
| 9921 | p->ops_len = i; |
| 9922 | |
| 9923 | ret = 0; |
| 9924 | if (copy_to_user(arg, p, size)) |
| 9925 | ret = -EFAULT; |
| 9926 | out: |
| 9927 | kfree(p); |
| 9928 | return ret; |
| 9929 | } |
| 9930 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9931 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9932 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9933 | struct io_identity *id; |
| 9934 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9935 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9936 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9937 | if (unlikely(!id)) |
| 9938 | return -ENOMEM; |
| 9939 | |
| 9940 | io_init_identity(id); |
| 9941 | id->creds = get_current_cred(); |
| 9942 | |
| 9943 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9944 | if (ret < 0) { |
| 9945 | put_cred(id->creds); |
| 9946 | kfree(id); |
| 9947 | } |
| 9948 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9949 | } |
| 9950 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9951 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9952 | unsigned int nr_args) |
| 9953 | { |
| 9954 | struct io_uring_restriction *res; |
| 9955 | size_t size; |
| 9956 | int i, ret; |
| 9957 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9958 | /* Restrictions allowed only if rings started disabled */ |
| 9959 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9960 | return -EBADFD; |
| 9961 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9962 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9963 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9964 | return -EBUSY; |
| 9965 | |
| 9966 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9967 | return -EINVAL; |
| 9968 | |
| 9969 | size = array_size(nr_args, sizeof(*res)); |
| 9970 | if (size == SIZE_MAX) |
| 9971 | return -EOVERFLOW; |
| 9972 | |
| 9973 | res = memdup_user(arg, size); |
| 9974 | if (IS_ERR(res)) |
| 9975 | return PTR_ERR(res); |
| 9976 | |
| 9977 | ret = 0; |
| 9978 | |
| 9979 | for (i = 0; i < nr_args; i++) { |
| 9980 | switch (res[i].opcode) { |
| 9981 | case IORING_RESTRICTION_REGISTER_OP: |
| 9982 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9983 | ret = -EINVAL; |
| 9984 | goto out; |
| 9985 | } |
| 9986 | |
| 9987 | __set_bit(res[i].register_op, |
| 9988 | ctx->restrictions.register_op); |
| 9989 | break; |
| 9990 | case IORING_RESTRICTION_SQE_OP: |
| 9991 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9992 | ret = -EINVAL; |
| 9993 | goto out; |
| 9994 | } |
| 9995 | |
| 9996 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9997 | break; |
| 9998 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9999 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10000 | break; |
| 10001 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10002 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10003 | break; |
| 10004 | default: |
| 10005 | ret = -EINVAL; |
| 10006 | goto out; |
| 10007 | } |
| 10008 | } |
| 10009 | |
| 10010 | out: |
| 10011 | /* Reset all restrictions if an error happened */ |
| 10012 | if (ret != 0) |
| 10013 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10014 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10015 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10016 | |
| 10017 | kfree(res); |
| 10018 | return ret; |
| 10019 | } |
| 10020 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10021 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10022 | { |
| 10023 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10024 | return -EBADFD; |
| 10025 | |
| 10026 | if (ctx->restrictions.registered) |
| 10027 | ctx->restricted = 1; |
| 10028 | |
| 10029 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10030 | |
| 10031 | io_sq_offload_start(ctx); |
| 10032 | |
| 10033 | return 0; |
| 10034 | } |
| 10035 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10036 | static bool io_register_op_must_quiesce(int op) |
| 10037 | { |
| 10038 | switch (op) { |
| 10039 | case IORING_UNREGISTER_FILES: |
| 10040 | case IORING_REGISTER_FILES_UPDATE: |
| 10041 | case IORING_REGISTER_PROBE: |
| 10042 | case IORING_REGISTER_PERSONALITY: |
| 10043 | case IORING_UNREGISTER_PERSONALITY: |
| 10044 | return false; |
| 10045 | default: |
| 10046 | return true; |
| 10047 | } |
| 10048 | } |
| 10049 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10050 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10051 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10052 | __releases(ctx->uring_lock) |
| 10053 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10054 | { |
| 10055 | int ret; |
| 10056 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10057 | /* |
| 10058 | * We're inside the ring mutex, if the ref is already dying, then |
| 10059 | * someone else killed the ctx or is already going through |
| 10060 | * io_uring_register(). |
| 10061 | */ |
| 10062 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10063 | return -ENXIO; |
| 10064 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10065 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10066 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10067 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10068 | /* |
| 10069 | * Drop uring mutex before waiting for references to exit. If |
| 10070 | * another thread is currently inside io_uring_enter() it might |
| 10071 | * need to grab the uring_lock to make progress. If we hold it |
| 10072 | * here across the drain wait, then we can deadlock. It's safe |
| 10073 | * to drop the mutex here, since no new references will come in |
| 10074 | * after we've killed the percpu ref. |
| 10075 | */ |
| 10076 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10077 | do { |
| 10078 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10079 | if (!ret) |
| 10080 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 10081 | ret = io_run_task_work_sig(); |
| 10082 | if (ret < 0) |
| 10083 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10084 | } while (1); |
| 10085 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10086 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10087 | |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 10088 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
| 10089 | return ret; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10090 | } |
| 10091 | |
| 10092 | if (ctx->restricted) { |
| 10093 | if (opcode >= IORING_REGISTER_LAST) { |
| 10094 | ret = -EINVAL; |
| 10095 | goto out; |
| 10096 | } |
| 10097 | |
| 10098 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10099 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10100 | goto out; |
| 10101 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10102 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10103 | |
| 10104 | switch (opcode) { |
| 10105 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10106 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10107 | break; |
| 10108 | case IORING_UNREGISTER_BUFFERS: |
| 10109 | ret = -EINVAL; |
| 10110 | if (arg || nr_args) |
| 10111 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10112 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10113 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10114 | case IORING_REGISTER_FILES: |
| 10115 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10116 | break; |
| 10117 | case IORING_UNREGISTER_FILES: |
| 10118 | ret = -EINVAL; |
| 10119 | if (arg || nr_args) |
| 10120 | break; |
| 10121 | ret = io_sqe_files_unregister(ctx); |
| 10122 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10123 | case IORING_REGISTER_FILES_UPDATE: |
| 10124 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10125 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10126 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10127 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10128 | ret = -EINVAL; |
| 10129 | if (nr_args != 1) |
| 10130 | break; |
| 10131 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10132 | if (ret) |
| 10133 | break; |
| 10134 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10135 | ctx->eventfd_async = 1; |
| 10136 | else |
| 10137 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10138 | break; |
| 10139 | case IORING_UNREGISTER_EVENTFD: |
| 10140 | ret = -EINVAL; |
| 10141 | if (arg || nr_args) |
| 10142 | break; |
| 10143 | ret = io_eventfd_unregister(ctx); |
| 10144 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10145 | case IORING_REGISTER_PROBE: |
| 10146 | ret = -EINVAL; |
| 10147 | if (!arg || nr_args > 256) |
| 10148 | break; |
| 10149 | ret = io_probe(ctx, arg, nr_args); |
| 10150 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10151 | case IORING_REGISTER_PERSONALITY: |
| 10152 | ret = -EINVAL; |
| 10153 | if (arg || nr_args) |
| 10154 | break; |
| 10155 | ret = io_register_personality(ctx); |
| 10156 | break; |
| 10157 | case IORING_UNREGISTER_PERSONALITY: |
| 10158 | ret = -EINVAL; |
| 10159 | if (arg) |
| 10160 | break; |
| 10161 | ret = io_unregister_personality(ctx, nr_args); |
| 10162 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10163 | case IORING_REGISTER_ENABLE_RINGS: |
| 10164 | ret = -EINVAL; |
| 10165 | if (arg || nr_args) |
| 10166 | break; |
| 10167 | ret = io_register_enable_rings(ctx); |
| 10168 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10169 | case IORING_REGISTER_RESTRICTIONS: |
| 10170 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10171 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10172 | default: |
| 10173 | ret = -EINVAL; |
| 10174 | break; |
| 10175 | } |
| 10176 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10177 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10178 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10179 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10180 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10181 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10182 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10183 | return ret; |
| 10184 | } |
| 10185 | |
| 10186 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10187 | void __user *, arg, unsigned int, nr_args) |
| 10188 | { |
| 10189 | struct io_ring_ctx *ctx; |
| 10190 | long ret = -EBADF; |
| 10191 | struct fd f; |
| 10192 | |
| 10193 | f = fdget(fd); |
| 10194 | if (!f.file) |
| 10195 | return -EBADF; |
| 10196 | |
| 10197 | ret = -EOPNOTSUPP; |
| 10198 | if (f.file->f_op != &io_uring_fops) |
| 10199 | goto out_fput; |
| 10200 | |
| 10201 | ctx = f.file->private_data; |
| 10202 | |
| 10203 | mutex_lock(&ctx->uring_lock); |
| 10204 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10205 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10206 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10207 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10208 | out_fput: |
| 10209 | fdput(f); |
| 10210 | return ret; |
| 10211 | } |
| 10212 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10213 | static int __init io_uring_init(void) |
| 10214 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10215 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10216 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10217 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10218 | } while (0) |
| 10219 | |
| 10220 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10221 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10222 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10223 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10224 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10225 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10226 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10227 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10228 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10229 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10230 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10231 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10232 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10233 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10234 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10235 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10236 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10237 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10238 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10239 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10240 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10241 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10242 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10243 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10244 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10245 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10246 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10247 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10248 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10249 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10250 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10251 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10252 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10253 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 10254 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 10255 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10256 | return 0; |
| 10257 | }; |
| 10258 | __initcall(io_uring_init); |