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 | |
| 107 | struct io_uring { |
| 108 | u32 head ____cacheline_aligned_in_smp; |
| 109 | u32 tail ____cacheline_aligned_in_smp; |
| 110 | }; |
| 111 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 112 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | * This data is shared with the application through the mmap at offsets |
| 114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | * |
| 116 | * The offsets to the member fields are published through struct |
| 117 | * io_sqring_offsets when calling io_uring_setup. |
| 118 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Head and tail offsets into the ring; the offsets need to be |
| 122 | * masked to get valid indices. |
| 123 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 125 | * and the application controls tail of the sq ring and the head of the |
| 126 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 129 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | * ring_entries - 1) |
| 132 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | u32 sq_ring_mask, cq_ring_mask; |
| 134 | /* Ring sizes (constant, power of 2) */ |
| 135 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Number of invalid entries dropped by the kernel due to |
| 138 | * invalid index stored in array |
| 139 | * |
| 140 | * Written by the kernel, shouldn't be modified by the |
| 141 | * application (i.e. get number of "new events" by comparing to |
| 142 | * cached value). |
| 143 | * |
| 144 | * After a new SQ head value was read by the application this |
| 145 | * counter includes all submissions that were dropped reaching |
| 146 | * the new SQ head (and possibly more). |
| 147 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 148 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 150 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 151 | * |
| 152 | * Written by the kernel, shouldn't be modified by the |
| 153 | * application. |
| 154 | * |
| 155 | * The application needs a full memory barrier before checking |
| 156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 157 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 158 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 159 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 160 | * Runtime CQ flags |
| 161 | * |
| 162 | * Written by the application, shouldn't be modified by the |
| 163 | * kernel. |
| 164 | */ |
| 165 | u32 cq_flags; |
| 166 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | * Number of completion events lost because the queue was full; |
| 168 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 169 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 170 | * the completion queue. |
| 171 | * |
| 172 | * Written by the kernel, shouldn't be modified by the |
| 173 | * application (i.e. get number of "new events" by comparing to |
| 174 | * cached value). |
| 175 | * |
| 176 | * As completion events come in out of order this counter is not |
| 177 | * ordered with any other data. |
| 178 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 179 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Ring buffer of completion events. |
| 182 | * |
| 183 | * The kernel writes completion events fresh every time they are |
| 184 | * produced, so the application is allowed to modify pending |
| 185 | * entries. |
| 186 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 190 | enum io_uring_cmd_flags { |
| 191 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 192 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 195 | struct io_mapped_ubuf { |
| 196 | u64 ubuf; |
| 197 | size_t len; |
| 198 | struct bio_vec *bvec; |
| 199 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 200 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 203 | struct io_ring_ctx; |
| 204 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 205 | struct io_rsrc_put { |
| 206 | struct list_head list; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 207 | union { |
| 208 | void *rsrc; |
| 209 | struct file *file; |
| 210 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct fixed_rsrc_table { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 214 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 217 | struct fixed_rsrc_ref_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 218 | struct percpu_ref refs; |
| 219 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 220 | struct list_head rsrc_list; |
| 221 | struct fixed_rsrc_data *rsrc_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 222 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 223 | struct io_rsrc_put *prsrc); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 224 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 225 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 226 | }; |
| 227 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 228 | struct fixed_rsrc_data { |
| 229 | struct fixed_rsrc_table *table; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 230 | struct io_ring_ctx *ctx; |
| 231 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 232 | struct fixed_rsrc_ref_node *node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 233 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 234 | struct completion done; |
| 235 | }; |
| 236 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 237 | struct io_buffer { |
| 238 | struct list_head list; |
| 239 | __u64 addr; |
| 240 | __s32 len; |
| 241 | __u16 bid; |
| 242 | }; |
| 243 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 244 | struct io_restriction { |
| 245 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 246 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 247 | u8 sqe_flags_allowed; |
| 248 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 249 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 250 | }; |
| 251 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 252 | struct io_sq_data { |
| 253 | refcount_t refs; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 254 | struct mutex lock; |
| 255 | |
| 256 | /* ctx's that are using this sqd */ |
| 257 | struct list_head ctx_list; |
| 258 | struct list_head ctx_new_list; |
| 259 | struct mutex ctx_lock; |
| 260 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 261 | struct task_struct *thread; |
| 262 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 263 | |
| 264 | unsigned sq_thread_idle; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 265 | }; |
| 266 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 267 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 268 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 269 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 270 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 271 | |
| 272 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 273 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 274 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 275 | unsigned int locked_free_nr; |
| 276 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 277 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 278 | /* IRQ completion list, under ->completion_lock */ |
| 279 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | struct io_submit_state { |
| 283 | struct blk_plug plug; |
| 284 | |
| 285 | /* |
| 286 | * io_kiocb alloc cache |
| 287 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 288 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 289 | unsigned int free_reqs; |
| 290 | |
| 291 | bool plug_started; |
| 292 | |
| 293 | /* |
| 294 | * Batch completion logic |
| 295 | */ |
| 296 | struct io_comp_state comp; |
| 297 | |
| 298 | /* |
| 299 | * File reference cache |
| 300 | */ |
| 301 | struct file *file; |
| 302 | unsigned int fd; |
| 303 | unsigned int file_refs; |
| 304 | unsigned int ios_left; |
| 305 | }; |
| 306 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 307 | struct io_ring_ctx { |
| 308 | struct { |
| 309 | struct percpu_ref refs; |
| 310 | } ____cacheline_aligned_in_smp; |
| 311 | |
| 312 | struct { |
| 313 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 314 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 315 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 316 | unsigned int cq_overflow_flushed: 1; |
| 317 | unsigned int drain_next: 1; |
| 318 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 319 | unsigned int restricted: 1; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 320 | unsigned int sqo_dead: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 321 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 322 | /* |
| 323 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 324 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 325 | * |
| 326 | * This indirection could e.g. be used to assign fixed |
| 327 | * io_uring_sqe entries to operations and only submit them to |
| 328 | * the queue when needed. |
| 329 | * |
| 330 | * The kernel modifies neither the indices array nor the entries |
| 331 | * array. |
| 332 | */ |
| 333 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 334 | unsigned cached_sq_head; |
| 335 | unsigned sq_entries; |
| 336 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 337 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 338 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 339 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 340 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 341 | |
| 342 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 343 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 344 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 345 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 346 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 347 | } ____cacheline_aligned_in_smp; |
| 348 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 349 | struct { |
| 350 | struct mutex uring_lock; |
| 351 | wait_queue_head_t wait; |
| 352 | } ____cacheline_aligned_in_smp; |
| 353 | |
| 354 | struct io_submit_state submit_state; |
| 355 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 356 | struct io_rings *rings; |
| 357 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 358 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 359 | struct io_wq *io_wq; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * For SQPOLL usage - we hold a reference to the parent task, so we |
| 363 | * have access to the ->files |
| 364 | */ |
| 365 | struct task_struct *sqo_task; |
| 366 | |
| 367 | /* Only used for accounting purposes */ |
| 368 | struct mm_struct *mm_account; |
| 369 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 370 | #ifdef CONFIG_BLK_CGROUP |
| 371 | struct cgroup_subsys_state *sqo_blkcg_css; |
| 372 | #endif |
| 373 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 374 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 375 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 376 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 377 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 378 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 379 | /* |
| 380 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 381 | * readers must ensure that ->refs is alive as long as the file* is |
| 382 | * used. Only updated through io_uring_register(2). |
| 383 | */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 384 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 385 | unsigned nr_user_files; |
| 386 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 387 | /* if used, fixed mapped user buffers */ |
| 388 | unsigned nr_user_bufs; |
| 389 | struct io_mapped_ubuf *user_bufs; |
| 390 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 391 | struct user_struct *user; |
| 392 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 393 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 394 | |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 395 | #ifdef CONFIG_AUDIT |
| 396 | kuid_t loginuid; |
| 397 | unsigned int sessionid; |
| 398 | #endif |
| 399 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 400 | struct completion ref_comp; |
| 401 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 402 | |
| 403 | #if defined(CONFIG_UNIX) |
| 404 | struct socket *ring_sock; |
| 405 | #endif |
| 406 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 407 | struct idr io_buffer_idr; |
| 408 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 409 | struct idr personality_idr; |
| 410 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 411 | struct { |
| 412 | unsigned cached_cq_tail; |
| 413 | unsigned cq_entries; |
| 414 | unsigned cq_mask; |
| 415 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 416 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 417 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 418 | struct wait_queue_head cq_wait; |
| 419 | struct fasync_struct *cq_fasync; |
| 420 | struct eventfd_ctx *cq_ev_fd; |
| 421 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 422 | |
| 423 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 424 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 425 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 426 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 427 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 428 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 429 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 430 | * manipulate the list, hence no extra locking is needed there. |
| 431 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 432 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 433 | struct hlist_head *cancel_hash; |
| 434 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 435 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 436 | |
| 437 | spinlock_t inflight_lock; |
| 438 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 439 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 440 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 441 | struct delayed_work rsrc_put_work; |
| 442 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 443 | struct list_head rsrc_ref_list; |
| 444 | spinlock_t rsrc_ref_lock; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 445 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 446 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 447 | |
| 448 | /* Keep this last, we don't need it for the fast path */ |
| 449 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 452 | /* |
| 453 | * First field must be the file pointer in all the |
| 454 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 455 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 456 | struct io_poll_iocb { |
| 457 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 458 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 459 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 460 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 461 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 462 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 463 | }; |
| 464 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 465 | struct io_poll_remove { |
| 466 | struct file *file; |
| 467 | u64 addr; |
| 468 | }; |
| 469 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 470 | struct io_close { |
| 471 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 472 | int fd; |
| 473 | }; |
| 474 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 475 | struct io_timeout_data { |
| 476 | struct io_kiocb *req; |
| 477 | struct hrtimer timer; |
| 478 | struct timespec64 ts; |
| 479 | enum hrtimer_mode mode; |
| 480 | }; |
| 481 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 482 | struct io_accept { |
| 483 | struct file *file; |
| 484 | struct sockaddr __user *addr; |
| 485 | int __user *addr_len; |
| 486 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 487 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | struct io_sync { |
| 491 | struct file *file; |
| 492 | loff_t len; |
| 493 | loff_t off; |
| 494 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 495 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 496 | }; |
| 497 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 498 | struct io_cancel { |
| 499 | struct file *file; |
| 500 | u64 addr; |
| 501 | }; |
| 502 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 503 | struct io_timeout { |
| 504 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 505 | u32 off; |
| 506 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 507 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 508 | /* head of the link, used by linked timeouts only */ |
| 509 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 512 | struct io_timeout_rem { |
| 513 | struct file *file; |
| 514 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 515 | |
| 516 | /* timeout update */ |
| 517 | struct timespec64 ts; |
| 518 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 519 | }; |
| 520 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 521 | struct io_rw { |
| 522 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 523 | struct kiocb kiocb; |
| 524 | u64 addr; |
| 525 | u64 len; |
| 526 | }; |
| 527 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 528 | struct io_connect { |
| 529 | struct file *file; |
| 530 | struct sockaddr __user *addr; |
| 531 | int addr_len; |
| 532 | }; |
| 533 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 534 | struct io_sr_msg { |
| 535 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 536 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 537 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 538 | void __user *buf; |
| 539 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 540 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 541 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 542 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 543 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 544 | }; |
| 545 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 546 | struct io_open { |
| 547 | struct file *file; |
| 548 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 549 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 550 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 551 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 552 | }; |
| 553 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 554 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 555 | struct file *file; |
| 556 | u64 arg; |
| 557 | u32 nr_args; |
| 558 | u32 offset; |
| 559 | }; |
| 560 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 561 | struct io_fadvise { |
| 562 | struct file *file; |
| 563 | u64 offset; |
| 564 | u32 len; |
| 565 | u32 advice; |
| 566 | }; |
| 567 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 568 | struct io_madvise { |
| 569 | struct file *file; |
| 570 | u64 addr; |
| 571 | u32 len; |
| 572 | u32 advice; |
| 573 | }; |
| 574 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 575 | struct io_epoll { |
| 576 | struct file *file; |
| 577 | int epfd; |
| 578 | int op; |
| 579 | int fd; |
| 580 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 583 | struct io_splice { |
| 584 | struct file *file_out; |
| 585 | struct file *file_in; |
| 586 | loff_t off_out; |
| 587 | loff_t off_in; |
| 588 | u64 len; |
| 589 | unsigned int flags; |
| 590 | }; |
| 591 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 592 | struct io_provide_buf { |
| 593 | struct file *file; |
| 594 | __u64 addr; |
| 595 | __s32 len; |
| 596 | __u32 bgid; |
| 597 | __u16 nbufs; |
| 598 | __u16 bid; |
| 599 | }; |
| 600 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 601 | struct io_statx { |
| 602 | struct file *file; |
| 603 | int dfd; |
| 604 | unsigned int mask; |
| 605 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 606 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 607 | struct statx __user *buffer; |
| 608 | }; |
| 609 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 610 | struct io_shutdown { |
| 611 | struct file *file; |
| 612 | int how; |
| 613 | }; |
| 614 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 615 | struct io_rename { |
| 616 | struct file *file; |
| 617 | int old_dfd; |
| 618 | int new_dfd; |
| 619 | struct filename *oldpath; |
| 620 | struct filename *newpath; |
| 621 | int flags; |
| 622 | }; |
| 623 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 624 | struct io_unlink { |
| 625 | struct file *file; |
| 626 | int dfd; |
| 627 | int flags; |
| 628 | struct filename *filename; |
| 629 | }; |
| 630 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 631 | struct io_completion { |
| 632 | struct file *file; |
| 633 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 634 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 635 | }; |
| 636 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 637 | struct io_async_connect { |
| 638 | struct sockaddr_storage address; |
| 639 | }; |
| 640 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 641 | struct io_async_msghdr { |
| 642 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 643 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 644 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 645 | struct sockaddr __user *uaddr; |
| 646 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 647 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 648 | }; |
| 649 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 650 | struct io_async_rw { |
| 651 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 652 | const struct iovec *free_iovec; |
| 653 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 654 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 655 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 656 | }; |
| 657 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 658 | enum { |
| 659 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 660 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 661 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 662 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 663 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 664 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 665 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 666 | REQ_F_FAIL_LINK_BIT, |
| 667 | REQ_F_INFLIGHT_BIT, |
| 668 | REQ_F_CUR_POS_BIT, |
| 669 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 670 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 671 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 672 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 673 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 674 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 675 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 676 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 677 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 678 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 679 | |
| 680 | /* not a real bit, just to check we're not overflowing the space */ |
| 681 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | enum { |
| 685 | /* ctx owns file */ |
| 686 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 687 | /* drain existing IO first */ |
| 688 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 689 | /* linked sqes */ |
| 690 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 691 | /* doesn't sever on completion < 0 */ |
| 692 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 693 | /* IOSQE_ASYNC */ |
| 694 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 695 | /* IOSQE_BUFFER_SELECT */ |
| 696 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 697 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 698 | /* fail rest of links */ |
| 699 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 700 | /* on inflight list */ |
| 701 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 702 | /* read/write uses file position */ |
| 703 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 704 | /* must not punt to workers */ |
| 705 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 706 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 707 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 708 | /* regular file */ |
| 709 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 710 | /* needs cleanup */ |
| 711 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 712 | /* already went through poll handler */ |
| 713 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 714 | /* buffer already selected */ |
| 715 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 716 | /* doesn't need file table for this request */ |
| 717 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 718 | /* io_wq_work is initialized */ |
| 719 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 720 | /* linked timeout is active, i.e. prepared by link's head */ |
| 721 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 722 | /* completion is deferred through io_comp_state */ |
| 723 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 724 | }; |
| 725 | |
| 726 | struct async_poll { |
| 727 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 728 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 729 | }; |
| 730 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 731 | struct io_task_work { |
| 732 | struct io_wq_work_node node; |
| 733 | task_work_func_t func; |
| 734 | }; |
| 735 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 736 | /* |
| 737 | * NOTE! Each of the iocb union members has the file pointer |
| 738 | * as the first entry in their struct definition. So you can |
| 739 | * access the file pointer through any of the sub-structs, |
| 740 | * or directly as just 'ki_filp' in this struct. |
| 741 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 742 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 743 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 744 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 745 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 746 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 747 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 748 | struct io_accept accept; |
| 749 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 750 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 751 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 752 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 753 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 754 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 755 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 756 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 757 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 758 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 759 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 760 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 761 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 762 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 763 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 764 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 765 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 766 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 767 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 768 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 769 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 770 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 771 | /* opcode allocated if it needs to store data for async defer */ |
| 772 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 773 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 774 | /* polled IO has completed */ |
| 775 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 776 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 777 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 778 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 779 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 780 | struct io_ring_ctx *ctx; |
| 781 | unsigned int flags; |
| 782 | refcount_t refs; |
| 783 | struct task_struct *task; |
| 784 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 785 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 786 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 787 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 788 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 789 | /* |
| 790 | * 1. used with ctx->iopoll_list with reads/writes |
| 791 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 792 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 793 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 794 | union { |
| 795 | struct io_task_work io_task_work; |
| 796 | struct callback_head task_work; |
| 797 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 798 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 799 | struct hlist_node hash_node; |
| 800 | struct async_poll *apoll; |
| 801 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 804 | struct io_defer_entry { |
| 805 | struct list_head list; |
| 806 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 807 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 808 | }; |
| 809 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 810 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 811 | /* needs req->file assigned */ |
| 812 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 813 | /* hash wq insertion if file is a regular file */ |
| 814 | unsigned hash_reg_file : 1; |
| 815 | /* unbound wq insertion if file is a non-regular file */ |
| 816 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 817 | /* opcode is not supported by this kernel */ |
| 818 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 819 | /* set if opcode supports polled "wait" */ |
| 820 | unsigned pollin : 1; |
| 821 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 822 | /* op supports buffer selection */ |
| 823 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 824 | /* must always have async data allocated */ |
| 825 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 826 | /* should block plug */ |
| 827 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 828 | /* size of async data needed, if any */ |
| 829 | unsigned short async_size; |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 830 | unsigned work_flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 831 | }; |
| 832 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 833 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 834 | [IORING_OP_NOP] = {}, |
| 835 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 836 | .needs_file = 1, |
| 837 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 838 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 839 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 840 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 841 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 842 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 843 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 844 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 845 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 846 | .needs_file = 1, |
| 847 | .hash_reg_file = 1, |
| 848 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 849 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 850 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 851 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 852 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 853 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 854 | IO_WQ_WORK_FSIZE, |
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_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 857 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 858 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 859 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 860 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 861 | .needs_file = 1, |
| 862 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 863 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 864 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 865 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 866 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 867 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 868 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 869 | .needs_file = 1, |
| 870 | .hash_reg_file = 1, |
| 871 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 872 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 873 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 874 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 875 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
| 876 | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 877 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 878 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 879 | .needs_file = 1, |
| 880 | .unbound_nonreg_file = 1, |
| 881 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 882 | [IORING_OP_POLL_REMOVE] = {}, |
| 883 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 884 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 885 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 886 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 887 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | .needs_file = 1, |
| 889 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 890 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 891 | .needs_async_data = 1, |
| 892 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | 92c75f7 | 2021-02-10 12:37:58 -0700 | [diff] [blame] | 893 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 894 | IO_WQ_WORK_FS, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 896 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | .needs_file = 1, |
| 898 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 899 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 900 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 901 | .needs_async_data = 1, |
| 902 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | 92c75f7 | 2021-02-10 12:37:58 -0700 | [diff] [blame] | 903 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 904 | IO_WQ_WORK_FS, |
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_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 907 | .needs_async_data = 1, |
| 908 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 909 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 910 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 911 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 912 | /* used by timeout updates' prep() */ |
| 913 | .work_flags = IO_WQ_WORK_MM, |
| 914 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 915 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 916 | .needs_file = 1, |
| 917 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 918 | .pollin = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 919 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 920 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 921 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 922 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 923 | .needs_async_data = 1, |
| 924 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 925 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 926 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 927 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 928 | .needs_file = 1, |
| 929 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 930 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 931 | .needs_async_data = 1, |
| 932 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 933 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 934 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 935 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 936 | .needs_file = 1, |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 937 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 938 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 939 | [IORING_OP_OPENAT] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 940 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 941 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 942 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 943 | [IORING_OP_CLOSE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 944 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 945 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 946 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 947 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 948 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 949 | [IORING_OP_STATX] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 950 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
| 951 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 952 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 953 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 954 | .needs_file = 1, |
| 955 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 956 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 957 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 958 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 959 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 960 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 961 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 962 | [IORING_OP_WRITE] = { |
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 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 966 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 967 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 968 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 969 | IO_WQ_WORK_FSIZE, |
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_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 972 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 973 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 974 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 975 | [IORING_OP_MADVISE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 976 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 977 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 978 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 979 | .needs_file = 1, |
| 980 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 981 | .pollout = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 982 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 983 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 984 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 985 | .needs_file = 1, |
| 986 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 987 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 988 | .buffer_select = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 989 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 990 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 991 | [IORING_OP_OPENAT2] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 992 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 993 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 994 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 995 | [IORING_OP_EPOLL_CTL] = { |
| 996 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 997 | .work_flags = IO_WQ_WORK_FILES, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 998 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 999 | [IORING_OP_SPLICE] = { |
| 1000 | .needs_file = 1, |
| 1001 | .hash_reg_file = 1, |
| 1002 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 1003 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1004 | }, |
| 1005 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1006 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1007 | [IORING_OP_TEE] = { |
| 1008 | .needs_file = 1, |
| 1009 | .hash_reg_file = 1, |
| 1010 | .unbound_nonreg_file = 1, |
| 1011 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1012 | [IORING_OP_SHUTDOWN] = { |
| 1013 | .needs_file = 1, |
| 1014 | }, |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 1015 | [IORING_OP_RENAMEAT] = { |
| 1016 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1017 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1018 | }, |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 1019 | [IORING_OP_UNLINKAT] = { |
| 1020 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1021 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1022 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1023 | }; |
| 1024 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1025 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1026 | struct task_struct *task, |
| 1027 | struct files_struct *files); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1028 | 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] | 1029 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1030 | struct io_ring_ctx *ctx); |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 1031 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 1032 | struct fixed_rsrc_ref_node *ref_node); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1033 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 1034 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1035 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1036 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1037 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1038 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1039 | static void io_dismantle_req(struct io_kiocb *req); |
| 1040 | static void io_put_task(struct task_struct *task, int nr); |
| 1041 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1042 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1043 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1044 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1045 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1046 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1047 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1048 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1049 | static struct file *io_file_get(struct io_submit_state *state, |
| 1050 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1051 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1052 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1053 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1054 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1055 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1056 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1057 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1058 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1059 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1060 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1061 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1062 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1063 | static struct kmem_cache *req_cachep; |
| 1064 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1065 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1066 | |
| 1067 | struct sock *io_uring_get_socket(struct file *file) |
| 1068 | { |
| 1069 | #if defined(CONFIG_UNIX) |
| 1070 | if (file->f_op == &io_uring_fops) { |
| 1071 | struct io_ring_ctx *ctx = file->private_data; |
| 1072 | |
| 1073 | return ctx->ring_sock->sk; |
| 1074 | } |
| 1075 | #endif |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1079 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1080 | #define io_for_each_link(pos, head) \ |
| 1081 | for (pos = (head); pos; pos = pos->link) |
| 1082 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1083 | static inline void io_clean_op(struct io_kiocb *req) |
| 1084 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1085 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1086 | __io_clean_op(req); |
| 1087 | } |
| 1088 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1089 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1090 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1091 | struct io_ring_ctx *ctx = req->ctx; |
| 1092 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1093 | if (!req->fixed_rsrc_refs) { |
| 1094 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1095 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1099 | static bool io_match_task(struct io_kiocb *head, |
| 1100 | struct task_struct *task, |
| 1101 | struct files_struct *files) |
| 1102 | { |
| 1103 | struct io_kiocb *req; |
| 1104 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1105 | if (task && head->task != task) { |
| 1106 | /* in terms of cancelation, always match if req task is dead */ |
| 1107 | if (head->task->flags & PF_EXITING) |
| 1108 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1109 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1110 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1111 | if (!files) |
| 1112 | return true; |
| 1113 | |
| 1114 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1115 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1116 | continue; |
| 1117 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1118 | return true; |
| 1119 | if ((req->work.flags & IO_WQ_WORK_FILES) && |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1120 | req->work.identity->files == files) |
| 1121 | return true; |
| 1122 | } |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1126 | static void io_sq_thread_drop_mm_files(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1127 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1128 | struct files_struct *files = current->files; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1129 | struct mm_struct *mm = current->mm; |
| 1130 | |
| 1131 | if (mm) { |
| 1132 | kthread_unuse_mm(mm); |
| 1133 | mmput(mm); |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1134 | current->mm = NULL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1135 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1136 | if (files) { |
| 1137 | struct nsproxy *nsproxy = current->nsproxy; |
| 1138 | |
| 1139 | task_lock(current); |
| 1140 | current->files = NULL; |
| 1141 | current->nsproxy = NULL; |
| 1142 | task_unlock(current); |
| 1143 | put_files_struct(files); |
| 1144 | put_nsproxy(nsproxy); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1148 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1149 | { |
| 1150 | if (!current->files) { |
| 1151 | struct files_struct *files; |
| 1152 | struct nsproxy *nsproxy; |
| 1153 | |
| 1154 | task_lock(ctx->sqo_task); |
| 1155 | files = ctx->sqo_task->files; |
| 1156 | if (!files) { |
| 1157 | task_unlock(ctx->sqo_task); |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1158 | return -EOWNERDEAD; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1159 | } |
| 1160 | atomic_inc(&files->count); |
| 1161 | get_nsproxy(ctx->sqo_task->nsproxy); |
| 1162 | nsproxy = ctx->sqo_task->nsproxy; |
| 1163 | task_unlock(ctx->sqo_task); |
| 1164 | |
| 1165 | task_lock(current); |
| 1166 | current->files = files; |
| 1167 | current->nsproxy = nsproxy; |
| 1168 | task_unlock(current); |
| 1169 | } |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1170 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 1174 | { |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1175 | struct mm_struct *mm; |
| 1176 | |
| 1177 | if (current->mm) |
| 1178 | return 0; |
| 1179 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1180 | task_lock(ctx->sqo_task); |
| 1181 | mm = ctx->sqo_task->mm; |
| 1182 | if (unlikely(!mm || !mmget_not_zero(mm))) |
| 1183 | mm = NULL; |
| 1184 | task_unlock(ctx->sqo_task); |
| 1185 | |
| 1186 | if (mm) { |
| 1187 | kthread_use_mm(mm); |
| 1188 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1189 | } |
| 1190 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1191 | return -EFAULT; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1192 | } |
| 1193 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1194 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1195 | struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1196 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1197 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1198 | int ret; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1199 | |
| 1200 | if (def->work_flags & IO_WQ_WORK_MM) { |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1201 | ret = __io_sq_thread_acquire_mm(ctx); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1202 | if (unlikely(ret)) |
| 1203 | return ret; |
| 1204 | } |
| 1205 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1206 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
| 1207 | ret = __io_sq_thread_acquire_files(ctx); |
| 1208 | if (unlikely(ret)) |
| 1209 | return ret; |
| 1210 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1211 | |
| 1212 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1213 | } |
| 1214 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1215 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1216 | struct io_kiocb *req) |
| 1217 | { |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1218 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
| 1219 | return 0; |
| 1220 | return __io_sq_thread_acquire_mm_files(ctx, req); |
| 1221 | } |
| 1222 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1223 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| 1224 | struct cgroup_subsys_state **cur_css) |
| 1225 | |
| 1226 | { |
| 1227 | #ifdef CONFIG_BLK_CGROUP |
| 1228 | /* puts the old one when swapping */ |
| 1229 | if (*cur_css != ctx->sqo_blkcg_css) { |
| 1230 | kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| 1231 | *cur_css = ctx->sqo_blkcg_css; |
| 1232 | } |
| 1233 | #endif |
| 1234 | } |
| 1235 | |
| 1236 | static void io_sq_thread_unassociate_blkcg(void) |
| 1237 | { |
| 1238 | #ifdef CONFIG_BLK_CGROUP |
| 1239 | kthread_associate_blkcg(NULL); |
| 1240 | #endif |
| 1241 | } |
| 1242 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1243 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1244 | { |
| 1245 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1246 | req->flags |= REQ_F_FAIL_LINK; |
| 1247 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1248 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1249 | /* |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1250 | * None of these are dereferenced, they are simply used to check if any of |
| 1251 | * them have changed. If we're under current and check they are still the |
| 1252 | * same, we're fine to grab references to them for actual out-of-line use. |
| 1253 | */ |
| 1254 | static void io_init_identity(struct io_identity *id) |
| 1255 | { |
| 1256 | id->files = current->files; |
| 1257 | id->mm = current->mm; |
| 1258 | #ifdef CONFIG_BLK_CGROUP |
| 1259 | rcu_read_lock(); |
| 1260 | id->blkcg_css = blkcg_css(); |
| 1261 | rcu_read_unlock(); |
| 1262 | #endif |
| 1263 | id->creds = current_cred(); |
| 1264 | id->nsproxy = current->nsproxy; |
| 1265 | id->fs = current->fs; |
| 1266 | id->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1267 | #ifdef CONFIG_AUDIT |
| 1268 | id->loginuid = current->loginuid; |
| 1269 | id->sessionid = current->sessionid; |
| 1270 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1271 | refcount_set(&id->count, 1); |
| 1272 | } |
| 1273 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1274 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1275 | { |
| 1276 | memset(&req->work, 0, sizeof(req->work)); |
| 1277 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1278 | } |
| 1279 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1280 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1281 | * Note: must call io_req_init_async() for the first time you |
| 1282 | * touch any members of io_wq_work. |
| 1283 | */ |
| 1284 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1285 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1286 | struct io_uring_task *tctx = current->io_uring; |
| 1287 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1288 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1289 | return; |
| 1290 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1291 | __io_req_init_async(req); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1292 | |
| 1293 | /* Grab a ref if this isn't our static identity */ |
| 1294 | req->work.identity = tctx->identity; |
| 1295 | if (tctx->identity != &tctx->__identity) |
| 1296 | refcount_inc(&req->work.identity->count); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1299 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1300 | { |
| 1301 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1302 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1303 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1306 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1307 | { |
| 1308 | return !req->timeout.off; |
| 1309 | } |
| 1310 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1311 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1312 | { |
| 1313 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1314 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1315 | |
| 1316 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1317 | if (!ctx) |
| 1318 | return NULL; |
| 1319 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1320 | /* |
| 1321 | * Use 5 bits less than the max cq entries, that should give us around |
| 1322 | * 32 entries per hash list if totally full and uniformly spread. |
| 1323 | */ |
| 1324 | hash_bits = ilog2(p->cq_entries); |
| 1325 | hash_bits -= 5; |
| 1326 | if (hash_bits <= 0) |
| 1327 | hash_bits = 1; |
| 1328 | ctx->cancel_hash_bits = hash_bits; |
| 1329 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1330 | GFP_KERNEL); |
| 1331 | if (!ctx->cancel_hash) |
| 1332 | goto err; |
| 1333 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1334 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1335 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1336 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1337 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1338 | |
| 1339 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1340 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1341 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1342 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1343 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1344 | init_completion(&ctx->ref_comp); |
| 1345 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1346 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1347 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1348 | mutex_init(&ctx->uring_lock); |
| 1349 | init_waitqueue_head(&ctx->wait); |
| 1350 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1351 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1352 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1353 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1354 | spin_lock_init(&ctx->inflight_lock); |
| 1355 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1356 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1357 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1358 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1359 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1360 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1361 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1362 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1363 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1364 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1365 | kfree(ctx); |
| 1366 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1369 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1370 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1371 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1372 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1373 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1374 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1375 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1376 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1377 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1378 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1379 | } |
| 1380 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1381 | 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] | 1382 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1383 | if (req->work.identity == &tctx->__identity) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1384 | return; |
| 1385 | if (refcount_dec_and_test(&req->work.identity->count)) |
| 1386 | kfree(req->work.identity); |
| 1387 | } |
| 1388 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1389 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1390 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1391 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1392 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1393 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1394 | if (req->work.flags & IO_WQ_WORK_MM) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1395 | mmdrop(req->work.identity->mm); |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1396 | #ifdef CONFIG_BLK_CGROUP |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1397 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1398 | css_put(req->work.identity->blkcg_css); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1399 | #endif |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1400 | if (req->work.flags & IO_WQ_WORK_CREDS) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1401 | put_cred(req->work.identity->creds); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1402 | if (req->work.flags & IO_WQ_WORK_FS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1403 | struct fs_struct *fs = req->work.identity->fs; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1404 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1405 | spin_lock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1406 | if (--fs->users) |
| 1407 | fs = NULL; |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1408 | spin_unlock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1409 | if (fs) |
| 1410 | free_fs_struct(fs); |
| 1411 | } |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1412 | if (req->work.flags & IO_WQ_WORK_FILES) { |
| 1413 | put_files_struct(req->work.identity->files); |
| 1414 | put_nsproxy(req->work.identity->nsproxy); |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1415 | } |
| 1416 | if (req->flags & REQ_F_INFLIGHT) { |
| 1417 | struct io_ring_ctx *ctx = req->ctx; |
| 1418 | struct io_uring_task *tctx = req->task->io_uring; |
| 1419 | unsigned long flags; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1420 | |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1421 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1422 | list_del(&req->inflight_entry); |
| 1423 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1424 | req->flags &= ~REQ_F_INFLIGHT; |
| 1425 | if (atomic_read(&tctx->in_idle)) |
| 1426 | wake_up(&tctx->wait); |
| 1427 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1428 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1429 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
| 1430 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | |
| 1431 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1432 | io_put_identity(req->task->io_uring, req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Create a private copy of io_identity, since some fields don't match |
| 1437 | * the current context. |
| 1438 | */ |
| 1439 | static bool io_identity_cow(struct io_kiocb *req) |
| 1440 | { |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1441 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1442 | const struct cred *creds = NULL; |
| 1443 | struct io_identity *id; |
| 1444 | |
| 1445 | if (req->work.flags & IO_WQ_WORK_CREDS) |
| 1446 | creds = req->work.identity->creds; |
| 1447 | |
| 1448 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| 1449 | if (unlikely(!id)) { |
| 1450 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1451 | return false; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * We can safely just re-init the creds we copied Either the field |
| 1456 | * matches the current one, or we haven't grabbed it yet. The only |
| 1457 | * exception is ->creds, through registered personalities, so handle |
| 1458 | * that one separately. |
| 1459 | */ |
| 1460 | io_init_identity(id); |
| 1461 | if (creds) |
Pavel Begunkov | e8c954d | 2020-12-06 22:22:46 +0000 | [diff] [blame] | 1462 | id->creds = creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1463 | |
| 1464 | /* add one for this request */ |
| 1465 | refcount_inc(&id->count); |
| 1466 | |
Jens Axboe | cb8a8ae | 2020-11-03 12:19:07 -0700 | [diff] [blame] | 1467 | /* drop tctx and req identity references, if needed */ |
| 1468 | if (tctx->identity != &tctx->__identity && |
| 1469 | refcount_dec_and_test(&tctx->identity->count)) |
| 1470 | kfree(tctx->identity); |
| 1471 | if (req->work.identity != &tctx->__identity && |
| 1472 | refcount_dec_and_test(&req->work.identity->count)) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1473 | kfree(req->work.identity); |
| 1474 | |
| 1475 | req->work.identity = id; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1476 | tctx->identity = id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1477 | return true; |
| 1478 | } |
| 1479 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1480 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1481 | { |
| 1482 | struct io_ring_ctx *ctx = req->ctx; |
| 1483 | |
| 1484 | if (!(req->flags & REQ_F_INFLIGHT)) { |
| 1485 | io_req_init_async(req); |
| 1486 | req->flags |= REQ_F_INFLIGHT; |
| 1487 | |
| 1488 | spin_lock_irq(&ctx->inflight_lock); |
| 1489 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1490 | spin_unlock_irq(&ctx->inflight_lock); |
| 1491 | } |
| 1492 | } |
| 1493 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1494 | static bool io_grab_identity(struct io_kiocb *req) |
| 1495 | { |
| 1496 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1497 | struct io_identity *id = req->work.identity; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1498 | |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 1499 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| 1500 | if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| 1501 | return false; |
| 1502 | req->work.flags |= IO_WQ_WORK_FSIZE; |
| 1503 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1504 | #ifdef CONFIG_BLK_CGROUP |
| 1505 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| 1506 | (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| 1507 | rcu_read_lock(); |
| 1508 | if (id->blkcg_css != blkcg_css()) { |
| 1509 | rcu_read_unlock(); |
| 1510 | return false; |
| 1511 | } |
| 1512 | /* |
| 1513 | * This should be rare, either the cgroup is dying or the task |
| 1514 | * is moving cgroups. Just punt to root for the handful of ios. |
| 1515 | */ |
| 1516 | if (css_tryget_online(id->blkcg_css)) |
| 1517 | req->work.flags |= IO_WQ_WORK_BLKCG; |
| 1518 | rcu_read_unlock(); |
| 1519 | } |
| 1520 | #endif |
| 1521 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| 1522 | if (id->creds != current_cred()) |
| 1523 | return false; |
| 1524 | get_cred(id->creds); |
| 1525 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 1526 | } |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1527 | #ifdef CONFIG_AUDIT |
| 1528 | if (!uid_eq(current->loginuid, id->loginuid) || |
| 1529 | current->sessionid != id->sessionid) |
| 1530 | return false; |
| 1531 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1532 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
| 1533 | (def->work_flags & IO_WQ_WORK_FS)) { |
| 1534 | if (current->fs != id->fs) |
| 1535 | return false; |
| 1536 | spin_lock(&id->fs->lock); |
| 1537 | if (!id->fs->in_exec) { |
| 1538 | id->fs->users++; |
| 1539 | req->work.flags |= IO_WQ_WORK_FS; |
| 1540 | } else { |
| 1541 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1542 | } |
| 1543 | spin_unlock(¤t->fs->lock); |
| 1544 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1545 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| 1546 | (def->work_flags & IO_WQ_WORK_FILES) && |
| 1547 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| 1548 | if (id->files != current->files || |
| 1549 | id->nsproxy != current->nsproxy) |
| 1550 | return false; |
| 1551 | atomic_inc(&id->files->count); |
| 1552 | get_nsproxy(id->nsproxy); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1553 | req->work.flags |= IO_WQ_WORK_FILES; |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1554 | io_req_track_inflight(req); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1555 | } |
Jens Axboe | 7778877 | 2020-12-29 10:50:46 -0700 | [diff] [blame] | 1556 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
| 1557 | (def->work_flags & IO_WQ_WORK_MM)) { |
| 1558 | if (id->mm != current->mm) |
| 1559 | return false; |
| 1560 | mmgrab(id->mm); |
| 1561 | req->work.flags |= IO_WQ_WORK_MM; |
| 1562 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1563 | |
| 1564 | return true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1565 | } |
| 1566 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1567 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1568 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1569 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1570 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1571 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1572 | io_req_init_async(req); |
| 1573 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1574 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1575 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1576 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1577 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1578 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1579 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1580 | } else { |
| 1581 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1582 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1583 | } |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1584 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1585 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| 1586 | if (io_grab_identity(req)) |
| 1587 | return; |
| 1588 | |
| 1589 | if (!io_identity_cow(req)) |
| 1590 | return; |
| 1591 | |
| 1592 | /* can't fail at this point */ |
| 1593 | if (!io_grab_identity(req)) |
| 1594 | WARN_ON(1); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1595 | } |
| 1596 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1597 | static void io_prep_async_link(struct io_kiocb *req) |
| 1598 | { |
| 1599 | struct io_kiocb *cur; |
| 1600 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1601 | io_for_each_link(cur, req) |
| 1602 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1603 | } |
| 1604 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1605 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1606 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1607 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1608 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1609 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1610 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1611 | &req->work, req->flags); |
| 1612 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1613 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1614 | } |
| 1615 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1616 | static void io_queue_async_work(struct io_kiocb *req) |
| 1617 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1618 | struct io_kiocb *link; |
| 1619 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1620 | /* init ->work of the whole link before punting */ |
| 1621 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1622 | link = __io_queue_async_work(req); |
| 1623 | |
| 1624 | if (link) |
| 1625 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1626 | } |
| 1627 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1628 | static void io_kill_timeout(struct io_kiocb *req) |
| 1629 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1630 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1631 | int ret; |
| 1632 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1633 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1634 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1635 | atomic_set(&req->ctx->cq_timeouts, |
| 1636 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1637 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1638 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1639 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1643 | /* |
| 1644 | * Returns true if we found and killed one or more timeouts |
| 1645 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1646 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1647 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1648 | { |
| 1649 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1650 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1651 | |
| 1652 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1653 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1654 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1655 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1656 | canceled++; |
| 1657 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1658 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1659 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1660 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1661 | } |
| 1662 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1663 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1664 | { |
| 1665 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1666 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1667 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1668 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1669 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1670 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1671 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1672 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1673 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1674 | } while (!list_empty(&ctx->defer_list)); |
| 1675 | } |
| 1676 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1677 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1678 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1679 | u32 seq; |
| 1680 | |
| 1681 | if (list_empty(&ctx->timeout_list)) |
| 1682 | return; |
| 1683 | |
| 1684 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1685 | |
| 1686 | do { |
| 1687 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1688 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1689 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1690 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1691 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1692 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * Since seq can easily wrap around over time, subtract |
| 1696 | * the last seq at which timeouts were flushed before comparing. |
| 1697 | * Assuming not more than 2^31-1 events have happened since, |
| 1698 | * these subtractions won't have wrapped, so we can check if |
| 1699 | * target is in [last_seq, current_seq] by comparing the two. |
| 1700 | */ |
| 1701 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1702 | events_got = seq - ctx->cq_last_tm_flush; |
| 1703 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1704 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1705 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1706 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1707 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1708 | } while (!list_empty(&ctx->timeout_list)); |
| 1709 | |
| 1710 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1711 | } |
| 1712 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1713 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1714 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1715 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1716 | |
| 1717 | /* order cqe stores with ring update */ |
| 1718 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1719 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1720 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1721 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1722 | } |
| 1723 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1724 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1725 | { |
| 1726 | struct io_rings *r = ctx->rings; |
| 1727 | |
| 1728 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1729 | } |
| 1730 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1731 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1732 | { |
| 1733 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1734 | } |
| 1735 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1736 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1737 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1738 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1739 | unsigned tail; |
| 1740 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1741 | /* |
| 1742 | * writes to the cq entry need to come after reading head; the |
| 1743 | * control dependency is enough as we're using WRITE_ONCE to |
| 1744 | * fill the cq entry |
| 1745 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1746 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1747 | return NULL; |
| 1748 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1749 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1750 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1753 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1754 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1755 | if (!ctx->cq_ev_fd) |
| 1756 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1757 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1758 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1759 | if (!ctx->eventfd_async) |
| 1760 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1761 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1764 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1765 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1766 | /* see waitqueue_active() comment */ |
| 1767 | smp_mb(); |
| 1768 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1769 | if (waitqueue_active(&ctx->wait)) |
| 1770 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1771 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1772 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1773 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1774 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1775 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1776 | wake_up_interruptible(&ctx->cq_wait); |
| 1777 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1778 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1779 | } |
| 1780 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1781 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1782 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1783 | /* see waitqueue_active() comment */ |
| 1784 | smp_mb(); |
| 1785 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1786 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1787 | if (waitqueue_active(&ctx->wait)) |
| 1788 | wake_up(&ctx->wait); |
| 1789 | } |
| 1790 | if (io_should_trigger_evfd(ctx)) |
| 1791 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1792 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1793 | wake_up_interruptible(&ctx->cq_wait); |
| 1794 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1795 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1798 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1799 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1800 | struct task_struct *tsk, |
| 1801 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1802 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1803 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1804 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1805 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1806 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1807 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1808 | LIST_HEAD(list); |
| 1809 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1810 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1811 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1812 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1813 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1814 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1815 | 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] | 1816 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1817 | continue; |
| 1818 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1819 | cqe = io_get_cqring(ctx); |
| 1820 | if (!cqe && !force) |
| 1821 | break; |
| 1822 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1823 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1824 | if (cqe) { |
| 1825 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1826 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1827 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1828 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1829 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1830 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1831 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1832 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1833 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1836 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1837 | if (all_flushed) { |
| 1838 | clear_bit(0, &ctx->sq_check_overflow); |
| 1839 | clear_bit(0, &ctx->cq_check_overflow); |
| 1840 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1841 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1842 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1843 | if (posted) |
| 1844 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1845 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1846 | if (posted) |
| 1847 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1848 | |
| 1849 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1850 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1851 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1852 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1853 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1854 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1855 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1858 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1859 | struct task_struct *tsk, |
| 1860 | struct files_struct *files) |
| 1861 | { |
| 1862 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1863 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1864 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1865 | mutex_lock(&ctx->uring_lock); |
| 1866 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1867 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1868 | mutex_unlock(&ctx->uring_lock); |
| 1869 | } |
| 1870 | } |
| 1871 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1872 | 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] | 1873 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1874 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1875 | struct io_uring_cqe *cqe; |
| 1876 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1877 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1878 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1879 | /* |
| 1880 | * If we can't get a cq entry, userspace overflowed the |
| 1881 | * submission (by quite a lot). Increment the overflow count in |
| 1882 | * the ring. |
| 1883 | */ |
| 1884 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1885 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1886 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1887 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1888 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1889 | } else if (ctx->cq_overflow_flushed || |
| 1890 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1891 | /* |
| 1892 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1893 | * then we cannot store the request for later flushing, we need |
| 1894 | * to drop it on the floor. |
| 1895 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1896 | ctx->cached_cq_overflow++; |
| 1897 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1898 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1899 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1900 | set_bit(0, &ctx->sq_check_overflow); |
| 1901 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1902 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1903 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1904 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1905 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1906 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1907 | refcount_inc(&req->refs); |
| 1908 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1912 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1913 | { |
| 1914 | __io_cqring_fill_event(req, res, 0); |
| 1915 | } |
| 1916 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1917 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1918 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1919 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1920 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1921 | unsigned long flags; |
| 1922 | |
| 1923 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1924 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1925 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1926 | /* |
| 1927 | * If we're the last reference to this request, add to our locked |
| 1928 | * free_list cache. |
| 1929 | */ |
| 1930 | if (refcount_dec_and_test(&req->refs)) { |
| 1931 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1932 | |
| 1933 | io_dismantle_req(req); |
| 1934 | io_put_task(req->task, 1); |
| 1935 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1936 | cs->locked_free_nr++; |
| 1937 | } else |
| 1938 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1939 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1940 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1941 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1942 | if (req) { |
| 1943 | io_queue_next(req); |
| 1944 | percpu_ref_put(&ctx->refs); |
| 1945 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1948 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1949 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1950 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1951 | io_clean_op(req); |
| 1952 | req->result = res; |
| 1953 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1954 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1955 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1956 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1957 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1958 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1959 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1960 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1961 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1962 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1963 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1964 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1965 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1966 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1967 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1968 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1971 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1972 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1973 | struct io_submit_state *state = &ctx->submit_state; |
| 1974 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1975 | struct io_kiocb *req = NULL; |
| 1976 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1977 | /* |
| 1978 | * If we have more than a batch's worth of requests in our IRQ side |
| 1979 | * locked cache, grab the lock and move them over to our submission |
| 1980 | * side cache. |
| 1981 | */ |
| 1982 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 1983 | spin_lock_irq(&ctx->completion_lock); |
| 1984 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1985 | cs->locked_free_nr = 0; |
| 1986 | spin_unlock_irq(&ctx->completion_lock); |
| 1987 | } |
| 1988 | |
| 1989 | while (!list_empty(&cs->free_list)) { |
| 1990 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1991 | compl.list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1992 | list_del(&req->compl.list); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1993 | state->reqs[state->free_reqs++] = req; |
| 1994 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 1995 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1996 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1997 | |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1998 | return req != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2001 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2002 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2003 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2004 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2005 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2006 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 2007 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2008 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 2009 | int ret; |
| 2010 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2011 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2012 | goto got_req; |
| 2013 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2014 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 2015 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2016 | |
| 2017 | /* |
| 2018 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2019 | * retry single alloc to be on the safe side. |
| 2020 | */ |
| 2021 | if (unlikely(ret <= 0)) { |
| 2022 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2023 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2024 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2025 | ret = 1; |
| 2026 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2027 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2028 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2029 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2030 | state->free_reqs--; |
| 2031 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2034 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 2035 | bool fixed) |
| 2036 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2037 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2038 | fput(file); |
| 2039 | } |
| 2040 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2041 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2042 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 2043 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2044 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2045 | if (req->async_data) |
| 2046 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2047 | if (req->file) |
| 2048 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 2049 | if (req->fixed_rsrc_refs) |
| 2050 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2051 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2052 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2053 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2054 | static inline void io_put_task(struct task_struct *task, int nr) |
| 2055 | { |
| 2056 | struct io_uring_task *tctx = task->io_uring; |
| 2057 | |
| 2058 | percpu_counter_sub(&tctx->inflight, nr); |
| 2059 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 2060 | wake_up(&tctx->wait); |
| 2061 | put_task_struct_many(task, nr); |
| 2062 | } |
| 2063 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2064 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2065 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2066 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2067 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2068 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2069 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2070 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2071 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2072 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2073 | } |
| 2074 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2075 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2076 | { |
| 2077 | struct io_kiocb *nxt = req->link; |
| 2078 | |
| 2079 | req->link = nxt->link; |
| 2080 | nxt->link = NULL; |
| 2081 | } |
| 2082 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2083 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2084 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2085 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2086 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2087 | bool cancelled = false; |
| 2088 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2089 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2090 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2091 | link = req->link; |
| 2092 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 2093 | /* |
| 2094 | * Can happen if a linked timeout fired and link had been like |
| 2095 | * req -> link t-out -> link t-out [-> ...] |
| 2096 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2097 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 2098 | struct io_timeout_data *io = link->async_data; |
| 2099 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2100 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2101 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2102 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2103 | ret = hrtimer_try_to_cancel(&io->timer); |
| 2104 | if (ret != -1) { |
| 2105 | io_cqring_fill_event(link, -ECANCELED); |
| 2106 | io_commit_cqring(ctx); |
| 2107 | cancelled = true; |
| 2108 | } |
| 2109 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2110 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2111 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 2112 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2113 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2114 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2115 | io_put_req(link); |
| 2116 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2117 | } |
| 2118 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2119 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2120 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2121 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2122 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2123 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2124 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2125 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2126 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2127 | link = req->link; |
| 2128 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2129 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2130 | while (link) { |
| 2131 | nxt = link->link; |
| 2132 | link->link = NULL; |
| 2133 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2134 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2135 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2136 | |
| 2137 | /* |
| 2138 | * It's ok to free under spinlock as they're not linked anymore, |
| 2139 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 2140 | * work.fs->lock. |
| 2141 | */ |
| 2142 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 2143 | io_put_req_deferred(link, 2); |
| 2144 | else |
| 2145 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2146 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2147 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2148 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2149 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2150 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2151 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2152 | } |
| 2153 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2154 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2155 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2156 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 2157 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2158 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2159 | /* |
| 2160 | * If LINK is set, we have dependent requests in this chain. If we |
| 2161 | * didn't fail this request, queue the first one up, moving any other |
| 2162 | * dependencies to the next request. In case of failure, fail the rest |
| 2163 | * of the chain. |
| 2164 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2165 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 2166 | struct io_kiocb *nxt = req->link; |
| 2167 | |
| 2168 | req->link = NULL; |
| 2169 | return nxt; |
| 2170 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2171 | io_fail_links(req); |
| 2172 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2173 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2174 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2175 | 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] | 2176 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 2177 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2178 | return NULL; |
| 2179 | return __io_req_find_next(req); |
| 2180 | } |
| 2181 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2182 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 2183 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2184 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2185 | struct io_wq_work_list list; |
| 2186 | struct io_wq_work_node *node; |
| 2187 | |
| 2188 | if (wq_list_empty(&tctx->task_list)) |
| 2189 | return false; |
| 2190 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2191 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2192 | list = tctx->task_list; |
| 2193 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2194 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2195 | |
| 2196 | node = list.first; |
| 2197 | while (node) { |
| 2198 | struct io_wq_work_node *next = node->next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2199 | struct io_ring_ctx *this_ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2200 | struct io_kiocb *req; |
| 2201 | |
| 2202 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2203 | this_ctx = req->ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2204 | req->task_work.func(&req->task_work); |
| 2205 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2206 | |
| 2207 | if (!ctx) { |
| 2208 | ctx = this_ctx; |
| 2209 | } else if (ctx != this_ctx) { |
| 2210 | mutex_lock(&ctx->uring_lock); |
| 2211 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2212 | mutex_unlock(&ctx->uring_lock); |
| 2213 | ctx = this_ctx; |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | if (ctx && ctx->submit_state.comp.nr) { |
| 2218 | mutex_lock(&ctx->uring_lock); |
| 2219 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2220 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | return list.first != NULL; |
| 2224 | } |
| 2225 | |
| 2226 | static void tctx_task_work(struct callback_head *cb) |
| 2227 | { |
| 2228 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 2229 | |
| 2230 | while (__tctx_task_work(tctx)) |
| 2231 | cond_resched(); |
| 2232 | |
| 2233 | clear_bit(0, &tctx->task_state); |
| 2234 | } |
| 2235 | |
| 2236 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 2237 | enum task_work_notify_mode notify) |
| 2238 | { |
| 2239 | struct io_uring_task *tctx = tsk->io_uring; |
| 2240 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2241 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2242 | int ret; |
| 2243 | |
| 2244 | WARN_ON_ONCE(!tctx); |
| 2245 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2246 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2247 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2248 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* task_work already pending, we're done */ |
| 2251 | if (test_bit(0, &tctx->task_state) || |
| 2252 | test_and_set_bit(0, &tctx->task_state)) |
| 2253 | return 0; |
| 2254 | |
| 2255 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 2256 | return 0; |
| 2257 | |
| 2258 | /* |
| 2259 | * Slow path - we failed, find and delete work. if the work is not |
| 2260 | * in the list, it got run and we're fine. |
| 2261 | */ |
| 2262 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2263 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2264 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 2265 | if (&req->io_task_work.node == node) { |
| 2266 | wq_list_del(&tctx->task_list, node, prev); |
| 2267 | ret = 1; |
| 2268 | break; |
| 2269 | } |
| 2270 | } |
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 | clear_bit(0, &tctx->task_state); |
| 2273 | return ret; |
| 2274 | } |
| 2275 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2276 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2277 | { |
| 2278 | struct task_struct *tsk = req->task; |
| 2279 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2280 | enum task_work_notify_mode notify; |
| 2281 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2282 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2283 | if (tsk->flags & PF_EXITING) |
| 2284 | return -ESRCH; |
| 2285 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2286 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2287 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2288 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2289 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2290 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2291 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2292 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2293 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2294 | notify = TWA_SIGNAL; |
| 2295 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2296 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2297 | if (!ret) |
| 2298 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2299 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2300 | return ret; |
| 2301 | } |
| 2302 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2303 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2304 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2305 | { |
| 2306 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); |
| 2307 | |
| 2308 | init_task_work(&req->task_work, cb); |
| 2309 | task_work_add(tsk, &req->task_work, TWA_NONE); |
| 2310 | wake_up_process(tsk); |
| 2311 | } |
| 2312 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2313 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2314 | { |
| 2315 | struct io_ring_ctx *ctx = req->ctx; |
| 2316 | |
| 2317 | spin_lock_irq(&ctx->completion_lock); |
| 2318 | io_cqring_fill_event(req, error); |
| 2319 | io_commit_cqring(ctx); |
| 2320 | spin_unlock_irq(&ctx->completion_lock); |
| 2321 | |
| 2322 | io_cqring_ev_posted(ctx); |
| 2323 | req_set_fail_links(req); |
| 2324 | io_double_put_req(req); |
| 2325 | } |
| 2326 | |
| 2327 | static void io_req_task_cancel(struct callback_head *cb) |
| 2328 | { |
| 2329 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2330 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2331 | |
| 2332 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2333 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2337 | { |
| 2338 | struct io_ring_ctx *ctx = req->ctx; |
| 2339 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2340 | /* 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] | 2341 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | dc0eced | 2021-02-12 18:41:15 +0000 | [diff] [blame] | 2342 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
| 2343 | !io_sq_thread_acquire_mm_files(ctx, req)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2344 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2345 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2346 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2347 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | aec18a5 | 2021-02-04 19:22:46 +0000 | [diff] [blame] | 2348 | |
| 2349 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 2350 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2351 | } |
| 2352 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2353 | static void io_req_task_submit(struct callback_head *cb) |
| 2354 | { |
| 2355 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2356 | |
| 2357 | __io_req_task_submit(req); |
| 2358 | } |
| 2359 | |
| 2360 | static void io_req_task_queue(struct io_kiocb *req) |
| 2361 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2362 | int ret; |
| 2363 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2364 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2365 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2366 | if (unlikely(ret)) { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2367 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2368 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2369 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2370 | } |
| 2371 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2372 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2373 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2374 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2375 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2376 | if (nxt) |
| 2377 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2378 | } |
| 2379 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2380 | static void io_free_req(struct io_kiocb *req) |
| 2381 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2382 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2383 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2384 | } |
| 2385 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2386 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2387 | struct task_struct *task; |
| 2388 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2389 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2390 | }; |
| 2391 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2392 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2393 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2394 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2395 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2396 | rb->task = NULL; |
| 2397 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2398 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2399 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2400 | struct req_batch *rb) |
| 2401 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2402 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2403 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2404 | if (rb->ctx_refs) |
| 2405 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2406 | } |
| 2407 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2408 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2409 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2410 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2411 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2412 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2413 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2414 | if (rb->task) |
| 2415 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2416 | rb->task = req->task; |
| 2417 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2418 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2419 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2420 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2421 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2422 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2423 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2424 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2425 | else |
| 2426 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2427 | } |
| 2428 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2429 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2430 | struct io_ring_ctx *ctx) |
| 2431 | { |
| 2432 | int i, nr = cs->nr; |
| 2433 | struct io_kiocb *req; |
| 2434 | struct req_batch rb; |
| 2435 | |
| 2436 | io_init_req_batch(&rb); |
| 2437 | spin_lock_irq(&ctx->completion_lock); |
| 2438 | for (i = 0; i < nr; i++) { |
| 2439 | req = cs->reqs[i]; |
| 2440 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2441 | } |
| 2442 | io_commit_cqring(ctx); |
| 2443 | spin_unlock_irq(&ctx->completion_lock); |
| 2444 | |
| 2445 | io_cqring_ev_posted(ctx); |
| 2446 | for (i = 0; i < nr; i++) { |
| 2447 | req = cs->reqs[i]; |
| 2448 | |
| 2449 | /* submission and completion refs */ |
| 2450 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2451 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | io_req_free_batch_finish(ctx, &rb); |
| 2455 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2456 | } |
| 2457 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2458 | /* |
| 2459 | * Drop reference to request, return next in chain (if there is one) if this |
| 2460 | * was the last reference to this request. |
| 2461 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2462 | 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] | 2463 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2464 | struct io_kiocb *nxt = NULL; |
| 2465 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2466 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2467 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2468 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2469 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2470 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2473 | static void io_put_req(struct io_kiocb *req) |
| 2474 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2475 | if (refcount_dec_and_test(&req->refs)) |
| 2476 | io_free_req(req); |
| 2477 | } |
| 2478 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2479 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2480 | { |
| 2481 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2482 | |
| 2483 | io_free_req(req); |
| 2484 | } |
| 2485 | |
| 2486 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2487 | { |
| 2488 | int ret; |
| 2489 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2490 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2491 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2492 | if (unlikely(ret)) |
| 2493 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2497 | { |
| 2498 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2499 | io_free_req_deferred(req); |
| 2500 | } |
| 2501 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2502 | static void io_double_put_req(struct io_kiocb *req) |
| 2503 | { |
| 2504 | /* drop both submit and complete references */ |
| 2505 | if (refcount_sub_and_test(2, &req->refs)) |
| 2506 | io_free_req(req); |
| 2507 | } |
| 2508 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2509 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2510 | { |
| 2511 | /* See comment at the top of this file */ |
| 2512 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2513 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2514 | } |
| 2515 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2516 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2517 | { |
| 2518 | struct io_rings *rings = ctx->rings; |
| 2519 | |
| 2520 | /* make sure SQ entry isn't read before tail */ |
| 2521 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2522 | } |
| 2523 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2524 | 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] | 2525 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2526 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2527 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2528 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2529 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2530 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2531 | kfree(kbuf); |
| 2532 | return cflags; |
| 2533 | } |
| 2534 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2535 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2536 | { |
| 2537 | struct io_buffer *kbuf; |
| 2538 | |
| 2539 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2540 | return io_put_kbuf(req, kbuf); |
| 2541 | } |
| 2542 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2543 | static inline bool io_run_task_work(void) |
| 2544 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2545 | /* |
| 2546 | * Not safe to run on exiting task, and the task_work handling will |
| 2547 | * not add work to such a task. |
| 2548 | */ |
| 2549 | if (unlikely(current->flags & PF_EXITING)) |
| 2550 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2551 | if (current->task_works) { |
| 2552 | __set_current_state(TASK_RUNNING); |
| 2553 | task_work_run(); |
| 2554 | return true; |
| 2555 | } |
| 2556 | |
| 2557 | return false; |
| 2558 | } |
| 2559 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2560 | /* |
| 2561 | * Find and free completed poll iocbs |
| 2562 | */ |
| 2563 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2564 | struct list_head *done) |
| 2565 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2566 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2567 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2568 | |
| 2569 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2570 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2571 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2572 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2573 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2574 | int cflags = 0; |
| 2575 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2576 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2577 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2578 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2579 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2580 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2581 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2582 | continue; |
| 2583 | } |
| 2584 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2585 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2586 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2587 | |
| 2588 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2589 | (*nr_events)++; |
| 2590 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2591 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2592 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2593 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2594 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2595 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2596 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2597 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2600 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2601 | long min) |
| 2602 | { |
| 2603 | struct io_kiocb *req, *tmp; |
| 2604 | LIST_HEAD(done); |
| 2605 | bool spin; |
| 2606 | int ret; |
| 2607 | |
| 2608 | /* |
| 2609 | * Only spin for completions if we don't have multiple devices hanging |
| 2610 | * off our complete list, and we're under the requested amount. |
| 2611 | */ |
| 2612 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2613 | |
| 2614 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2615 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2616 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2617 | |
| 2618 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2619 | * Move completed and retryable entries to our local lists. |
| 2620 | * If we find a request that requires polling, break out |
| 2621 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2622 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2623 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2624 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2625 | continue; |
| 2626 | } |
| 2627 | if (!list_empty(&done)) |
| 2628 | break; |
| 2629 | |
| 2630 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2631 | if (ret < 0) |
| 2632 | break; |
| 2633 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2634 | /* iopoll may have completed current req */ |
| 2635 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2636 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2637 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2638 | if (ret && spin) |
| 2639 | spin = false; |
| 2640 | ret = 0; |
| 2641 | } |
| 2642 | |
| 2643 | if (!list_empty(&done)) |
| 2644 | io_iopoll_complete(ctx, nr_events, &done); |
| 2645 | |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
| 2649 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2650 | * 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] | 2651 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2652 | * as a non-spinning completion check. |
| 2653 | */ |
| 2654 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2655 | long min) |
| 2656 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2657 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2658 | int ret; |
| 2659 | |
| 2660 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2661 | if (ret < 0) |
| 2662 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2663 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | return 1; |
| 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * We can't just wait for polled events to come to us, we have to actively |
| 2672 | * find and complete them. |
| 2673 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2674 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2675 | { |
| 2676 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2677 | return; |
| 2678 | |
| 2679 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2680 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2681 | unsigned int nr_events = 0; |
| 2682 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2683 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2684 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2685 | /* let it sleep and repeat later if can't complete a request */ |
| 2686 | if (nr_events == 0) |
| 2687 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2688 | /* |
| 2689 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2690 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2691 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2692 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2693 | if (need_resched()) { |
| 2694 | mutex_unlock(&ctx->uring_lock); |
| 2695 | cond_resched(); |
| 2696 | mutex_lock(&ctx->uring_lock); |
| 2697 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2698 | } |
| 2699 | mutex_unlock(&ctx->uring_lock); |
| 2700 | } |
| 2701 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2702 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2703 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2704 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2705 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2706 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2707 | /* |
| 2708 | * We disallow the app entering submit/complete with polling, but we |
| 2709 | * still need to lock the ring to prevent racing with polled issue |
| 2710 | * that got punted to a workqueue. |
| 2711 | */ |
| 2712 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2713 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2714 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2715 | * Don't enter poll loop if we already have events pending. |
| 2716 | * If we do, we can potentially be spinning for commands that |
| 2717 | * already triggered a CQE (eg in error). |
| 2718 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2719 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2720 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2721 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2722 | break; |
| 2723 | |
| 2724 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2725 | * If a submit got punted to a workqueue, we can have the |
| 2726 | * application entering polling for a command before it gets |
| 2727 | * issued. That app will hold the uring_lock for the duration |
| 2728 | * of the poll right here, so we need to take a breather every |
| 2729 | * now and then to ensure that the issue has a chance to add |
| 2730 | * the poll to the issued list. Otherwise we can spin here |
| 2731 | * forever, while the workqueue is stuck trying to acquire the |
| 2732 | * very same mutex. |
| 2733 | */ |
| 2734 | if (!(++iters & 7)) { |
| 2735 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2736 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2737 | mutex_lock(&ctx->uring_lock); |
| 2738 | } |
| 2739 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2740 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2741 | if (ret <= 0) |
| 2742 | break; |
| 2743 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2744 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2745 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2746 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2747 | return ret; |
| 2748 | } |
| 2749 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2750 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2751 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2752 | /* |
| 2753 | * Tell lockdep we inherited freeze protection from submission |
| 2754 | * thread. |
| 2755 | */ |
| 2756 | if (req->flags & REQ_F_ISREG) { |
| 2757 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2758 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2759 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2760 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2761 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2764 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2765 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2766 | { |
| 2767 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2768 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2769 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2770 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2771 | /* already prepared */ |
| 2772 | if (req->async_data) |
| 2773 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2774 | |
| 2775 | switch (req->opcode) { |
| 2776 | case IORING_OP_READV: |
| 2777 | case IORING_OP_READ_FIXED: |
| 2778 | case IORING_OP_READ: |
| 2779 | rw = READ; |
| 2780 | break; |
| 2781 | case IORING_OP_WRITEV: |
| 2782 | case IORING_OP_WRITE_FIXED: |
| 2783 | case IORING_OP_WRITE: |
| 2784 | rw = WRITE; |
| 2785 | break; |
| 2786 | default: |
| 2787 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2788 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2789 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2790 | } |
| 2791 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2792 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2793 | if (ret < 0) |
| 2794 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2795 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2796 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2797 | #endif |
| 2798 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2799 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2800 | { |
| 2801 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2802 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2803 | int ret; |
| 2804 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2805 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2806 | return false; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2807 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2808 | return false; |
| 2809 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2810 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2811 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2812 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2813 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2814 | if (!ret && io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2815 | refcount_inc(&req->refs); |
| 2816 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2817 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2818 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2819 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2820 | #endif |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2824 | 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] | 2825 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2826 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2827 | int cflags = 0; |
| 2828 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2829 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2830 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2831 | if (res != req->result) |
| 2832 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2833 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2834 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2835 | kiocb_end_write(req); |
| 2836 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2837 | cflags = io_put_rw_kbuf(req); |
| 2838 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2842 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2843 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2844 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2845 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2848 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2849 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2850 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2851 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2852 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2853 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2854 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2855 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2856 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2857 | |
| 2858 | WRITE_ONCE(req->result, res); |
| 2859 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2860 | smp_wmb(); |
| 2861 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | /* |
| 2865 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2866 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2867 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2868 | * accessing the kiocb cookie. |
| 2869 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2870 | 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] | 2871 | { |
| 2872 | struct io_ring_ctx *ctx = req->ctx; |
| 2873 | |
| 2874 | /* |
| 2875 | * Track whether we have multiple files in our lists. This will impact |
| 2876 | * how we do polling eventually, not spinning if we're on potentially |
| 2877 | * different devices. |
| 2878 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2879 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2880 | ctx->poll_multi_file = false; |
| 2881 | } else if (!ctx->poll_multi_file) { |
| 2882 | struct io_kiocb *list_req; |
| 2883 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2884 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2885 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2886 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2887 | ctx->poll_multi_file = true; |
| 2888 | } |
| 2889 | |
| 2890 | /* |
| 2891 | * For fast devices, IO may have already completed. If it has, add |
| 2892 | * it to the front so we find it first. |
| 2893 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2894 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2895 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2896 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2897 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2898 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2899 | /* |
| 2900 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2901 | * task context or in io worker task context. If current task context is |
| 2902 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2903 | */ |
| 2904 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2905 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2906 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2909 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2910 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2911 | if (state->file_refs) { |
| 2912 | fput_many(state->file, state->file_refs); |
| 2913 | state->file_refs = 0; |
| 2914 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * Get as many references to a file as we have IOs left in this submission, |
| 2919 | * assuming most submissions are for one file, or at least that each file |
| 2920 | * has more than one submission. |
| 2921 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2922 | 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] | 2923 | { |
| 2924 | if (!state) |
| 2925 | return fget(fd); |
| 2926 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2927 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2928 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2929 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2930 | return state->file; |
| 2931 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2932 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2933 | } |
| 2934 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2935 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2936 | return NULL; |
| 2937 | |
| 2938 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2939 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2940 | return state->file; |
| 2941 | } |
| 2942 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2943 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2944 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2945 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2946 | } |
| 2947 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2948 | /* |
| 2949 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2950 | * any file. For now, just ensure that anything potentially problematic is done |
| 2951 | * inline. |
| 2952 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2953 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2954 | { |
| 2955 | umode_t mode = file_inode(file)->i_mode; |
| 2956 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2957 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2958 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2959 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2960 | return true; |
| 2961 | return false; |
| 2962 | } |
| 2963 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2964 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2965 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2966 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2967 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2968 | file->f_op != &io_uring_fops) |
| 2969 | return true; |
| 2970 | return false; |
| 2971 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2972 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2973 | /* any ->read/write should understand O_NONBLOCK */ |
| 2974 | if (file->f_flags & O_NONBLOCK) |
| 2975 | return true; |
| 2976 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2977 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2978 | return false; |
| 2979 | |
| 2980 | if (rw == READ) |
| 2981 | return file->f_op->read_iter != NULL; |
| 2982 | |
| 2983 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2986 | 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] | 2987 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2988 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2989 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2990 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2991 | unsigned ioprio; |
| 2992 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2993 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2994 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2995 | req->flags |= REQ_F_ISREG; |
| 2996 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2997 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2998 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2999 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3000 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3001 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3002 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 3003 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 3004 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 3005 | if (unlikely(ret)) |
| 3006 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3007 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3008 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 3009 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 3010 | req->flags |= REQ_F_NOWAIT; |
| 3011 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3012 | ioprio = READ_ONCE(sqe->ioprio); |
| 3013 | if (ioprio) { |
| 3014 | ret = ioprio_check_cap(ioprio); |
| 3015 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3016 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3017 | |
| 3018 | kiocb->ki_ioprio = ioprio; |
| 3019 | } else |
| 3020 | kiocb->ki_ioprio = get_current_ioprio(); |
| 3021 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3022 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3023 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 3024 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3025 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3026 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3027 | kiocb->ki_flags |= IOCB_HIPRI; |
| 3028 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3029 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3030 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3031 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3032 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3033 | kiocb->ki_complete = io_complete_rw; |
| 3034 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3035 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3036 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3037 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3038 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3039 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3043 | { |
| 3044 | switch (ret) { |
| 3045 | case -EIOCBQUEUED: |
| 3046 | break; |
| 3047 | case -ERESTARTSYS: |
| 3048 | case -ERESTARTNOINTR: |
| 3049 | case -ERESTARTNOHAND: |
| 3050 | case -ERESTART_RESTARTBLOCK: |
| 3051 | /* |
| 3052 | * We can't just restart the syscall, since previously |
| 3053 | * submitted sqes may already be in progress. Just fail this |
| 3054 | * IO with EINTR. |
| 3055 | */ |
| 3056 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3057 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3058 | default: |
| 3059 | kiocb->ki_complete(kiocb, ret, 0); |
| 3060 | } |
| 3061 | } |
| 3062 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3063 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3064 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3065 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3066 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3067 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3068 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3069 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3070 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3071 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3072 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3073 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3074 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3075 | } |
| 3076 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3077 | if (req->flags & REQ_F_CUR_POS) |
| 3078 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 3079 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3080 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3081 | else |
| 3082 | io_rw_done(kiocb, ret); |
| 3083 | } |
| 3084 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3085 | 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] | 3086 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3087 | struct io_ring_ctx *ctx = req->ctx; |
| 3088 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3089 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 3090 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3091 | size_t offset; |
| 3092 | u64 buf_addr; |
| 3093 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3094 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3095 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3096 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3097 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3098 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3099 | |
| 3100 | /* overflow */ |
| 3101 | if (buf_addr + len < buf_addr) |
| 3102 | return -EFAULT; |
| 3103 | /* not inside the mapped region */ |
| 3104 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3105 | return -EFAULT; |
| 3106 | |
| 3107 | /* |
| 3108 | * May not be a start of buffer, set size appropriately |
| 3109 | * and advance us to the beginning. |
| 3110 | */ |
| 3111 | offset = buf_addr - imu->ubuf; |
| 3112 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3113 | |
| 3114 | if (offset) { |
| 3115 | /* |
| 3116 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3117 | * using the latter parts of a big fixed buffer - it iterates |
| 3118 | * over each segment manually. We can cheat a bit here, because |
| 3119 | * we know that: |
| 3120 | * |
| 3121 | * 1) it's a BVEC iter, we set it up |
| 3122 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3123 | * first and last bvec |
| 3124 | * |
| 3125 | * So just find our index, and adjust the iterator afterwards. |
| 3126 | * If the offset is within the first bvec (or the whole first |
| 3127 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3128 | * since we can just skip the first segment, which may not |
| 3129 | * be PAGE_SIZE aligned. |
| 3130 | */ |
| 3131 | const struct bio_vec *bvec = imu->bvec; |
| 3132 | |
| 3133 | if (offset <= bvec->bv_len) { |
| 3134 | iov_iter_advance(iter, offset); |
| 3135 | } else { |
| 3136 | unsigned long seg_skip; |
| 3137 | |
| 3138 | /* skip first vec */ |
| 3139 | offset -= bvec->bv_len; |
| 3140 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3141 | |
| 3142 | iter->bvec = bvec + seg_skip; |
| 3143 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3144 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3145 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3149 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3152 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3153 | { |
| 3154 | if (needs_lock) |
| 3155 | mutex_unlock(&ctx->uring_lock); |
| 3156 | } |
| 3157 | |
| 3158 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3159 | { |
| 3160 | /* |
| 3161 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3162 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3163 | * The only exception is when we've detached the request and issue it |
| 3164 | * from an async worker thread, grab the lock for that case. |
| 3165 | */ |
| 3166 | if (needs_lock) |
| 3167 | mutex_lock(&ctx->uring_lock); |
| 3168 | } |
| 3169 | |
| 3170 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3171 | int bgid, struct io_buffer *kbuf, |
| 3172 | bool needs_lock) |
| 3173 | { |
| 3174 | struct io_buffer *head; |
| 3175 | |
| 3176 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3177 | return kbuf; |
| 3178 | |
| 3179 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3180 | |
| 3181 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3182 | |
| 3183 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3184 | if (head) { |
| 3185 | if (!list_empty(&head->list)) { |
| 3186 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3187 | list); |
| 3188 | list_del(&kbuf->list); |
| 3189 | } else { |
| 3190 | kbuf = head; |
| 3191 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3192 | } |
| 3193 | if (*len > kbuf->len) |
| 3194 | *len = kbuf->len; |
| 3195 | } else { |
| 3196 | kbuf = ERR_PTR(-ENOBUFS); |
| 3197 | } |
| 3198 | |
| 3199 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3200 | |
| 3201 | return kbuf; |
| 3202 | } |
| 3203 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3204 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3205 | bool needs_lock) |
| 3206 | { |
| 3207 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3208 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3209 | |
| 3210 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3211 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3212 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3213 | if (IS_ERR(kbuf)) |
| 3214 | return kbuf; |
| 3215 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3216 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3217 | return u64_to_user_ptr(kbuf->addr); |
| 3218 | } |
| 3219 | |
| 3220 | #ifdef CONFIG_COMPAT |
| 3221 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3222 | bool needs_lock) |
| 3223 | { |
| 3224 | struct compat_iovec __user *uiov; |
| 3225 | compat_ssize_t clen; |
| 3226 | void __user *buf; |
| 3227 | ssize_t len; |
| 3228 | |
| 3229 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3230 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3231 | return -EFAULT; |
| 3232 | if (__get_user(clen, &uiov->iov_len)) |
| 3233 | return -EFAULT; |
| 3234 | if (clen < 0) |
| 3235 | return -EINVAL; |
| 3236 | |
| 3237 | len = clen; |
| 3238 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3239 | if (IS_ERR(buf)) |
| 3240 | return PTR_ERR(buf); |
| 3241 | iov[0].iov_base = buf; |
| 3242 | iov[0].iov_len = (compat_size_t) len; |
| 3243 | return 0; |
| 3244 | } |
| 3245 | #endif |
| 3246 | |
| 3247 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3248 | bool needs_lock) |
| 3249 | { |
| 3250 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3251 | void __user *buf; |
| 3252 | ssize_t len; |
| 3253 | |
| 3254 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3255 | return -EFAULT; |
| 3256 | |
| 3257 | len = iov[0].iov_len; |
| 3258 | if (len < 0) |
| 3259 | return -EINVAL; |
| 3260 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3261 | if (IS_ERR(buf)) |
| 3262 | return PTR_ERR(buf); |
| 3263 | iov[0].iov_base = buf; |
| 3264 | iov[0].iov_len = len; |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
| 3268 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3269 | bool needs_lock) |
| 3270 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3271 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3272 | struct io_buffer *kbuf; |
| 3273 | |
| 3274 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3275 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3276 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3277 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3278 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3279 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3280 | return -EINVAL; |
| 3281 | |
| 3282 | #ifdef CONFIG_COMPAT |
| 3283 | if (req->ctx->compat) |
| 3284 | return io_compat_import(req, iov, needs_lock); |
| 3285 | #endif |
| 3286 | |
| 3287 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3288 | } |
| 3289 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3290 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3291 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3292 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3293 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3294 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3295 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3296 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3297 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3298 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3299 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3300 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3301 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3302 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3303 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3304 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3305 | return -EINVAL; |
| 3306 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3307 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3308 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3309 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3310 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3311 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3312 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3315 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3316 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3317 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3320 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3321 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3322 | if (!ret) |
| 3323 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3324 | *iovec = NULL; |
| 3325 | return ret; |
| 3326 | } |
| 3327 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3328 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3329 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3332 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3333 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3334 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3335 | } |
| 3336 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3337 | /* |
| 3338 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3339 | * by looping over ->read() or ->write() manually. |
| 3340 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3341 | 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] | 3342 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3343 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3344 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3345 | ssize_t ret = 0; |
| 3346 | |
| 3347 | /* |
| 3348 | * Don't support polled IO through this interface, and we can't |
| 3349 | * support non-blocking either. For the latter, this just causes |
| 3350 | * the kiocb to be handled from an async context. |
| 3351 | */ |
| 3352 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3353 | return -EOPNOTSUPP; |
| 3354 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3355 | return -EAGAIN; |
| 3356 | |
| 3357 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3358 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3359 | ssize_t nr; |
| 3360 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3361 | if (!iov_iter_is_bvec(iter)) { |
| 3362 | iovec = iov_iter_iovec(iter); |
| 3363 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3364 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3365 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3366 | } |
| 3367 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3368 | if (rw == READ) { |
| 3369 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3370 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3371 | } else { |
| 3372 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3373 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | if (nr < 0) { |
| 3377 | if (!ret) |
| 3378 | ret = nr; |
| 3379 | break; |
| 3380 | } |
| 3381 | ret += nr; |
| 3382 | if (nr != iovec.iov_len) |
| 3383 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3384 | req->rw.len -= nr; |
| 3385 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3386 | iov_iter_advance(iter, nr); |
| 3387 | } |
| 3388 | |
| 3389 | return ret; |
| 3390 | } |
| 3391 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3392 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3393 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3394 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3395 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3396 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3397 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3398 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3399 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3400 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3401 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3402 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3403 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3404 | unsigned iov_off = 0; |
| 3405 | |
| 3406 | rw->iter.iov = rw->fast_iov; |
| 3407 | if (iter->iov != fast_iov) { |
| 3408 | iov_off = iter->iov - fast_iov; |
| 3409 | rw->iter.iov += iov_off; |
| 3410 | } |
| 3411 | if (rw->fast_iov != fast_iov) |
| 3412 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3413 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3414 | } else { |
| 3415 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3419 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3420 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3421 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3422 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3423 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3424 | } |
| 3425 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3426 | static int io_alloc_async_data(struct io_kiocb *req) |
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 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3429 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3430 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3431 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3434 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3435 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3436 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3437 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3438 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3439 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3440 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3441 | if (__io_alloc_async_data(req)) { |
| 3442 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3443 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3444 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3445 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3446 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3447 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3448 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3449 | } |
| 3450 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3451 | 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] | 3452 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3453 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3454 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3455 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3456 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3457 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3458 | if (unlikely(ret < 0)) |
| 3459 | return ret; |
| 3460 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3461 | iorw->bytes_done = 0; |
| 3462 | iorw->free_iovec = iov; |
| 3463 | if (iov) |
| 3464 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3465 | return 0; |
| 3466 | } |
| 3467 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3468 | 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] | 3469 | { |
| 3470 | ssize_t ret; |
| 3471 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3472 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3473 | if (ret) |
| 3474 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3475 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3476 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3477 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3478 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3479 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3480 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3481 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3482 | return io_rw_prep_async(req, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3485 | /* |
| 3486 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3487 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3488 | * This gets called when the page is unlocked, and we generally expect that to |
| 3489 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3490 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3491 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3492 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3493 | * slow path. |
| 3494 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3495 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3496 | int sync, void *arg) |
| 3497 | { |
| 3498 | struct wait_page_queue *wpq; |
| 3499 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3500 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3501 | |
| 3502 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3503 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3504 | if (!wake_page_match(wpq, key)) |
| 3505 | return 0; |
| 3506 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3507 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3508 | list_del_init(&wait->entry); |
| 3509 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3510 | /* submit ref gets dropped, acquire a new one */ |
| 3511 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3512 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3513 | return 1; |
| 3514 | } |
| 3515 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3516 | /* |
| 3517 | * This controls whether a given IO request should be armed for async page |
| 3518 | * based retry. If we return false here, the request is handed to the async |
| 3519 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3520 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3521 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3522 | * will register a callback when the page is unlocked at IO completion. Through |
| 3523 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3524 | * That retry will attempt the buffered read again. The retry will generally |
| 3525 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3526 | * async worker threads for a blocking retry. |
| 3527 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3528 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3529 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3530 | struct io_async_rw *rw = req->async_data; |
| 3531 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3532 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3533 | |
| 3534 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3535 | if (req->flags & REQ_F_NOWAIT) |
| 3536 | return false; |
| 3537 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3538 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3539 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3540 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3541 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3542 | /* |
| 3543 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3544 | * support callback based unlocks |
| 3545 | */ |
| 3546 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3547 | return false; |
| 3548 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3549 | wait->wait.func = io_async_buf_func; |
| 3550 | wait->wait.private = req; |
| 3551 | wait->wait.flags = 0; |
| 3552 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3553 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3554 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3555 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3556 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3560 | { |
| 3561 | if (req->file->f_op->read_iter) |
| 3562 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3563 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3564 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3565 | else |
| 3566 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3567 | } |
| 3568 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3569 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3570 | { |
| 3571 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3572 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3573 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3574 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3575 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3576 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3577 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3578 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3579 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3580 | iovec = NULL; |
| 3581 | } else { |
| 3582 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3583 | if (ret < 0) |
| 3584 | return ret; |
| 3585 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3586 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3587 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3588 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3589 | /* Ensure we clear previously set non-block flag */ |
| 3590 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3591 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3592 | else |
| 3593 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3594 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3595 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3596 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3597 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3598 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3599 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3600 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3601 | 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] | 3602 | if (unlikely(ret)) { |
| 3603 | kfree(iovec); |
| 3604 | return ret; |
| 3605 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3606 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3607 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3608 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3609 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3610 | /* it's faster to check here then delegate to kfree */ |
| 3611 | if (iovec) |
| 3612 | kfree(iovec); |
| 3613 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3614 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3615 | /* IOPOLL retry should happen for io-wq threads */ |
| 3616 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3617 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3618 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3619 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3620 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3621 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3622 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3623 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3624 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3625 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3626 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3627 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3628 | } |
| 3629 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3630 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3631 | if (ret2) |
| 3632 | return ret2; |
| 3633 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3634 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3635 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3636 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3637 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3638 | do { |
| 3639 | io_size -= ret; |
| 3640 | rw->bytes_done += ret; |
| 3641 | /* if we can retry, do so with the callbacks armed */ |
| 3642 | if (!io_rw_should_retry(req)) { |
| 3643 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3644 | return -EAGAIN; |
| 3645 | } |
| 3646 | |
| 3647 | /* |
| 3648 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3649 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3650 | * desired page gets unlocked. We can also get a partial read |
| 3651 | * here, and if we do, then just retry at the new offset. |
| 3652 | */ |
| 3653 | ret = io_iter_do_read(req, iter); |
| 3654 | if (ret == -EIOCBQUEUED) |
| 3655 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3656 | /* we got some bytes, but not all. retry. */ |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3657 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3658 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3659 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3660 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3663 | 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] | 3664 | { |
| 3665 | ssize_t ret; |
| 3666 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3667 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3668 | if (ret) |
| 3669 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3670 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3671 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3672 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3673 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3674 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3675 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3676 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3677 | return io_rw_prep_async(req, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3680 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3681 | { |
| 3682 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3683 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3684 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3685 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3686 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3687 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3688 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3689 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3690 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3691 | iovec = NULL; |
| 3692 | } else { |
| 3693 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3694 | if (ret < 0) |
| 3695 | return ret; |
| 3696 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3697 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3698 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3699 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3700 | /* Ensure we clear previously set non-block flag */ |
| 3701 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3702 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3703 | else |
| 3704 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3705 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3706 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3707 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3708 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3709 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3710 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3711 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3712 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3713 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3714 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3715 | 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] | 3716 | if (unlikely(ret)) |
| 3717 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3718 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3719 | /* |
| 3720 | * Open-code file_start_write here to grab freeze protection, |
| 3721 | * which will be released by another thread in |
| 3722 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3723 | * released so that it doesn't complain about the held lock when |
| 3724 | * we return to userspace. |
| 3725 | */ |
| 3726 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3727 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3728 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3729 | SB_FREEZE_WRITE); |
| 3730 | } |
| 3731 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3732 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3733 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3734 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3735 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3736 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3737 | else |
| 3738 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3739 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3740 | /* |
| 3741 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3742 | * retry them without IOCB_NOWAIT. |
| 3743 | */ |
| 3744 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3745 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3746 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3747 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3748 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3749 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3750 | /* IOPOLL retry should happen for io-wq threads */ |
| 3751 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3752 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3753 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3754 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3755 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3756 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3757 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3758 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3759 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3760 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3761 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3762 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3763 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3764 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3765 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3766 | return ret; |
| 3767 | } |
| 3768 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3769 | static int io_renameat_prep(struct io_kiocb *req, |
| 3770 | const struct io_uring_sqe *sqe) |
| 3771 | { |
| 3772 | struct io_rename *ren = &req->rename; |
| 3773 | const char __user *oldf, *newf; |
| 3774 | |
| 3775 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3776 | return -EBADF; |
| 3777 | |
| 3778 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3779 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3780 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3781 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3782 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3783 | |
| 3784 | ren->oldpath = getname(oldf); |
| 3785 | if (IS_ERR(ren->oldpath)) |
| 3786 | return PTR_ERR(ren->oldpath); |
| 3787 | |
| 3788 | ren->newpath = getname(newf); |
| 3789 | if (IS_ERR(ren->newpath)) { |
| 3790 | putname(ren->oldpath); |
| 3791 | return PTR_ERR(ren->newpath); |
| 3792 | } |
| 3793 | |
| 3794 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3798 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3799 | { |
| 3800 | struct io_rename *ren = &req->rename; |
| 3801 | int ret; |
| 3802 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3803 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3804 | return -EAGAIN; |
| 3805 | |
| 3806 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3807 | ren->newpath, ren->flags); |
| 3808 | |
| 3809 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3810 | if (ret < 0) |
| 3811 | req_set_fail_links(req); |
| 3812 | io_req_complete(req, ret); |
| 3813 | return 0; |
| 3814 | } |
| 3815 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3816 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3817 | const struct io_uring_sqe *sqe) |
| 3818 | { |
| 3819 | struct io_unlink *un = &req->unlink; |
| 3820 | const char __user *fname; |
| 3821 | |
| 3822 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3823 | return -EBADF; |
| 3824 | |
| 3825 | un->dfd = READ_ONCE(sqe->fd); |
| 3826 | |
| 3827 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3828 | if (un->flags & ~AT_REMOVEDIR) |
| 3829 | return -EINVAL; |
| 3830 | |
| 3831 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3832 | un->filename = getname(fname); |
| 3833 | if (IS_ERR(un->filename)) |
| 3834 | return PTR_ERR(un->filename); |
| 3835 | |
| 3836 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3837 | return 0; |
| 3838 | } |
| 3839 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3840 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3841 | { |
| 3842 | struct io_unlink *un = &req->unlink; |
| 3843 | int ret; |
| 3844 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3845 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3846 | return -EAGAIN; |
| 3847 | |
| 3848 | if (un->flags & AT_REMOVEDIR) |
| 3849 | ret = do_rmdir(un->dfd, un->filename); |
| 3850 | else |
| 3851 | ret = do_unlinkat(un->dfd, un->filename); |
| 3852 | |
| 3853 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3854 | if (ret < 0) |
| 3855 | req_set_fail_links(req); |
| 3856 | io_req_complete(req, ret); |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3860 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3861 | const struct io_uring_sqe *sqe) |
| 3862 | { |
| 3863 | #if defined(CONFIG_NET) |
| 3864 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3865 | return -EINVAL; |
| 3866 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3867 | sqe->buf_index) |
| 3868 | return -EINVAL; |
| 3869 | |
| 3870 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3871 | return 0; |
| 3872 | #else |
| 3873 | return -EOPNOTSUPP; |
| 3874 | #endif |
| 3875 | } |
| 3876 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3877 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3878 | { |
| 3879 | #if defined(CONFIG_NET) |
| 3880 | struct socket *sock; |
| 3881 | int ret; |
| 3882 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3883 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3884 | return -EAGAIN; |
| 3885 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3886 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3887 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3888 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3889 | |
| 3890 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3891 | if (ret < 0) |
| 3892 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3893 | io_req_complete(req, ret); |
| 3894 | return 0; |
| 3895 | #else |
| 3896 | return -EOPNOTSUPP; |
| 3897 | #endif |
| 3898 | } |
| 3899 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3900 | static int __io_splice_prep(struct io_kiocb *req, |
| 3901 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3902 | { |
| 3903 | struct io_splice* sp = &req->splice; |
| 3904 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3905 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3906 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3907 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3908 | |
| 3909 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3910 | sp->len = READ_ONCE(sqe->len); |
| 3911 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3912 | |
| 3913 | if (unlikely(sp->flags & ~valid_flags)) |
| 3914 | return -EINVAL; |
| 3915 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3916 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3917 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3918 | if (!sp->file_in) |
| 3919 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3920 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3921 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3922 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3923 | /* |
| 3924 | * Splice operation will be punted aync, and here need to |
| 3925 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3926 | */ |
| 3927 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3928 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3929 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3930 | |
| 3931 | return 0; |
| 3932 | } |
| 3933 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3934 | static int io_tee_prep(struct io_kiocb *req, |
| 3935 | const struct io_uring_sqe *sqe) |
| 3936 | { |
| 3937 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3938 | return -EINVAL; |
| 3939 | return __io_splice_prep(req, sqe); |
| 3940 | } |
| 3941 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3942 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3943 | { |
| 3944 | struct io_splice *sp = &req->splice; |
| 3945 | struct file *in = sp->file_in; |
| 3946 | struct file *out = sp->file_out; |
| 3947 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3948 | long ret = 0; |
| 3949 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3950 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3951 | return -EAGAIN; |
| 3952 | if (sp->len) |
| 3953 | ret = do_tee(in, out, sp->len, flags); |
| 3954 | |
| 3955 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3956 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3957 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3958 | if (ret != sp->len) |
| 3959 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3960 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3961 | return 0; |
| 3962 | } |
| 3963 | |
| 3964 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3965 | { |
| 3966 | struct io_splice* sp = &req->splice; |
| 3967 | |
| 3968 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3969 | sp->off_out = READ_ONCE(sqe->off); |
| 3970 | return __io_splice_prep(req, sqe); |
| 3971 | } |
| 3972 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3973 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3974 | { |
| 3975 | struct io_splice *sp = &req->splice; |
| 3976 | struct file *in = sp->file_in; |
| 3977 | struct file *out = sp->file_out; |
| 3978 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3979 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3980 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3981 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3982 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3983 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3984 | |
| 3985 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3986 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3987 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3988 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3989 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3990 | |
| 3991 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3992 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3993 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3994 | if (ret != sp->len) |
| 3995 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3996 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3997 | return 0; |
| 3998 | } |
| 3999 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4000 | /* |
| 4001 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4002 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4003 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4004 | { |
| 4005 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4006 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4007 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4008 | return -EINVAL; |
| 4009 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4010 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4011 | return 0; |
| 4012 | } |
| 4013 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4014 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4015 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4016 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4017 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4018 | if (!req->file) |
| 4019 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4020 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4021 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4022 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4023 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4024 | return -EINVAL; |
| 4025 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4026 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4027 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4028 | return -EINVAL; |
| 4029 | |
| 4030 | req->sync.off = READ_ONCE(sqe->off); |
| 4031 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4032 | return 0; |
| 4033 | } |
| 4034 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4035 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4036 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4037 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4038 | int ret; |
| 4039 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4040 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4041 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4042 | return -EAGAIN; |
| 4043 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4044 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4045 | end > 0 ? end : LLONG_MAX, |
| 4046 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4047 | if (ret < 0) |
| 4048 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4049 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4050 | return 0; |
| 4051 | } |
| 4052 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4053 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4054 | const struct io_uring_sqe *sqe) |
| 4055 | { |
| 4056 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 4057 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4058 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4059 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4060 | |
| 4061 | req->sync.off = READ_ONCE(sqe->off); |
| 4062 | req->sync.len = READ_ONCE(sqe->addr); |
| 4063 | req->sync.mode = READ_ONCE(sqe->len); |
| 4064 | return 0; |
| 4065 | } |
| 4066 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4067 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4068 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4069 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4070 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4071 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4072 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4073 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4074 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4075 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4076 | if (ret < 0) |
| 4077 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4078 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4082 | 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] | 4083 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4084 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4085 | int ret; |
| 4086 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4087 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4088 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4089 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4090 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4091 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4092 | /* open.how should be already initialised */ |
| 4093 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4094 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4095 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4096 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4097 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4098 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4099 | if (IS_ERR(req->open.filename)) { |
| 4100 | ret = PTR_ERR(req->open.filename); |
| 4101 | req->open.filename = NULL; |
| 4102 | return ret; |
| 4103 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4104 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4105 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4106 | return 0; |
| 4107 | } |
| 4108 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4109 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4110 | { |
| 4111 | u64 flags, mode; |
| 4112 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4113 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4114 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4115 | mode = READ_ONCE(sqe->len); |
| 4116 | flags = READ_ONCE(sqe->open_flags); |
| 4117 | req->open.how = build_open_how(flags, mode); |
| 4118 | return __io_openat_prep(req, sqe); |
| 4119 | } |
| 4120 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4121 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4122 | { |
| 4123 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4124 | size_t len; |
| 4125 | int ret; |
| 4126 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4127 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4128 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4129 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4130 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4131 | if (len < OPEN_HOW_SIZE_VER0) |
| 4132 | return -EINVAL; |
| 4133 | |
| 4134 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4135 | len); |
| 4136 | if (ret) |
| 4137 | return ret; |
| 4138 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4139 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4142 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4143 | { |
| 4144 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4145 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4146 | bool nonblock_set; |
| 4147 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4148 | int ret; |
| 4149 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4150 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4151 | if (ret) |
| 4152 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4153 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4154 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4155 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4156 | /* |
| 4157 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4158 | * it'll always -EAGAIN |
| 4159 | */ |
| 4160 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4161 | return -EAGAIN; |
| 4162 | op.lookup_flags |= LOOKUP_CACHED; |
| 4163 | op.open_flag |= O_NONBLOCK; |
| 4164 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4165 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4166 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4167 | if (ret < 0) |
| 4168 | goto err; |
| 4169 | |
| 4170 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4171 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4172 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 4173 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4174 | /* |
| 4175 | * We could hang on to this 'fd', but seems like marginal |
| 4176 | * gain for something that is now known to be a slower path. |
| 4177 | * So just put it, and we'll get a new one when we retry. |
| 4178 | */ |
| 4179 | put_unused_fd(ret); |
| 4180 | return -EAGAIN; |
| 4181 | } |
| 4182 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4183 | if (IS_ERR(file)) { |
| 4184 | put_unused_fd(ret); |
| 4185 | ret = PTR_ERR(file); |
| 4186 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4187 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4188 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4189 | fsnotify_open(file); |
| 4190 | fd_install(ret, file); |
| 4191 | } |
| 4192 | err: |
| 4193 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4194 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4195 | if (ret < 0) |
| 4196 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4197 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4198 | return 0; |
| 4199 | } |
| 4200 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4201 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4202 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4203 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4206 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4207 | const struct io_uring_sqe *sqe) |
| 4208 | { |
| 4209 | struct io_provide_buf *p = &req->pbuf; |
| 4210 | u64 tmp; |
| 4211 | |
| 4212 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4213 | return -EINVAL; |
| 4214 | |
| 4215 | tmp = READ_ONCE(sqe->fd); |
| 4216 | if (!tmp || tmp > USHRT_MAX) |
| 4217 | return -EINVAL; |
| 4218 | |
| 4219 | memset(p, 0, sizeof(*p)); |
| 4220 | p->nbufs = tmp; |
| 4221 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4222 | return 0; |
| 4223 | } |
| 4224 | |
| 4225 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4226 | int bgid, unsigned nbufs) |
| 4227 | { |
| 4228 | unsigned i = 0; |
| 4229 | |
| 4230 | /* shouldn't happen */ |
| 4231 | if (!nbufs) |
| 4232 | return 0; |
| 4233 | |
| 4234 | /* the head kbuf is the list itself */ |
| 4235 | while (!list_empty(&buf->list)) { |
| 4236 | struct io_buffer *nxt; |
| 4237 | |
| 4238 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4239 | list_del(&nxt->list); |
| 4240 | kfree(nxt); |
| 4241 | if (++i == nbufs) |
| 4242 | return i; |
| 4243 | } |
| 4244 | i++; |
| 4245 | kfree(buf); |
| 4246 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4247 | |
| 4248 | return i; |
| 4249 | } |
| 4250 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4251 | 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] | 4252 | { |
| 4253 | struct io_provide_buf *p = &req->pbuf; |
| 4254 | struct io_ring_ctx *ctx = req->ctx; |
| 4255 | struct io_buffer *head; |
| 4256 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4257 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4258 | |
| 4259 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4260 | |
| 4261 | lockdep_assert_held(&ctx->uring_lock); |
| 4262 | |
| 4263 | ret = -ENOENT; |
| 4264 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4265 | if (head) |
| 4266 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4267 | if (ret < 0) |
| 4268 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4269 | |
| 4270 | /* need to hold the lock to complete IOPOLL requests */ |
| 4271 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4272 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4273 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4274 | } else { |
| 4275 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4276 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4277 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4278 | return 0; |
| 4279 | } |
| 4280 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4281 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4282 | const struct io_uring_sqe *sqe) |
| 4283 | { |
| 4284 | struct io_provide_buf *p = &req->pbuf; |
| 4285 | u64 tmp; |
| 4286 | |
| 4287 | if (sqe->ioprio || sqe->rw_flags) |
| 4288 | return -EINVAL; |
| 4289 | |
| 4290 | tmp = READ_ONCE(sqe->fd); |
| 4291 | if (!tmp || tmp > USHRT_MAX) |
| 4292 | return -E2BIG; |
| 4293 | p->nbufs = tmp; |
| 4294 | p->addr = READ_ONCE(sqe->addr); |
| 4295 | p->len = READ_ONCE(sqe->len); |
| 4296 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4297 | 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] | 4298 | return -EFAULT; |
| 4299 | |
| 4300 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4301 | tmp = READ_ONCE(sqe->off); |
| 4302 | if (tmp > USHRT_MAX) |
| 4303 | return -E2BIG; |
| 4304 | p->bid = tmp; |
| 4305 | return 0; |
| 4306 | } |
| 4307 | |
| 4308 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4309 | { |
| 4310 | struct io_buffer *buf; |
| 4311 | u64 addr = pbuf->addr; |
| 4312 | int i, bid = pbuf->bid; |
| 4313 | |
| 4314 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4315 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4316 | if (!buf) |
| 4317 | break; |
| 4318 | |
| 4319 | buf->addr = addr; |
| 4320 | buf->len = pbuf->len; |
| 4321 | buf->bid = bid; |
| 4322 | addr += pbuf->len; |
| 4323 | bid++; |
| 4324 | if (!*head) { |
| 4325 | INIT_LIST_HEAD(&buf->list); |
| 4326 | *head = buf; |
| 4327 | } else { |
| 4328 | list_add_tail(&buf->list, &(*head)->list); |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | return i ? i : -ENOMEM; |
| 4333 | } |
| 4334 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4335 | 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] | 4336 | { |
| 4337 | struct io_provide_buf *p = &req->pbuf; |
| 4338 | struct io_ring_ctx *ctx = req->ctx; |
| 4339 | struct io_buffer *head, *list; |
| 4340 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4341 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4342 | |
| 4343 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4344 | |
| 4345 | lockdep_assert_held(&ctx->uring_lock); |
| 4346 | |
| 4347 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4348 | |
| 4349 | ret = io_add_buffers(p, &head); |
| 4350 | if (ret < 0) |
| 4351 | goto out; |
| 4352 | |
| 4353 | if (!list) { |
| 4354 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4355 | GFP_KERNEL); |
| 4356 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4357 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4358 | goto out; |
| 4359 | } |
| 4360 | } |
| 4361 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4362 | if (ret < 0) |
| 4363 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4364 | |
| 4365 | /* need to hold the lock to complete IOPOLL requests */ |
| 4366 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4367 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4368 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4369 | } else { |
| 4370 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4371 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4372 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4373 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4376 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4377 | const struct io_uring_sqe *sqe) |
| 4378 | { |
| 4379 | #if defined(CONFIG_EPOLL) |
| 4380 | if (sqe->ioprio || sqe->buf_index) |
| 4381 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4382 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4383 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4384 | |
| 4385 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4386 | req->epoll.op = READ_ONCE(sqe->len); |
| 4387 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4388 | |
| 4389 | if (ep_op_has_event(req->epoll.op)) { |
| 4390 | struct epoll_event __user *ev; |
| 4391 | |
| 4392 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4393 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4394 | return -EFAULT; |
| 4395 | } |
| 4396 | |
| 4397 | return 0; |
| 4398 | #else |
| 4399 | return -EOPNOTSUPP; |
| 4400 | #endif |
| 4401 | } |
| 4402 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4403 | 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] | 4404 | { |
| 4405 | #if defined(CONFIG_EPOLL) |
| 4406 | struct io_epoll *ie = &req->epoll; |
| 4407 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4408 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4409 | |
| 4410 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4411 | if (force_nonblock && ret == -EAGAIN) |
| 4412 | return -EAGAIN; |
| 4413 | |
| 4414 | if (ret < 0) |
| 4415 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4416 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4417 | return 0; |
| 4418 | #else |
| 4419 | return -EOPNOTSUPP; |
| 4420 | #endif |
| 4421 | } |
| 4422 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4423 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4424 | { |
| 4425 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4426 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4427 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4428 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4429 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4430 | |
| 4431 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4432 | req->madvise.len = READ_ONCE(sqe->len); |
| 4433 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4434 | return 0; |
| 4435 | #else |
| 4436 | return -EOPNOTSUPP; |
| 4437 | #endif |
| 4438 | } |
| 4439 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4440 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4441 | { |
| 4442 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4443 | struct io_madvise *ma = &req->madvise; |
| 4444 | int ret; |
| 4445 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4446 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4447 | return -EAGAIN; |
| 4448 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4449 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4450 | if (ret < 0) |
| 4451 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4452 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4453 | return 0; |
| 4454 | #else |
| 4455 | return -EOPNOTSUPP; |
| 4456 | #endif |
| 4457 | } |
| 4458 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4459 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4460 | { |
| 4461 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4462 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4463 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4464 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4465 | |
| 4466 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4467 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4468 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4469 | return 0; |
| 4470 | } |
| 4471 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4472 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4473 | { |
| 4474 | struct io_fadvise *fa = &req->fadvise; |
| 4475 | int ret; |
| 4476 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4477 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4478 | switch (fa->advice) { |
| 4479 | case POSIX_FADV_NORMAL: |
| 4480 | case POSIX_FADV_RANDOM: |
| 4481 | case POSIX_FADV_SEQUENTIAL: |
| 4482 | break; |
| 4483 | default: |
| 4484 | return -EAGAIN; |
| 4485 | } |
| 4486 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4487 | |
| 4488 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4489 | if (ret < 0) |
| 4490 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4491 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4492 | return 0; |
| 4493 | } |
| 4494 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4495 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4496 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4497 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4498 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4499 | if (sqe->ioprio || sqe->buf_index) |
| 4500 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4501 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4502 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4503 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4504 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4505 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4506 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4507 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4508 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4509 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4510 | return 0; |
| 4511 | } |
| 4512 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4513 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4514 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4515 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4516 | int ret; |
| 4517 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4518 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4519 | /* only need file table for an actual valid fd */ |
| 4520 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4521 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4522 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4523 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4524 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4525 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4526 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4527 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4528 | if (ret < 0) |
| 4529 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4530 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4534 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4535 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4536 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4537 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4538 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4539 | sqe->rw_flags || sqe->buf_index) |
| 4540 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4541 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4542 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4543 | |
| 4544 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4545 | return 0; |
| 4546 | } |
| 4547 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4548 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4549 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4550 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4551 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4552 | struct fdtable *fdt; |
| 4553 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4554 | int ret; |
| 4555 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4556 | file = NULL; |
| 4557 | ret = -EBADF; |
| 4558 | spin_lock(&files->file_lock); |
| 4559 | fdt = files_fdtable(files); |
| 4560 | if (close->fd >= fdt->max_fds) { |
| 4561 | spin_unlock(&files->file_lock); |
| 4562 | goto err; |
| 4563 | } |
| 4564 | file = fdt->fd[close->fd]; |
| 4565 | if (!file) { |
| 4566 | spin_unlock(&files->file_lock); |
| 4567 | goto err; |
| 4568 | } |
| 4569 | |
| 4570 | if (file->f_op == &io_uring_fops) { |
| 4571 | spin_unlock(&files->file_lock); |
| 4572 | file = NULL; |
| 4573 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4574 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4575 | |
| 4576 | /* 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] | 4577 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4578 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4579 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4580 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4581 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4582 | ret = __close_fd_get_file(close->fd, &file); |
| 4583 | spin_unlock(&files->file_lock); |
| 4584 | if (ret < 0) { |
| 4585 | if (ret == -ENOENT) |
| 4586 | ret = -EBADF; |
| 4587 | goto err; |
| 4588 | } |
| 4589 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4590 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4591 | ret = filp_close(file, current->files); |
| 4592 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4593 | if (ret < 0) |
| 4594 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4595 | if (file) |
| 4596 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4597 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4598 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4599 | } |
| 4600 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4601 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4602 | { |
| 4603 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4604 | |
| 4605 | if (!req->file) |
| 4606 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4607 | |
| 4608 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4609 | return -EINVAL; |
| 4610 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4611 | return -EINVAL; |
| 4612 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4613 | req->sync.off = READ_ONCE(sqe->off); |
| 4614 | req->sync.len = READ_ONCE(sqe->len); |
| 4615 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4616 | return 0; |
| 4617 | } |
| 4618 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4619 | 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] | 4620 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4621 | int ret; |
| 4622 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4623 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4624 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4625 | return -EAGAIN; |
| 4626 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4627 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4628 | req->sync.flags); |
| 4629 | if (ret < 0) |
| 4630 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4631 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4632 | return 0; |
| 4633 | } |
| 4634 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4635 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4636 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4637 | struct io_async_msghdr *kmsg) |
| 4638 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4639 | struct io_async_msghdr *async_msg = req->async_data; |
| 4640 | |
| 4641 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4642 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4643 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4644 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4645 | return -ENOMEM; |
| 4646 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4647 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4648 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4649 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4650 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4651 | /* if were using fast_iov, set it to the new one */ |
| 4652 | if (!async_msg->free_iov) |
| 4653 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4654 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4655 | return -EAGAIN; |
| 4656 | } |
| 4657 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4658 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4659 | struct io_async_msghdr *iomsg) |
| 4660 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4661 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4662 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4663 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4664 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4665 | } |
| 4666 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4667 | 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] | 4668 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4669 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4670 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4671 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4672 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4673 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4674 | return -EINVAL; |
| 4675 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4676 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4677 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4678 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4679 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4680 | #ifdef CONFIG_COMPAT |
| 4681 | if (req->ctx->compat) |
| 4682 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4683 | #endif |
| 4684 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4685 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4686 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4687 | ret = io_sendmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4688 | if (!ret) |
| 4689 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4690 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4693 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4694 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4695 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4696 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4697 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4698 | int ret; |
| 4699 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4700 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4701 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4702 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4703 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4704 | kmsg = req->async_data; |
| 4705 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4706 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4707 | if (ret) |
| 4708 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4709 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4712 | flags = req->sr_msg.msg_flags; |
| 4713 | if (flags & MSG_DONTWAIT) |
| 4714 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4715 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4716 | flags |= MSG_DONTWAIT; |
| 4717 | |
| 4718 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4719 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4720 | return io_setup_async_msg(req, kmsg); |
| 4721 | if (ret == -ERESTARTSYS) |
| 4722 | ret = -EINTR; |
| 4723 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4724 | /* fast path, check for non-NULL to avoid function call */ |
| 4725 | if (kmsg->free_iov) |
| 4726 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4727 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4728 | if (ret < 0) |
| 4729 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4730 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4731 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4734 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4735 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4736 | struct io_sr_msg *sr = &req->sr_msg; |
| 4737 | struct msghdr msg; |
| 4738 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4739 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4740 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4741 | int ret; |
| 4742 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4743 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4744 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4745 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4746 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4747 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4748 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4749 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4750 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4751 | msg.msg_name = NULL; |
| 4752 | msg.msg_control = NULL; |
| 4753 | msg.msg_controllen = 0; |
| 4754 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4755 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4756 | flags = req->sr_msg.msg_flags; |
| 4757 | if (flags & MSG_DONTWAIT) |
| 4758 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4759 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4760 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4761 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4762 | msg.msg_flags = flags; |
| 4763 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4764 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4765 | return -EAGAIN; |
| 4766 | if (ret == -ERESTARTSYS) |
| 4767 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4768 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4769 | if (ret < 0) |
| 4770 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4771 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4772 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4773 | } |
| 4774 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4775 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4776 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4777 | { |
| 4778 | struct io_sr_msg *sr = &req->sr_msg; |
| 4779 | struct iovec __user *uiov; |
| 4780 | size_t iov_len; |
| 4781 | int ret; |
| 4782 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4783 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4784 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4785 | if (ret) |
| 4786 | return ret; |
| 4787 | |
| 4788 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4789 | if (iov_len > 1) |
| 4790 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4791 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4792 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4793 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4794 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4795 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4796 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4797 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4798 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4799 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4800 | if (ret > 0) |
| 4801 | ret = 0; |
| 4802 | } |
| 4803 | |
| 4804 | return ret; |
| 4805 | } |
| 4806 | |
| 4807 | #ifdef CONFIG_COMPAT |
| 4808 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4809 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4810 | { |
| 4811 | struct compat_msghdr __user *msg_compat; |
| 4812 | struct io_sr_msg *sr = &req->sr_msg; |
| 4813 | struct compat_iovec __user *uiov; |
| 4814 | compat_uptr_t ptr; |
| 4815 | compat_size_t len; |
| 4816 | int ret; |
| 4817 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4818 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4819 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4820 | &ptr, &len); |
| 4821 | if (ret) |
| 4822 | return ret; |
| 4823 | |
| 4824 | uiov = compat_ptr(ptr); |
| 4825 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4826 | compat_ssize_t clen; |
| 4827 | |
| 4828 | if (len > 1) |
| 4829 | return -EINVAL; |
| 4830 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4831 | return -EFAULT; |
| 4832 | if (__get_user(clen, &uiov->iov_len)) |
| 4833 | return -EFAULT; |
| 4834 | if (clen < 0) |
| 4835 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4836 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4837 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4838 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4839 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4840 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4841 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4842 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4843 | if (ret < 0) |
| 4844 | return ret; |
| 4845 | } |
| 4846 | |
| 4847 | return 0; |
| 4848 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4849 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4850 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4851 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4852 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4853 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4854 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4855 | |
| 4856 | #ifdef CONFIG_COMPAT |
| 4857 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4858 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4859 | #endif |
| 4860 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4861 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4864 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4865 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4866 | { |
| 4867 | struct io_sr_msg *sr = &req->sr_msg; |
| 4868 | struct io_buffer *kbuf; |
| 4869 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4870 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4871 | if (IS_ERR(kbuf)) |
| 4872 | return kbuf; |
| 4873 | |
| 4874 | sr->kbuf = kbuf; |
| 4875 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4876 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4879 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4880 | { |
| 4881 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4882 | } |
| 4883 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4884 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4885 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4886 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4887 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4888 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4889 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4890 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4891 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4892 | return -EINVAL; |
| 4893 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4894 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4895 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4896 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4897 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4898 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4899 | #ifdef CONFIG_COMPAT |
| 4900 | if (req->ctx->compat) |
| 4901 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4902 | #endif |
| 4903 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4904 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4905 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4906 | ret = io_recvmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4907 | if (!ret) |
| 4908 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4909 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4912 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4913 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4914 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4915 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4916 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4917 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4918 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4919 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4920 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4921 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4922 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4923 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4924 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4925 | kmsg = req->async_data; |
| 4926 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4927 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4928 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4929 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4930 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4931 | } |
| 4932 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4933 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4934 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4935 | if (IS_ERR(kbuf)) |
| 4936 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4937 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4938 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4939 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4940 | 1, req->sr_msg.len); |
| 4941 | } |
| 4942 | |
| 4943 | flags = req->sr_msg.msg_flags; |
| 4944 | if (flags & MSG_DONTWAIT) |
| 4945 | req->flags |= REQ_F_NOWAIT; |
| 4946 | else if (force_nonblock) |
| 4947 | flags |= MSG_DONTWAIT; |
| 4948 | |
| 4949 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4950 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4951 | if (force_nonblock && ret == -EAGAIN) |
| 4952 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4953 | if (ret == -ERESTARTSYS) |
| 4954 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4955 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4956 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4957 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4958 | /* fast path, check for non-NULL to avoid function call */ |
| 4959 | if (kmsg->free_iov) |
| 4960 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4961 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4962 | if (ret < 0) |
| 4963 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4964 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4965 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4966 | } |
| 4967 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4968 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4969 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4970 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4971 | struct io_sr_msg *sr = &req->sr_msg; |
| 4972 | struct msghdr msg; |
| 4973 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4974 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4975 | struct iovec iov; |
| 4976 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4977 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4978 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4979 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4980 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4981 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4982 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4983 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4984 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4985 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4986 | if (IS_ERR(kbuf)) |
| 4987 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4988 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4991 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4992 | if (unlikely(ret)) |
| 4993 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4994 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4995 | msg.msg_name = NULL; |
| 4996 | msg.msg_control = NULL; |
| 4997 | msg.msg_controllen = 0; |
| 4998 | msg.msg_namelen = 0; |
| 4999 | msg.msg_iocb = NULL; |
| 5000 | msg.msg_flags = 0; |
| 5001 | |
| 5002 | flags = req->sr_msg.msg_flags; |
| 5003 | if (flags & MSG_DONTWAIT) |
| 5004 | req->flags |= REQ_F_NOWAIT; |
| 5005 | else if (force_nonblock) |
| 5006 | flags |= MSG_DONTWAIT; |
| 5007 | |
| 5008 | ret = sock_recvmsg(sock, &msg, flags); |
| 5009 | if (force_nonblock && ret == -EAGAIN) |
| 5010 | return -EAGAIN; |
| 5011 | if (ret == -ERESTARTSYS) |
| 5012 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5013 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5014 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5015 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5016 | if (ret < 0) |
| 5017 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5018 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5019 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5020 | } |
| 5021 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5022 | 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] | 5023 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5024 | struct io_accept *accept = &req->accept; |
| 5025 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5026 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5027 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 5028 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5029 | return -EINVAL; |
| 5030 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5031 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5032 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5033 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5034 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5035 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5036 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5037 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5038 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5039 | { |
| 5040 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5041 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5042 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5043 | int ret; |
| 5044 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5045 | if (req->file->f_flags & O_NONBLOCK) |
| 5046 | req->flags |= REQ_F_NOWAIT; |
| 5047 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5048 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5049 | accept->addr_len, accept->flags, |
| 5050 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5051 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5052 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5053 | if (ret < 0) { |
| 5054 | if (ret == -ERESTARTSYS) |
| 5055 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5056 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5057 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5058 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5059 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5062 | 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] | 5063 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5064 | struct io_connect *conn = &req->connect; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5065 | struct io_async_connect *io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5066 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5067 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5068 | return -EINVAL; |
| 5069 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5070 | return -EINVAL; |
| 5071 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5072 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5073 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 5074 | |
| 5075 | if (!io) |
| 5076 | return 0; |
| 5077 | |
| 5078 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5079 | &io->address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5080 | } |
| 5081 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5082 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5083 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5084 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5085 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5086 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5087 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5088 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5089 | if (req->async_data) { |
| 5090 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5091 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5092 | ret = move_addr_to_kernel(req->connect.addr, |
| 5093 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5094 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5095 | if (ret) |
| 5096 | goto out; |
| 5097 | io = &__io; |
| 5098 | } |
| 5099 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5100 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5101 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5102 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5103 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5104 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5105 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5106 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5107 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5108 | ret = -ENOMEM; |
| 5109 | goto out; |
| 5110 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5111 | io = req->async_data; |
| 5112 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5113 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5114 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5115 | if (ret == -ERESTARTSYS) |
| 5116 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5117 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5118 | if (ret < 0) |
| 5119 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5120 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5121 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5122 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5123 | #else /* !CONFIG_NET */ |
| 5124 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5125 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5126 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5127 | } |
| 5128 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5129 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5130 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5131 | return -EOPNOTSUPP; |
| 5132 | } |
| 5133 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5134 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5135 | { |
| 5136 | return -EOPNOTSUPP; |
| 5137 | } |
| 5138 | |
| 5139 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 5140 | const struct io_uring_sqe *sqe) |
| 5141 | { |
| 5142 | return -EOPNOTSUPP; |
| 5143 | } |
| 5144 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5145 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5146 | { |
| 5147 | return -EOPNOTSUPP; |
| 5148 | } |
| 5149 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5150 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5151 | { |
| 5152 | return -EOPNOTSUPP; |
| 5153 | } |
| 5154 | |
| 5155 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5156 | { |
| 5157 | return -EOPNOTSUPP; |
| 5158 | } |
| 5159 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5160 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5161 | { |
| 5162 | return -EOPNOTSUPP; |
| 5163 | } |
| 5164 | |
| 5165 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5166 | { |
| 5167 | return -EOPNOTSUPP; |
| 5168 | } |
| 5169 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5170 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5171 | { |
| 5172 | return -EOPNOTSUPP; |
| 5173 | } |
| 5174 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5175 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5176 | struct io_poll_table { |
| 5177 | struct poll_table_struct pt; |
| 5178 | struct io_kiocb *req; |
| 5179 | int error; |
| 5180 | }; |
| 5181 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5182 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5183 | __poll_t mask, task_work_func_t func) |
| 5184 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5185 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5186 | |
| 5187 | /* for instances that support it check for an event match first: */ |
| 5188 | if (mask && !(mask & poll->events)) |
| 5189 | return 0; |
| 5190 | |
| 5191 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5192 | |
| 5193 | list_del_init(&poll->wait.entry); |
| 5194 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5195 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 5196 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5197 | percpu_ref_get(&req->ctx->refs); |
| 5198 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5199 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5200 | * If this fails, then the task is exiting. When a task exits, the |
| 5201 | * work gets canceled, so just cancel this request as well instead |
| 5202 | * of executing it. We can't safely execute it anyway, as we may not |
| 5203 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5204 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5205 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5206 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5207 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 5208 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5209 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5210 | return 1; |
| 5211 | } |
| 5212 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5213 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5214 | __acquires(&req->ctx->completion_lock) |
| 5215 | { |
| 5216 | struct io_ring_ctx *ctx = req->ctx; |
| 5217 | |
| 5218 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5219 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5220 | |
| 5221 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5222 | } |
| 5223 | |
| 5224 | spin_lock_irq(&ctx->completion_lock); |
| 5225 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5226 | add_wait_queue(poll->head, &poll->wait); |
| 5227 | return true; |
| 5228 | } |
| 5229 | |
| 5230 | return false; |
| 5231 | } |
| 5232 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5233 | 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] | 5234 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5235 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5236 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5237 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5238 | return req->apoll->double_poll; |
| 5239 | } |
| 5240 | |
| 5241 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5242 | { |
| 5243 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5244 | return &req->poll; |
| 5245 | return &req->apoll->poll; |
| 5246 | } |
| 5247 | |
| 5248 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5249 | { |
| 5250 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5251 | |
| 5252 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5253 | |
| 5254 | if (poll && poll->head) { |
| 5255 | struct wait_queue_head *head = poll->head; |
| 5256 | |
| 5257 | spin_lock(&head->lock); |
| 5258 | list_del_init(&poll->wait.entry); |
| 5259 | if (poll->wait.private) |
| 5260 | refcount_dec(&req->refs); |
| 5261 | poll->head = NULL; |
| 5262 | spin_unlock(&head->lock); |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5267 | { |
| 5268 | struct io_ring_ctx *ctx = req->ctx; |
| 5269 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5270 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5271 | req->poll.done = true; |
| 5272 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5273 | io_commit_cqring(ctx); |
| 5274 | } |
| 5275 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5276 | static void io_poll_task_func(struct callback_head *cb) |
| 5277 | { |
| 5278 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5279 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5280 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5281 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5282 | if (io_poll_rewait(req, &req->poll)) { |
| 5283 | spin_unlock_irq(&ctx->completion_lock); |
| 5284 | } else { |
| 5285 | hash_del(&req->hash_node); |
| 5286 | io_poll_complete(req, req->result, 0); |
| 5287 | spin_unlock_irq(&ctx->completion_lock); |
| 5288 | |
| 5289 | nxt = io_put_req_find_next(req); |
| 5290 | io_cqring_ev_posted(ctx); |
| 5291 | if (nxt) |
| 5292 | __io_req_task_submit(nxt); |
| 5293 | } |
| 5294 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5295 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5299 | int sync, void *key) |
| 5300 | { |
| 5301 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5302 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5303 | __poll_t mask = key_to_poll(key); |
| 5304 | |
| 5305 | /* for instances that support it check for an event match first: */ |
| 5306 | if (mask && !(mask & poll->events)) |
| 5307 | return 0; |
| 5308 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5309 | list_del_init(&wait->entry); |
| 5310 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5311 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5312 | bool done; |
| 5313 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5314 | spin_lock(&poll->head->lock); |
| 5315 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5316 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5317 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5318 | /* make sure double remove sees this as being gone */ |
| 5319 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5320 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5321 | if (!done) { |
| 5322 | /* use wait func handler, so it matches the rq type */ |
| 5323 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5324 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5325 | } |
| 5326 | refcount_dec(&req->refs); |
| 5327 | return 1; |
| 5328 | } |
| 5329 | |
| 5330 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5331 | wait_queue_func_t wake_func) |
| 5332 | { |
| 5333 | poll->head = NULL; |
| 5334 | poll->done = false; |
| 5335 | poll->canceled = false; |
| 5336 | poll->events = events; |
| 5337 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5338 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5339 | } |
| 5340 | |
| 5341 | 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] | 5342 | struct wait_queue_head *head, |
| 5343 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5344 | { |
| 5345 | struct io_kiocb *req = pt->req; |
| 5346 | |
| 5347 | /* |
| 5348 | * If poll->head is already set, it's because the file being polled |
| 5349 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5350 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5351 | */ |
| 5352 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5353 | struct io_poll_iocb *poll_one = poll; |
| 5354 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5355 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5356 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5357 | pt->error = -EINVAL; |
| 5358 | return; |
| 5359 | } |
| 5360 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5361 | if (!poll) { |
| 5362 | pt->error = -ENOMEM; |
| 5363 | return; |
| 5364 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5365 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5366 | refcount_inc(&req->refs); |
| 5367 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5368 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | pt->error = 0; |
| 5372 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5373 | |
| 5374 | if (poll->events & EPOLLEXCLUSIVE) |
| 5375 | add_wait_queue_exclusive(head, &poll->wait); |
| 5376 | else |
| 5377 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5381 | struct poll_table_struct *p) |
| 5382 | { |
| 5383 | 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] | 5384 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5385 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5386 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5387 | } |
| 5388 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5389 | static void io_async_task_func(struct callback_head *cb) |
| 5390 | { |
| 5391 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5392 | struct async_poll *apoll = req->apoll; |
| 5393 | struct io_ring_ctx *ctx = req->ctx; |
| 5394 | |
| 5395 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5396 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5397 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5398 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5399 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5400 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5401 | } |
| 5402 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5403 | /* 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] | 5404 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5405 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5406 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5407 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5408 | spin_unlock_irq(&ctx->completion_lock); |
| 5409 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5410 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5411 | __io_req_task_submit(req); |
| 5412 | else |
| 5413 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5414 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5415 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5416 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5417 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5421 | void *key) |
| 5422 | { |
| 5423 | struct io_kiocb *req = wait->private; |
| 5424 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5425 | |
| 5426 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5427 | key_to_poll(key)); |
| 5428 | |
| 5429 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5430 | } |
| 5431 | |
| 5432 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5433 | { |
| 5434 | struct io_ring_ctx *ctx = req->ctx; |
| 5435 | struct hlist_head *list; |
| 5436 | |
| 5437 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5438 | hlist_add_head(&req->hash_node, list); |
| 5439 | } |
| 5440 | |
| 5441 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5442 | struct io_poll_iocb *poll, |
| 5443 | struct io_poll_table *ipt, __poll_t mask, |
| 5444 | wait_queue_func_t wake_func) |
| 5445 | __acquires(&ctx->completion_lock) |
| 5446 | { |
| 5447 | struct io_ring_ctx *ctx = req->ctx; |
| 5448 | bool cancel = false; |
| 5449 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5450 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5451 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5452 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5453 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5454 | |
| 5455 | ipt->pt._key = mask; |
| 5456 | ipt->req = req; |
| 5457 | ipt->error = -EINVAL; |
| 5458 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5459 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5460 | |
| 5461 | spin_lock_irq(&ctx->completion_lock); |
| 5462 | if (likely(poll->head)) { |
| 5463 | spin_lock(&poll->head->lock); |
| 5464 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5465 | if (ipt->error) |
| 5466 | cancel = true; |
| 5467 | ipt->error = 0; |
| 5468 | mask = 0; |
| 5469 | } |
| 5470 | if (mask || ipt->error) |
| 5471 | list_del_init(&poll->wait.entry); |
| 5472 | else if (cancel) |
| 5473 | WRITE_ONCE(poll->canceled, true); |
| 5474 | else if (!poll->done) /* actually waiting for an event */ |
| 5475 | io_poll_req_insert(req); |
| 5476 | spin_unlock(&poll->head->lock); |
| 5477 | } |
| 5478 | |
| 5479 | return mask; |
| 5480 | } |
| 5481 | |
| 5482 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5483 | { |
| 5484 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5485 | struct io_ring_ctx *ctx = req->ctx; |
| 5486 | struct async_poll *apoll; |
| 5487 | struct io_poll_table ipt; |
| 5488 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5489 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5490 | |
| 5491 | if (!req->file || !file_can_poll(req->file)) |
| 5492 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5493 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5494 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5495 | if (def->pollin) |
| 5496 | rw = READ; |
| 5497 | else if (def->pollout) |
| 5498 | rw = WRITE; |
| 5499 | else |
| 5500 | return false; |
| 5501 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5502 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5503 | return false; |
| 5504 | |
| 5505 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5506 | if (unlikely(!apoll)) |
| 5507 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5508 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5509 | |
| 5510 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5511 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5512 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5513 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5514 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5515 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5516 | if (def->pollout) |
| 5517 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5518 | |
| 5519 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5520 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5521 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5522 | mask &= ~POLLIN; |
| 5523 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5524 | mask |= POLLERR | POLLPRI; |
| 5525 | |
| 5526 | ipt.pt._qproc = io_async_queue_proc; |
| 5527 | |
| 5528 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5529 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5530 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5531 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5532 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5533 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5534 | kfree(apoll); |
| 5535 | return false; |
| 5536 | } |
| 5537 | spin_unlock_irq(&ctx->completion_lock); |
| 5538 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5539 | apoll->poll.events); |
| 5540 | return true; |
| 5541 | } |
| 5542 | |
| 5543 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5544 | struct io_poll_iocb *poll) |
| 5545 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5546 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5547 | |
| 5548 | spin_lock(&poll->head->lock); |
| 5549 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5550 | if (!list_empty(&poll->wait.entry)) { |
| 5551 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5552 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5553 | } |
| 5554 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5555 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5556 | return do_complete; |
| 5557 | } |
| 5558 | |
| 5559 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5560 | { |
| 5561 | bool do_complete; |
| 5562 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5563 | io_poll_remove_double(req); |
| 5564 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5565 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5566 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5567 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5568 | struct async_poll *apoll = req->apoll; |
| 5569 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5570 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5571 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5572 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5573 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5574 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5575 | kfree(apoll); |
| 5576 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5577 | } |
| 5578 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5579 | if (do_complete) { |
| 5580 | io_cqring_fill_event(req, -ECANCELED); |
| 5581 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5582 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5583 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5587 | } |
| 5588 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5589 | /* |
| 5590 | * Returns true if we found and killed one or more poll requests |
| 5591 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5592 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5593 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5594 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5595 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5596 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5597 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5598 | |
| 5599 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5600 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5601 | struct hlist_head *list; |
| 5602 | |
| 5603 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5604 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5605 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5606 | posted += io_poll_remove_one(req); |
| 5607 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5608 | } |
| 5609 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5610 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5611 | if (posted) |
| 5612 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5613 | |
| 5614 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5615 | } |
| 5616 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5617 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5618 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5619 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5620 | struct io_kiocb *req; |
| 5621 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5622 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5623 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5624 | if (sqe_addr != req->user_data) |
| 5625 | continue; |
| 5626 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5627 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5628 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | return -ENOENT; |
| 5632 | } |
| 5633 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5634 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5635 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5636 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5637 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5638 | return -EINVAL; |
| 5639 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5640 | sqe->poll_events) |
| 5641 | return -EINVAL; |
| 5642 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5643 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5644 | return 0; |
| 5645 | } |
| 5646 | |
| 5647 | /* |
| 5648 | * Find a running poll command that matches one specified in sqe->addr, |
| 5649 | * and remove it if found. |
| 5650 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5651 | 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] | 5652 | { |
| 5653 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5654 | int ret; |
| 5655 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5656 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5657 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5658 | spin_unlock_irq(&ctx->completion_lock); |
| 5659 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5660 | if (ret < 0) |
| 5661 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5662 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5663 | return 0; |
| 5664 | } |
| 5665 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5666 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5667 | void *key) |
| 5668 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5669 | struct io_kiocb *req = wait->private; |
| 5670 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5671 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5672 | 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] | 5673 | } |
| 5674 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5675 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5676 | struct poll_table_struct *p) |
| 5677 | { |
| 5678 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5679 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5680 | __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] | 5681 | } |
| 5682 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5683 | 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] | 5684 | { |
| 5685 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5686 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5687 | |
| 5688 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5689 | return -EINVAL; |
| 5690 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5691 | return -EINVAL; |
| 5692 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5693 | events = READ_ONCE(sqe->poll32_events); |
| 5694 | #ifdef __BIG_ENDIAN |
| 5695 | events = swahw32(events); |
| 5696 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5697 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5698 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5699 | return 0; |
| 5700 | } |
| 5701 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5702 | 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] | 5703 | { |
| 5704 | struct io_poll_iocb *poll = &req->poll; |
| 5705 | struct io_ring_ctx *ctx = req->ctx; |
| 5706 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5707 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5708 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5709 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5710 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5711 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5712 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5713 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5714 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5715 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5716 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5717 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5718 | spin_unlock_irq(&ctx->completion_lock); |
| 5719 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5720 | if (mask) { |
| 5721 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5722 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5723 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5724 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5725 | } |
| 5726 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5727 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5728 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5729 | struct io_timeout_data *data = container_of(timer, |
| 5730 | struct io_timeout_data, timer); |
| 5731 | struct io_kiocb *req = data->req; |
| 5732 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5733 | unsigned long flags; |
| 5734 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5735 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5736 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5737 | atomic_set(&req->ctx->cq_timeouts, |
| 5738 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5739 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5740 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5741 | io_commit_cqring(ctx); |
| 5742 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5743 | |
| 5744 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5745 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5746 | io_put_req(req); |
| 5747 | return HRTIMER_NORESTART; |
| 5748 | } |
| 5749 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5750 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5751 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5752 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5753 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5754 | struct io_kiocb *req; |
| 5755 | int ret = -ENOENT; |
| 5756 | |
| 5757 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5758 | if (user_data == req->user_data) { |
| 5759 | ret = 0; |
| 5760 | break; |
| 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5765 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5766 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5767 | io = req->async_data; |
| 5768 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5769 | if (ret == -1) |
| 5770 | return ERR_PTR(-EALREADY); |
| 5771 | list_del_init(&req->timeout.list); |
| 5772 | return req; |
| 5773 | } |
| 5774 | |
| 5775 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5776 | { |
| 5777 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5778 | |
| 5779 | if (IS_ERR(req)) |
| 5780 | return PTR_ERR(req); |
| 5781 | |
| 5782 | req_set_fail_links(req); |
| 5783 | io_cqring_fill_event(req, -ECANCELED); |
| 5784 | io_put_req_deferred(req, 1); |
| 5785 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5786 | } |
| 5787 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5788 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5789 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5790 | { |
| 5791 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5792 | struct io_timeout_data *data; |
| 5793 | |
| 5794 | if (IS_ERR(req)) |
| 5795 | return PTR_ERR(req); |
| 5796 | |
| 5797 | req->timeout.off = 0; /* noseq */ |
| 5798 | data = req->async_data; |
| 5799 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5800 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5801 | data->timer.function = io_timeout_fn; |
| 5802 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5803 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5804 | } |
| 5805 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5806 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5807 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5808 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5809 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5810 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5811 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5812 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5813 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5814 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5815 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5816 | return -EINVAL; |
| 5817 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5818 | tr->addr = READ_ONCE(sqe->addr); |
| 5819 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5820 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5821 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5822 | return -EINVAL; |
| 5823 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5824 | return -EFAULT; |
| 5825 | } else if (tr->flags) { |
| 5826 | /* timeout removal doesn't support flags */ |
| 5827 | return -EINVAL; |
| 5828 | } |
| 5829 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5830 | return 0; |
| 5831 | } |
| 5832 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5833 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5834 | { |
| 5835 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5836 | : HRTIMER_MODE_REL; |
| 5837 | } |
| 5838 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5839 | /* |
| 5840 | * Remove or update an existing timeout command |
| 5841 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5842 | 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] | 5843 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5844 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5845 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5846 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5847 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5848 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5849 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5850 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5851 | else |
| 5852 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5853 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5854 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5855 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5856 | io_commit_cqring(ctx); |
| 5857 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5858 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5859 | if (ret < 0) |
| 5860 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5861 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5862 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5863 | } |
| 5864 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5865 | 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] | 5866 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5867 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5868 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5869 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5870 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5871 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5872 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5873 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5874 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5875 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5876 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5877 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5878 | flags = READ_ONCE(sqe->timeout_flags); |
| 5879 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5880 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5881 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5882 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5883 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5884 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5885 | return -ENOMEM; |
| 5886 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5887 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5888 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5889 | |
| 5890 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5891 | return -EFAULT; |
| 5892 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5893 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5894 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5895 | return 0; |
| 5896 | } |
| 5897 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5898 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5899 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5900 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5901 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5902 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5903 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5904 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5905 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5906 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5907 | /* |
| 5908 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5909 | * timeout event to be satisfied. If it isn't set, then this is |
| 5910 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5911 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5912 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5913 | entry = ctx->timeout_list.prev; |
| 5914 | goto add; |
| 5915 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5916 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5917 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5918 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5919 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5920 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5921 | * This is safe because ->completion_lock is held, and submissions |
| 5922 | * and completions are never mixed in the same ->completion_lock section. |
| 5923 | */ |
| 5924 | ctx->cq_last_tm_flush = tail; |
| 5925 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5926 | /* |
| 5927 | * Insertion sort, ensuring the first entry in the list is always |
| 5928 | * the one we need first. |
| 5929 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5930 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5931 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5932 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5933 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5934 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5935 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5936 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5937 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5938 | break; |
| 5939 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5940 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5941 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5942 | data->timer.function = io_timeout_fn; |
| 5943 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5944 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5945 | return 0; |
| 5946 | } |
| 5947 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5948 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5949 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5950 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5951 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5952 | return req->user_data == (unsigned long) data; |
| 5953 | } |
| 5954 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5955 | 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] | 5956 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5957 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5958 | int ret = 0; |
| 5959 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5960 | 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] | 5961 | switch (cancel_ret) { |
| 5962 | case IO_WQ_CANCEL_OK: |
| 5963 | ret = 0; |
| 5964 | break; |
| 5965 | case IO_WQ_CANCEL_RUNNING: |
| 5966 | ret = -EALREADY; |
| 5967 | break; |
| 5968 | case IO_WQ_CANCEL_NOTFOUND: |
| 5969 | ret = -ENOENT; |
| 5970 | break; |
| 5971 | } |
| 5972 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5973 | return ret; |
| 5974 | } |
| 5975 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5976 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5977 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5978 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5979 | { |
| 5980 | unsigned long flags; |
| 5981 | int ret; |
| 5982 | |
| 5983 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5984 | if (ret != -ENOENT) { |
| 5985 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5986 | goto done; |
| 5987 | } |
| 5988 | |
| 5989 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5990 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5991 | if (ret != -ENOENT) |
| 5992 | goto done; |
| 5993 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5994 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5995 | if (!ret) |
| 5996 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5997 | io_cqring_fill_event(req, ret); |
| 5998 | io_commit_cqring(ctx); |
| 5999 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6000 | io_cqring_ev_posted(ctx); |
| 6001 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6002 | if (ret < 0) |
| 6003 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6004 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6005 | } |
| 6006 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6007 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6008 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6009 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6010 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6011 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6012 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6013 | return -EINVAL; |
| 6014 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6015 | return -EINVAL; |
| 6016 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6017 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6018 | return 0; |
| 6019 | } |
| 6020 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6021 | 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] | 6022 | { |
| 6023 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6024 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6025 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6026 | return 0; |
| 6027 | } |
| 6028 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6029 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6030 | const struct io_uring_sqe *sqe) |
| 6031 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 6032 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 6033 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6034 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6035 | return -EINVAL; |
| 6036 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6037 | return -EINVAL; |
| 6038 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6039 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6040 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6041 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6042 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6043 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6044 | return 0; |
| 6045 | } |
| 6046 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6047 | 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] | 6048 | { |
| 6049 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6050 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6051 | int ret; |
| 6052 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6053 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6054 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6055 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6056 | up.offset = req->rsrc_update.offset; |
| 6057 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6058 | |
| 6059 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6060 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6061 | mutex_unlock(&ctx->uring_lock); |
| 6062 | |
| 6063 | if (ret < 0) |
| 6064 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6065 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6066 | return 0; |
| 6067 | } |
| 6068 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6069 | 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] | 6070 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6071 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6072 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6073 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6074 | case IORING_OP_READV: |
| 6075 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6076 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6077 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6078 | case IORING_OP_WRITEV: |
| 6079 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6080 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6081 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6082 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6083 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6084 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6085 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6086 | case IORING_OP_FSYNC: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6087 | return io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6088 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6089 | return io_prep_sfr(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6090 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6091 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6092 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6093 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6094 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6095 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6096 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6097 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6098 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6099 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6100 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6101 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6102 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6103 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6104 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6105 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6106 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6107 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6108 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6109 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6110 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6111 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6112 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6113 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6114 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6115 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6116 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6117 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6118 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6119 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6120 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6121 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6122 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6123 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6124 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6125 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6126 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6127 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6128 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6129 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6130 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6131 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6132 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6133 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6134 | case IORING_OP_SHUTDOWN: |
| 6135 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6136 | case IORING_OP_RENAMEAT: |
| 6137 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6138 | case IORING_OP_UNLINKAT: |
| 6139 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6140 | } |
| 6141 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6142 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6143 | req->opcode); |
| 6144 | return-EINVAL; |
| 6145 | } |
| 6146 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6147 | static int io_req_defer_prep(struct io_kiocb *req, |
| 6148 | const struct io_uring_sqe *sqe) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6149 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6150 | if (!sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6151 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6152 | if (io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6153 | return -EAGAIN; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6154 | return io_req_prep(req, sqe); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6155 | } |
| 6156 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6157 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6158 | { |
| 6159 | struct io_kiocb *pos; |
| 6160 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6161 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6162 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6163 | io_for_each_link(pos, req) |
| 6164 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6165 | |
| 6166 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6167 | return total_submitted - nr_reqs; |
| 6168 | } |
| 6169 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6170 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6171 | { |
| 6172 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6173 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6174 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6175 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6176 | |
| 6177 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6178 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6179 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6180 | return 0; |
| 6181 | |
| 6182 | seq = io_get_sequence(req); |
| 6183 | /* Still a chance to pass the sequence check */ |
| 6184 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6185 | return 0; |
| 6186 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6187 | if (!req->async_data) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6188 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6189 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6190 | return ret; |
| 6191 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6192 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6193 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6194 | if (!de) |
| 6195 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6196 | |
| 6197 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6198 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6199 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6200 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6201 | io_queue_async_work(req); |
| 6202 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6206 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6207 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6208 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6209 | spin_unlock_irq(&ctx->completion_lock); |
| 6210 | return -EIOCBQUEUED; |
| 6211 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6212 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6213 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6214 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6215 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6216 | switch (req->opcode) { |
| 6217 | case IORING_OP_READV: |
| 6218 | case IORING_OP_READ_FIXED: |
| 6219 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6220 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6221 | break; |
| 6222 | case IORING_OP_RECVMSG: |
| 6223 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6224 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6225 | break; |
| 6226 | } |
| 6227 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6228 | } |
| 6229 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6230 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6231 | switch (req->opcode) { |
| 6232 | case IORING_OP_READV: |
| 6233 | case IORING_OP_READ_FIXED: |
| 6234 | case IORING_OP_READ: |
| 6235 | case IORING_OP_WRITEV: |
| 6236 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6237 | case IORING_OP_WRITE: { |
| 6238 | struct io_async_rw *io = req->async_data; |
| 6239 | if (io->free_iovec) |
| 6240 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6241 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6242 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6243 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6244 | case IORING_OP_SENDMSG: { |
| 6245 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6246 | |
| 6247 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6248 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6249 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6250 | case IORING_OP_SPLICE: |
| 6251 | case IORING_OP_TEE: |
| 6252 | io_put_file(req, req->splice.file_in, |
| 6253 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6254 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6255 | case IORING_OP_OPENAT: |
| 6256 | case IORING_OP_OPENAT2: |
| 6257 | if (req->open.filename) |
| 6258 | putname(req->open.filename); |
| 6259 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6260 | case IORING_OP_RENAMEAT: |
| 6261 | putname(req->rename.oldpath); |
| 6262 | putname(req->rename.newpath); |
| 6263 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6264 | case IORING_OP_UNLINKAT: |
| 6265 | putname(req->unlink.filename); |
| 6266 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6267 | } |
| 6268 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6269 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6270 | } |
| 6271 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6272 | 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] | 6273 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6274 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6275 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6276 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6277 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6278 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6279 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6280 | break; |
| 6281 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6282 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6283 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6284 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6285 | break; |
| 6286 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6287 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6288 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6289 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6290 | break; |
| 6291 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6292 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6293 | break; |
| 6294 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6295 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6296 | break; |
| 6297 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6298 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6299 | break; |
| 6300 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6301 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6302 | break; |
| 6303 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6304 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6305 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6306 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6307 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6308 | break; |
| 6309 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6310 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6311 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6312 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6313 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6314 | break; |
| 6315 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6316 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6317 | break; |
| 6318 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6319 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6320 | break; |
| 6321 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6322 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6323 | break; |
| 6324 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6325 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6326 | break; |
| 6327 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6328 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6329 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6330 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6331 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6332 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6333 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6334 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6335 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6336 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6337 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6338 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6339 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6340 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6341 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6342 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6343 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6344 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6345 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6346 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6347 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6348 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6349 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6350 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6351 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6352 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6353 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6354 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6355 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6356 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6357 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6358 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6359 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6360 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6361 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6362 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6363 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6364 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6365 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6366 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6367 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6368 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6369 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6370 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6371 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6372 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6373 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6374 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6375 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6376 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6377 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6378 | default: |
| 6379 | ret = -EINVAL; |
| 6380 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6384 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6385 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6386 | /* If the op doesn't have a file, we're not polling for it */ |
| 6387 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6388 | const bool in_async = io_wq_current_is_worker(); |
| 6389 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6390 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6391 | if (in_async) |
| 6392 | mutex_lock(&ctx->uring_lock); |
| 6393 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6394 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6395 | |
| 6396 | if (in_async) |
| 6397 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6398 | } |
| 6399 | |
| 6400 | return 0; |
| 6401 | } |
| 6402 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6403 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6404 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6405 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6406 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6407 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6408 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6409 | timeout = io_prep_linked_timeout(req); |
| 6410 | if (timeout) |
| 6411 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6412 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6413 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6414 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6415 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6416 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6417 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6418 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6419 | /* |
| 6420 | * We can get EAGAIN for polled IO even though we're |
| 6421 | * forcing a sync submission from here, since we can't |
| 6422 | * wait for request slots on the block side. |
| 6423 | */ |
| 6424 | if (ret != -EAGAIN) |
| 6425 | break; |
| 6426 | cond_resched(); |
| 6427 | } while (1); |
| 6428 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6429 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6430 | if (ret) { |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6431 | struct io_ring_ctx *lock_ctx = NULL; |
Xiaoguang Wang | dad1b12 | 2020-12-06 22:22:42 +0000 | [diff] [blame] | 6432 | |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6433 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 6434 | lock_ctx = req->ctx; |
| 6435 | |
| 6436 | /* |
| 6437 | * io_iopoll_complete() does not hold completion_lock to |
| 6438 | * complete polled io, so here for polled io, we can not call |
| 6439 | * io_req_complete() directly, otherwise there maybe concurrent |
| 6440 | * access to cqring, defer_list, etc, which is not safe. Given |
| 6441 | * that io_iopoll_complete() is always called under uring_lock, |
| 6442 | * so here for polled io, we also get uring_lock to complete |
| 6443 | * it. |
| 6444 | */ |
| 6445 | if (lock_ctx) |
| 6446 | mutex_lock(&lock_ctx->uring_lock); |
| 6447 | |
| 6448 | req_set_fail_links(req); |
| 6449 | io_req_complete(req, ret); |
| 6450 | |
| 6451 | if (lock_ctx) |
| 6452 | mutex_unlock(&lock_ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6453 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6454 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6455 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6456 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6457 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6458 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6459 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6460 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6461 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6462 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6463 | } |
| 6464 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6465 | static struct file *io_file_get(struct io_submit_state *state, |
| 6466 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6467 | { |
| 6468 | struct io_ring_ctx *ctx = req->ctx; |
| 6469 | struct file *file; |
| 6470 | |
| 6471 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6472 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6473 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6474 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6475 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6476 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6477 | } else { |
| 6478 | trace_io_uring_file_get(ctx, fd); |
| 6479 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6480 | } |
| 6481 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6482 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6483 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6484 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6485 | } |
| 6486 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6487 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6488 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6489 | struct io_timeout_data *data = container_of(timer, |
| 6490 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6491 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6492 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6493 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6494 | |
| 6495 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6496 | prev = req->timeout.head; |
| 6497 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6498 | |
| 6499 | /* |
| 6500 | * We don't expect the list to be empty, that will only happen if we |
| 6501 | * race with the completion of the linked work. |
| 6502 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6503 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6504 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6505 | else |
| 6506 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6507 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6508 | |
| 6509 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6510 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6511 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6512 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6513 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6514 | io_req_complete_post(req, -ETIME, 0); |
| 6515 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6516 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6517 | return HRTIMER_NORESTART; |
| 6518 | } |
| 6519 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6520 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6521 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6522 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6523 | * If the back reference is NULL, then our linked request finished |
| 6524 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6525 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6526 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6527 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6528 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6529 | data->timer.function = io_link_timeout_fn; |
| 6530 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6531 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6532 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6536 | { |
| 6537 | struct io_ring_ctx *ctx = req->ctx; |
| 6538 | |
| 6539 | spin_lock_irq(&ctx->completion_lock); |
| 6540 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6541 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6542 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6543 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6544 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6545 | } |
| 6546 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6547 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6548 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6549 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6550 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6551 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6552 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6553 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6554 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6555 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6556 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6557 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6558 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6559 | } |
| 6560 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6561 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6562 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6563 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6564 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6565 | int ret; |
| 6566 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6567 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6568 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6569 | req->work.identity->creds != current_cred()) |
| 6570 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6571 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6572 | 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] | 6573 | |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6574 | if (old_creds) |
| 6575 | revert_creds(old_creds); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6576 | |
| 6577 | /* |
| 6578 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6579 | * doesn't support non-blocking read/write attempts |
| 6580 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6581 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6582 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6583 | /* |
| 6584 | * Queued up for async execution, worker will release |
| 6585 | * submit reference when the iocb is actually submitted. |
| 6586 | */ |
| 6587 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6588 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6589 | } else if (likely(!ret)) { |
| 6590 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6591 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6592 | struct io_ring_ctx *ctx = req->ctx; |
| 6593 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6594 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6595 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6596 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6597 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6598 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6599 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6600 | } |
| 6601 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6602 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6603 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6604 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6605 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6606 | if (linked_timeout) |
| 6607 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6608 | } |
| 6609 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6610 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6611 | { |
| 6612 | int ret; |
| 6613 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6614 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6615 | if (ret) { |
| 6616 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6617 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6618 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6619 | io_put_req(req); |
| 6620 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6621 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6622 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6623 | if (!req->async_data) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6624 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6625 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6626 | goto fail_req; |
| 6627 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6628 | io_queue_async_work(req); |
| 6629 | } else { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6630 | if (sqe) { |
| 6631 | ret = io_req_prep(req, sqe); |
| 6632 | if (unlikely(ret)) |
| 6633 | goto fail_req; |
| 6634 | } |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6635 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6636 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6637 | } |
| 6638 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6639 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6640 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6641 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6642 | io_put_req(req); |
| 6643 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6644 | } else |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6645 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6646 | } |
| 6647 | |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6648 | struct io_submit_link { |
| 6649 | struct io_kiocb *head; |
| 6650 | struct io_kiocb *last; |
| 6651 | }; |
| 6652 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6653 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6654 | struct io_submit_link *link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6655 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6656 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6657 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6658 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6659 | /* |
| 6660 | * If we already have a head request, queue this one for async |
| 6661 | * submittal once the head completes. If we don't have a head but |
| 6662 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6663 | * submitted sync once the chain is complete. If none of those |
| 6664 | * conditions are true (normal request), then just queue it. |
| 6665 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6666 | if (link->head) { |
| 6667 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6668 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6669 | /* |
| 6670 | * Taking sequential execution of a link, draining both sides |
| 6671 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6672 | * requests in the link. So, it drains the head and the |
| 6673 | * next after the link request. The last one is done via |
| 6674 | * drain_next flag to persist the effect across calls. |
| 6675 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6676 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6677 | head->flags |= REQ_F_IO_DRAIN; |
| 6678 | ctx->drain_next = 1; |
| 6679 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6680 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6681 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6682 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6683 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6684 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6685 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6686 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6687 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6688 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6689 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6690 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6691 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6692 | io_queue_link_head(head); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6693 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6694 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6695 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6696 | if (unlikely(ctx->drain_next)) { |
| 6697 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6698 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6699 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6700 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6701 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6702 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6703 | req->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6704 | link->head = req; |
| 6705 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6706 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6707 | io_queue_sqe(req, sqe); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6708 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6709 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6710 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6711 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6712 | } |
| 6713 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6714 | /* |
| 6715 | * Batched submission is done, ensure local IO is flushed out. |
| 6716 | */ |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6717 | static void io_submit_state_end(struct io_submit_state *state, |
| 6718 | struct io_ring_ctx *ctx) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6719 | { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6720 | if (state->comp.nr) |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6721 | io_submit_flush_completions(&state->comp, ctx); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6722 | if (state->plug_started) |
| 6723 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6724 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | /* |
| 6728 | * Start submission side cache. |
| 6729 | */ |
| 6730 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6731 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6732 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6733 | state->plug_started = false; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6734 | state->ios_left = max_ios; |
| 6735 | } |
| 6736 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6737 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6738 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6739 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6740 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6741 | /* |
| 6742 | * Ensure any loads from the SQEs are done at this point, |
| 6743 | * since once we write the new head, the application could |
| 6744 | * write new data to them. |
| 6745 | */ |
| 6746 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6750 | * 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] | 6751 | * that is mapped by userspace. This means that care needs to be taken to |
| 6752 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6753 | * being a good citizen. If members of the sqe are validated and then later |
| 6754 | * used, it's important that those reads are done through READ_ONCE() to |
| 6755 | * prevent a re-load down the line. |
| 6756 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6757 | 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] | 6758 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6759 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6760 | unsigned head; |
| 6761 | |
| 6762 | /* |
| 6763 | * The cached sq head (or cq tail) serves two purposes: |
| 6764 | * |
| 6765 | * 1) allows us to batch the cost of updating the user visible |
| 6766 | * head updates. |
| 6767 | * 2) allows the kernel side to track the head on its own, even |
| 6768 | * though the application is the one updating it. |
| 6769 | */ |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6770 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6771 | if (likely(head < ctx->sq_entries)) |
| 6772 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6773 | |
| 6774 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6775 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6776 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6777 | return NULL; |
| 6778 | } |
| 6779 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6780 | /* |
| 6781 | * Check SQE restrictions (opcode and flags). |
| 6782 | * |
| 6783 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6784 | */ |
| 6785 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6786 | struct io_kiocb *req, |
| 6787 | unsigned int sqe_flags) |
| 6788 | { |
| 6789 | if (!ctx->restricted) |
| 6790 | return true; |
| 6791 | |
| 6792 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6793 | return false; |
| 6794 | |
| 6795 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6796 | ctx->restrictions.sqe_flags_required) |
| 6797 | return false; |
| 6798 | |
| 6799 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6800 | ctx->restrictions.sqe_flags_required)) |
| 6801 | return false; |
| 6802 | |
| 6803 | return true; |
| 6804 | } |
| 6805 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6806 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6807 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6808 | IOSQE_BUFFER_SELECT) |
| 6809 | |
| 6810 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6811 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6812 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6813 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6814 | unsigned int sqe_flags; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6815 | int id, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6816 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6817 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6818 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6819 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6820 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6821 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6822 | req->file = NULL; |
| 6823 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6824 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6825 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6826 | /* one is dropped after submission, the other at completion */ |
| 6827 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6828 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6829 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6830 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6831 | /* enforce forwards compatibility on users */ |
| 6832 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6833 | return -EINVAL; |
| 6834 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6835 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6836 | return -EINVAL; |
| 6837 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6838 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6839 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6840 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6841 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6842 | return -EACCES; |
| 6843 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6844 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6845 | !io_op_defs[req->opcode].buffer_select) |
| 6846 | return -EOPNOTSUPP; |
| 6847 | |
| 6848 | id = READ_ONCE(sqe->personality); |
| 6849 | if (id) { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6850 | struct io_identity *iod; |
| 6851 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6852 | iod = idr_find(&ctx->personality_idr, id); |
| 6853 | if (unlikely(!iod)) |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6854 | return -EINVAL; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6855 | refcount_inc(&iod->count); |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6856 | |
| 6857 | __io_req_init_async(req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6858 | get_cred(iod->creds); |
| 6859 | req->work.identity = iod; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6860 | req->work.flags |= IO_WQ_WORK_CREDS; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6861 | } |
| 6862 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6863 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6864 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6865 | /* |
| 6866 | * Plug now if we have more than 1 IO left after this, and the target |
| 6867 | * is potentially a read/write to block based storage. |
| 6868 | */ |
| 6869 | if (!state->plug_started && state->ios_left > 1 && |
| 6870 | io_op_defs[req->opcode].plug) { |
| 6871 | blk_start_plug(&state->plug); |
| 6872 | state->plug_started = true; |
| 6873 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6874 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6875 | if (io_op_defs[req->opcode].needs_file) { |
| 6876 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6877 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6878 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6879 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6880 | ret = -EBADF; |
| 6881 | } |
| 6882 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6883 | state->ios_left--; |
| 6884 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6885 | } |
| 6886 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6887 | 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] | 6888 | { |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6889 | struct io_submit_link link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6890 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6891 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6892 | /* 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] | 6893 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6894 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6895 | return -EBUSY; |
| 6896 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6897 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6898 | /* make sure SQ entry isn't read before tail */ |
| 6899 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6900 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6901 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6902 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6903 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6904 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6905 | refcount_add(nr, ¤t->usage); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6906 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6907 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6908 | link.head = NULL; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6909 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6910 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6911 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6912 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6913 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6914 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6915 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6916 | if (unlikely(!req)) { |
| 6917 | if (!submitted) |
| 6918 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6919 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6920 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6921 | sqe = io_get_sqe(ctx); |
| 6922 | if (unlikely(!sqe)) { |
| 6923 | kmem_cache_free(req_cachep, req); |
| 6924 | break; |
| 6925 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6926 | /* will complete beyond this point, count as submitted */ |
| 6927 | submitted++; |
| 6928 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6929 | err = io_init_req(ctx, req, sqe); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6930 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6931 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6932 | io_put_req(req); |
| 6933 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6934 | break; |
| 6935 | } |
| 6936 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6937 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 2d7e935 | 2021-01-19 13:32:37 +0000 | [diff] [blame] | 6938 | true, ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6939 | err = io_submit_sqe(req, sqe, &link); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6940 | if (err) |
| 6941 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6942 | } |
| 6943 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6944 | if (unlikely(submitted != nr)) { |
| 6945 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6946 | struct io_uring_task *tctx = current->io_uring; |
| 6947 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6948 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6949 | percpu_ref_put_many(&ctx->refs, unused); |
| 6950 | percpu_counter_sub(&tctx->inflight, unused); |
| 6951 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6952 | } |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6953 | if (link.head) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6954 | io_queue_link_head(link.head); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6955 | io_submit_state_end(&ctx->submit_state, ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6956 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6957 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6958 | io_commit_sqring(ctx); |
| 6959 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6960 | return submitted; |
| 6961 | } |
| 6962 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6963 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6964 | { |
| 6965 | /* Tell userspace we may need a wakeup call */ |
| 6966 | spin_lock_irq(&ctx->completion_lock); |
| 6967 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6968 | spin_unlock_irq(&ctx->completion_lock); |
| 6969 | } |
| 6970 | |
| 6971 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6972 | { |
| 6973 | spin_lock_irq(&ctx->completion_lock); |
| 6974 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6975 | spin_unlock_irq(&ctx->completion_lock); |
| 6976 | } |
| 6977 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6978 | 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] | 6979 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6980 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6981 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6982 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6983 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6984 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6985 | if (cap_entries && to_submit > 8) |
| 6986 | to_submit = 8; |
| 6987 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6988 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6989 | unsigned nr_events = 0; |
| 6990 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6991 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6992 | if (!list_empty(&ctx->iopoll_list)) |
| 6993 | io_do_iopoll(ctx, &nr_events, 0); |
| 6994 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6995 | if (to_submit && !ctx->sqo_dead && |
| 6996 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6997 | ret = io_submit_sqes(ctx, to_submit); |
| 6998 | mutex_unlock(&ctx->uring_lock); |
| 6999 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7000 | |
| 7001 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7002 | wake_up(&ctx->sqo_sq_wait); |
| 7003 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7004 | return ret; |
| 7005 | } |
| 7006 | |
| 7007 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7008 | { |
| 7009 | struct io_ring_ctx *ctx; |
| 7010 | unsigned sq_thread_idle = 0; |
| 7011 | |
| 7012 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7013 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 7014 | sq_thread_idle = ctx->sq_thread_idle; |
| 7015 | } |
| 7016 | |
| 7017 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7018 | } |
| 7019 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7020 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7021 | { |
| 7022 | struct io_ring_ctx *ctx; |
| 7023 | |
| 7024 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7025 | 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] | 7026 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7027 | complete(&ctx->sq_thread_comp); |
| 7028 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7029 | |
| 7030 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7031 | } |
| 7032 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7033 | static int io_sq_thread(void *data) |
| 7034 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7035 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7036 | struct files_struct *old_files = current->files; |
| 7037 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7038 | const struct cred *old_cred = NULL; |
| 7039 | struct io_sq_data *sqd = data; |
| 7040 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7041 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7042 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7043 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7044 | task_lock(current); |
| 7045 | current->files = NULL; |
| 7046 | current->nsproxy = NULL; |
| 7047 | task_unlock(current); |
| 7048 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7049 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7050 | int ret; |
| 7051 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7052 | |
| 7053 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7054 | * Any changes to the sqd lists are synchronized through the |
| 7055 | * kthread parking. This synchronizes the thread vs users, |
| 7056 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7057 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7058 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7059 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7060 | /* |
| 7061 | * When sq thread is unparked, in case the previous park operation |
| 7062 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7063 | * to be stopped, so here needs to have a check. |
| 7064 | */ |
| 7065 | if (kthread_should_stop()) |
| 7066 | break; |
| 7067 | } |
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 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7070 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7071 | timeout = jiffies + sqd->sq_thread_idle; |
| 7072 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7073 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7074 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7075 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7076 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7077 | if (current->cred != ctx->creds) { |
| 7078 | if (old_cred) |
| 7079 | revert_creds(old_cred); |
| 7080 | old_cred = override_creds(ctx->creds); |
| 7081 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7082 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7083 | #ifdef CONFIG_AUDIT |
| 7084 | current->loginuid = ctx->loginuid; |
| 7085 | current->sessionid = ctx->sessionid; |
| 7086 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7087 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7088 | ret = __io_sq_thread(ctx, cap_entries); |
| 7089 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7090 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7091 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7092 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7093 | } |
| 7094 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7095 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7096 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7097 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7098 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7099 | if (sqt_spin) |
| 7100 | timeout = jiffies + sqd->sq_thread_idle; |
| 7101 | continue; |
| 7102 | } |
| 7103 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7104 | needs_sched = true; |
| 7105 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7106 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7107 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7108 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7109 | needs_sched = false; |
| 7110 | break; |
| 7111 | } |
| 7112 | if (io_sqring_entries(ctx)) { |
| 7113 | needs_sched = false; |
| 7114 | break; |
| 7115 | } |
| 7116 | } |
| 7117 | |
Hao Xu | 8b28fdf | 2021-01-31 22:39:04 +0800 | [diff] [blame] | 7118 | if (needs_sched && !kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7119 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7120 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7121 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7122 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7123 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7124 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7125 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7126 | |
| 7127 | finish_wait(&sqd->wait, &wait); |
| 7128 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7129 | } |
| 7130 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7131 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7132 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7133 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7134 | if (cur_css) |
| 7135 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7136 | if (old_cred) |
| 7137 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7138 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7139 | task_lock(current); |
| 7140 | current->files = old_files; |
| 7141 | current->nsproxy = old_nsproxy; |
| 7142 | task_unlock(current); |
| 7143 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7144 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7145 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7146 | return 0; |
| 7147 | } |
| 7148 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7149 | struct io_wait_queue { |
| 7150 | struct wait_queue_entry wq; |
| 7151 | struct io_ring_ctx *ctx; |
| 7152 | unsigned to_wait; |
| 7153 | unsigned nr_timeouts; |
| 7154 | }; |
| 7155 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7156 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7157 | { |
| 7158 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7159 | |
| 7160 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7161 | * 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] | 7162 | * started waiting. For timeouts, we always want to return to userspace, |
| 7163 | * regardless of event count. |
| 7164 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7165 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7166 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7167 | } |
| 7168 | |
| 7169 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7170 | int wake_flags, void *key) |
| 7171 | { |
| 7172 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7173 | wq); |
| 7174 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7175 | /* |
| 7176 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7177 | * the task, and the next invocation will do it. |
| 7178 | */ |
| 7179 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7180 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7181 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7182 | } |
| 7183 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7184 | static int io_run_task_work_sig(void) |
| 7185 | { |
| 7186 | if (io_run_task_work()) |
| 7187 | return 1; |
| 7188 | if (!signal_pending(current)) |
| 7189 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7190 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7191 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7192 | return -EINTR; |
| 7193 | } |
| 7194 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7195 | /* when returns >0, the caller should retry */ |
| 7196 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7197 | struct io_wait_queue *iowq, |
| 7198 | signed long *timeout) |
| 7199 | { |
| 7200 | int ret; |
| 7201 | |
| 7202 | /* make sure we run task_work before checking for signals */ |
| 7203 | ret = io_run_task_work_sig(); |
| 7204 | if (ret || io_should_wake(iowq)) |
| 7205 | return ret; |
| 7206 | /* let the caller flush overflows, retry */ |
| 7207 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7208 | return 1; |
| 7209 | |
| 7210 | *timeout = schedule_timeout(*timeout); |
| 7211 | return !*timeout ? -ETIME : 1; |
| 7212 | } |
| 7213 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7214 | /* |
| 7215 | * Wait until events become available, if we don't already have some. The |
| 7216 | * application must reap them itself, as they reside on the shared cq ring. |
| 7217 | */ |
| 7218 | 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] | 7219 | const sigset_t __user *sig, size_t sigsz, |
| 7220 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7221 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7222 | struct io_wait_queue iowq = { |
| 7223 | .wq = { |
| 7224 | .private = current, |
| 7225 | .func = io_wake_function, |
| 7226 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7227 | }, |
| 7228 | .ctx = ctx, |
| 7229 | .to_wait = min_events, |
| 7230 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7231 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7232 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7233 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7234 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7235 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7236 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7237 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7238 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7239 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7240 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7241 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7242 | |
| 7243 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7244 | #ifdef CONFIG_COMPAT |
| 7245 | if (in_compat_syscall()) |
| 7246 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7247 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7248 | else |
| 7249 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7250 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7251 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7252 | if (ret) |
| 7253 | return ret; |
| 7254 | } |
| 7255 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7256 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7257 | struct timespec64 ts; |
| 7258 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7259 | if (get_timespec64(&ts, uts)) |
| 7260 | return -EFAULT; |
| 7261 | timeout = timespec64_to_jiffies(&ts); |
| 7262 | } |
| 7263 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7264 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7265 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7266 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7267 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7268 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7269 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7270 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7271 | finish_wait(&ctx->wait, &iowq.wq); |
| 7272 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7273 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7274 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7275 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7276 | 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] | 7277 | } |
| 7278 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7279 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7280 | { |
| 7281 | #if defined(CONFIG_UNIX) |
| 7282 | if (ctx->ring_sock) { |
| 7283 | struct sock *sock = ctx->ring_sock->sk; |
| 7284 | struct sk_buff *skb; |
| 7285 | |
| 7286 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7287 | kfree_skb(skb); |
| 7288 | } |
| 7289 | #else |
| 7290 | int i; |
| 7291 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7292 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7293 | struct file *file; |
| 7294 | |
| 7295 | file = io_file_from_index(ctx, i); |
| 7296 | if (file) |
| 7297 | fput(file); |
| 7298 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7299 | #endif |
| 7300 | } |
| 7301 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7302 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7303 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7304 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7305 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7306 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7307 | complete(&data->done); |
| 7308 | } |
| 7309 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7310 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7311 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7312 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7315 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7316 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7317 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7318 | } |
| 7319 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7320 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 7321 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7322 | struct fixed_rsrc_ref_node *ref_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7323 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7324 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7325 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7326 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7327 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7328 | percpu_ref_get(&rsrc_data->refs); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7329 | } |
| 7330 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7331 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 7332 | struct io_ring_ctx *ctx, |
| 7333 | struct fixed_rsrc_ref_node *backup_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7334 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7335 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7336 | int ret; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7337 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7338 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7339 | ref_node = data->node; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7340 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7341 | if (ref_node) |
| 7342 | percpu_ref_kill(&ref_node->refs); |
| 7343 | |
| 7344 | percpu_ref_kill(&data->refs); |
| 7345 | |
| 7346 | /* wait for all refs nodes to complete */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7347 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7348 | do { |
| 7349 | ret = wait_for_completion_interruptible(&data->done); |
| 7350 | if (!ret) |
| 7351 | break; |
| 7352 | ret = io_run_task_work_sig(); |
| 7353 | if (ret < 0) { |
| 7354 | percpu_ref_resurrect(&data->refs); |
| 7355 | reinit_completion(&data->done); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7356 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7357 | return ret; |
| 7358 | } |
| 7359 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7360 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7361 | destroy_fixed_rsrc_ref_node(backup_node); |
| 7362 | return 0; |
| 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; |
| 7393 | struct fixed_rsrc_ref_node *backup_node; |
| 7394 | unsigned nr_tables, i; |
| 7395 | int ret; |
| 7396 | |
| 7397 | if (!data) |
| 7398 | return -ENXIO; |
| 7399 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 7400 | if (!backup_node) |
| 7401 | return -ENOMEM; |
| 7402 | init_fixed_file_ref_node(ctx, backup_node); |
| 7403 | |
| 7404 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); |
| 7405 | if (ret) |
| 7406 | return ret; |
| 7407 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7408 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7409 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7410 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7411 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7412 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7413 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7414 | ctx->nr_user_files = 0; |
| 7415 | return 0; |
| 7416 | } |
| 7417 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7418 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7419 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7420 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7421 | /* |
| 7422 | * The park is a bit of a work-around, without it we get |
| 7423 | * warning spews on shutdown with SQPOLL set and affinity |
| 7424 | * set to a single CPU. |
| 7425 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7426 | if (sqd->thread) { |
| 7427 | kthread_park(sqd->thread); |
| 7428 | kthread_stop(sqd->thread); |
| 7429 | } |
| 7430 | |
| 7431 | kfree(sqd); |
| 7432 | } |
| 7433 | } |
| 7434 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7435 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7436 | { |
| 7437 | struct io_ring_ctx *ctx_attach; |
| 7438 | struct io_sq_data *sqd; |
| 7439 | struct fd f; |
| 7440 | |
| 7441 | f = fdget(p->wq_fd); |
| 7442 | if (!f.file) |
| 7443 | return ERR_PTR(-ENXIO); |
| 7444 | if (f.file->f_op != &io_uring_fops) { |
| 7445 | fdput(f); |
| 7446 | return ERR_PTR(-EINVAL); |
| 7447 | } |
| 7448 | |
| 7449 | ctx_attach = f.file->private_data; |
| 7450 | sqd = ctx_attach->sq_data; |
| 7451 | if (!sqd) { |
| 7452 | fdput(f); |
| 7453 | return ERR_PTR(-EINVAL); |
| 7454 | } |
| 7455 | |
| 7456 | refcount_inc(&sqd->refs); |
| 7457 | fdput(f); |
| 7458 | return sqd; |
| 7459 | } |
| 7460 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7461 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7462 | { |
| 7463 | struct io_sq_data *sqd; |
| 7464 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7465 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7466 | return io_attach_sq_data(p); |
| 7467 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7468 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7469 | if (!sqd) |
| 7470 | return ERR_PTR(-ENOMEM); |
| 7471 | |
| 7472 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7473 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7474 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7475 | mutex_init(&sqd->ctx_lock); |
| 7476 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7477 | init_waitqueue_head(&sqd->wait); |
| 7478 | return sqd; |
| 7479 | } |
| 7480 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7481 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7482 | __releases(&sqd->lock) |
| 7483 | { |
| 7484 | if (!sqd->thread) |
| 7485 | return; |
| 7486 | kthread_unpark(sqd->thread); |
| 7487 | mutex_unlock(&sqd->lock); |
| 7488 | } |
| 7489 | |
| 7490 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7491 | __acquires(&sqd->lock) |
| 7492 | { |
| 7493 | if (!sqd->thread) |
| 7494 | return; |
| 7495 | mutex_lock(&sqd->lock); |
| 7496 | kthread_park(sqd->thread); |
| 7497 | } |
| 7498 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7499 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7500 | { |
| 7501 | struct io_sq_data *sqd = ctx->sq_data; |
| 7502 | |
| 7503 | if (sqd) { |
| 7504 | if (sqd->thread) { |
| 7505 | /* |
| 7506 | * We may arrive here from the error branch in |
| 7507 | * io_sq_offload_create() where the kthread is created |
| 7508 | * without being waked up, thus wake it up now to make |
| 7509 | * sure the wait will complete. |
| 7510 | */ |
| 7511 | wake_up_process(sqd->thread); |
| 7512 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7513 | |
| 7514 | io_sq_thread_park(sqd); |
| 7515 | } |
| 7516 | |
| 7517 | mutex_lock(&sqd->ctx_lock); |
| 7518 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7519 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7520 | mutex_unlock(&sqd->ctx_lock); |
| 7521 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7522 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7523 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7524 | |
| 7525 | io_put_sq_data(sqd); |
| 7526 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7527 | } |
| 7528 | } |
| 7529 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7530 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7531 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7532 | io_sq_thread_stop(ctx); |
| 7533 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7534 | if (ctx->io_wq) { |
| 7535 | io_wq_destroy(ctx->io_wq); |
| 7536 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7537 | } |
| 7538 | } |
| 7539 | |
| 7540 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7541 | /* |
| 7542 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7543 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7544 | * loops in the file referencing. |
| 7545 | */ |
| 7546 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7547 | { |
| 7548 | struct sock *sk = ctx->ring_sock->sk; |
| 7549 | struct scm_fp_list *fpl; |
| 7550 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7551 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7552 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7553 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7554 | if (!fpl) |
| 7555 | return -ENOMEM; |
| 7556 | |
| 7557 | skb = alloc_skb(0, GFP_KERNEL); |
| 7558 | if (!skb) { |
| 7559 | kfree(fpl); |
| 7560 | return -ENOMEM; |
| 7561 | } |
| 7562 | |
| 7563 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7564 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7565 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7566 | fpl->user = get_uid(ctx->user); |
| 7567 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7568 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7569 | |
| 7570 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7571 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7572 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7573 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7574 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7575 | } |
| 7576 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7577 | if (nr_files) { |
| 7578 | fpl->max = SCM_MAX_FD; |
| 7579 | fpl->count = nr_files; |
| 7580 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7581 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7582 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7583 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7584 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7585 | for (i = 0; i < nr_files; i++) |
| 7586 | fput(fpl->fp[i]); |
| 7587 | } else { |
| 7588 | kfree_skb(skb); |
| 7589 | kfree(fpl); |
| 7590 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7591 | |
| 7592 | return 0; |
| 7593 | } |
| 7594 | |
| 7595 | /* |
| 7596 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7597 | * causes regular reference counting to break down. We rely on the UNIX |
| 7598 | * garbage collection to take care of this problem for us. |
| 7599 | */ |
| 7600 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7601 | { |
| 7602 | unsigned left, total; |
| 7603 | int ret = 0; |
| 7604 | |
| 7605 | total = 0; |
| 7606 | left = ctx->nr_user_files; |
| 7607 | while (left) { |
| 7608 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7609 | |
| 7610 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7611 | if (ret) |
| 7612 | break; |
| 7613 | left -= this_files; |
| 7614 | total += this_files; |
| 7615 | } |
| 7616 | |
| 7617 | if (!ret) |
| 7618 | return 0; |
| 7619 | |
| 7620 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7621 | struct file *file = io_file_from_index(ctx, total); |
| 7622 | |
| 7623 | if (file) |
| 7624 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7625 | total++; |
| 7626 | } |
| 7627 | |
| 7628 | return ret; |
| 7629 | } |
| 7630 | #else |
| 7631 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7632 | { |
| 7633 | return 0; |
| 7634 | } |
| 7635 | #endif |
| 7636 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7637 | 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] | 7638 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7639 | { |
| 7640 | int i; |
| 7641 | |
| 7642 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7643 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7644 | unsigned this_files; |
| 7645 | |
| 7646 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7647 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7648 | GFP_KERNEL); |
| 7649 | if (!table->files) |
| 7650 | break; |
| 7651 | nr_files -= this_files; |
| 7652 | } |
| 7653 | |
| 7654 | if (i == nr_tables) |
| 7655 | return 0; |
| 7656 | |
| 7657 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7658 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7659 | kfree(table->files); |
| 7660 | } |
| 7661 | return 1; |
| 7662 | } |
| 7663 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7664 | 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] | 7665 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7666 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7667 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7668 | struct sock *sock = ctx->ring_sock->sk; |
| 7669 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7670 | struct sk_buff *skb; |
| 7671 | int i; |
| 7672 | |
| 7673 | __skb_queue_head_init(&list); |
| 7674 | |
| 7675 | /* |
| 7676 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7677 | * remove this entry and rearrange the file array. |
| 7678 | */ |
| 7679 | skb = skb_dequeue(head); |
| 7680 | while (skb) { |
| 7681 | struct scm_fp_list *fp; |
| 7682 | |
| 7683 | fp = UNIXCB(skb).fp; |
| 7684 | for (i = 0; i < fp->count; i++) { |
| 7685 | int left; |
| 7686 | |
| 7687 | if (fp->fp[i] != file) |
| 7688 | continue; |
| 7689 | |
| 7690 | unix_notinflight(fp->user, fp->fp[i]); |
| 7691 | left = fp->count - 1 - i; |
| 7692 | if (left) { |
| 7693 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7694 | left * sizeof(struct file *)); |
| 7695 | } |
| 7696 | fp->count--; |
| 7697 | if (!fp->count) { |
| 7698 | kfree_skb(skb); |
| 7699 | skb = NULL; |
| 7700 | } else { |
| 7701 | __skb_queue_tail(&list, skb); |
| 7702 | } |
| 7703 | fput(file); |
| 7704 | file = NULL; |
| 7705 | break; |
| 7706 | } |
| 7707 | |
| 7708 | if (!file) |
| 7709 | break; |
| 7710 | |
| 7711 | __skb_queue_tail(&list, skb); |
| 7712 | |
| 7713 | skb = skb_dequeue(head); |
| 7714 | } |
| 7715 | |
| 7716 | if (skb_peek(&list)) { |
| 7717 | spin_lock_irq(&head->lock); |
| 7718 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7719 | __skb_queue_tail(head, skb); |
| 7720 | spin_unlock_irq(&head->lock); |
| 7721 | } |
| 7722 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7723 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7724 | #endif |
| 7725 | } |
| 7726 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7727 | 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] | 7728 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7729 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7730 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7731 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7732 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7733 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7734 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7735 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7736 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7737 | } |
| 7738 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7739 | percpu_ref_exit(&ref_node->refs); |
| 7740 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7741 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7742 | } |
| 7743 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7744 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7745 | { |
| 7746 | struct io_ring_ctx *ctx; |
| 7747 | struct llist_node *node; |
| 7748 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7749 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7750 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7751 | |
| 7752 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7753 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7754 | struct llist_node *next = node->next; |
| 7755 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7756 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7757 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7758 | node = next; |
| 7759 | } |
| 7760 | } |
| 7761 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7762 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7763 | unsigned i) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7764 | { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7765 | struct fixed_rsrc_table *table; |
| 7766 | |
| 7767 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7768 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7769 | } |
| 7770 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7771 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7772 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7773 | struct fixed_rsrc_ref_node *ref_node; |
| 7774 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7775 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7776 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7777 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7778 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7779 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7780 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7781 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7782 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7783 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7784 | ref_node->done = true; |
| 7785 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7786 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7787 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7788 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7789 | /* recycle ref nodes in order */ |
| 7790 | if (!ref_node->done) |
| 7791 | break; |
| 7792 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7793 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7794 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7795 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7796 | |
| 7797 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7798 | delay = 0; |
| 7799 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7800 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7801 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7802 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7803 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7804 | } |
| 7805 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7806 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7807 | struct io_ring_ctx *ctx) |
| 7808 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7809 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7810 | |
| 7811 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7812 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7813 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7814 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7815 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7816 | 0, GFP_KERNEL)) { |
| 7817 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7818 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7819 | } |
| 7820 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7821 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7822 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7823 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7824 | } |
| 7825 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7826 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7827 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7828 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7829 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7830 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7831 | } |
| 7832 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7833 | 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] | 7834 | { |
| 7835 | percpu_ref_exit(&ref_node->refs); |
| 7836 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7837 | } |
| 7838 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7839 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7840 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7841 | unsigned nr_args) |
| 7842 | { |
| 7843 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7844 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7845 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7846 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7847 | struct fixed_rsrc_ref_node *ref_node; |
| 7848 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7849 | |
| 7850 | if (ctx->file_data) |
| 7851 | return -EBUSY; |
| 7852 | if (!nr_args) |
| 7853 | return -EINVAL; |
| 7854 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7855 | return -EMFILE; |
| 7856 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7857 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7858 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7859 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7860 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7861 | |
| 7862 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7863 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7864 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7865 | if (!file_data->table) |
| 7866 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7867 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7868 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7869 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7870 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7871 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7872 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7873 | ret = -EFAULT; |
| 7874 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7875 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7876 | /* allow sparse sets */ |
| 7877 | if (fd == -1) |
| 7878 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7879 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7880 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7881 | ret = -EBADF; |
| 7882 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7883 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7884 | |
| 7885 | /* |
| 7886 | * Don't allow io_uring instances to be registered. If UNIX |
| 7887 | * isn't enabled, then this causes a reference cycle and this |
| 7888 | * instance can never get freed. If UNIX is enabled we'll |
| 7889 | * handle it just fine, but there's still no point in allowing |
| 7890 | * a ring fd as it doesn't support regular read/write anyway. |
| 7891 | */ |
| 7892 | if (file->f_op == &io_uring_fops) { |
| 7893 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7894 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7895 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7896 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7897 | } |
| 7898 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7899 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7900 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7901 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7902 | return ret; |
| 7903 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7904 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7905 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7906 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7907 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7908 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7909 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7910 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7911 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7912 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7913 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7914 | out_fput: |
| 7915 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7916 | file = io_file_from_index(ctx, i); |
| 7917 | if (file) |
| 7918 | fput(file); |
| 7919 | } |
| 7920 | for (i = 0; i < nr_tables; i++) |
| 7921 | kfree(file_data->table[i].files); |
| 7922 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7923 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7924 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7925 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7926 | return ret; |
| 7927 | } |
| 7928 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7929 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7930 | int index) |
| 7931 | { |
| 7932 | #if defined(CONFIG_UNIX) |
| 7933 | struct sock *sock = ctx->ring_sock->sk; |
| 7934 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7935 | struct sk_buff *skb; |
| 7936 | |
| 7937 | /* |
| 7938 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7939 | * file set. If there's no room, fall back to allocating a new skb |
| 7940 | * and filling it in. |
| 7941 | */ |
| 7942 | spin_lock_irq(&head->lock); |
| 7943 | skb = skb_peek(head); |
| 7944 | if (skb) { |
| 7945 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7946 | |
| 7947 | if (fpl->count < SCM_MAX_FD) { |
| 7948 | __skb_unlink(skb, head); |
| 7949 | spin_unlock_irq(&head->lock); |
| 7950 | fpl->fp[fpl->count] = get_file(file); |
| 7951 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7952 | fpl->count++; |
| 7953 | spin_lock_irq(&head->lock); |
| 7954 | __skb_queue_head(head, skb); |
| 7955 | } else { |
| 7956 | skb = NULL; |
| 7957 | } |
| 7958 | } |
| 7959 | spin_unlock_irq(&head->lock); |
| 7960 | |
| 7961 | if (skb) { |
| 7962 | fput(file); |
| 7963 | return 0; |
| 7964 | } |
| 7965 | |
| 7966 | return __io_sqe_files_scm(ctx, 1, index); |
| 7967 | #else |
| 7968 | return 0; |
| 7969 | #endif |
| 7970 | } |
| 7971 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7972 | 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] | 7973 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7974 | struct io_rsrc_put *prsrc; |
| 7975 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7976 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7977 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7978 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7979 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7980 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7981 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7982 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7983 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7984 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7985 | } |
| 7986 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7987 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7988 | struct file *file) |
| 7989 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7990 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7993 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7994 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7995 | unsigned nr_args) |
| 7996 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7997 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7998 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7999 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8000 | __s32 __user *fds; |
| 8001 | int fd, i, err; |
| 8002 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8003 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8004 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8005 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8006 | return -EOVERFLOW; |
| 8007 | if (done > ctx->nr_user_files) |
| 8008 | return -EINVAL; |
| 8009 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8010 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 8011 | if (!ref_node) |
| 8012 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8013 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8014 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8015 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8016 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8017 | err = 0; |
| 8018 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 8019 | err = -EFAULT; |
| 8020 | break; |
| 8021 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8022 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8023 | continue; |
| 8024 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8025 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8026 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 8027 | |
| 8028 | if (*file_slot) { |
| 8029 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8030 | if (err) |
| 8031 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8032 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8033 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8034 | } |
| 8035 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8036 | file = fget(fd); |
| 8037 | if (!file) { |
| 8038 | err = -EBADF; |
| 8039 | break; |
| 8040 | } |
| 8041 | /* |
| 8042 | * Don't allow io_uring instances to be registered. If |
| 8043 | * UNIX isn't enabled, then this causes a reference |
| 8044 | * cycle and this instance can never get freed. If UNIX |
| 8045 | * is enabled we'll handle it just fine, but there's |
| 8046 | * still no point in allowing a ring fd as it doesn't |
| 8047 | * support regular read/write anyway. |
| 8048 | */ |
| 8049 | if (file->f_op == &io_uring_fops) { |
| 8050 | fput(file); |
| 8051 | err = -EBADF; |
| 8052 | break; |
| 8053 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8054 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8055 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8056 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8057 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8058 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8059 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8060 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8061 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8062 | } |
| 8063 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8064 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8065 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 8066 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8067 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8068 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8069 | |
| 8070 | return done ? done : err; |
| 8071 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8072 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8073 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8074 | unsigned nr_args) |
| 8075 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8076 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8077 | |
| 8078 | if (!ctx->file_data) |
| 8079 | return -ENXIO; |
| 8080 | if (!nr_args) |
| 8081 | return -EINVAL; |
| 8082 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8083 | return -EFAULT; |
| 8084 | if (up.resv) |
| 8085 | return -EINVAL; |
| 8086 | |
| 8087 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8088 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8089 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8090 | 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] | 8091 | { |
| 8092 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8093 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8094 | req = io_put_req_find_next(req); |
| 8095 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8096 | } |
| 8097 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8098 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8099 | struct io_uring_params *p) |
| 8100 | { |
| 8101 | struct io_wq_data data; |
| 8102 | struct fd f; |
| 8103 | struct io_ring_ctx *ctx_attach; |
| 8104 | unsigned int concurrency; |
| 8105 | int ret = 0; |
| 8106 | |
| 8107 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8108 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8109 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8110 | |
| 8111 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8112 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8113 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8114 | |
| 8115 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8116 | if (IS_ERR(ctx->io_wq)) { |
| 8117 | ret = PTR_ERR(ctx->io_wq); |
| 8118 | ctx->io_wq = NULL; |
| 8119 | } |
| 8120 | return ret; |
| 8121 | } |
| 8122 | |
| 8123 | f = fdget(p->wq_fd); |
| 8124 | if (!f.file) |
| 8125 | return -EBADF; |
| 8126 | |
| 8127 | if (f.file->f_op != &io_uring_fops) { |
| 8128 | ret = -EINVAL; |
| 8129 | goto out_fput; |
| 8130 | } |
| 8131 | |
| 8132 | ctx_attach = f.file->private_data; |
| 8133 | /* @io_wq is protected by holding the fd */ |
| 8134 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8135 | ret = -EINVAL; |
| 8136 | goto out_fput; |
| 8137 | } |
| 8138 | |
| 8139 | ctx->io_wq = ctx_attach->io_wq; |
| 8140 | out_fput: |
| 8141 | fdput(f); |
| 8142 | return ret; |
| 8143 | } |
| 8144 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8145 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8146 | { |
| 8147 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8148 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8149 | |
| 8150 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8151 | if (unlikely(!tctx)) |
| 8152 | return -ENOMEM; |
| 8153 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8154 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8155 | if (unlikely(ret)) { |
| 8156 | kfree(tctx); |
| 8157 | return ret; |
| 8158 | } |
| 8159 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8160 | xa_init(&tctx->xa); |
| 8161 | init_waitqueue_head(&tctx->wait); |
| 8162 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8163 | atomic_set(&tctx->in_idle, 0); |
| 8164 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8165 | io_init_identity(&tctx->__identity); |
| 8166 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8167 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8168 | spin_lock_init(&tctx->task_lock); |
| 8169 | INIT_WQ_LIST(&tctx->task_list); |
| 8170 | tctx->task_state = 0; |
| 8171 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8172 | return 0; |
| 8173 | } |
| 8174 | |
| 8175 | void __io_uring_free(struct task_struct *tsk) |
| 8176 | { |
| 8177 | struct io_uring_task *tctx = tsk->io_uring; |
| 8178 | |
| 8179 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8180 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8181 | if (tctx->identity != &tctx->__identity) |
| 8182 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8183 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8184 | kfree(tctx); |
| 8185 | tsk->io_uring = NULL; |
| 8186 | } |
| 8187 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8188 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8189 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8190 | { |
| 8191 | int ret; |
| 8192 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8193 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8194 | struct io_sq_data *sqd; |
| 8195 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8196 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8197 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8198 | goto err; |
| 8199 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8200 | sqd = io_get_sq_data(p); |
| 8201 | if (IS_ERR(sqd)) { |
| 8202 | ret = PTR_ERR(sqd); |
| 8203 | goto err; |
| 8204 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8205 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8206 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8207 | io_sq_thread_park(sqd); |
| 8208 | mutex_lock(&sqd->ctx_lock); |
| 8209 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8210 | mutex_unlock(&sqd->ctx_lock); |
| 8211 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8212 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8213 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8214 | if (!ctx->sq_thread_idle) |
| 8215 | ctx->sq_thread_idle = HZ; |
| 8216 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8217 | if (sqd->thread) |
| 8218 | goto done; |
| 8219 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8220 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8221 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8222 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8223 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8224 | if (cpu >= nr_cpu_ids) |
| 8225 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8226 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8227 | goto err; |
| 8228 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8229 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8230 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8231 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8232 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8233 | "io_uring-sq"); |
| 8234 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8235 | if (IS_ERR(sqd->thread)) { |
| 8236 | ret = PTR_ERR(sqd->thread); |
| 8237 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8238 | goto err; |
| 8239 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8240 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8241 | if (ret) |
| 8242 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8243 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8244 | /* Can't have SQ_AFF without SQPOLL */ |
| 8245 | ret = -EINVAL; |
| 8246 | goto err; |
| 8247 | } |
| 8248 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8249 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8250 | ret = io_init_wq_offload(ctx, p); |
| 8251 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8252 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8253 | |
| 8254 | return 0; |
| 8255 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8256 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8257 | return ret; |
| 8258 | } |
| 8259 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8260 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8261 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8262 | struct io_sq_data *sqd = ctx->sq_data; |
| 8263 | |
| 8264 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8265 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8266 | } |
| 8267 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8268 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8269 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8270 | { |
| 8271 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8272 | } |
| 8273 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8274 | static inline int __io_account_mem(struct user_struct *user, |
| 8275 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8276 | { |
| 8277 | unsigned long page_limit, cur_pages, new_pages; |
| 8278 | |
| 8279 | /* Don't allow more pages than we can safely lock */ |
| 8280 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8281 | |
| 8282 | do { |
| 8283 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8284 | new_pages = cur_pages + nr_pages; |
| 8285 | if (new_pages > page_limit) |
| 8286 | return -ENOMEM; |
| 8287 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8288 | new_pages) != cur_pages); |
| 8289 | |
| 8290 | return 0; |
| 8291 | } |
| 8292 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8293 | 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] | 8294 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8295 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8296 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8297 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8298 | if (ctx->mm_account) |
| 8299 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8300 | } |
| 8301 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8302 | 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] | 8303 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8304 | int ret; |
| 8305 | |
| 8306 | if (ctx->limit_mem) { |
| 8307 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8308 | if (ret) |
| 8309 | return ret; |
| 8310 | } |
| 8311 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8312 | if (ctx->mm_account) |
| 8313 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8314 | |
| 8315 | return 0; |
| 8316 | } |
| 8317 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8318 | static void io_mem_free(void *ptr) |
| 8319 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8320 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8321 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8322 | if (!ptr) |
| 8323 | return; |
| 8324 | |
| 8325 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8326 | if (put_page_testzero(page)) |
| 8327 | free_compound_page(page); |
| 8328 | } |
| 8329 | |
| 8330 | static void *io_mem_alloc(size_t size) |
| 8331 | { |
| 8332 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8333 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8334 | |
| 8335 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8336 | } |
| 8337 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8338 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8339 | size_t *sq_offset) |
| 8340 | { |
| 8341 | struct io_rings *rings; |
| 8342 | size_t off, sq_array_size; |
| 8343 | |
| 8344 | off = struct_size(rings, cqes, cq_entries); |
| 8345 | if (off == SIZE_MAX) |
| 8346 | return SIZE_MAX; |
| 8347 | |
| 8348 | #ifdef CONFIG_SMP |
| 8349 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8350 | if (off == 0) |
| 8351 | return SIZE_MAX; |
| 8352 | #endif |
| 8353 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8354 | if (sq_offset) |
| 8355 | *sq_offset = off; |
| 8356 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8357 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8358 | if (sq_array_size == SIZE_MAX) |
| 8359 | return SIZE_MAX; |
| 8360 | |
| 8361 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8362 | return SIZE_MAX; |
| 8363 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8364 | return off; |
| 8365 | } |
| 8366 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8367 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8368 | { |
| 8369 | int i, j; |
| 8370 | |
| 8371 | if (!ctx->user_bufs) |
| 8372 | return -ENXIO; |
| 8373 | |
| 8374 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8375 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8376 | |
| 8377 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8378 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8379 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8380 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8381 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8382 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8383 | imu->nr_bvecs = 0; |
| 8384 | } |
| 8385 | |
| 8386 | kfree(ctx->user_bufs); |
| 8387 | ctx->user_bufs = NULL; |
| 8388 | ctx->nr_user_bufs = 0; |
| 8389 | return 0; |
| 8390 | } |
| 8391 | |
| 8392 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8393 | void __user *arg, unsigned index) |
| 8394 | { |
| 8395 | struct iovec __user *src; |
| 8396 | |
| 8397 | #ifdef CONFIG_COMPAT |
| 8398 | if (ctx->compat) { |
| 8399 | struct compat_iovec __user *ciovs; |
| 8400 | struct compat_iovec ciov; |
| 8401 | |
| 8402 | ciovs = (struct compat_iovec __user *) arg; |
| 8403 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8404 | return -EFAULT; |
| 8405 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8406 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8407 | dst->iov_len = ciov.iov_len; |
| 8408 | return 0; |
| 8409 | } |
| 8410 | #endif |
| 8411 | src = (struct iovec __user *) arg; |
| 8412 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8413 | return -EFAULT; |
| 8414 | return 0; |
| 8415 | } |
| 8416 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8417 | /* |
| 8418 | * Not super efficient, but this is just a registration time. And we do cache |
| 8419 | * the last compound head, so generally we'll only do a full search if we don't |
| 8420 | * match that one. |
| 8421 | * |
| 8422 | * We check if the given compound head page has already been accounted, to |
| 8423 | * avoid double accounting it. This allows us to account the full size of the |
| 8424 | * page, not just the constituent pages of a huge page. |
| 8425 | */ |
| 8426 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8427 | int nr_pages, struct page *hpage) |
| 8428 | { |
| 8429 | int i, j; |
| 8430 | |
| 8431 | /* check current page array */ |
| 8432 | for (i = 0; i < nr_pages; i++) { |
| 8433 | if (!PageCompound(pages[i])) |
| 8434 | continue; |
| 8435 | if (compound_head(pages[i]) == hpage) |
| 8436 | return true; |
| 8437 | } |
| 8438 | |
| 8439 | /* check previously registered pages */ |
| 8440 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8441 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8442 | |
| 8443 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8444 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8445 | continue; |
| 8446 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8447 | return true; |
| 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | return false; |
| 8452 | } |
| 8453 | |
| 8454 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8455 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8456 | struct page **last_hpage) |
| 8457 | { |
| 8458 | int i, ret; |
| 8459 | |
| 8460 | for (i = 0; i < nr_pages; i++) { |
| 8461 | if (!PageCompound(pages[i])) { |
| 8462 | imu->acct_pages++; |
| 8463 | } else { |
| 8464 | struct page *hpage; |
| 8465 | |
| 8466 | hpage = compound_head(pages[i]); |
| 8467 | if (hpage == *last_hpage) |
| 8468 | continue; |
| 8469 | *last_hpage = hpage; |
| 8470 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8471 | continue; |
| 8472 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8473 | } |
| 8474 | } |
| 8475 | |
| 8476 | if (!imu->acct_pages) |
| 8477 | return 0; |
| 8478 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8479 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8480 | if (ret) |
| 8481 | imu->acct_pages = 0; |
| 8482 | return ret; |
| 8483 | } |
| 8484 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8485 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8486 | struct io_mapped_ubuf *imu, |
| 8487 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8488 | { |
| 8489 | struct vm_area_struct **vmas = NULL; |
| 8490 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8491 | unsigned long off, start, end, ubuf; |
| 8492 | size_t size; |
| 8493 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8494 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8495 | ubuf = (unsigned long) iov->iov_base; |
| 8496 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8497 | start = ubuf >> PAGE_SHIFT; |
| 8498 | nr_pages = end - start; |
| 8499 | |
| 8500 | ret = -ENOMEM; |
| 8501 | |
| 8502 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8503 | if (!pages) |
| 8504 | goto done; |
| 8505 | |
| 8506 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8507 | GFP_KERNEL); |
| 8508 | if (!vmas) |
| 8509 | goto done; |
| 8510 | |
| 8511 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8512 | GFP_KERNEL); |
| 8513 | if (!imu->bvec) |
| 8514 | goto done; |
| 8515 | |
| 8516 | ret = 0; |
| 8517 | mmap_read_lock(current->mm); |
| 8518 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8519 | pages, vmas); |
| 8520 | if (pret == nr_pages) { |
| 8521 | /* don't support file backed memory */ |
| 8522 | for (i = 0; i < nr_pages; i++) { |
| 8523 | struct vm_area_struct *vma = vmas[i]; |
| 8524 | |
| 8525 | if (vma->vm_file && |
| 8526 | !is_file_hugepages(vma->vm_file)) { |
| 8527 | ret = -EOPNOTSUPP; |
| 8528 | break; |
| 8529 | } |
| 8530 | } |
| 8531 | } else { |
| 8532 | ret = pret < 0 ? pret : -EFAULT; |
| 8533 | } |
| 8534 | mmap_read_unlock(current->mm); |
| 8535 | if (ret) { |
| 8536 | /* |
| 8537 | * if we did partial map, or found file backed vmas, |
| 8538 | * release any pages we did get |
| 8539 | */ |
| 8540 | if (pret > 0) |
| 8541 | unpin_user_pages(pages, pret); |
| 8542 | kvfree(imu->bvec); |
| 8543 | goto done; |
| 8544 | } |
| 8545 | |
| 8546 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8547 | if (ret) { |
| 8548 | unpin_user_pages(pages, pret); |
| 8549 | kvfree(imu->bvec); |
| 8550 | goto done; |
| 8551 | } |
| 8552 | |
| 8553 | off = ubuf & ~PAGE_MASK; |
| 8554 | size = iov->iov_len; |
| 8555 | for (i = 0; i < nr_pages; i++) { |
| 8556 | size_t vec_len; |
| 8557 | |
| 8558 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8559 | imu->bvec[i].bv_page = pages[i]; |
| 8560 | imu->bvec[i].bv_len = vec_len; |
| 8561 | imu->bvec[i].bv_offset = off; |
| 8562 | off = 0; |
| 8563 | size -= vec_len; |
| 8564 | } |
| 8565 | /* store original address for later verification */ |
| 8566 | imu->ubuf = ubuf; |
| 8567 | imu->len = iov->iov_len; |
| 8568 | imu->nr_bvecs = nr_pages; |
| 8569 | ret = 0; |
| 8570 | done: |
| 8571 | kvfree(pages); |
| 8572 | kvfree(vmas); |
| 8573 | return ret; |
| 8574 | } |
| 8575 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8576 | 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] | 8577 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8578 | if (ctx->user_bufs) |
| 8579 | return -EBUSY; |
| 8580 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8581 | return -EINVAL; |
| 8582 | |
| 8583 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8584 | GFP_KERNEL); |
| 8585 | if (!ctx->user_bufs) |
| 8586 | return -ENOMEM; |
| 8587 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8588 | return 0; |
| 8589 | } |
| 8590 | |
| 8591 | static int io_buffer_validate(struct iovec *iov) |
| 8592 | { |
| 8593 | /* |
| 8594 | * Don't impose further limits on the size and buffer |
| 8595 | * constraints here, we'll -EINVAL later when IO is |
| 8596 | * submitted if they are wrong. |
| 8597 | */ |
| 8598 | if (!iov->iov_base || !iov->iov_len) |
| 8599 | return -EFAULT; |
| 8600 | |
| 8601 | /* arbitrary limit, but we need something */ |
| 8602 | if (iov->iov_len > SZ_1G) |
| 8603 | return -EFAULT; |
| 8604 | |
| 8605 | return 0; |
| 8606 | } |
| 8607 | |
| 8608 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8609 | unsigned int nr_args) |
| 8610 | { |
| 8611 | int i, ret; |
| 8612 | struct iovec iov; |
| 8613 | struct page *last_hpage = NULL; |
| 8614 | |
| 8615 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8616 | if (ret) |
| 8617 | return ret; |
| 8618 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8619 | for (i = 0; i < nr_args; i++) { |
| 8620 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8621 | |
| 8622 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8623 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8624 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8625 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8626 | ret = io_buffer_validate(&iov); |
| 8627 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8628 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8629 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8630 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8631 | if (ret) |
| 8632 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8633 | |
| 8634 | ctx->nr_user_bufs++; |
| 8635 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8636 | |
| 8637 | if (ret) |
| 8638 | io_sqe_buffers_unregister(ctx); |
| 8639 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8640 | return ret; |
| 8641 | } |
| 8642 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8643 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8644 | { |
| 8645 | __s32 __user *fds = arg; |
| 8646 | int fd; |
| 8647 | |
| 8648 | if (ctx->cq_ev_fd) |
| 8649 | return -EBUSY; |
| 8650 | |
| 8651 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8652 | return -EFAULT; |
| 8653 | |
| 8654 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8655 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8656 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8657 | ctx->cq_ev_fd = NULL; |
| 8658 | return ret; |
| 8659 | } |
| 8660 | |
| 8661 | return 0; |
| 8662 | } |
| 8663 | |
| 8664 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8665 | { |
| 8666 | if (ctx->cq_ev_fd) { |
| 8667 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8668 | ctx->cq_ev_fd = NULL; |
| 8669 | return 0; |
| 8670 | } |
| 8671 | |
| 8672 | return -ENXIO; |
| 8673 | } |
| 8674 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8675 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8676 | { |
| 8677 | struct io_ring_ctx *ctx = data; |
| 8678 | struct io_buffer *buf = p; |
| 8679 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8680 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8681 | return 0; |
| 8682 | } |
| 8683 | |
| 8684 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8685 | { |
| 8686 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8687 | idr_destroy(&ctx->io_buffer_idr); |
| 8688 | } |
| 8689 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8690 | 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] | 8691 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8692 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8693 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8694 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8695 | if (tsk && req->task != tsk) |
| 8696 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8697 | list_del(&req->compl.list); |
| 8698 | kmem_cache_free(req_cachep, req); |
| 8699 | } |
| 8700 | } |
| 8701 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8702 | 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] | 8703 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8704 | struct io_submit_state *submit_state = &ctx->submit_state; |
| 8705 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8706 | mutex_lock(&ctx->uring_lock); |
| 8707 | |
| 8708 | if (submit_state->free_reqs) |
| 8709 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8710 | submit_state->reqs); |
| 8711 | |
| 8712 | io_req_cache_free(&submit_state->comp.free_list, NULL); |
| 8713 | |
| 8714 | spin_lock_irq(&ctx->completion_lock); |
| 8715 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); |
| 8716 | spin_unlock_irq(&ctx->completion_lock); |
| 8717 | |
| 8718 | mutex_unlock(&ctx->uring_lock); |
| 8719 | } |
| 8720 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8721 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8722 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8723 | /* |
| 8724 | * Some may use context even when all refs and requests have been put, |
| 8725 | * and they are free to do so while still holding uring_lock, see |
| 8726 | * __io_req_task_submit(). Wait for them to finish. |
| 8727 | */ |
| 8728 | mutex_lock(&ctx->uring_lock); |
| 8729 | mutex_unlock(&ctx->uring_lock); |
| 8730 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8731 | io_finish_async(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8732 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8733 | |
| 8734 | if (ctx->sqo_task) { |
| 8735 | put_task_struct(ctx->sqo_task); |
| 8736 | ctx->sqo_task = NULL; |
| 8737 | mmdrop(ctx->mm_account); |
| 8738 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8739 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8740 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8741 | #ifdef CONFIG_BLK_CGROUP |
| 8742 | if (ctx->sqo_blkcg_css) |
| 8743 | css_put(ctx->sqo_blkcg_css); |
| 8744 | #endif |
| 8745 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8746 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8747 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8748 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8749 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8750 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8751 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8752 | if (ctx->ring_sock) { |
| 8753 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8754 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8755 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8756 | #endif |
| 8757 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8758 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8759 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8760 | |
| 8761 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8762 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8763 | put_cred(ctx->creds); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8764 | io_req_caches_free(ctx, NULL); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8765 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8766 | kfree(ctx); |
| 8767 | } |
| 8768 | |
| 8769 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8770 | { |
| 8771 | struct io_ring_ctx *ctx = file->private_data; |
| 8772 | __poll_t mask = 0; |
| 8773 | |
| 8774 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8775 | /* |
| 8776 | * synchronizes with barrier from wq_has_sleeper call in |
| 8777 | * io_commit_cqring |
| 8778 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8779 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8780 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8781 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8782 | |
| 8783 | /* |
| 8784 | * Don't flush cqring overflow list here, just do a simple check. |
| 8785 | * Otherwise there could possible be ABBA deadlock: |
| 8786 | * CPU0 CPU1 |
| 8787 | * ---- ---- |
| 8788 | * lock(&ctx->uring_lock); |
| 8789 | * lock(&ep->mtx); |
| 8790 | * lock(&ctx->uring_lock); |
| 8791 | * lock(&ep->mtx); |
| 8792 | * |
| 8793 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8794 | * pushs them to do the flush. |
| 8795 | */ |
| 8796 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8797 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8798 | |
| 8799 | return mask; |
| 8800 | } |
| 8801 | |
| 8802 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8803 | { |
| 8804 | struct io_ring_ctx *ctx = file->private_data; |
| 8805 | |
| 8806 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8807 | } |
| 8808 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8809 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8810 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8811 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8812 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8813 | iod = idr_remove(&ctx->personality_idr, id); |
| 8814 | if (iod) { |
| 8815 | put_cred(iod->creds); |
| 8816 | if (refcount_dec_and_test(&iod->count)) |
| 8817 | kfree(iod); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8818 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8819 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8820 | |
| 8821 | return -EINVAL; |
| 8822 | } |
| 8823 | |
| 8824 | static int io_remove_personalities(int id, void *p, void *data) |
| 8825 | { |
| 8826 | struct io_ring_ctx *ctx = data; |
| 8827 | |
| 8828 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8829 | return 0; |
| 8830 | } |
| 8831 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8832 | static void io_ring_exit_work(struct work_struct *work) |
| 8833 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8834 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8835 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8836 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8837 | /* |
| 8838 | * If we're doing polled IO and end up having requests being |
| 8839 | * submitted async (out-of-line), then completions can come in while |
| 8840 | * we're waiting for refs to drop. We need to reap these manually, |
| 8841 | * as nobody else will be looking for them. |
| 8842 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8843 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8844 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8845 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8846 | io_ring_ctx_free(ctx); |
| 8847 | } |
| 8848 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8849 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8850 | { |
| 8851 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8852 | |
| 8853 | return req->ctx == data; |
| 8854 | } |
| 8855 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8856 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8857 | { |
| 8858 | mutex_lock(&ctx->uring_lock); |
| 8859 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8860 | |
| 8861 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8862 | ctx->sqo_dead = 1; |
| 8863 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8864 | /* if force is set, the ring is going away. always drop after that */ |
| 8865 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8866 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8867 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8868 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8869 | mutex_unlock(&ctx->uring_lock); |
| 8870 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8871 | io_kill_timeouts(ctx, NULL, NULL); |
| 8872 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8873 | |
| 8874 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8875 | 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] | 8876 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8877 | /* 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] | 8878 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8879 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8880 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8881 | /* |
| 8882 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8883 | * if we're exiting a ton of rings at the same time. It just adds |
| 8884 | * noise and overhead, there's no discernable change in runtime |
| 8885 | * over using system_wq. |
| 8886 | */ |
| 8887 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8888 | } |
| 8889 | |
| 8890 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8891 | { |
| 8892 | struct io_ring_ctx *ctx = file->private_data; |
| 8893 | |
| 8894 | file->private_data = NULL; |
| 8895 | io_ring_ctx_wait_and_kill(ctx); |
| 8896 | return 0; |
| 8897 | } |
| 8898 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8899 | struct io_task_cancel { |
| 8900 | struct task_struct *task; |
| 8901 | struct files_struct *files; |
| 8902 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8903 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8904 | 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] | 8905 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8906 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8907 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8908 | bool ret; |
| 8909 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8910 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8911 | unsigned long flags; |
| 8912 | struct io_ring_ctx *ctx = req->ctx; |
| 8913 | |
| 8914 | /* protect against races with linked timeouts */ |
| 8915 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8916 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8917 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8918 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8919 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8920 | } |
| 8921 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8922 | } |
| 8923 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8924 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8925 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8926 | struct files_struct *files) |
| 8927 | { |
| 8928 | struct io_defer_entry *de = NULL; |
| 8929 | LIST_HEAD(list); |
| 8930 | |
| 8931 | spin_lock_irq(&ctx->completion_lock); |
| 8932 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8933 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8934 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8935 | break; |
| 8936 | } |
| 8937 | } |
| 8938 | spin_unlock_irq(&ctx->completion_lock); |
| 8939 | |
| 8940 | while (!list_empty(&list)) { |
| 8941 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8942 | list_del_init(&de->list); |
| 8943 | req_set_fail_links(de->req); |
| 8944 | io_put_req(de->req); |
| 8945 | io_req_complete(de->req, -ECANCELED); |
| 8946 | kfree(de); |
| 8947 | } |
| 8948 | } |
| 8949 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8950 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8951 | struct task_struct *task, |
| 8952 | struct files_struct *files) |
| 8953 | { |
| 8954 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
| 8955 | |
| 8956 | while (1) { |
| 8957 | enum io_wq_cancel cret; |
| 8958 | bool ret = false; |
| 8959 | |
| 8960 | if (ctx->io_wq) { |
| 8961 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8962 | &cancel, true); |
| 8963 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8964 | } |
| 8965 | |
| 8966 | /* SQPOLL thread does its own polling */ |
| 8967 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8968 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8969 | io_iopoll_try_reap_events(ctx); |
| 8970 | ret = true; |
| 8971 | } |
| 8972 | } |
| 8973 | |
| 8974 | ret |= io_poll_remove_all(ctx, task, files); |
| 8975 | ret |= io_kill_timeouts(ctx, task, files); |
| 8976 | ret |= io_run_task_work(); |
| 8977 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8978 | if (!ret) |
| 8979 | break; |
| 8980 | cond_resched(); |
| 8981 | } |
| 8982 | } |
| 8983 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8984 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8985 | struct task_struct *task, |
| 8986 | struct files_struct *files) |
| 8987 | { |
| 8988 | struct io_kiocb *req; |
| 8989 | int cnt = 0; |
| 8990 | |
| 8991 | spin_lock_irq(&ctx->inflight_lock); |
| 8992 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8993 | cnt += io_match_task(req, task, files); |
| 8994 | spin_unlock_irq(&ctx->inflight_lock); |
| 8995 | return cnt; |
| 8996 | } |
| 8997 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8998 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8999 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9000 | struct files_struct *files) |
| 9001 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9002 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 9003 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9004 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9005 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9006 | inflight = io_uring_count_inflight(ctx, task, files); |
| 9007 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9008 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9009 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9010 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9011 | |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9012 | if (ctx->sq_data) |
| 9013 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9014 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 9015 | TASK_UNINTERRUPTIBLE); |
| 9016 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 9017 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 9018 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9019 | if (ctx->sq_data) |
| 9020 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9021 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9022 | } |
| 9023 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9024 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 9025 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9026 | mutex_lock(&ctx->uring_lock); |
| 9027 | ctx->sqo_dead = 1; |
| 9028 | mutex_unlock(&ctx->uring_lock); |
| 9029 | |
| 9030 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 9031 | if (ctx->rings) |
| 9032 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9033 | } |
| 9034 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9035 | /* |
| 9036 | * We need to iteratively cancel requests, in case a request has dependent |
| 9037 | * hard links. These persist even for failure of cancelations, hence keep |
| 9038 | * looping until none are found. |
| 9039 | */ |
| 9040 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 9041 | struct files_struct *files) |
| 9042 | { |
| 9043 | struct task_struct *task = current; |
| 9044 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9045 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9046 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9047 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9048 | atomic_inc(&task->io_uring->in_idle); |
| 9049 | io_sq_thread_park(ctx->sq_data); |
| 9050 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9051 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 9052 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9053 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 9054 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 9055 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9056 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9057 | |
| 9058 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 9059 | atomic_dec(&task->io_uring->in_idle); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9060 | io_sq_thread_unpark(ctx->sq_data); |
| 9061 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | /* |
| 9065 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9066 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9067 | 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] | 9068 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9069 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9070 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9071 | |
| 9072 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9073 | ret = io_uring_alloc_task_context(current); |
| 9074 | if (unlikely(ret)) |
| 9075 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9076 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9077 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9078 | if (tctx->last != file) { |
| 9079 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9080 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9081 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9082 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9083 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 9084 | file, GFP_KERNEL)); |
| 9085 | if (ret) { |
| 9086 | fput(file); |
| 9087 | return ret; |
| 9088 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 9089 | |
| 9090 | /* one and only SQPOLL file note, held by sqo_task */ |
| 9091 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 9092 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9093 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9094 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9095 | } |
| 9096 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9097 | /* |
| 9098 | * This is race safe in that the task itself is doing this, hence it |
| 9099 | * cannot be going through the exit/cancel paths at the same time. |
| 9100 | * This cannot be modified while exit/cancel is running. |
| 9101 | */ |
| 9102 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9103 | tctx->sqpoll = true; |
| 9104 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9105 | return 0; |
| 9106 | } |
| 9107 | |
| 9108 | /* |
| 9109 | * Remove this io_uring_file -> task mapping. |
| 9110 | */ |
| 9111 | static void io_uring_del_task_file(struct file *file) |
| 9112 | { |
| 9113 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9114 | |
| 9115 | if (tctx->last == file) |
| 9116 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9117 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9118 | if (file) |
| 9119 | fput(file); |
| 9120 | } |
| 9121 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9122 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9123 | { |
| 9124 | struct file *file; |
| 9125 | unsigned long index; |
| 9126 | |
| 9127 | xa_for_each(&tctx->xa, index, file) |
| 9128 | io_uring_del_task_file(file); |
| 9129 | } |
| 9130 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9131 | void __io_uring_files_cancel(struct files_struct *files) |
| 9132 | { |
| 9133 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9134 | struct file *file; |
| 9135 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9136 | |
| 9137 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9138 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9139 | xa_for_each(&tctx->xa, index, file) |
| 9140 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9141 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9142 | |
| 9143 | if (files) |
| 9144 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9145 | } |
| 9146 | |
| 9147 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9148 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9149 | return percpu_counter_sum(&tctx->inflight); |
| 9150 | } |
| 9151 | |
| 9152 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 9153 | { |
| 9154 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9155 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9156 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9157 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9158 | if (!ctx->sq_data) |
| 9159 | return; |
| 9160 | tctx = ctx->sq_data->thread->io_uring; |
| 9161 | io_disable_sqo_submit(ctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9162 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9163 | atomic_inc(&tctx->in_idle); |
| 9164 | do { |
| 9165 | /* read completions before cancelations */ |
| 9166 | inflight = tctx_inflight(tctx); |
| 9167 | if (!inflight) |
| 9168 | break; |
| 9169 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9170 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9171 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9172 | /* |
| 9173 | * If we've seen completions, retry without waiting. This |
| 9174 | * avoids a race where a completion comes in before we did |
| 9175 | * prepare_to_wait(). |
| 9176 | */ |
| 9177 | if (inflight == tctx_inflight(tctx)) |
| 9178 | schedule(); |
| 9179 | finish_wait(&tctx->wait, &wait); |
| 9180 | } while (1); |
| 9181 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9182 | } |
| 9183 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9184 | /* |
| 9185 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9186 | * requests. |
| 9187 | */ |
| 9188 | void __io_uring_task_cancel(void) |
| 9189 | { |
| 9190 | struct io_uring_task *tctx = current->io_uring; |
| 9191 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9192 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9193 | |
| 9194 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9195 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9196 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9197 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9198 | if (tctx->sqpoll) { |
| 9199 | struct file *file; |
| 9200 | unsigned long index; |
| 9201 | |
| 9202 | xa_for_each(&tctx->xa, index, file) |
| 9203 | io_uring_cancel_sqpoll(file->private_data); |
| 9204 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9205 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9206 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9207 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9208 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9209 | if (!inflight) |
| 9210 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9211 | __io_uring_files_cancel(NULL); |
| 9212 | |
| 9213 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9214 | |
| 9215 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9216 | * If we've seen completions, retry without waiting. This |
| 9217 | * avoids a race where a completion comes in before we did |
| 9218 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9219 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9220 | if (inflight == tctx_inflight(tctx)) |
| 9221 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9222 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9223 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9224 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9225 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9226 | |
| 9227 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9228 | } |
| 9229 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9230 | static int io_uring_flush(struct file *file, void *data) |
| 9231 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9232 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9233 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9234 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9235 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9236 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9237 | io_req_caches_free(ctx, current); |
| 9238 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9239 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9240 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9241 | return 0; |
| 9242 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9243 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9244 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9245 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9246 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9247 | /* |
| 9248 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9249 | * task file note. If the task is exiting, drop regardless of count. |
| 9250 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9251 | if (atomic_long_read(&file->f_count) != 2) |
| 9252 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9253 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9254 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9255 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 9256 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9257 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9258 | /* sqo_dead check is for when this happens after cancellation */ |
| 9259 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9260 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9261 | |
| 9262 | io_disable_sqo_submit(ctx); |
| 9263 | } |
| 9264 | |
| 9265 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9266 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9267 | return 0; |
| 9268 | } |
| 9269 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9270 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9271 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9272 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9273 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9274 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9275 | struct page *page; |
| 9276 | void *ptr; |
| 9277 | |
| 9278 | switch (offset) { |
| 9279 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9280 | case IORING_OFF_CQ_RING: |
| 9281 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9282 | break; |
| 9283 | case IORING_OFF_SQES: |
| 9284 | ptr = ctx->sq_sqes; |
| 9285 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9286 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9287 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9291 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9292 | return ERR_PTR(-EINVAL); |
| 9293 | |
| 9294 | return ptr; |
| 9295 | } |
| 9296 | |
| 9297 | #ifdef CONFIG_MMU |
| 9298 | |
| 9299 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9300 | { |
| 9301 | size_t sz = vma->vm_end - vma->vm_start; |
| 9302 | unsigned long pfn; |
| 9303 | void *ptr; |
| 9304 | |
| 9305 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9306 | if (IS_ERR(ptr)) |
| 9307 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9308 | |
| 9309 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9310 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9311 | } |
| 9312 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9313 | #else /* !CONFIG_MMU */ |
| 9314 | |
| 9315 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9316 | { |
| 9317 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9318 | } |
| 9319 | |
| 9320 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9321 | { |
| 9322 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9323 | } |
| 9324 | |
| 9325 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9326 | unsigned long addr, unsigned long len, |
| 9327 | unsigned long pgoff, unsigned long flags) |
| 9328 | { |
| 9329 | void *ptr; |
| 9330 | |
| 9331 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9332 | if (IS_ERR(ptr)) |
| 9333 | return PTR_ERR(ptr); |
| 9334 | |
| 9335 | return (unsigned long) ptr; |
| 9336 | } |
| 9337 | |
| 9338 | #endif /* !CONFIG_MMU */ |
| 9339 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9340 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9341 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9342 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9343 | DEFINE_WAIT(wait); |
| 9344 | |
| 9345 | do { |
| 9346 | if (!io_sqring_full(ctx)) |
| 9347 | break; |
| 9348 | |
| 9349 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9350 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9351 | if (unlikely(ctx->sqo_dead)) { |
| 9352 | ret = -EOWNERDEAD; |
| 9353 | goto out; |
| 9354 | } |
| 9355 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9356 | if (!io_sqring_full(ctx)) |
| 9357 | break; |
| 9358 | |
| 9359 | schedule(); |
| 9360 | } while (!signal_pending(current)); |
| 9361 | |
| 9362 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9363 | out: |
| 9364 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9365 | } |
| 9366 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9367 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9368 | struct __kernel_timespec __user **ts, |
| 9369 | const sigset_t __user **sig) |
| 9370 | { |
| 9371 | struct io_uring_getevents_arg arg; |
| 9372 | |
| 9373 | /* |
| 9374 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9375 | * is just a pointer to the sigset_t. |
| 9376 | */ |
| 9377 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9378 | *sig = (const sigset_t __user *) argp; |
| 9379 | *ts = NULL; |
| 9380 | return 0; |
| 9381 | } |
| 9382 | |
| 9383 | /* |
| 9384 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9385 | * timespec and sigset_t pointers if good. |
| 9386 | */ |
| 9387 | if (*argsz != sizeof(arg)) |
| 9388 | return -EINVAL; |
| 9389 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9390 | return -EFAULT; |
| 9391 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9392 | *argsz = arg.sigmask_sz; |
| 9393 | *ts = u64_to_user_ptr(arg.ts); |
| 9394 | return 0; |
| 9395 | } |
| 9396 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9397 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9398 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9399 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9400 | { |
| 9401 | struct io_ring_ctx *ctx; |
| 9402 | long ret = -EBADF; |
| 9403 | int submitted = 0; |
| 9404 | struct fd f; |
| 9405 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9406 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9407 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9408 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9409 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9410 | return -EINVAL; |
| 9411 | |
| 9412 | f = fdget(fd); |
| 9413 | if (!f.file) |
| 9414 | return -EBADF; |
| 9415 | |
| 9416 | ret = -EOPNOTSUPP; |
| 9417 | if (f.file->f_op != &io_uring_fops) |
| 9418 | goto out_fput; |
| 9419 | |
| 9420 | ret = -ENXIO; |
| 9421 | ctx = f.file->private_data; |
| 9422 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9423 | goto out_fput; |
| 9424 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9425 | ret = -EBADFD; |
| 9426 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9427 | goto out; |
| 9428 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9429 | /* |
| 9430 | * For SQ polling, the thread will do all submissions and completions. |
| 9431 | * Just return the requested submit count, and wake the thread if |
| 9432 | * we were asked to. |
| 9433 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9434 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9435 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9436 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9437 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9438 | ret = -EOWNERDEAD; |
| 9439 | if (unlikely(ctx->sqo_dead)) |
| 9440 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9441 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9442 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9443 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9444 | ret = io_sqpoll_wait_sq(ctx); |
| 9445 | if (ret) |
| 9446 | goto out; |
| 9447 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9448 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9449 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9450 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9451 | if (unlikely(ret)) |
| 9452 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9453 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9454 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9455 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9456 | |
| 9457 | if (submitted != to_submit) |
| 9458 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9459 | } |
| 9460 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9461 | const sigset_t __user *sig; |
| 9462 | struct __kernel_timespec __user *ts; |
| 9463 | |
| 9464 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9465 | if (unlikely(ret)) |
| 9466 | goto out; |
| 9467 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9468 | min_complete = min(min_complete, ctx->cq_entries); |
| 9469 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9470 | /* |
| 9471 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9472 | * space applications don't need to do io completion events |
| 9473 | * polling again, they can rely on io_sq_thread to do polling |
| 9474 | * work, which can reduce cpu usage and uring_lock contention. |
| 9475 | */ |
| 9476 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9477 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9478 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9479 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9480 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9481 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9482 | } |
| 9483 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9484 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9485 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9486 | out_fput: |
| 9487 | fdput(f); |
| 9488 | return submitted ? submitted : ret; |
| 9489 | } |
| 9490 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9491 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9492 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9493 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9494 | struct io_identity *iod = p; |
| 9495 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9496 | struct seq_file *m = data; |
| 9497 | struct user_namespace *uns = seq_user_ns(m); |
| 9498 | struct group_info *gi; |
| 9499 | kernel_cap_t cap; |
| 9500 | unsigned __capi; |
| 9501 | int g; |
| 9502 | |
| 9503 | seq_printf(m, "%5d\n", id); |
| 9504 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9505 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9506 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9507 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9508 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9509 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9510 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9511 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9512 | seq_puts(m, "\n\tGroups:\t"); |
| 9513 | gi = cred->group_info; |
| 9514 | for (g = 0; g < gi->ngroups; g++) { |
| 9515 | seq_put_decimal_ull(m, g ? " " : "", |
| 9516 | from_kgid_munged(uns, gi->gid[g])); |
| 9517 | } |
| 9518 | seq_puts(m, "\n\tCapEff:\t"); |
| 9519 | cap = cred->cap_effective; |
| 9520 | CAP_FOR_EACH_U32(__capi) |
| 9521 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9522 | seq_putc(m, '\n'); |
| 9523 | return 0; |
| 9524 | } |
| 9525 | |
| 9526 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9527 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9528 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9529 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9530 | int i; |
| 9531 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9532 | /* |
| 9533 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9534 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9535 | * cases. If we fail to get the lock, we just don't iterate any |
| 9536 | * structures that could be going away outside the io_uring mutex. |
| 9537 | */ |
| 9538 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9539 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9540 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9541 | sq = ctx->sq_data; |
| 9542 | |
| 9543 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9544 | 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] | 9545 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9546 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9547 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9548 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9549 | if (f) |
| 9550 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9551 | else |
| 9552 | seq_printf(m, "%5u: <none>\n", i); |
| 9553 | } |
| 9554 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9555 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9556 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9557 | |
| 9558 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9559 | (unsigned int) buf->len); |
| 9560 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9561 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9562 | seq_printf(m, "Personalities:\n"); |
| 9563 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9564 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9565 | seq_printf(m, "PollList:\n"); |
| 9566 | spin_lock_irq(&ctx->completion_lock); |
| 9567 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9568 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9569 | struct io_kiocb *req; |
| 9570 | |
| 9571 | hlist_for_each_entry(req, list, hash_node) |
| 9572 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9573 | req->task->task_works != NULL); |
| 9574 | } |
| 9575 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9576 | if (has_lock) |
| 9577 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9581 | { |
| 9582 | struct io_ring_ctx *ctx = f->private_data; |
| 9583 | |
| 9584 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9585 | __io_uring_show_fdinfo(ctx, m); |
| 9586 | percpu_ref_put(&ctx->refs); |
| 9587 | } |
| 9588 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9589 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9590 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9591 | static const struct file_operations io_uring_fops = { |
| 9592 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9593 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9594 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9595 | #ifndef CONFIG_MMU |
| 9596 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9597 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9598 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9599 | .poll = io_uring_poll, |
| 9600 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9601 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9602 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9603 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9604 | }; |
| 9605 | |
| 9606 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9607 | struct io_uring_params *p) |
| 9608 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9609 | struct io_rings *rings; |
| 9610 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9611 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9612 | /* make sure these are sane, as we already accounted them */ |
| 9613 | ctx->sq_entries = p->sq_entries; |
| 9614 | ctx->cq_entries = p->cq_entries; |
| 9615 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9616 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9617 | if (size == SIZE_MAX) |
| 9618 | return -EOVERFLOW; |
| 9619 | |
| 9620 | rings = io_mem_alloc(size); |
| 9621 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9622 | return -ENOMEM; |
| 9623 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9624 | ctx->rings = rings; |
| 9625 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9626 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9627 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9628 | rings->sq_ring_entries = p->sq_entries; |
| 9629 | rings->cq_ring_entries = p->cq_entries; |
| 9630 | ctx->sq_mask = rings->sq_ring_mask; |
| 9631 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9632 | |
| 9633 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9634 | if (size == SIZE_MAX) { |
| 9635 | io_mem_free(ctx->rings); |
| 9636 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9637 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9638 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9639 | |
| 9640 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9641 | if (!ctx->sq_sqes) { |
| 9642 | io_mem_free(ctx->rings); |
| 9643 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9644 | return -ENOMEM; |
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 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9647 | return 0; |
| 9648 | } |
| 9649 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9650 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9651 | { |
| 9652 | int ret, fd; |
| 9653 | |
| 9654 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9655 | if (fd < 0) |
| 9656 | return fd; |
| 9657 | |
| 9658 | ret = io_uring_add_task_file(ctx, file); |
| 9659 | if (ret) { |
| 9660 | put_unused_fd(fd); |
| 9661 | return ret; |
| 9662 | } |
| 9663 | fd_install(fd, file); |
| 9664 | return fd; |
| 9665 | } |
| 9666 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9667 | /* |
| 9668 | * Allocate an anonymous fd, this is what constitutes the application |
| 9669 | * visible backing of an io_uring instance. The application mmaps this |
| 9670 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9671 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9672 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9673 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9674 | { |
| 9675 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9676 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9677 | int ret; |
| 9678 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9679 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9680 | &ctx->ring_sock); |
| 9681 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9682 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9683 | #endif |
| 9684 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9685 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9686 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9687 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9688 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9689 | sock_release(ctx->ring_sock); |
| 9690 | ctx->ring_sock = NULL; |
| 9691 | } else { |
| 9692 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9693 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9694 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9695 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9696 | } |
| 9697 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9698 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9699 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9700 | { |
| 9701 | struct user_struct *user = NULL; |
| 9702 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9703 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9704 | int ret; |
| 9705 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9706 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9707 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9708 | if (entries > IORING_MAX_ENTRIES) { |
| 9709 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9710 | return -EINVAL; |
| 9711 | entries = IORING_MAX_ENTRIES; |
| 9712 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9713 | |
| 9714 | /* |
| 9715 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9716 | * application to drive a higher depth than the size of the SQ ring, |
| 9717 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9718 | * some flexibility in overcommitting a bit. If the application has |
| 9719 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9720 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9721 | */ |
| 9722 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9723 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9724 | /* |
| 9725 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9726 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9727 | * any cq vs sq ring sizing. |
| 9728 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9729 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9730 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9731 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9732 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9733 | return -EINVAL; |
| 9734 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9735 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9736 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9737 | if (p->cq_entries < p->sq_entries) |
| 9738 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9739 | } else { |
| 9740 | p->cq_entries = 2 * p->sq_entries; |
| 9741 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9742 | |
| 9743 | user = get_uid(current_user()); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9744 | |
| 9745 | ctx = io_ring_ctx_alloc(p); |
| 9746 | if (!ctx) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9747 | free_uid(user); |
| 9748 | return -ENOMEM; |
| 9749 | } |
| 9750 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9751 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9752 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9753 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9754 | #ifdef CONFIG_AUDIT |
| 9755 | ctx->loginuid = current->loginuid; |
| 9756 | ctx->sessionid = current->sessionid; |
| 9757 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9758 | ctx->sqo_task = get_task_struct(current); |
| 9759 | |
| 9760 | /* |
| 9761 | * This is just grabbed for accounting purposes. When a process exits, |
| 9762 | * the mm is exited and dropped before the files, hence we need to hang |
| 9763 | * on to this mm purely for the purposes of being able to unaccount |
| 9764 | * memory (locked/pinned vm). It's not used for anything else. |
| 9765 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9766 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9767 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9768 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9769 | #ifdef CONFIG_BLK_CGROUP |
| 9770 | /* |
| 9771 | * The sq thread will belong to the original cgroup it was inited in. |
| 9772 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9773 | * issued bios will be associated with the closest cgroup later in the |
| 9774 | * block layer. |
| 9775 | */ |
| 9776 | rcu_read_lock(); |
| 9777 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9778 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9779 | rcu_read_unlock(); |
| 9780 | if (!ret) { |
| 9781 | /* don't init against a dying cgroup, have the user try again */ |
| 9782 | ctx->sqo_blkcg_css = NULL; |
| 9783 | ret = -ENODEV; |
| 9784 | goto err; |
| 9785 | } |
| 9786 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9787 | ret = io_allocate_scq_urings(ctx, p); |
| 9788 | if (ret) |
| 9789 | goto err; |
| 9790 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9791 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9792 | if (ret) |
| 9793 | goto err; |
| 9794 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9795 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9796 | io_sq_offload_start(ctx); |
| 9797 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9798 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9799 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9800 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9801 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9802 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9803 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9804 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9805 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9806 | |
| 9807 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9808 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9809 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9810 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9811 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9812 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9813 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9814 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9815 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9816 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9817 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9818 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9819 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9820 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9821 | |
| 9822 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9823 | ret = -EFAULT; |
| 9824 | goto err; |
| 9825 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9826 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9827 | file = io_uring_get_file(ctx); |
| 9828 | if (IS_ERR(file)) { |
| 9829 | ret = PTR_ERR(file); |
| 9830 | goto err; |
| 9831 | } |
| 9832 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9833 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9834 | * Install ring fd as the very last thing, so we don't risk someone |
| 9835 | * having closed it before we finish setup |
| 9836 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9837 | ret = io_uring_install_fd(ctx, file); |
| 9838 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9839 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9840 | /* fput will clean it up */ |
| 9841 | fput(file); |
| 9842 | return ret; |
| 9843 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9844 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9845 | 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] | 9846 | return ret; |
| 9847 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9848 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9849 | io_ring_ctx_wait_and_kill(ctx); |
| 9850 | return ret; |
| 9851 | } |
| 9852 | |
| 9853 | /* |
| 9854 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9855 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9856 | * params structure passed in. |
| 9857 | */ |
| 9858 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9859 | { |
| 9860 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9861 | int i; |
| 9862 | |
| 9863 | if (copy_from_user(&p, params, sizeof(p))) |
| 9864 | return -EFAULT; |
| 9865 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9866 | if (p.resv[i]) |
| 9867 | return -EINVAL; |
| 9868 | } |
| 9869 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9870 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9871 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9872 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9873 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9874 | return -EINVAL; |
| 9875 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9876 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9877 | } |
| 9878 | |
| 9879 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9880 | struct io_uring_params __user *, params) |
| 9881 | { |
| 9882 | return io_uring_setup(entries, params); |
| 9883 | } |
| 9884 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9885 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9886 | { |
| 9887 | struct io_uring_probe *p; |
| 9888 | size_t size; |
| 9889 | int i, ret; |
| 9890 | |
| 9891 | size = struct_size(p, ops, nr_args); |
| 9892 | if (size == SIZE_MAX) |
| 9893 | return -EOVERFLOW; |
| 9894 | p = kzalloc(size, GFP_KERNEL); |
| 9895 | if (!p) |
| 9896 | return -ENOMEM; |
| 9897 | |
| 9898 | ret = -EFAULT; |
| 9899 | if (copy_from_user(p, arg, size)) |
| 9900 | goto out; |
| 9901 | ret = -EINVAL; |
| 9902 | if (memchr_inv(p, 0, size)) |
| 9903 | goto out; |
| 9904 | |
| 9905 | p->last_op = IORING_OP_LAST - 1; |
| 9906 | if (nr_args > IORING_OP_LAST) |
| 9907 | nr_args = IORING_OP_LAST; |
| 9908 | |
| 9909 | for (i = 0; i < nr_args; i++) { |
| 9910 | p->ops[i].op = i; |
| 9911 | if (!io_op_defs[i].not_supported) |
| 9912 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9913 | } |
| 9914 | p->ops_len = i; |
| 9915 | |
| 9916 | ret = 0; |
| 9917 | if (copy_to_user(arg, p, size)) |
| 9918 | ret = -EFAULT; |
| 9919 | out: |
| 9920 | kfree(p); |
| 9921 | return ret; |
| 9922 | } |
| 9923 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9924 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9925 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9926 | struct io_identity *id; |
| 9927 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9928 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9929 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9930 | if (unlikely(!id)) |
| 9931 | return -ENOMEM; |
| 9932 | |
| 9933 | io_init_identity(id); |
| 9934 | id->creds = get_current_cred(); |
| 9935 | |
| 9936 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9937 | if (ret < 0) { |
| 9938 | put_cred(id->creds); |
| 9939 | kfree(id); |
| 9940 | } |
| 9941 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9942 | } |
| 9943 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9944 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9945 | unsigned int nr_args) |
| 9946 | { |
| 9947 | struct io_uring_restriction *res; |
| 9948 | size_t size; |
| 9949 | int i, ret; |
| 9950 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9951 | /* Restrictions allowed only if rings started disabled */ |
| 9952 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9953 | return -EBADFD; |
| 9954 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9955 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9956 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9957 | return -EBUSY; |
| 9958 | |
| 9959 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9960 | return -EINVAL; |
| 9961 | |
| 9962 | size = array_size(nr_args, sizeof(*res)); |
| 9963 | if (size == SIZE_MAX) |
| 9964 | return -EOVERFLOW; |
| 9965 | |
| 9966 | res = memdup_user(arg, size); |
| 9967 | if (IS_ERR(res)) |
| 9968 | return PTR_ERR(res); |
| 9969 | |
| 9970 | ret = 0; |
| 9971 | |
| 9972 | for (i = 0; i < nr_args; i++) { |
| 9973 | switch (res[i].opcode) { |
| 9974 | case IORING_RESTRICTION_REGISTER_OP: |
| 9975 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9976 | ret = -EINVAL; |
| 9977 | goto out; |
| 9978 | } |
| 9979 | |
| 9980 | __set_bit(res[i].register_op, |
| 9981 | ctx->restrictions.register_op); |
| 9982 | break; |
| 9983 | case IORING_RESTRICTION_SQE_OP: |
| 9984 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9985 | ret = -EINVAL; |
| 9986 | goto out; |
| 9987 | } |
| 9988 | |
| 9989 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9990 | break; |
| 9991 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9992 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9993 | break; |
| 9994 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9995 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9996 | break; |
| 9997 | default: |
| 9998 | ret = -EINVAL; |
| 9999 | goto out; |
| 10000 | } |
| 10001 | } |
| 10002 | |
| 10003 | out: |
| 10004 | /* Reset all restrictions if an error happened */ |
| 10005 | if (ret != 0) |
| 10006 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10007 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10008 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10009 | |
| 10010 | kfree(res); |
| 10011 | return ret; |
| 10012 | } |
| 10013 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10014 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10015 | { |
| 10016 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10017 | return -EBADFD; |
| 10018 | |
| 10019 | if (ctx->restrictions.registered) |
| 10020 | ctx->restricted = 1; |
| 10021 | |
| 10022 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10023 | |
| 10024 | io_sq_offload_start(ctx); |
| 10025 | |
| 10026 | return 0; |
| 10027 | } |
| 10028 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10029 | static bool io_register_op_must_quiesce(int op) |
| 10030 | { |
| 10031 | switch (op) { |
| 10032 | case IORING_UNREGISTER_FILES: |
| 10033 | case IORING_REGISTER_FILES_UPDATE: |
| 10034 | case IORING_REGISTER_PROBE: |
| 10035 | case IORING_REGISTER_PERSONALITY: |
| 10036 | case IORING_UNREGISTER_PERSONALITY: |
| 10037 | return false; |
| 10038 | default: |
| 10039 | return true; |
| 10040 | } |
| 10041 | } |
| 10042 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10043 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10044 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10045 | __releases(ctx->uring_lock) |
| 10046 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10047 | { |
| 10048 | int ret; |
| 10049 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10050 | /* |
| 10051 | * We're inside the ring mutex, if the ref is already dying, then |
| 10052 | * someone else killed the ctx or is already going through |
| 10053 | * io_uring_register(). |
| 10054 | */ |
| 10055 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10056 | return -ENXIO; |
| 10057 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10058 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10059 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10060 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10061 | /* |
| 10062 | * Drop uring mutex before waiting for references to exit. If |
| 10063 | * another thread is currently inside io_uring_enter() it might |
| 10064 | * need to grab the uring_lock to make progress. If we hold it |
| 10065 | * here across the drain wait, then we can deadlock. It's safe |
| 10066 | * to drop the mutex here, since no new references will come in |
| 10067 | * after we've killed the percpu ref. |
| 10068 | */ |
| 10069 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10070 | do { |
| 10071 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10072 | if (!ret) |
| 10073 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 10074 | ret = io_run_task_work_sig(); |
| 10075 | if (ret < 0) |
| 10076 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10077 | } while (1); |
| 10078 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10079 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10080 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10081 | if (ret) { |
| 10082 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10083 | goto out_quiesce; |
| 10084 | } |
| 10085 | } |
| 10086 | |
| 10087 | if (ctx->restricted) { |
| 10088 | if (opcode >= IORING_REGISTER_LAST) { |
| 10089 | ret = -EINVAL; |
| 10090 | goto out; |
| 10091 | } |
| 10092 | |
| 10093 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10094 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10095 | goto out; |
| 10096 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10097 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10098 | |
| 10099 | switch (opcode) { |
| 10100 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10101 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10102 | break; |
| 10103 | case IORING_UNREGISTER_BUFFERS: |
| 10104 | ret = -EINVAL; |
| 10105 | if (arg || nr_args) |
| 10106 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10107 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10108 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10109 | case IORING_REGISTER_FILES: |
| 10110 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10111 | break; |
| 10112 | case IORING_UNREGISTER_FILES: |
| 10113 | ret = -EINVAL; |
| 10114 | if (arg || nr_args) |
| 10115 | break; |
| 10116 | ret = io_sqe_files_unregister(ctx); |
| 10117 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10118 | case IORING_REGISTER_FILES_UPDATE: |
| 10119 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10120 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10121 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10122 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10123 | ret = -EINVAL; |
| 10124 | if (nr_args != 1) |
| 10125 | break; |
| 10126 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10127 | if (ret) |
| 10128 | break; |
| 10129 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10130 | ctx->eventfd_async = 1; |
| 10131 | else |
| 10132 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10133 | break; |
| 10134 | case IORING_UNREGISTER_EVENTFD: |
| 10135 | ret = -EINVAL; |
| 10136 | if (arg || nr_args) |
| 10137 | break; |
| 10138 | ret = io_eventfd_unregister(ctx); |
| 10139 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10140 | case IORING_REGISTER_PROBE: |
| 10141 | ret = -EINVAL; |
| 10142 | if (!arg || nr_args > 256) |
| 10143 | break; |
| 10144 | ret = io_probe(ctx, arg, nr_args); |
| 10145 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10146 | case IORING_REGISTER_PERSONALITY: |
| 10147 | ret = -EINVAL; |
| 10148 | if (arg || nr_args) |
| 10149 | break; |
| 10150 | ret = io_register_personality(ctx); |
| 10151 | break; |
| 10152 | case IORING_UNREGISTER_PERSONALITY: |
| 10153 | ret = -EINVAL; |
| 10154 | if (arg) |
| 10155 | break; |
| 10156 | ret = io_unregister_personality(ctx, nr_args); |
| 10157 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10158 | case IORING_REGISTER_ENABLE_RINGS: |
| 10159 | ret = -EINVAL; |
| 10160 | if (arg || nr_args) |
| 10161 | break; |
| 10162 | ret = io_register_enable_rings(ctx); |
| 10163 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10164 | case IORING_REGISTER_RESTRICTIONS: |
| 10165 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10166 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10167 | default: |
| 10168 | ret = -EINVAL; |
| 10169 | break; |
| 10170 | } |
| 10171 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10172 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10173 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10174 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10175 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10176 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10177 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10178 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10179 | return ret; |
| 10180 | } |
| 10181 | |
| 10182 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10183 | void __user *, arg, unsigned int, nr_args) |
| 10184 | { |
| 10185 | struct io_ring_ctx *ctx; |
| 10186 | long ret = -EBADF; |
| 10187 | struct fd f; |
| 10188 | |
| 10189 | f = fdget(fd); |
| 10190 | if (!f.file) |
| 10191 | return -EBADF; |
| 10192 | |
| 10193 | ret = -EOPNOTSUPP; |
| 10194 | if (f.file->f_op != &io_uring_fops) |
| 10195 | goto out_fput; |
| 10196 | |
| 10197 | ctx = f.file->private_data; |
| 10198 | |
| 10199 | mutex_lock(&ctx->uring_lock); |
| 10200 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10201 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10202 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10203 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10204 | out_fput: |
| 10205 | fdput(f); |
| 10206 | return ret; |
| 10207 | } |
| 10208 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10209 | static int __init io_uring_init(void) |
| 10210 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10211 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10212 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10213 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10214 | } while (0) |
| 10215 | |
| 10216 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10217 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10218 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10219 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10220 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10221 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10222 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10223 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10224 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10225 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10226 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10227 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10228 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10229 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10230 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10231 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10232 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10233 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10234 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10235 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10236 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10237 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10238 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10239 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10240 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10241 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10242 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10243 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10244 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10245 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10246 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10247 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10248 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10249 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 10250 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 10251 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10252 | return 0; |
| 10253 | }; |
| 10254 | __initcall(io_uring_init); |