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), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 893 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 894 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 895 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 896 | .needs_file = 1, |
| 897 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 898 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 899 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 900 | .needs_async_data = 1, |
| 901 | .async_size = sizeof(struct io_async_msghdr), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 902 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 903 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 904 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 905 | .needs_async_data = 1, |
| 906 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 907 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 908 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 909 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 910 | /* used by timeout updates' prep() */ |
| 911 | .work_flags = IO_WQ_WORK_MM, |
| 912 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 913 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 914 | .needs_file = 1, |
| 915 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 916 | .pollin = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 917 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 918 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 919 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 920 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 921 | .needs_async_data = 1, |
| 922 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 923 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 924 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 925 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 926 | .needs_file = 1, |
| 927 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 928 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 929 | .needs_async_data = 1, |
| 930 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 931 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 932 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 933 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 934 | .needs_file = 1, |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 935 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 936 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 937 | [IORING_OP_OPENAT] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 938 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 939 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 940 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 941 | [IORING_OP_CLOSE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 942 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 943 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 944 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 945 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 946 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 947 | [IORING_OP_STATX] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 948 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
| 949 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 950 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 951 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 952 | .needs_file = 1, |
| 953 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 954 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 955 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 956 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 957 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 958 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 959 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 960 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 961 | .needs_file = 1, |
| 962 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 963 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 964 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 965 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 966 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 967 | IO_WQ_WORK_FSIZE, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 968 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 969 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 970 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 971 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 972 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 973 | [IORING_OP_MADVISE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 974 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 975 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 976 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 977 | .needs_file = 1, |
| 978 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 979 | .pollout = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 980 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 981 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 982 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 983 | .needs_file = 1, |
| 984 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 985 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 986 | .buffer_select = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 987 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 988 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 989 | [IORING_OP_OPENAT2] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 990 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 991 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 992 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 993 | [IORING_OP_EPOLL_CTL] = { |
| 994 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 995 | .work_flags = IO_WQ_WORK_FILES, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 996 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 997 | [IORING_OP_SPLICE] = { |
| 998 | .needs_file = 1, |
| 999 | .hash_reg_file = 1, |
| 1000 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 1001 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1002 | }, |
| 1003 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1004 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1005 | [IORING_OP_TEE] = { |
| 1006 | .needs_file = 1, |
| 1007 | .hash_reg_file = 1, |
| 1008 | .unbound_nonreg_file = 1, |
| 1009 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1010 | [IORING_OP_SHUTDOWN] = { |
| 1011 | .needs_file = 1, |
| 1012 | }, |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 1013 | [IORING_OP_RENAMEAT] = { |
| 1014 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1015 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1016 | }, |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 1017 | [IORING_OP_UNLINKAT] = { |
| 1018 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 1019 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 1020 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1021 | }; |
| 1022 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1023 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1024 | struct task_struct *task, |
| 1025 | struct files_struct *files); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1026 | 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] | 1027 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1028 | struct io_ring_ctx *ctx); |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 1029 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 1030 | struct fixed_rsrc_ref_node *ref_node); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1031 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 1032 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1033 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1034 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1035 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1036 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1037 | static void io_dismantle_req(struct io_kiocb *req); |
| 1038 | static void io_put_task(struct task_struct *task, int nr); |
| 1039 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1040 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1041 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1042 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1043 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1044 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1045 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1046 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1047 | static struct file *io_file_get(struct io_submit_state *state, |
| 1048 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1049 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1050 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1051 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1052 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1053 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1054 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1055 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1056 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1057 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1058 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1059 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1060 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1061 | static struct kmem_cache *req_cachep; |
| 1062 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1063 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1064 | |
| 1065 | struct sock *io_uring_get_socket(struct file *file) |
| 1066 | { |
| 1067 | #if defined(CONFIG_UNIX) |
| 1068 | if (file->f_op == &io_uring_fops) { |
| 1069 | struct io_ring_ctx *ctx = file->private_data; |
| 1070 | |
| 1071 | return ctx->ring_sock->sk; |
| 1072 | } |
| 1073 | #endif |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1077 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1078 | #define io_for_each_link(pos, head) \ |
| 1079 | for (pos = (head); pos; pos = pos->link) |
| 1080 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1081 | static inline void io_clean_op(struct io_kiocb *req) |
| 1082 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1083 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1084 | __io_clean_op(req); |
| 1085 | } |
| 1086 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1087 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1088 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1089 | struct io_ring_ctx *ctx = req->ctx; |
| 1090 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1091 | if (!req->fixed_rsrc_refs) { |
| 1092 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1093 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1097 | static bool io_match_task(struct io_kiocb *head, |
| 1098 | struct task_struct *task, |
| 1099 | struct files_struct *files) |
| 1100 | { |
| 1101 | struct io_kiocb *req; |
| 1102 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1103 | if (task && head->task != task) { |
| 1104 | /* in terms of cancelation, always match if req task is dead */ |
| 1105 | if (head->task->flags & PF_EXITING) |
| 1106 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1107 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1108 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1109 | if (!files) |
| 1110 | return true; |
| 1111 | |
| 1112 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1113 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1114 | continue; |
| 1115 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1116 | return true; |
| 1117 | if ((req->work.flags & IO_WQ_WORK_FILES) && |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1118 | req->work.identity->files == files) |
| 1119 | return true; |
| 1120 | } |
| 1121 | return false; |
| 1122 | } |
| 1123 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1124 | static void io_sq_thread_drop_mm_files(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1125 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1126 | struct files_struct *files = current->files; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1127 | struct mm_struct *mm = current->mm; |
| 1128 | |
| 1129 | if (mm) { |
| 1130 | kthread_unuse_mm(mm); |
| 1131 | mmput(mm); |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1132 | current->mm = NULL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1133 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1134 | if (files) { |
| 1135 | struct nsproxy *nsproxy = current->nsproxy; |
| 1136 | |
| 1137 | task_lock(current); |
| 1138 | current->files = NULL; |
| 1139 | current->nsproxy = NULL; |
| 1140 | task_unlock(current); |
| 1141 | put_files_struct(files); |
| 1142 | put_nsproxy(nsproxy); |
| 1143 | } |
| 1144 | } |
| 1145 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1146 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1147 | { |
| 1148 | if (!current->files) { |
| 1149 | struct files_struct *files; |
| 1150 | struct nsproxy *nsproxy; |
| 1151 | |
| 1152 | task_lock(ctx->sqo_task); |
| 1153 | files = ctx->sqo_task->files; |
| 1154 | if (!files) { |
| 1155 | task_unlock(ctx->sqo_task); |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1156 | return -EOWNERDEAD; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1157 | } |
| 1158 | atomic_inc(&files->count); |
| 1159 | get_nsproxy(ctx->sqo_task->nsproxy); |
| 1160 | nsproxy = ctx->sqo_task->nsproxy; |
| 1161 | task_unlock(ctx->sqo_task); |
| 1162 | |
| 1163 | task_lock(current); |
| 1164 | current->files = files; |
| 1165 | current->nsproxy = nsproxy; |
| 1166 | task_unlock(current); |
| 1167 | } |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1168 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 1172 | { |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1173 | struct mm_struct *mm; |
| 1174 | |
| 1175 | if (current->mm) |
| 1176 | return 0; |
| 1177 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1178 | task_lock(ctx->sqo_task); |
| 1179 | mm = ctx->sqo_task->mm; |
| 1180 | if (unlikely(!mm || !mmget_not_zero(mm))) |
| 1181 | mm = NULL; |
| 1182 | task_unlock(ctx->sqo_task); |
| 1183 | |
| 1184 | if (mm) { |
| 1185 | kthread_use_mm(mm); |
| 1186 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1187 | } |
| 1188 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1189 | return -EFAULT; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1190 | } |
| 1191 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1192 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1193 | struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1194 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1195 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1196 | int ret; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1197 | |
| 1198 | if (def->work_flags & IO_WQ_WORK_MM) { |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1199 | ret = __io_sq_thread_acquire_mm(ctx); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1200 | if (unlikely(ret)) |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1204 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
| 1205 | ret = __io_sq_thread_acquire_files(ctx); |
| 1206 | if (unlikely(ret)) |
| 1207 | return ret; |
| 1208 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1209 | |
| 1210 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1211 | } |
| 1212 | |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1213 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1214 | struct io_kiocb *req) |
| 1215 | { |
Pavel Begunkov | 4e32635 | 2021-02-12 03:23:52 +0000 | [diff] [blame] | 1216 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
| 1217 | return 0; |
| 1218 | return __io_sq_thread_acquire_mm_files(ctx, req); |
| 1219 | } |
| 1220 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1221 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| 1222 | struct cgroup_subsys_state **cur_css) |
| 1223 | |
| 1224 | { |
| 1225 | #ifdef CONFIG_BLK_CGROUP |
| 1226 | /* puts the old one when swapping */ |
| 1227 | if (*cur_css != ctx->sqo_blkcg_css) { |
| 1228 | kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| 1229 | *cur_css = ctx->sqo_blkcg_css; |
| 1230 | } |
| 1231 | #endif |
| 1232 | } |
| 1233 | |
| 1234 | static void io_sq_thread_unassociate_blkcg(void) |
| 1235 | { |
| 1236 | #ifdef CONFIG_BLK_CGROUP |
| 1237 | kthread_associate_blkcg(NULL); |
| 1238 | #endif |
| 1239 | } |
| 1240 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1241 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1242 | { |
| 1243 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1244 | req->flags |= REQ_F_FAIL_LINK; |
| 1245 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1246 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1247 | /* |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1248 | * None of these are dereferenced, they are simply used to check if any of |
| 1249 | * them have changed. If we're under current and check they are still the |
| 1250 | * same, we're fine to grab references to them for actual out-of-line use. |
| 1251 | */ |
| 1252 | static void io_init_identity(struct io_identity *id) |
| 1253 | { |
| 1254 | id->files = current->files; |
| 1255 | id->mm = current->mm; |
| 1256 | #ifdef CONFIG_BLK_CGROUP |
| 1257 | rcu_read_lock(); |
| 1258 | id->blkcg_css = blkcg_css(); |
| 1259 | rcu_read_unlock(); |
| 1260 | #endif |
| 1261 | id->creds = current_cred(); |
| 1262 | id->nsproxy = current->nsproxy; |
| 1263 | id->fs = current->fs; |
| 1264 | id->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1265 | #ifdef CONFIG_AUDIT |
| 1266 | id->loginuid = current->loginuid; |
| 1267 | id->sessionid = current->sessionid; |
| 1268 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1269 | refcount_set(&id->count, 1); |
| 1270 | } |
| 1271 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1272 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1273 | { |
| 1274 | memset(&req->work, 0, sizeof(req->work)); |
| 1275 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1276 | } |
| 1277 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1278 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1279 | * Note: must call io_req_init_async() for the first time you |
| 1280 | * touch any members of io_wq_work. |
| 1281 | */ |
| 1282 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1283 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1284 | struct io_uring_task *tctx = current->io_uring; |
| 1285 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1286 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1287 | return; |
| 1288 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1289 | __io_req_init_async(req); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1290 | |
| 1291 | /* Grab a ref if this isn't our static identity */ |
| 1292 | req->work.identity = tctx->identity; |
| 1293 | if (tctx->identity != &tctx->__identity) |
| 1294 | refcount_inc(&req->work.identity->count); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1295 | } |
| 1296 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1297 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1298 | { |
| 1299 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1300 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1301 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1304 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1305 | { |
| 1306 | return !req->timeout.off; |
| 1307 | } |
| 1308 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1309 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1310 | { |
| 1311 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1312 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1313 | |
| 1314 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1315 | if (!ctx) |
| 1316 | return NULL; |
| 1317 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1318 | /* |
| 1319 | * Use 5 bits less than the max cq entries, that should give us around |
| 1320 | * 32 entries per hash list if totally full and uniformly spread. |
| 1321 | */ |
| 1322 | hash_bits = ilog2(p->cq_entries); |
| 1323 | hash_bits -= 5; |
| 1324 | if (hash_bits <= 0) |
| 1325 | hash_bits = 1; |
| 1326 | ctx->cancel_hash_bits = hash_bits; |
| 1327 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1328 | GFP_KERNEL); |
| 1329 | if (!ctx->cancel_hash) |
| 1330 | goto err; |
| 1331 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1332 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1333 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1334 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1335 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1336 | |
| 1337 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1338 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1339 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1340 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1341 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1342 | init_completion(&ctx->ref_comp); |
| 1343 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1344 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1345 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1346 | mutex_init(&ctx->uring_lock); |
| 1347 | init_waitqueue_head(&ctx->wait); |
| 1348 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1349 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1350 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1351 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1352 | spin_lock_init(&ctx->inflight_lock); |
| 1353 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1354 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1355 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1356 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1357 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1358 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1359 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1360 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1361 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1362 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1363 | kfree(ctx); |
| 1364 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1367 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1368 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1369 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1370 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1371 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1372 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1373 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1374 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1375 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1376 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1377 | } |
| 1378 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1379 | 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] | 1380 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1381 | if (req->work.identity == &tctx->__identity) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1382 | return; |
| 1383 | if (refcount_dec_and_test(&req->work.identity->count)) |
| 1384 | kfree(req->work.identity); |
| 1385 | } |
| 1386 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1387 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1388 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1389 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1390 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1391 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1392 | if (req->work.flags & IO_WQ_WORK_MM) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1393 | mmdrop(req->work.identity->mm); |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1394 | #ifdef CONFIG_BLK_CGROUP |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1395 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1396 | css_put(req->work.identity->blkcg_css); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1397 | #endif |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1398 | if (req->work.flags & IO_WQ_WORK_CREDS) |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1399 | put_cred(req->work.identity->creds); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1400 | if (req->work.flags & IO_WQ_WORK_FS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1401 | struct fs_struct *fs = req->work.identity->fs; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1402 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1403 | spin_lock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1404 | if (--fs->users) |
| 1405 | fs = NULL; |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1406 | spin_unlock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1407 | if (fs) |
| 1408 | free_fs_struct(fs); |
| 1409 | } |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1410 | if (req->work.flags & IO_WQ_WORK_FILES) { |
| 1411 | put_files_struct(req->work.identity->files); |
| 1412 | put_nsproxy(req->work.identity->nsproxy); |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1413 | } |
| 1414 | if (req->flags & REQ_F_INFLIGHT) { |
| 1415 | struct io_ring_ctx *ctx = req->ctx; |
| 1416 | struct io_uring_task *tctx = req->task->io_uring; |
| 1417 | unsigned long flags; |
| 1418 | |
| 1419 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1420 | list_del(&req->inflight_entry); |
| 1421 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1422 | req->flags &= ~REQ_F_INFLIGHT; |
| 1423 | if (atomic_read(&tctx->in_idle)) |
| 1424 | wake_up(&tctx->wait); |
| 1425 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1426 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1427 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
| 1428 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | |
| 1429 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1430 | io_put_identity(req->task->io_uring, req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Create a private copy of io_identity, since some fields don't match |
| 1435 | * the current context. |
| 1436 | */ |
| 1437 | static bool io_identity_cow(struct io_kiocb *req) |
| 1438 | { |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1439 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1440 | const struct cred *creds = NULL; |
| 1441 | struct io_identity *id; |
| 1442 | |
| 1443 | if (req->work.flags & IO_WQ_WORK_CREDS) |
| 1444 | creds = req->work.identity->creds; |
| 1445 | |
| 1446 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| 1447 | if (unlikely(!id)) { |
| 1448 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * We can safely just re-init the creds we copied Either the field |
| 1454 | * matches the current one, or we haven't grabbed it yet. The only |
| 1455 | * exception is ->creds, through registered personalities, so handle |
| 1456 | * that one separately. |
| 1457 | */ |
| 1458 | io_init_identity(id); |
| 1459 | if (creds) |
Pavel Begunkov | e8c954d | 2020-12-06 22:22:46 +0000 | [diff] [blame] | 1460 | id->creds = creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1461 | |
| 1462 | /* add one for this request */ |
| 1463 | refcount_inc(&id->count); |
| 1464 | |
Jens Axboe | cb8a8ae | 2020-11-03 12:19:07 -0700 | [diff] [blame] | 1465 | /* drop tctx and req identity references, if needed */ |
| 1466 | if (tctx->identity != &tctx->__identity && |
| 1467 | refcount_dec_and_test(&tctx->identity->count)) |
| 1468 | kfree(tctx->identity); |
| 1469 | if (req->work.identity != &tctx->__identity && |
| 1470 | refcount_dec_and_test(&req->work.identity->count)) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1471 | kfree(req->work.identity); |
| 1472 | |
| 1473 | req->work.identity = id; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1474 | tctx->identity = id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1475 | return true; |
| 1476 | } |
| 1477 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1478 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1479 | { |
| 1480 | struct io_ring_ctx *ctx = req->ctx; |
| 1481 | |
| 1482 | if (!(req->flags & REQ_F_INFLIGHT)) { |
| 1483 | io_req_init_async(req); |
| 1484 | req->flags |= REQ_F_INFLIGHT; |
| 1485 | |
| 1486 | spin_lock_irq(&ctx->inflight_lock); |
| 1487 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1488 | spin_unlock_irq(&ctx->inflight_lock); |
| 1489 | } |
| 1490 | } |
| 1491 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1492 | static bool io_grab_identity(struct io_kiocb *req) |
| 1493 | { |
| 1494 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1495 | struct io_identity *id = req->work.identity; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1496 | |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 1497 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| 1498 | if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| 1499 | return false; |
| 1500 | req->work.flags |= IO_WQ_WORK_FSIZE; |
| 1501 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1502 | #ifdef CONFIG_BLK_CGROUP |
| 1503 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| 1504 | (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| 1505 | rcu_read_lock(); |
| 1506 | if (id->blkcg_css != blkcg_css()) { |
| 1507 | rcu_read_unlock(); |
| 1508 | return false; |
| 1509 | } |
| 1510 | /* |
| 1511 | * This should be rare, either the cgroup is dying or the task |
| 1512 | * is moving cgroups. Just punt to root for the handful of ios. |
| 1513 | */ |
| 1514 | if (css_tryget_online(id->blkcg_css)) |
| 1515 | req->work.flags |= IO_WQ_WORK_BLKCG; |
| 1516 | rcu_read_unlock(); |
| 1517 | } |
| 1518 | #endif |
| 1519 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| 1520 | if (id->creds != current_cred()) |
| 1521 | return false; |
| 1522 | get_cred(id->creds); |
| 1523 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 1524 | } |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1525 | #ifdef CONFIG_AUDIT |
| 1526 | if (!uid_eq(current->loginuid, id->loginuid) || |
| 1527 | current->sessionid != id->sessionid) |
| 1528 | return false; |
| 1529 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1530 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
| 1531 | (def->work_flags & IO_WQ_WORK_FS)) { |
| 1532 | if (current->fs != id->fs) |
| 1533 | return false; |
| 1534 | spin_lock(&id->fs->lock); |
| 1535 | if (!id->fs->in_exec) { |
| 1536 | id->fs->users++; |
| 1537 | req->work.flags |= IO_WQ_WORK_FS; |
| 1538 | } else { |
| 1539 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1540 | } |
| 1541 | spin_unlock(¤t->fs->lock); |
| 1542 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1543 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| 1544 | (def->work_flags & IO_WQ_WORK_FILES) && |
| 1545 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| 1546 | if (id->files != current->files || |
| 1547 | id->nsproxy != current->nsproxy) |
| 1548 | return false; |
| 1549 | atomic_inc(&id->files->count); |
| 1550 | get_nsproxy(id->nsproxy); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1551 | req->work.flags |= IO_WQ_WORK_FILES; |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1552 | io_req_track_inflight(req); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1553 | } |
Jens Axboe | 7778877 | 2020-12-29 10:50:46 -0700 | [diff] [blame] | 1554 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
| 1555 | (def->work_flags & IO_WQ_WORK_MM)) { |
| 1556 | if (id->mm != current->mm) |
| 1557 | return false; |
| 1558 | mmgrab(id->mm); |
| 1559 | req->work.flags |= IO_WQ_WORK_MM; |
| 1560 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1561 | |
| 1562 | return true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1563 | } |
| 1564 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1565 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1566 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1567 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1568 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1569 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1570 | io_req_init_async(req); |
| 1571 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1572 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1573 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1574 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1575 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1576 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1577 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1578 | } else { |
| 1579 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1580 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1581 | } |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1582 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1583 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| 1584 | if (io_grab_identity(req)) |
| 1585 | return; |
| 1586 | |
| 1587 | if (!io_identity_cow(req)) |
| 1588 | return; |
| 1589 | |
| 1590 | /* can't fail at this point */ |
| 1591 | if (!io_grab_identity(req)) |
| 1592 | WARN_ON(1); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1595 | static void io_prep_async_link(struct io_kiocb *req) |
| 1596 | { |
| 1597 | struct io_kiocb *cur; |
| 1598 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1599 | io_for_each_link(cur, req) |
| 1600 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1601 | } |
| 1602 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1603 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1604 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1605 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1606 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1607 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1608 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1609 | &req->work, req->flags); |
| 1610 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1611 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1612 | } |
| 1613 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1614 | static void io_queue_async_work(struct io_kiocb *req) |
| 1615 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1616 | struct io_kiocb *link; |
| 1617 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1618 | /* init ->work of the whole link before punting */ |
| 1619 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1620 | link = __io_queue_async_work(req); |
| 1621 | |
| 1622 | if (link) |
| 1623 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1624 | } |
| 1625 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1626 | static void io_kill_timeout(struct io_kiocb *req) |
| 1627 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1628 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1629 | int ret; |
| 1630 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1631 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1632 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1633 | atomic_set(&req->ctx->cq_timeouts, |
| 1634 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1635 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1636 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1637 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1641 | /* |
| 1642 | * Returns true if we found and killed one or more timeouts |
| 1643 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1644 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1645 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1646 | { |
| 1647 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1648 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1649 | |
| 1650 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1651 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1652 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1653 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1654 | canceled++; |
| 1655 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1656 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1657 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1658 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1659 | } |
| 1660 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1661 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1662 | { |
| 1663 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1664 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1665 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1666 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1667 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1668 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1669 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1670 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1671 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1672 | } while (!list_empty(&ctx->defer_list)); |
| 1673 | } |
| 1674 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1675 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1676 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1677 | u32 seq; |
| 1678 | |
| 1679 | if (list_empty(&ctx->timeout_list)) |
| 1680 | return; |
| 1681 | |
| 1682 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1683 | |
| 1684 | do { |
| 1685 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1686 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1687 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1688 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1689 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1690 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1691 | |
| 1692 | /* |
| 1693 | * Since seq can easily wrap around over time, subtract |
| 1694 | * the last seq at which timeouts were flushed before comparing. |
| 1695 | * Assuming not more than 2^31-1 events have happened since, |
| 1696 | * these subtractions won't have wrapped, so we can check if |
| 1697 | * target is in [last_seq, current_seq] by comparing the two. |
| 1698 | */ |
| 1699 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1700 | events_got = seq - ctx->cq_last_tm_flush; |
| 1701 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1702 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1703 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1704 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1705 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1706 | } while (!list_empty(&ctx->timeout_list)); |
| 1707 | |
| 1708 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1709 | } |
| 1710 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1711 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1712 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1713 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1714 | |
| 1715 | /* order cqe stores with ring update */ |
| 1716 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1717 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1718 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1719 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1720 | } |
| 1721 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1722 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1723 | { |
| 1724 | struct io_rings *r = ctx->rings; |
| 1725 | |
| 1726 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1727 | } |
| 1728 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1729 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1730 | { |
| 1731 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1732 | } |
| 1733 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1734 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1735 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1736 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1737 | unsigned tail; |
| 1738 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1739 | /* |
| 1740 | * writes to the cq entry need to come after reading head; the |
| 1741 | * control dependency is enough as we're using WRITE_ONCE to |
| 1742 | * fill the cq entry |
| 1743 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1744 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1745 | return NULL; |
| 1746 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1747 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1748 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1751 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1752 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1753 | if (!ctx->cq_ev_fd) |
| 1754 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1755 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1756 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1757 | if (!ctx->eventfd_async) |
| 1758 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1759 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1762 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1763 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1764 | /* see waitqueue_active() comment */ |
| 1765 | smp_mb(); |
| 1766 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1767 | if (waitqueue_active(&ctx->wait)) |
| 1768 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1769 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1770 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1771 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1772 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1773 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1774 | wake_up_interruptible(&ctx->cq_wait); |
| 1775 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1776 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1777 | } |
| 1778 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1779 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1780 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1781 | /* see waitqueue_active() comment */ |
| 1782 | smp_mb(); |
| 1783 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1784 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1785 | if (waitqueue_active(&ctx->wait)) |
| 1786 | wake_up(&ctx->wait); |
| 1787 | } |
| 1788 | if (io_should_trigger_evfd(ctx)) |
| 1789 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1790 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1791 | wake_up_interruptible(&ctx->cq_wait); |
| 1792 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1793 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1796 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1797 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1798 | struct task_struct *tsk, |
| 1799 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1800 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1801 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1802 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1803 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1804 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1805 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1806 | LIST_HEAD(list); |
| 1807 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1808 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1809 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1810 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1811 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1812 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1813 | 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] | 1814 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1815 | continue; |
| 1816 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1817 | cqe = io_get_cqring(ctx); |
| 1818 | if (!cqe && !force) |
| 1819 | break; |
| 1820 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1821 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1822 | if (cqe) { |
| 1823 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1824 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1825 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1826 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1827 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1828 | WRITE_ONCE(ctx->rings->cq_overflow, |
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 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1831 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1834 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1835 | if (all_flushed) { |
| 1836 | clear_bit(0, &ctx->sq_check_overflow); |
| 1837 | clear_bit(0, &ctx->cq_check_overflow); |
| 1838 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1839 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1840 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1841 | if (posted) |
| 1842 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1843 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1844 | if (posted) |
| 1845 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1846 | |
| 1847 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1848 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1849 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1850 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1851 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1852 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1853 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1856 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1857 | struct task_struct *tsk, |
| 1858 | struct files_struct *files) |
| 1859 | { |
| 1860 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1861 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1862 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1863 | mutex_lock(&ctx->uring_lock); |
| 1864 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1865 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1866 | mutex_unlock(&ctx->uring_lock); |
| 1867 | } |
| 1868 | } |
| 1869 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1870 | 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] | 1871 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1872 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1873 | struct io_uring_cqe *cqe; |
| 1874 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1875 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1876 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1877 | /* |
| 1878 | * If we can't get a cq entry, userspace overflowed the |
| 1879 | * submission (by quite a lot). Increment the overflow count in |
| 1880 | * the ring. |
| 1881 | */ |
| 1882 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1883 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1884 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1885 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1886 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1887 | } else if (ctx->cq_overflow_flushed || |
| 1888 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1889 | /* |
| 1890 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1891 | * then we cannot store the request for later flushing, we need |
| 1892 | * to drop it on the floor. |
| 1893 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1894 | ctx->cached_cq_overflow++; |
| 1895 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1896 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1897 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1898 | set_bit(0, &ctx->sq_check_overflow); |
| 1899 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1900 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1901 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1902 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1903 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1904 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1905 | refcount_inc(&req->refs); |
| 1906 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1910 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1911 | { |
| 1912 | __io_cqring_fill_event(req, res, 0); |
| 1913 | } |
| 1914 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1915 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1916 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1917 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1918 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1919 | unsigned long flags; |
| 1920 | |
| 1921 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1922 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1923 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1924 | /* |
| 1925 | * If we're the last reference to this request, add to our locked |
| 1926 | * free_list cache. |
| 1927 | */ |
| 1928 | if (refcount_dec_and_test(&req->refs)) { |
| 1929 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1930 | |
| 1931 | io_dismantle_req(req); |
| 1932 | io_put_task(req->task, 1); |
| 1933 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1934 | cs->locked_free_nr++; |
| 1935 | } else |
| 1936 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1937 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1938 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1939 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1940 | if (req) { |
| 1941 | io_queue_next(req); |
| 1942 | percpu_ref_put(&ctx->refs); |
| 1943 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1946 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1947 | unsigned int cflags) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1948 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1949 | io_clean_op(req); |
| 1950 | req->result = res; |
| 1951 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1952 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1953 | } |
| 1954 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1955 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1956 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1957 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1958 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1959 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1960 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1961 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1965 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1966 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1969 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1970 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1971 | struct io_submit_state *state = &ctx->submit_state; |
| 1972 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1973 | struct io_kiocb *req = NULL; |
| 1974 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1975 | /* |
| 1976 | * If we have more than a batch's worth of requests in our IRQ side |
| 1977 | * locked cache, grab the lock and move them over to our submission |
| 1978 | * side cache. |
| 1979 | */ |
| 1980 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 1981 | spin_lock_irq(&ctx->completion_lock); |
| 1982 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1983 | cs->locked_free_nr = 0; |
| 1984 | spin_unlock_irq(&ctx->completion_lock); |
| 1985 | } |
| 1986 | |
| 1987 | while (!list_empty(&cs->free_list)) { |
| 1988 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1989 | compl.list); |
| 1990 | list_del(&req->compl.list); |
| 1991 | state->reqs[state->free_reqs++] = req; |
| 1992 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | return req != NULL; |
| 1997 | } |
| 1998 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1999 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2000 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2001 | struct io_submit_state *state = &ctx->submit_state; |
| 2002 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2003 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
| 2004 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 2005 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2006 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 2007 | int ret; |
| 2008 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2009 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2010 | goto got_req; |
| 2011 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 2012 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 2013 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2014 | |
| 2015 | /* |
| 2016 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2017 | * retry single alloc to be on the safe side. |
| 2018 | */ |
| 2019 | if (unlikely(ret <= 0)) { |
| 2020 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2021 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2022 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 2023 | ret = 1; |
| 2024 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2025 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2027 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2028 | state->free_reqs--; |
| 2029 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2032 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 2033 | bool fixed) |
| 2034 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2035 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2036 | fput(file); |
| 2037 | } |
| 2038 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2039 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2040 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 2041 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2042 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2043 | if (req->async_data) |
| 2044 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2045 | if (req->file) |
| 2046 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 2047 | if (req->fixed_rsrc_refs) |
| 2048 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2049 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2050 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2051 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2052 | static inline void io_put_task(struct task_struct *task, int nr) |
| 2053 | { |
| 2054 | struct io_uring_task *tctx = task->io_uring; |
| 2055 | |
| 2056 | percpu_counter_sub(&tctx->inflight, nr); |
| 2057 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 2058 | wake_up(&tctx->wait); |
| 2059 | put_task_struct_many(task, nr); |
| 2060 | } |
| 2061 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2062 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2063 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2064 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2065 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2066 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2067 | io_put_task(req->task, 1); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2068 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 2069 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2070 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2071 | } |
| 2072 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2073 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2074 | { |
| 2075 | struct io_kiocb *nxt = req->link; |
| 2076 | |
| 2077 | req->link = nxt->link; |
| 2078 | nxt->link = NULL; |
| 2079 | } |
| 2080 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2081 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2082 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2083 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2084 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2085 | bool cancelled = false; |
| 2086 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2087 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2088 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2089 | link = req->link; |
| 2090 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 2091 | /* |
| 2092 | * Can happen if a linked timeout fired and link had been like |
| 2093 | * req -> link t-out -> link t-out [-> ...] |
| 2094 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2095 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 2096 | struct io_timeout_data *io = link->async_data; |
| 2097 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2098 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2099 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2100 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2101 | ret = hrtimer_try_to_cancel(&io->timer); |
| 2102 | if (ret != -1) { |
| 2103 | io_cqring_fill_event(link, -ECANCELED); |
| 2104 | io_commit_cqring(ctx); |
| 2105 | cancelled = true; |
| 2106 | } |
| 2107 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2108 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2109 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 2110 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2111 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2112 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2113 | io_put_req(link); |
| 2114 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2115 | } |
| 2116 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2117 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2118 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2119 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2120 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2121 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2122 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2123 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2124 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2125 | link = req->link; |
| 2126 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2127 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2128 | while (link) { |
| 2129 | nxt = link->link; |
| 2130 | link->link = NULL; |
| 2131 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2132 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2133 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2134 | |
| 2135 | /* |
| 2136 | * It's ok to free under spinlock as they're not linked anymore, |
| 2137 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 2138 | * work.fs->lock. |
| 2139 | */ |
| 2140 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 2141 | io_put_req_deferred(link, 2); |
| 2142 | else |
| 2143 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2144 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2145 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2146 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2147 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2148 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2149 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2150 | } |
| 2151 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2152 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2153 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2154 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 2155 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2156 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2157 | /* |
| 2158 | * If LINK is set, we have dependent requests in this chain. If we |
| 2159 | * didn't fail this request, queue the first one up, moving any other |
| 2160 | * dependencies to the next request. In case of failure, fail the rest |
| 2161 | * of the chain. |
| 2162 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2163 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 2164 | struct io_kiocb *nxt = req->link; |
| 2165 | |
| 2166 | req->link = NULL; |
| 2167 | return nxt; |
| 2168 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2169 | io_fail_links(req); |
| 2170 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2171 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2172 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2173 | 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] | 2174 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 2175 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2176 | return NULL; |
| 2177 | return __io_req_find_next(req); |
| 2178 | } |
| 2179 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2180 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 2181 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2182 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2183 | struct io_wq_work_list list; |
| 2184 | struct io_wq_work_node *node; |
| 2185 | |
| 2186 | if (wq_list_empty(&tctx->task_list)) |
| 2187 | return false; |
| 2188 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2189 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2190 | list = tctx->task_list; |
| 2191 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2192 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2193 | |
| 2194 | node = list.first; |
| 2195 | while (node) { |
| 2196 | struct io_wq_work_node *next = node->next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2197 | struct io_ring_ctx *this_ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2198 | struct io_kiocb *req; |
| 2199 | |
| 2200 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2201 | this_ctx = req->ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2202 | req->task_work.func(&req->task_work); |
| 2203 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (!ctx) { |
| 2206 | ctx = this_ctx; |
| 2207 | } else if (ctx != this_ctx) { |
| 2208 | mutex_lock(&ctx->uring_lock); |
| 2209 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2210 | mutex_unlock(&ctx->uring_lock); |
| 2211 | ctx = this_ctx; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | if (ctx && ctx->submit_state.comp.nr) { |
| 2216 | mutex_lock(&ctx->uring_lock); |
| 2217 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 2218 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | return list.first != NULL; |
| 2222 | } |
| 2223 | |
| 2224 | static void tctx_task_work(struct callback_head *cb) |
| 2225 | { |
| 2226 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 2227 | |
| 2228 | while (__tctx_task_work(tctx)) |
| 2229 | cond_resched(); |
| 2230 | |
| 2231 | clear_bit(0, &tctx->task_state); |
| 2232 | } |
| 2233 | |
| 2234 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 2235 | enum task_work_notify_mode notify) |
| 2236 | { |
| 2237 | struct io_uring_task *tctx = tsk->io_uring; |
| 2238 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2239 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2240 | int ret; |
| 2241 | |
| 2242 | WARN_ON_ONCE(!tctx); |
| 2243 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2244 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2245 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2246 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2247 | |
| 2248 | /* task_work already pending, we're done */ |
| 2249 | if (test_bit(0, &tctx->task_state) || |
| 2250 | test_and_set_bit(0, &tctx->task_state)) |
| 2251 | return 0; |
| 2252 | |
| 2253 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 2254 | return 0; |
| 2255 | |
| 2256 | /* |
| 2257 | * Slow path - we failed, find and delete work. if the work is not |
| 2258 | * in the list, it got run and we're fine. |
| 2259 | */ |
| 2260 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2261 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2262 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 2263 | if (&req->io_task_work.node == node) { |
| 2264 | wq_list_del(&tctx->task_list, node, prev); |
| 2265 | ret = 1; |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame^] | 2269 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2270 | clear_bit(0, &tctx->task_state); |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2274 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2275 | { |
| 2276 | struct task_struct *tsk = req->task; |
| 2277 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2278 | enum task_work_notify_mode notify; |
| 2279 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2280 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2281 | if (tsk->flags & PF_EXITING) |
| 2282 | return -ESRCH; |
| 2283 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2284 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2285 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2286 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2287 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2288 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2289 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2290 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2291 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2292 | notify = TWA_SIGNAL; |
| 2293 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2294 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2295 | if (!ret) |
| 2296 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2297 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2298 | return ret; |
| 2299 | } |
| 2300 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2301 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2302 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2303 | { |
| 2304 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); |
| 2305 | |
| 2306 | init_task_work(&req->task_work, cb); |
| 2307 | task_work_add(tsk, &req->task_work, TWA_NONE); |
| 2308 | wake_up_process(tsk); |
| 2309 | } |
| 2310 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2311 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2312 | { |
| 2313 | struct io_ring_ctx *ctx = req->ctx; |
| 2314 | |
| 2315 | spin_lock_irq(&ctx->completion_lock); |
| 2316 | io_cqring_fill_event(req, error); |
| 2317 | io_commit_cqring(ctx); |
| 2318 | spin_unlock_irq(&ctx->completion_lock); |
| 2319 | |
| 2320 | io_cqring_ev_posted(ctx); |
| 2321 | req_set_fail_links(req); |
| 2322 | io_double_put_req(req); |
| 2323 | } |
| 2324 | |
| 2325 | static void io_req_task_cancel(struct callback_head *cb) |
| 2326 | { |
| 2327 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2328 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2329 | |
| 2330 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2331 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2335 | { |
| 2336 | struct io_ring_ctx *ctx = req->ctx; |
| 2337 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2338 | /* 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] | 2339 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | dc0eced | 2021-02-12 18:41:15 +0000 | [diff] [blame] | 2340 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
| 2341 | !io_sq_thread_acquire_mm_files(ctx, req)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2342 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2343 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2344 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2345 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2346 | } |
| 2347 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2348 | static void io_req_task_submit(struct callback_head *cb) |
| 2349 | { |
| 2350 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2351 | |
| 2352 | __io_req_task_submit(req); |
| 2353 | } |
| 2354 | |
| 2355 | static void io_req_task_queue(struct io_kiocb *req) |
| 2356 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2357 | int ret; |
| 2358 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2359 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2360 | ret = io_req_task_work_add(req); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2361 | if (unlikely(ret)) { |
| 2362 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2363 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2364 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2365 | } |
| 2366 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2367 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2368 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2369 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2370 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2371 | if (nxt) |
| 2372 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2373 | } |
| 2374 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2375 | static void io_free_req(struct io_kiocb *req) |
| 2376 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2377 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2378 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2379 | } |
| 2380 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2381 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2382 | struct task_struct *task; |
| 2383 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2384 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2385 | }; |
| 2386 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2387 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2388 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2389 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2390 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2391 | rb->task = NULL; |
| 2392 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2393 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2394 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2395 | struct req_batch *rb) |
| 2396 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2397 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2398 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2399 | if (rb->ctx_refs) |
| 2400 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2401 | } |
| 2402 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2403 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2404 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2405 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2406 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2407 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2408 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2409 | if (rb->task) |
| 2410 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2411 | rb->task = req->task; |
| 2412 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2413 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2414 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2415 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2416 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2417 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2418 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2419 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2420 | else |
| 2421 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2422 | } |
| 2423 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2424 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2425 | struct io_ring_ctx *ctx) |
| 2426 | { |
| 2427 | int i, nr = cs->nr; |
| 2428 | struct io_kiocb *req; |
| 2429 | struct req_batch rb; |
| 2430 | |
| 2431 | io_init_req_batch(&rb); |
| 2432 | spin_lock_irq(&ctx->completion_lock); |
| 2433 | for (i = 0; i < nr; i++) { |
| 2434 | req = cs->reqs[i]; |
| 2435 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2436 | } |
| 2437 | io_commit_cqring(ctx); |
| 2438 | spin_unlock_irq(&ctx->completion_lock); |
| 2439 | |
| 2440 | io_cqring_ev_posted(ctx); |
| 2441 | for (i = 0; i < nr; i++) { |
| 2442 | req = cs->reqs[i]; |
| 2443 | |
| 2444 | /* submission and completion refs */ |
| 2445 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2446 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | io_req_free_batch_finish(ctx, &rb); |
| 2450 | cs->nr = 0; |
| 2451 | } |
| 2452 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2453 | /* |
| 2454 | * Drop reference to request, return next in chain (if there is one) if this |
| 2455 | * was the last reference to this request. |
| 2456 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2457 | 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] | 2458 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2459 | struct io_kiocb *nxt = NULL; |
| 2460 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2461 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2462 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2463 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2464 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2465 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2468 | static void io_put_req(struct io_kiocb *req) |
| 2469 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2470 | if (refcount_dec_and_test(&req->refs)) |
| 2471 | io_free_req(req); |
| 2472 | } |
| 2473 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2474 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2475 | { |
| 2476 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2477 | |
| 2478 | io_free_req(req); |
| 2479 | } |
| 2480 | |
| 2481 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2482 | { |
| 2483 | int ret; |
| 2484 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2485 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2486 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2487 | if (unlikely(ret)) |
| 2488 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2492 | { |
| 2493 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2494 | io_free_req_deferred(req); |
| 2495 | } |
| 2496 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2497 | static void io_double_put_req(struct io_kiocb *req) |
| 2498 | { |
| 2499 | /* drop both submit and complete references */ |
| 2500 | if (refcount_sub_and_test(2, &req->refs)) |
| 2501 | io_free_req(req); |
| 2502 | } |
| 2503 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2504 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2505 | { |
| 2506 | /* See comment at the top of this file */ |
| 2507 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2508 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2509 | } |
| 2510 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2511 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2512 | { |
| 2513 | struct io_rings *rings = ctx->rings; |
| 2514 | |
| 2515 | /* make sure SQ entry isn't read before tail */ |
| 2516 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2517 | } |
| 2518 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2519 | 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] | 2520 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2521 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2522 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2523 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2524 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2525 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2526 | kfree(kbuf); |
| 2527 | return cflags; |
| 2528 | } |
| 2529 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2530 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2531 | { |
| 2532 | struct io_buffer *kbuf; |
| 2533 | |
| 2534 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2535 | return io_put_kbuf(req, kbuf); |
| 2536 | } |
| 2537 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2538 | static inline bool io_run_task_work(void) |
| 2539 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2540 | /* |
| 2541 | * Not safe to run on exiting task, and the task_work handling will |
| 2542 | * not add work to such a task. |
| 2543 | */ |
| 2544 | if (unlikely(current->flags & PF_EXITING)) |
| 2545 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2546 | if (current->task_works) { |
| 2547 | __set_current_state(TASK_RUNNING); |
| 2548 | task_work_run(); |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
| 2552 | return false; |
| 2553 | } |
| 2554 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2555 | /* |
| 2556 | * Find and free completed poll iocbs |
| 2557 | */ |
| 2558 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2559 | struct list_head *done) |
| 2560 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2561 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2562 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2563 | |
| 2564 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2565 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2566 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2567 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2568 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2569 | int cflags = 0; |
| 2570 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2571 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2572 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2573 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2574 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2575 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2576 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2577 | continue; |
| 2578 | } |
| 2579 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2580 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2581 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2582 | |
| 2583 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2584 | (*nr_events)++; |
| 2585 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2586 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2587 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2588 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2589 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2590 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2591 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2592 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2595 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2596 | long min) |
| 2597 | { |
| 2598 | struct io_kiocb *req, *tmp; |
| 2599 | LIST_HEAD(done); |
| 2600 | bool spin; |
| 2601 | int ret; |
| 2602 | |
| 2603 | /* |
| 2604 | * Only spin for completions if we don't have multiple devices hanging |
| 2605 | * off our complete list, and we're under the requested amount. |
| 2606 | */ |
| 2607 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2608 | |
| 2609 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2610 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2611 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2612 | |
| 2613 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2614 | * Move completed and retryable entries to our local lists. |
| 2615 | * If we find a request that requires polling, break out |
| 2616 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2617 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2618 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2619 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2620 | continue; |
| 2621 | } |
| 2622 | if (!list_empty(&done)) |
| 2623 | break; |
| 2624 | |
| 2625 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2626 | if (ret < 0) |
| 2627 | break; |
| 2628 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2629 | /* iopoll may have completed current req */ |
| 2630 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2631 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2632 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2633 | if (ret && spin) |
| 2634 | spin = false; |
| 2635 | ret = 0; |
| 2636 | } |
| 2637 | |
| 2638 | if (!list_empty(&done)) |
| 2639 | io_iopoll_complete(ctx, nr_events, &done); |
| 2640 | |
| 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2645 | * 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] | 2646 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2647 | * as a non-spinning completion check. |
| 2648 | */ |
| 2649 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2650 | long min) |
| 2651 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2652 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2653 | int ret; |
| 2654 | |
| 2655 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2656 | if (ret < 0) |
| 2657 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2658 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2659 | return 0; |
| 2660 | } |
| 2661 | |
| 2662 | return 1; |
| 2663 | } |
| 2664 | |
| 2665 | /* |
| 2666 | * We can't just wait for polled events to come to us, we have to actively |
| 2667 | * find and complete them. |
| 2668 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2669 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2670 | { |
| 2671 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2672 | return; |
| 2673 | |
| 2674 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2675 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2676 | unsigned int nr_events = 0; |
| 2677 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2678 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2679 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2680 | /* let it sleep and repeat later if can't complete a request */ |
| 2681 | if (nr_events == 0) |
| 2682 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2683 | /* |
| 2684 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2685 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2686 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2687 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2688 | if (need_resched()) { |
| 2689 | mutex_unlock(&ctx->uring_lock); |
| 2690 | cond_resched(); |
| 2691 | mutex_lock(&ctx->uring_lock); |
| 2692 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2693 | } |
| 2694 | mutex_unlock(&ctx->uring_lock); |
| 2695 | } |
| 2696 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2697 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2698 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2699 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2700 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2701 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2702 | /* |
| 2703 | * We disallow the app entering submit/complete with polling, but we |
| 2704 | * still need to lock the ring to prevent racing with polled issue |
| 2705 | * that got punted to a workqueue. |
| 2706 | */ |
| 2707 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2708 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2709 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2710 | * Don't enter poll loop if we already have events pending. |
| 2711 | * If we do, we can potentially be spinning for commands that |
| 2712 | * already triggered a CQE (eg in error). |
| 2713 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2714 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2715 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2716 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2717 | break; |
| 2718 | |
| 2719 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2720 | * If a submit got punted to a workqueue, we can have the |
| 2721 | * application entering polling for a command before it gets |
| 2722 | * issued. That app will hold the uring_lock for the duration |
| 2723 | * of the poll right here, so we need to take a breather every |
| 2724 | * now and then to ensure that the issue has a chance to add |
| 2725 | * the poll to the issued list. Otherwise we can spin here |
| 2726 | * forever, while the workqueue is stuck trying to acquire the |
| 2727 | * very same mutex. |
| 2728 | */ |
| 2729 | if (!(++iters & 7)) { |
| 2730 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2731 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2732 | mutex_lock(&ctx->uring_lock); |
| 2733 | } |
| 2734 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2735 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2736 | if (ret <= 0) |
| 2737 | break; |
| 2738 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2739 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2740 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2741 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2742 | return ret; |
| 2743 | } |
| 2744 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2745 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2746 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2747 | /* |
| 2748 | * Tell lockdep we inherited freeze protection from submission |
| 2749 | * thread. |
| 2750 | */ |
| 2751 | if (req->flags & REQ_F_ISREG) { |
| 2752 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2753 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2754 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2755 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2756 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2759 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2760 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2761 | { |
| 2762 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2763 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2764 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2765 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2766 | /* already prepared */ |
| 2767 | if (req->async_data) |
| 2768 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2769 | |
| 2770 | switch (req->opcode) { |
| 2771 | case IORING_OP_READV: |
| 2772 | case IORING_OP_READ_FIXED: |
| 2773 | case IORING_OP_READ: |
| 2774 | rw = READ; |
| 2775 | break; |
| 2776 | case IORING_OP_WRITEV: |
| 2777 | case IORING_OP_WRITE_FIXED: |
| 2778 | case IORING_OP_WRITE: |
| 2779 | rw = WRITE; |
| 2780 | break; |
| 2781 | default: |
| 2782 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2783 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2784 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2785 | } |
| 2786 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2787 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2788 | if (ret < 0) |
| 2789 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2790 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2791 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2792 | #endif |
| 2793 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2794 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2795 | { |
| 2796 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2797 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2798 | int ret; |
| 2799 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2800 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2801 | return false; |
| 2802 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2803 | return false; |
| 2804 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2805 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2806 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2807 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2808 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2809 | if (!ret && io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2810 | refcount_inc(&req->refs); |
| 2811 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2812 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2813 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2814 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2815 | #endif |
| 2816 | return false; |
| 2817 | } |
| 2818 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2819 | 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] | 2820 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2821 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2822 | int cflags = 0; |
| 2823 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2824 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2825 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2826 | if (res != req->result) |
| 2827 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2828 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2829 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2830 | kiocb_end_write(req); |
| 2831 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2832 | cflags = io_put_rw_kbuf(req); |
| 2833 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2837 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2838 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2839 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2840 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2843 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2844 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2845 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2846 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2847 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2848 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2849 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2850 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2851 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2852 | |
| 2853 | WRITE_ONCE(req->result, res); |
| 2854 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2855 | smp_wmb(); |
| 2856 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | /* |
| 2860 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2861 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2862 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2863 | * accessing the kiocb cookie. |
| 2864 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2865 | 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] | 2866 | { |
| 2867 | struct io_ring_ctx *ctx = req->ctx; |
| 2868 | |
| 2869 | /* |
| 2870 | * Track whether we have multiple files in our lists. This will impact |
| 2871 | * how we do polling eventually, not spinning if we're on potentially |
| 2872 | * different devices. |
| 2873 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2874 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2875 | ctx->poll_multi_file = false; |
| 2876 | } else if (!ctx->poll_multi_file) { |
| 2877 | struct io_kiocb *list_req; |
| 2878 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2879 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2880 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2881 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2882 | ctx->poll_multi_file = true; |
| 2883 | } |
| 2884 | |
| 2885 | /* |
| 2886 | * For fast devices, IO may have already completed. If it has, add |
| 2887 | * it to the front so we find it first. |
| 2888 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2889 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2890 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2891 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2892 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2893 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2894 | /* |
| 2895 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2896 | * task context or in io worker task context. If current task context is |
| 2897 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2898 | */ |
| 2899 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2900 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2901 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2904 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2905 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2906 | if (state->file_refs) { |
| 2907 | fput_many(state->file, state->file_refs); |
| 2908 | state->file_refs = 0; |
| 2909 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | /* |
| 2913 | * Get as many references to a file as we have IOs left in this submission, |
| 2914 | * assuming most submissions are for one file, or at least that each file |
| 2915 | * has more than one submission. |
| 2916 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2917 | 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] | 2918 | { |
| 2919 | if (!state) |
| 2920 | return fget(fd); |
| 2921 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2922 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2923 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2924 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2925 | return state->file; |
| 2926 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2927 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2928 | } |
| 2929 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2930 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2931 | return NULL; |
| 2932 | |
| 2933 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2934 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2935 | return state->file; |
| 2936 | } |
| 2937 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2938 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2939 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2940 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2941 | } |
| 2942 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2943 | /* |
| 2944 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2945 | * any file. For now, just ensure that anything potentially problematic is done |
| 2946 | * inline. |
| 2947 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2948 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2949 | { |
| 2950 | umode_t mode = file_inode(file)->i_mode; |
| 2951 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2952 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2953 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2954 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2955 | return true; |
| 2956 | return false; |
| 2957 | } |
| 2958 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2959 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2960 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2961 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2962 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2963 | file->f_op != &io_uring_fops) |
| 2964 | return true; |
| 2965 | return false; |
| 2966 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2967 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2968 | /* any ->read/write should understand O_NONBLOCK */ |
| 2969 | if (file->f_flags & O_NONBLOCK) |
| 2970 | return true; |
| 2971 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2972 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2973 | return false; |
| 2974 | |
| 2975 | if (rw == READ) |
| 2976 | return file->f_op->read_iter != NULL; |
| 2977 | |
| 2978 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2981 | 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] | 2982 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2983 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2984 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2985 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2986 | unsigned ioprio; |
| 2987 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2988 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2989 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2990 | req->flags |= REQ_F_ISREG; |
| 2991 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2992 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2993 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2994 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2995 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2996 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2997 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2998 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2999 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 3000 | if (unlikely(ret)) |
| 3001 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3002 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3003 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 3004 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 3005 | req->flags |= REQ_F_NOWAIT; |
| 3006 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3007 | ioprio = READ_ONCE(sqe->ioprio); |
| 3008 | if (ioprio) { |
| 3009 | ret = ioprio_check_cap(ioprio); |
| 3010 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3011 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3012 | |
| 3013 | kiocb->ki_ioprio = ioprio; |
| 3014 | } else |
| 3015 | kiocb->ki_ioprio = get_current_ioprio(); |
| 3016 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3017 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3018 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 3019 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3020 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3021 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3022 | kiocb->ki_flags |= IOCB_HIPRI; |
| 3023 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3024 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3025 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3026 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3027 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3028 | kiocb->ki_complete = io_complete_rw; |
| 3029 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3030 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3031 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3032 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3033 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3034 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3038 | { |
| 3039 | switch (ret) { |
| 3040 | case -EIOCBQUEUED: |
| 3041 | break; |
| 3042 | case -ERESTARTSYS: |
| 3043 | case -ERESTARTNOINTR: |
| 3044 | case -ERESTARTNOHAND: |
| 3045 | case -ERESTART_RESTARTBLOCK: |
| 3046 | /* |
| 3047 | * We can't just restart the syscall, since previously |
| 3048 | * submitted sqes may already be in progress. Just fail this |
| 3049 | * IO with EINTR. |
| 3050 | */ |
| 3051 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3052 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3053 | default: |
| 3054 | kiocb->ki_complete(kiocb, ret, 0); |
| 3055 | } |
| 3056 | } |
| 3057 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3058 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3059 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3060 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3061 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3062 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3063 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3064 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3065 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3066 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3067 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3068 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3069 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3070 | } |
| 3071 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3072 | if (req->flags & REQ_F_CUR_POS) |
| 3073 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 3074 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3075 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3076 | else |
| 3077 | io_rw_done(kiocb, ret); |
| 3078 | } |
| 3079 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3080 | 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] | 3081 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3082 | struct io_ring_ctx *ctx = req->ctx; |
| 3083 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3084 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 3085 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3086 | size_t offset; |
| 3087 | u64 buf_addr; |
| 3088 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3089 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3090 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3091 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3092 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3093 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3094 | |
| 3095 | /* overflow */ |
| 3096 | if (buf_addr + len < buf_addr) |
| 3097 | return -EFAULT; |
| 3098 | /* not inside the mapped region */ |
| 3099 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3100 | return -EFAULT; |
| 3101 | |
| 3102 | /* |
| 3103 | * May not be a start of buffer, set size appropriately |
| 3104 | * and advance us to the beginning. |
| 3105 | */ |
| 3106 | offset = buf_addr - imu->ubuf; |
| 3107 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3108 | |
| 3109 | if (offset) { |
| 3110 | /* |
| 3111 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3112 | * using the latter parts of a big fixed buffer - it iterates |
| 3113 | * over each segment manually. We can cheat a bit here, because |
| 3114 | * we know that: |
| 3115 | * |
| 3116 | * 1) it's a BVEC iter, we set it up |
| 3117 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3118 | * first and last bvec |
| 3119 | * |
| 3120 | * So just find our index, and adjust the iterator afterwards. |
| 3121 | * If the offset is within the first bvec (or the whole first |
| 3122 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3123 | * since we can just skip the first segment, which may not |
| 3124 | * be PAGE_SIZE aligned. |
| 3125 | */ |
| 3126 | const struct bio_vec *bvec = imu->bvec; |
| 3127 | |
| 3128 | if (offset <= bvec->bv_len) { |
| 3129 | iov_iter_advance(iter, offset); |
| 3130 | } else { |
| 3131 | unsigned long seg_skip; |
| 3132 | |
| 3133 | /* skip first vec */ |
| 3134 | offset -= bvec->bv_len; |
| 3135 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3136 | |
| 3137 | iter->bvec = bvec + seg_skip; |
| 3138 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3139 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3140 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3144 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3147 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3148 | { |
| 3149 | if (needs_lock) |
| 3150 | mutex_unlock(&ctx->uring_lock); |
| 3151 | } |
| 3152 | |
| 3153 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3154 | { |
| 3155 | /* |
| 3156 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3157 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3158 | * The only exception is when we've detached the request and issue it |
| 3159 | * from an async worker thread, grab the lock for that case. |
| 3160 | */ |
| 3161 | if (needs_lock) |
| 3162 | mutex_lock(&ctx->uring_lock); |
| 3163 | } |
| 3164 | |
| 3165 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3166 | int bgid, struct io_buffer *kbuf, |
| 3167 | bool needs_lock) |
| 3168 | { |
| 3169 | struct io_buffer *head; |
| 3170 | |
| 3171 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3172 | return kbuf; |
| 3173 | |
| 3174 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3175 | |
| 3176 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3177 | |
| 3178 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3179 | if (head) { |
| 3180 | if (!list_empty(&head->list)) { |
| 3181 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3182 | list); |
| 3183 | list_del(&kbuf->list); |
| 3184 | } else { |
| 3185 | kbuf = head; |
| 3186 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3187 | } |
| 3188 | if (*len > kbuf->len) |
| 3189 | *len = kbuf->len; |
| 3190 | } else { |
| 3191 | kbuf = ERR_PTR(-ENOBUFS); |
| 3192 | } |
| 3193 | |
| 3194 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3195 | |
| 3196 | return kbuf; |
| 3197 | } |
| 3198 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3199 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3200 | bool needs_lock) |
| 3201 | { |
| 3202 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3203 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3204 | |
| 3205 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3206 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3207 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3208 | if (IS_ERR(kbuf)) |
| 3209 | return kbuf; |
| 3210 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3211 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3212 | return u64_to_user_ptr(kbuf->addr); |
| 3213 | } |
| 3214 | |
| 3215 | #ifdef CONFIG_COMPAT |
| 3216 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3217 | bool needs_lock) |
| 3218 | { |
| 3219 | struct compat_iovec __user *uiov; |
| 3220 | compat_ssize_t clen; |
| 3221 | void __user *buf; |
| 3222 | ssize_t len; |
| 3223 | |
| 3224 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3225 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3226 | return -EFAULT; |
| 3227 | if (__get_user(clen, &uiov->iov_len)) |
| 3228 | return -EFAULT; |
| 3229 | if (clen < 0) |
| 3230 | return -EINVAL; |
| 3231 | |
| 3232 | len = clen; |
| 3233 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3234 | if (IS_ERR(buf)) |
| 3235 | return PTR_ERR(buf); |
| 3236 | iov[0].iov_base = buf; |
| 3237 | iov[0].iov_len = (compat_size_t) len; |
| 3238 | return 0; |
| 3239 | } |
| 3240 | #endif |
| 3241 | |
| 3242 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3243 | bool needs_lock) |
| 3244 | { |
| 3245 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3246 | void __user *buf; |
| 3247 | ssize_t len; |
| 3248 | |
| 3249 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3250 | return -EFAULT; |
| 3251 | |
| 3252 | len = iov[0].iov_len; |
| 3253 | if (len < 0) |
| 3254 | return -EINVAL; |
| 3255 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3256 | if (IS_ERR(buf)) |
| 3257 | return PTR_ERR(buf); |
| 3258 | iov[0].iov_base = buf; |
| 3259 | iov[0].iov_len = len; |
| 3260 | return 0; |
| 3261 | } |
| 3262 | |
| 3263 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3264 | bool needs_lock) |
| 3265 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3266 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3267 | struct io_buffer *kbuf; |
| 3268 | |
| 3269 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3270 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3271 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3272 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3273 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3274 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3275 | return -EINVAL; |
| 3276 | |
| 3277 | #ifdef CONFIG_COMPAT |
| 3278 | if (req->ctx->compat) |
| 3279 | return io_compat_import(req, iov, needs_lock); |
| 3280 | #endif |
| 3281 | |
| 3282 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3283 | } |
| 3284 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3285 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3286 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3287 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3288 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3289 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3290 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3291 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3292 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3293 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3294 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3295 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3296 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3297 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3298 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3299 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3300 | return -EINVAL; |
| 3301 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3302 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3303 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3304 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3305 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3306 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3307 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3310 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3311 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3312 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3315 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3316 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3317 | if (!ret) |
| 3318 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3319 | *iovec = NULL; |
| 3320 | return ret; |
| 3321 | } |
| 3322 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3323 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3324 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3325 | } |
| 3326 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3327 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3328 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3329 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3330 | } |
| 3331 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3332 | /* |
| 3333 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3334 | * by looping over ->read() or ->write() manually. |
| 3335 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3336 | 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] | 3337 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3338 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3339 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3340 | ssize_t ret = 0; |
| 3341 | |
| 3342 | /* |
| 3343 | * Don't support polled IO through this interface, and we can't |
| 3344 | * support non-blocking either. For the latter, this just causes |
| 3345 | * the kiocb to be handled from an async context. |
| 3346 | */ |
| 3347 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3348 | return -EOPNOTSUPP; |
| 3349 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3350 | return -EAGAIN; |
| 3351 | |
| 3352 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3353 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3354 | ssize_t nr; |
| 3355 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3356 | if (!iov_iter_is_bvec(iter)) { |
| 3357 | iovec = iov_iter_iovec(iter); |
| 3358 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3359 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3360 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3361 | } |
| 3362 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3363 | if (rw == READ) { |
| 3364 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3365 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3366 | } else { |
| 3367 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3368 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | if (nr < 0) { |
| 3372 | if (!ret) |
| 3373 | ret = nr; |
| 3374 | break; |
| 3375 | } |
| 3376 | ret += nr; |
| 3377 | if (nr != iovec.iov_len) |
| 3378 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3379 | req->rw.len -= nr; |
| 3380 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3381 | iov_iter_advance(iter, nr); |
| 3382 | } |
| 3383 | |
| 3384 | return ret; |
| 3385 | } |
| 3386 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3387 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3388 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3389 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3390 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3391 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3392 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3393 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3394 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3395 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3396 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3397 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3398 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3399 | unsigned iov_off = 0; |
| 3400 | |
| 3401 | rw->iter.iov = rw->fast_iov; |
| 3402 | if (iter->iov != fast_iov) { |
| 3403 | iov_off = iter->iov - fast_iov; |
| 3404 | rw->iter.iov += iov_off; |
| 3405 | } |
| 3406 | if (rw->fast_iov != fast_iov) |
| 3407 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3408 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3409 | } else { |
| 3410 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3411 | } |
| 3412 | } |
| 3413 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3414 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3415 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3416 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3417 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3418 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3419 | } |
| 3420 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3421 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3422 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3423 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3424 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3425 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3426 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3427 | } |
| 3428 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3429 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3430 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3431 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3432 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3433 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3434 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3435 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3436 | if (__io_alloc_async_data(req)) { |
| 3437 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3438 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3439 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3440 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3441 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3442 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3443 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3446 | 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] | 3447 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3448 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3449 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3450 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3451 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3452 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3453 | if (unlikely(ret < 0)) |
| 3454 | return ret; |
| 3455 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3456 | iorw->bytes_done = 0; |
| 3457 | iorw->free_iovec = iov; |
| 3458 | if (iov) |
| 3459 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3460 | return 0; |
| 3461 | } |
| 3462 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3463 | 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] | 3464 | { |
| 3465 | ssize_t ret; |
| 3466 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3467 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3468 | if (ret) |
| 3469 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3470 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3471 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3472 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3473 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3474 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3475 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3476 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3477 | return io_rw_prep_async(req, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3480 | /* |
| 3481 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3482 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3483 | * This gets called when the page is unlocked, and we generally expect that to |
| 3484 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3485 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3486 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3487 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3488 | * slow path. |
| 3489 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3490 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3491 | int sync, void *arg) |
| 3492 | { |
| 3493 | struct wait_page_queue *wpq; |
| 3494 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3495 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3496 | |
| 3497 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3498 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3499 | if (!wake_page_match(wpq, key)) |
| 3500 | return 0; |
| 3501 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3502 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3503 | list_del_init(&wait->entry); |
| 3504 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3505 | /* submit ref gets dropped, acquire a new one */ |
| 3506 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3507 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3508 | return 1; |
| 3509 | } |
| 3510 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3511 | /* |
| 3512 | * This controls whether a given IO request should be armed for async page |
| 3513 | * based retry. If we return false here, the request is handed to the async |
| 3514 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3515 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3516 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3517 | * will register a callback when the page is unlocked at IO completion. Through |
| 3518 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3519 | * That retry will attempt the buffered read again. The retry will generally |
| 3520 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3521 | * async worker threads for a blocking retry. |
| 3522 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3523 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3524 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3525 | struct io_async_rw *rw = req->async_data; |
| 3526 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3527 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3528 | |
| 3529 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3530 | if (req->flags & REQ_F_NOWAIT) |
| 3531 | return false; |
| 3532 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3533 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3534 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3535 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3536 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3537 | /* |
| 3538 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3539 | * support callback based unlocks |
| 3540 | */ |
| 3541 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3542 | return false; |
| 3543 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3544 | wait->wait.func = io_async_buf_func; |
| 3545 | wait->wait.private = req; |
| 3546 | wait->wait.flags = 0; |
| 3547 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3548 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3549 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3550 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3551 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3555 | { |
| 3556 | if (req->file->f_op->read_iter) |
| 3557 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3558 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3559 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3560 | else |
| 3561 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3562 | } |
| 3563 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3564 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3565 | { |
| 3566 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3567 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3568 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3569 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3570 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3571 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3572 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3573 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3574 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3575 | iovec = NULL; |
| 3576 | } else { |
| 3577 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3578 | if (ret < 0) |
| 3579 | return ret; |
| 3580 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3581 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3582 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3583 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3584 | /* Ensure we clear previously set non-block flag */ |
| 3585 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3586 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3587 | else |
| 3588 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3589 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3590 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3591 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3592 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3593 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3594 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3595 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3596 | 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] | 3597 | if (unlikely(ret)) { |
| 3598 | kfree(iovec); |
| 3599 | return ret; |
| 3600 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3601 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3602 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3603 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3604 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3605 | /* it's faster to check here then delegate to kfree */ |
| 3606 | if (iovec) |
| 3607 | kfree(iovec); |
| 3608 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3609 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3610 | /* IOPOLL retry should happen for io-wq threads */ |
| 3611 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3612 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3613 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3614 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3615 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3616 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3617 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3618 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3619 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3620 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3621 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3622 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3623 | } |
| 3624 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3625 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3626 | if (ret2) |
| 3627 | return ret2; |
| 3628 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3629 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3630 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3631 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3632 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3633 | do { |
| 3634 | io_size -= ret; |
| 3635 | rw->bytes_done += ret; |
| 3636 | /* if we can retry, do so with the callbacks armed */ |
| 3637 | if (!io_rw_should_retry(req)) { |
| 3638 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3639 | return -EAGAIN; |
| 3640 | } |
| 3641 | |
| 3642 | /* |
| 3643 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3644 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3645 | * desired page gets unlocked. We can also get a partial read |
| 3646 | * here, and if we do, then just retry at the new offset. |
| 3647 | */ |
| 3648 | ret = io_iter_do_read(req, iter); |
| 3649 | if (ret == -EIOCBQUEUED) |
| 3650 | return 0; |
| 3651 | /* we got some bytes, but not all. retry. */ |
| 3652 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3653 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3654 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3655 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3656 | } |
| 3657 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3658 | 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] | 3659 | { |
| 3660 | ssize_t ret; |
| 3661 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3662 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3663 | if (ret) |
| 3664 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3665 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3666 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3667 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3668 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3669 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3670 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3671 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3672 | return io_rw_prep_async(req, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3673 | } |
| 3674 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3675 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3676 | { |
| 3677 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3678 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3679 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3680 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3681 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3682 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3683 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3684 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3685 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3686 | iovec = NULL; |
| 3687 | } else { |
| 3688 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3689 | if (ret < 0) |
| 3690 | return ret; |
| 3691 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3692 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3693 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3694 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3695 | /* Ensure we clear previously set non-block flag */ |
| 3696 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3697 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3698 | else |
| 3699 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3700 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3701 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3702 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3703 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3704 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3705 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3706 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3707 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3708 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3709 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3710 | 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] | 3711 | if (unlikely(ret)) |
| 3712 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3713 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3714 | /* |
| 3715 | * Open-code file_start_write here to grab freeze protection, |
| 3716 | * which will be released by another thread in |
| 3717 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3718 | * released so that it doesn't complain about the held lock when |
| 3719 | * we return to userspace. |
| 3720 | */ |
| 3721 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3722 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3723 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3724 | SB_FREEZE_WRITE); |
| 3725 | } |
| 3726 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3727 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3728 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3729 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3730 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3731 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3732 | else |
| 3733 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3734 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3735 | /* |
| 3736 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3737 | * retry them without IOCB_NOWAIT. |
| 3738 | */ |
| 3739 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3740 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3741 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3742 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3743 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3744 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3745 | /* IOPOLL retry should happen for io-wq threads */ |
| 3746 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3747 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3748 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3749 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3750 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3751 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3752 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3753 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3754 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3755 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3756 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3757 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3758 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3759 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3760 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3761 | return ret; |
| 3762 | } |
| 3763 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3764 | static int io_renameat_prep(struct io_kiocb *req, |
| 3765 | const struct io_uring_sqe *sqe) |
| 3766 | { |
| 3767 | struct io_rename *ren = &req->rename; |
| 3768 | const char __user *oldf, *newf; |
| 3769 | |
| 3770 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3771 | return -EBADF; |
| 3772 | |
| 3773 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3774 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3775 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3776 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3777 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3778 | |
| 3779 | ren->oldpath = getname(oldf); |
| 3780 | if (IS_ERR(ren->oldpath)) |
| 3781 | return PTR_ERR(ren->oldpath); |
| 3782 | |
| 3783 | ren->newpath = getname(newf); |
| 3784 | if (IS_ERR(ren->newpath)) { |
| 3785 | putname(ren->oldpath); |
| 3786 | return PTR_ERR(ren->newpath); |
| 3787 | } |
| 3788 | |
| 3789 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3793 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3794 | { |
| 3795 | struct io_rename *ren = &req->rename; |
| 3796 | int ret; |
| 3797 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3798 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3799 | return -EAGAIN; |
| 3800 | |
| 3801 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3802 | ren->newpath, ren->flags); |
| 3803 | |
| 3804 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3805 | if (ret < 0) |
| 3806 | req_set_fail_links(req); |
| 3807 | io_req_complete(req, ret); |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3811 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3812 | const struct io_uring_sqe *sqe) |
| 3813 | { |
| 3814 | struct io_unlink *un = &req->unlink; |
| 3815 | const char __user *fname; |
| 3816 | |
| 3817 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3818 | return -EBADF; |
| 3819 | |
| 3820 | un->dfd = READ_ONCE(sqe->fd); |
| 3821 | |
| 3822 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3823 | if (un->flags & ~AT_REMOVEDIR) |
| 3824 | return -EINVAL; |
| 3825 | |
| 3826 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3827 | un->filename = getname(fname); |
| 3828 | if (IS_ERR(un->filename)) |
| 3829 | return PTR_ERR(un->filename); |
| 3830 | |
| 3831 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3832 | return 0; |
| 3833 | } |
| 3834 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3835 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3836 | { |
| 3837 | struct io_unlink *un = &req->unlink; |
| 3838 | int ret; |
| 3839 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3840 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3841 | return -EAGAIN; |
| 3842 | |
| 3843 | if (un->flags & AT_REMOVEDIR) |
| 3844 | ret = do_rmdir(un->dfd, un->filename); |
| 3845 | else |
| 3846 | ret = do_unlinkat(un->dfd, un->filename); |
| 3847 | |
| 3848 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3849 | if (ret < 0) |
| 3850 | req_set_fail_links(req); |
| 3851 | io_req_complete(req, ret); |
| 3852 | return 0; |
| 3853 | } |
| 3854 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3855 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3856 | const struct io_uring_sqe *sqe) |
| 3857 | { |
| 3858 | #if defined(CONFIG_NET) |
| 3859 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3860 | return -EINVAL; |
| 3861 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3862 | sqe->buf_index) |
| 3863 | return -EINVAL; |
| 3864 | |
| 3865 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3866 | return 0; |
| 3867 | #else |
| 3868 | return -EOPNOTSUPP; |
| 3869 | #endif |
| 3870 | } |
| 3871 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3872 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3873 | { |
| 3874 | #if defined(CONFIG_NET) |
| 3875 | struct socket *sock; |
| 3876 | int ret; |
| 3877 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3878 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3879 | return -EAGAIN; |
| 3880 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3881 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3882 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3883 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3884 | |
| 3885 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3886 | if (ret < 0) |
| 3887 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3888 | io_req_complete(req, ret); |
| 3889 | return 0; |
| 3890 | #else |
| 3891 | return -EOPNOTSUPP; |
| 3892 | #endif |
| 3893 | } |
| 3894 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3895 | static int __io_splice_prep(struct io_kiocb *req, |
| 3896 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3897 | { |
| 3898 | struct io_splice* sp = &req->splice; |
| 3899 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3900 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3901 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3902 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3903 | |
| 3904 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3905 | sp->len = READ_ONCE(sqe->len); |
| 3906 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3907 | |
| 3908 | if (unlikely(sp->flags & ~valid_flags)) |
| 3909 | return -EINVAL; |
| 3910 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3911 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3912 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3913 | if (!sp->file_in) |
| 3914 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3915 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3916 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3917 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3918 | /* |
| 3919 | * Splice operation will be punted aync, and here need to |
| 3920 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3921 | */ |
| 3922 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3923 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3924 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3925 | |
| 3926 | return 0; |
| 3927 | } |
| 3928 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3929 | static int io_tee_prep(struct io_kiocb *req, |
| 3930 | const struct io_uring_sqe *sqe) |
| 3931 | { |
| 3932 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3933 | return -EINVAL; |
| 3934 | return __io_splice_prep(req, sqe); |
| 3935 | } |
| 3936 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3937 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3938 | { |
| 3939 | struct io_splice *sp = &req->splice; |
| 3940 | struct file *in = sp->file_in; |
| 3941 | struct file *out = sp->file_out; |
| 3942 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3943 | long ret = 0; |
| 3944 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3945 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3946 | return -EAGAIN; |
| 3947 | if (sp->len) |
| 3948 | ret = do_tee(in, out, sp->len, flags); |
| 3949 | |
| 3950 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3951 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3952 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3953 | if (ret != sp->len) |
| 3954 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3955 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3956 | return 0; |
| 3957 | } |
| 3958 | |
| 3959 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3960 | { |
| 3961 | struct io_splice* sp = &req->splice; |
| 3962 | |
| 3963 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3964 | sp->off_out = READ_ONCE(sqe->off); |
| 3965 | return __io_splice_prep(req, sqe); |
| 3966 | } |
| 3967 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3968 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3969 | { |
| 3970 | struct io_splice *sp = &req->splice; |
| 3971 | struct file *in = sp->file_in; |
| 3972 | struct file *out = sp->file_out; |
| 3973 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3974 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3975 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3976 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3977 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3978 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3979 | |
| 3980 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3981 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3982 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3983 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3984 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3985 | |
| 3986 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3987 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3988 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3989 | if (ret != sp->len) |
| 3990 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3991 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3992 | return 0; |
| 3993 | } |
| 3994 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3995 | /* |
| 3996 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3997 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3998 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3999 | { |
| 4000 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4001 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4002 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4003 | return -EINVAL; |
| 4004 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4005 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4006 | return 0; |
| 4007 | } |
| 4008 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4009 | 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] | 4010 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4011 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4012 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4013 | if (!req->file) |
| 4014 | return -EBADF; |
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 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4017 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4018 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4019 | return -EINVAL; |
| 4020 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4021 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4022 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4023 | return -EINVAL; |
| 4024 | |
| 4025 | req->sync.off = READ_ONCE(sqe->off); |
| 4026 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4027 | return 0; |
| 4028 | } |
| 4029 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4030 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4031 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4032 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4033 | int ret; |
| 4034 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4035 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4036 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4037 | return -EAGAIN; |
| 4038 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4039 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4040 | end > 0 ? end : LLONG_MAX, |
| 4041 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4042 | if (ret < 0) |
| 4043 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4044 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4045 | return 0; |
| 4046 | } |
| 4047 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4048 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4049 | const struct io_uring_sqe *sqe) |
| 4050 | { |
| 4051 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 4052 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4053 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4054 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4055 | |
| 4056 | req->sync.off = READ_ONCE(sqe->off); |
| 4057 | req->sync.len = READ_ONCE(sqe->addr); |
| 4058 | req->sync.mode = READ_ONCE(sqe->len); |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4062 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4063 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4064 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4065 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4066 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4067 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4068 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4069 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4070 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4071 | if (ret < 0) |
| 4072 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4073 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4074 | return 0; |
| 4075 | } |
| 4076 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4077 | 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] | 4078 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4079 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4080 | int ret; |
| 4081 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4082 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4083 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4084 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4085 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4086 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4087 | /* open.how should be already initialised */ |
| 4088 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4089 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4090 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4091 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4092 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4093 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4094 | if (IS_ERR(req->open.filename)) { |
| 4095 | ret = PTR_ERR(req->open.filename); |
| 4096 | req->open.filename = NULL; |
| 4097 | return ret; |
| 4098 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4099 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4100 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4101 | return 0; |
| 4102 | } |
| 4103 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4104 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4105 | { |
| 4106 | u64 flags, mode; |
| 4107 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4108 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4109 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4110 | mode = READ_ONCE(sqe->len); |
| 4111 | flags = READ_ONCE(sqe->open_flags); |
| 4112 | req->open.how = build_open_how(flags, mode); |
| 4113 | return __io_openat_prep(req, sqe); |
| 4114 | } |
| 4115 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4116 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4117 | { |
| 4118 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4119 | size_t len; |
| 4120 | int ret; |
| 4121 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4122 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4123 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4124 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4125 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4126 | if (len < OPEN_HOW_SIZE_VER0) |
| 4127 | return -EINVAL; |
| 4128 | |
| 4129 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4130 | len); |
| 4131 | if (ret) |
| 4132 | return ret; |
| 4133 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4134 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4135 | } |
| 4136 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4137 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4138 | { |
| 4139 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4140 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4141 | bool nonblock_set; |
| 4142 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4143 | int ret; |
| 4144 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4145 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4146 | if (ret) |
| 4147 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4148 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4149 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4150 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4151 | /* |
| 4152 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4153 | * it'll always -EAGAIN |
| 4154 | */ |
| 4155 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4156 | return -EAGAIN; |
| 4157 | op.lookup_flags |= LOOKUP_CACHED; |
| 4158 | op.open_flag |= O_NONBLOCK; |
| 4159 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4160 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4161 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4162 | if (ret < 0) |
| 4163 | goto err; |
| 4164 | |
| 4165 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4166 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4167 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 4168 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4169 | /* |
| 4170 | * We could hang on to this 'fd', but seems like marginal |
| 4171 | * gain for something that is now known to be a slower path. |
| 4172 | * So just put it, and we'll get a new one when we retry. |
| 4173 | */ |
| 4174 | put_unused_fd(ret); |
| 4175 | return -EAGAIN; |
| 4176 | } |
| 4177 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4178 | if (IS_ERR(file)) { |
| 4179 | put_unused_fd(ret); |
| 4180 | ret = PTR_ERR(file); |
| 4181 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4182 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4183 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4184 | fsnotify_open(file); |
| 4185 | fd_install(ret, file); |
| 4186 | } |
| 4187 | err: |
| 4188 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4189 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4190 | if (ret < 0) |
| 4191 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4192 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4193 | return 0; |
| 4194 | } |
| 4195 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4196 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4197 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4198 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4199 | } |
| 4200 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4201 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4202 | const struct io_uring_sqe *sqe) |
| 4203 | { |
| 4204 | struct io_provide_buf *p = &req->pbuf; |
| 4205 | u64 tmp; |
| 4206 | |
| 4207 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4208 | return -EINVAL; |
| 4209 | |
| 4210 | tmp = READ_ONCE(sqe->fd); |
| 4211 | if (!tmp || tmp > USHRT_MAX) |
| 4212 | return -EINVAL; |
| 4213 | |
| 4214 | memset(p, 0, sizeof(*p)); |
| 4215 | p->nbufs = tmp; |
| 4216 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4221 | int bgid, unsigned nbufs) |
| 4222 | { |
| 4223 | unsigned i = 0; |
| 4224 | |
| 4225 | /* shouldn't happen */ |
| 4226 | if (!nbufs) |
| 4227 | return 0; |
| 4228 | |
| 4229 | /* the head kbuf is the list itself */ |
| 4230 | while (!list_empty(&buf->list)) { |
| 4231 | struct io_buffer *nxt; |
| 4232 | |
| 4233 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4234 | list_del(&nxt->list); |
| 4235 | kfree(nxt); |
| 4236 | if (++i == nbufs) |
| 4237 | return i; |
| 4238 | } |
| 4239 | i++; |
| 4240 | kfree(buf); |
| 4241 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4242 | |
| 4243 | return i; |
| 4244 | } |
| 4245 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4246 | 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] | 4247 | { |
| 4248 | struct io_provide_buf *p = &req->pbuf; |
| 4249 | struct io_ring_ctx *ctx = req->ctx; |
| 4250 | struct io_buffer *head; |
| 4251 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4252 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4253 | |
| 4254 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4255 | |
| 4256 | lockdep_assert_held(&ctx->uring_lock); |
| 4257 | |
| 4258 | ret = -ENOENT; |
| 4259 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4260 | if (head) |
| 4261 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4262 | if (ret < 0) |
| 4263 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4264 | |
| 4265 | /* need to hold the lock to complete IOPOLL requests */ |
| 4266 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4267 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4268 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4269 | } else { |
| 4270 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4271 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4272 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4273 | return 0; |
| 4274 | } |
| 4275 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4276 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4277 | const struct io_uring_sqe *sqe) |
| 4278 | { |
| 4279 | struct io_provide_buf *p = &req->pbuf; |
| 4280 | u64 tmp; |
| 4281 | |
| 4282 | if (sqe->ioprio || sqe->rw_flags) |
| 4283 | return -EINVAL; |
| 4284 | |
| 4285 | tmp = READ_ONCE(sqe->fd); |
| 4286 | if (!tmp || tmp > USHRT_MAX) |
| 4287 | return -E2BIG; |
| 4288 | p->nbufs = tmp; |
| 4289 | p->addr = READ_ONCE(sqe->addr); |
| 4290 | p->len = READ_ONCE(sqe->len); |
| 4291 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4292 | 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] | 4293 | return -EFAULT; |
| 4294 | |
| 4295 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4296 | tmp = READ_ONCE(sqe->off); |
| 4297 | if (tmp > USHRT_MAX) |
| 4298 | return -E2BIG; |
| 4299 | p->bid = tmp; |
| 4300 | return 0; |
| 4301 | } |
| 4302 | |
| 4303 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4304 | { |
| 4305 | struct io_buffer *buf; |
| 4306 | u64 addr = pbuf->addr; |
| 4307 | int i, bid = pbuf->bid; |
| 4308 | |
| 4309 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4310 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4311 | if (!buf) |
| 4312 | break; |
| 4313 | |
| 4314 | buf->addr = addr; |
| 4315 | buf->len = pbuf->len; |
| 4316 | buf->bid = bid; |
| 4317 | addr += pbuf->len; |
| 4318 | bid++; |
| 4319 | if (!*head) { |
| 4320 | INIT_LIST_HEAD(&buf->list); |
| 4321 | *head = buf; |
| 4322 | } else { |
| 4323 | list_add_tail(&buf->list, &(*head)->list); |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | return i ? i : -ENOMEM; |
| 4328 | } |
| 4329 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4330 | 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] | 4331 | { |
| 4332 | struct io_provide_buf *p = &req->pbuf; |
| 4333 | struct io_ring_ctx *ctx = req->ctx; |
| 4334 | struct io_buffer *head, *list; |
| 4335 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4336 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4337 | |
| 4338 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4339 | |
| 4340 | lockdep_assert_held(&ctx->uring_lock); |
| 4341 | |
| 4342 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4343 | |
| 4344 | ret = io_add_buffers(p, &head); |
| 4345 | if (ret < 0) |
| 4346 | goto out; |
| 4347 | |
| 4348 | if (!list) { |
| 4349 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4350 | GFP_KERNEL); |
| 4351 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4352 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4353 | goto out; |
| 4354 | } |
| 4355 | } |
| 4356 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4357 | if (ret < 0) |
| 4358 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4359 | |
| 4360 | /* need to hold the lock to complete IOPOLL requests */ |
| 4361 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4362 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4363 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4364 | } else { |
| 4365 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4366 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4367 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4368 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4371 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4372 | const struct io_uring_sqe *sqe) |
| 4373 | { |
| 4374 | #if defined(CONFIG_EPOLL) |
| 4375 | if (sqe->ioprio || sqe->buf_index) |
| 4376 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4377 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4378 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4379 | |
| 4380 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4381 | req->epoll.op = READ_ONCE(sqe->len); |
| 4382 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4383 | |
| 4384 | if (ep_op_has_event(req->epoll.op)) { |
| 4385 | struct epoll_event __user *ev; |
| 4386 | |
| 4387 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4388 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4389 | return -EFAULT; |
| 4390 | } |
| 4391 | |
| 4392 | return 0; |
| 4393 | #else |
| 4394 | return -EOPNOTSUPP; |
| 4395 | #endif |
| 4396 | } |
| 4397 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4398 | 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] | 4399 | { |
| 4400 | #if defined(CONFIG_EPOLL) |
| 4401 | struct io_epoll *ie = &req->epoll; |
| 4402 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4403 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4404 | |
| 4405 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4406 | if (force_nonblock && ret == -EAGAIN) |
| 4407 | return -EAGAIN; |
| 4408 | |
| 4409 | if (ret < 0) |
| 4410 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4411 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4412 | return 0; |
| 4413 | #else |
| 4414 | return -EOPNOTSUPP; |
| 4415 | #endif |
| 4416 | } |
| 4417 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4418 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4419 | { |
| 4420 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4421 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4422 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4423 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4424 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4425 | |
| 4426 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4427 | req->madvise.len = READ_ONCE(sqe->len); |
| 4428 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4429 | return 0; |
| 4430 | #else |
| 4431 | return -EOPNOTSUPP; |
| 4432 | #endif |
| 4433 | } |
| 4434 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4435 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4436 | { |
| 4437 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4438 | struct io_madvise *ma = &req->madvise; |
| 4439 | int ret; |
| 4440 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4441 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4442 | return -EAGAIN; |
| 4443 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4444 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4445 | if (ret < 0) |
| 4446 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4447 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4448 | return 0; |
| 4449 | #else |
| 4450 | return -EOPNOTSUPP; |
| 4451 | #endif |
| 4452 | } |
| 4453 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4454 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4455 | { |
| 4456 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4457 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4458 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4459 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4460 | |
| 4461 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4462 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4463 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4467 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4468 | { |
| 4469 | struct io_fadvise *fa = &req->fadvise; |
| 4470 | int ret; |
| 4471 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4472 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4473 | switch (fa->advice) { |
| 4474 | case POSIX_FADV_NORMAL: |
| 4475 | case POSIX_FADV_RANDOM: |
| 4476 | case POSIX_FADV_SEQUENTIAL: |
| 4477 | break; |
| 4478 | default: |
| 4479 | return -EAGAIN; |
| 4480 | } |
| 4481 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4482 | |
| 4483 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4484 | if (ret < 0) |
| 4485 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4486 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4487 | return 0; |
| 4488 | } |
| 4489 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4490 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4491 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4492 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4493 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4494 | if (sqe->ioprio || sqe->buf_index) |
| 4495 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4496 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4497 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4498 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4499 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4500 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4501 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4502 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4503 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4504 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4505 | return 0; |
| 4506 | } |
| 4507 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4508 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4509 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4510 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4511 | int ret; |
| 4512 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4513 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4514 | /* only need file table for an actual valid fd */ |
| 4515 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4516 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4517 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4518 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4519 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4520 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4521 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4522 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4523 | if (ret < 0) |
| 4524 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4525 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4526 | return 0; |
| 4527 | } |
| 4528 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4529 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4530 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4531 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4532 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4533 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4534 | sqe->rw_flags || sqe->buf_index) |
| 4535 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4536 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4537 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4538 | |
| 4539 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4540 | return 0; |
| 4541 | } |
| 4542 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4543 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4544 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4545 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4546 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4547 | struct fdtable *fdt; |
| 4548 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4549 | int ret; |
| 4550 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4551 | file = NULL; |
| 4552 | ret = -EBADF; |
| 4553 | spin_lock(&files->file_lock); |
| 4554 | fdt = files_fdtable(files); |
| 4555 | if (close->fd >= fdt->max_fds) { |
| 4556 | spin_unlock(&files->file_lock); |
| 4557 | goto err; |
| 4558 | } |
| 4559 | file = fdt->fd[close->fd]; |
| 4560 | if (!file) { |
| 4561 | spin_unlock(&files->file_lock); |
| 4562 | goto err; |
| 4563 | } |
| 4564 | |
| 4565 | if (file->f_op == &io_uring_fops) { |
| 4566 | spin_unlock(&files->file_lock); |
| 4567 | file = NULL; |
| 4568 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4569 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4570 | |
| 4571 | /* 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] | 4572 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4573 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4574 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4575 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4576 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4577 | ret = __close_fd_get_file(close->fd, &file); |
| 4578 | spin_unlock(&files->file_lock); |
| 4579 | if (ret < 0) { |
| 4580 | if (ret == -ENOENT) |
| 4581 | ret = -EBADF; |
| 4582 | goto err; |
| 4583 | } |
| 4584 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4585 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4586 | ret = filp_close(file, current->files); |
| 4587 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4588 | if (ret < 0) |
| 4589 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4590 | if (file) |
| 4591 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4592 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4593 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4596 | 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] | 4597 | { |
| 4598 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4599 | |
| 4600 | if (!req->file) |
| 4601 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4602 | |
| 4603 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4604 | return -EINVAL; |
| 4605 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4606 | return -EINVAL; |
| 4607 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4608 | req->sync.off = READ_ONCE(sqe->off); |
| 4609 | req->sync.len = READ_ONCE(sqe->len); |
| 4610 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4611 | return 0; |
| 4612 | } |
| 4613 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4614 | 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] | 4615 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4616 | int ret; |
| 4617 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4618 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4619 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4620 | return -EAGAIN; |
| 4621 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4622 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4623 | req->sync.flags); |
| 4624 | if (ret < 0) |
| 4625 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4626 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4627 | return 0; |
| 4628 | } |
| 4629 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4630 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4631 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4632 | struct io_async_msghdr *kmsg) |
| 4633 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4634 | struct io_async_msghdr *async_msg = req->async_data; |
| 4635 | |
| 4636 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4637 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4638 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4639 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4640 | return -ENOMEM; |
| 4641 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4642 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4643 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4644 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4645 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4646 | /* if were using fast_iov, set it to the new one */ |
| 4647 | if (!async_msg->free_iov) |
| 4648 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4649 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4650 | return -EAGAIN; |
| 4651 | } |
| 4652 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4653 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4654 | struct io_async_msghdr *iomsg) |
| 4655 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4656 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4657 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4658 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4659 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4660 | } |
| 4661 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4662 | 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] | 4663 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4664 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4665 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4666 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4667 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4668 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4669 | return -EINVAL; |
| 4670 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4671 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4672 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4673 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4674 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4675 | #ifdef CONFIG_COMPAT |
| 4676 | if (req->ctx->compat) |
| 4677 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4678 | #endif |
| 4679 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4680 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4681 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4682 | ret = io_sendmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4683 | if (!ret) |
| 4684 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4685 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4688 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4689 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4690 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4691 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4692 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4693 | int ret; |
| 4694 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4695 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4696 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4697 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4698 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4699 | kmsg = req->async_data; |
| 4700 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4701 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4702 | if (ret) |
| 4703 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4704 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4707 | flags = req->sr_msg.msg_flags; |
| 4708 | if (flags & MSG_DONTWAIT) |
| 4709 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4710 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4711 | flags |= MSG_DONTWAIT; |
| 4712 | |
| 4713 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4714 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4715 | return io_setup_async_msg(req, kmsg); |
| 4716 | if (ret == -ERESTARTSYS) |
| 4717 | ret = -EINTR; |
| 4718 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4719 | /* fast path, check for non-NULL to avoid function call */ |
| 4720 | if (kmsg->free_iov) |
| 4721 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4722 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4723 | if (ret < 0) |
| 4724 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4725 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4726 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4727 | } |
| 4728 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4729 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4730 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4731 | struct io_sr_msg *sr = &req->sr_msg; |
| 4732 | struct msghdr msg; |
| 4733 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4734 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4735 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4736 | int ret; |
| 4737 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4738 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4739 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4740 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4741 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4742 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4743 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4744 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4745 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4746 | msg.msg_name = NULL; |
| 4747 | msg.msg_control = NULL; |
| 4748 | msg.msg_controllen = 0; |
| 4749 | msg.msg_namelen = 0; |
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 | flags = req->sr_msg.msg_flags; |
| 4752 | if (flags & MSG_DONTWAIT) |
| 4753 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4754 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4755 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4756 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4757 | msg.msg_flags = flags; |
| 4758 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4759 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4760 | return -EAGAIN; |
| 4761 | if (ret == -ERESTARTSYS) |
| 4762 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4763 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4764 | if (ret < 0) |
| 4765 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4766 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4767 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4768 | } |
| 4769 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4770 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4771 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4772 | { |
| 4773 | struct io_sr_msg *sr = &req->sr_msg; |
| 4774 | struct iovec __user *uiov; |
| 4775 | size_t iov_len; |
| 4776 | int ret; |
| 4777 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4778 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4779 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4780 | if (ret) |
| 4781 | return ret; |
| 4782 | |
| 4783 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4784 | if (iov_len > 1) |
| 4785 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4786 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4787 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4788 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4789 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4790 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4791 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4792 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4793 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4794 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4795 | if (ret > 0) |
| 4796 | ret = 0; |
| 4797 | } |
| 4798 | |
| 4799 | return ret; |
| 4800 | } |
| 4801 | |
| 4802 | #ifdef CONFIG_COMPAT |
| 4803 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4804 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4805 | { |
| 4806 | struct compat_msghdr __user *msg_compat; |
| 4807 | struct io_sr_msg *sr = &req->sr_msg; |
| 4808 | struct compat_iovec __user *uiov; |
| 4809 | compat_uptr_t ptr; |
| 4810 | compat_size_t len; |
| 4811 | int ret; |
| 4812 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4813 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4814 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4815 | &ptr, &len); |
| 4816 | if (ret) |
| 4817 | return ret; |
| 4818 | |
| 4819 | uiov = compat_ptr(ptr); |
| 4820 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4821 | compat_ssize_t clen; |
| 4822 | |
| 4823 | if (len > 1) |
| 4824 | return -EINVAL; |
| 4825 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4826 | return -EFAULT; |
| 4827 | if (__get_user(clen, &uiov->iov_len)) |
| 4828 | return -EFAULT; |
| 4829 | if (clen < 0) |
| 4830 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4831 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4832 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4833 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4834 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4835 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4836 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4837 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4838 | if (ret < 0) |
| 4839 | return ret; |
| 4840 | } |
| 4841 | |
| 4842 | return 0; |
| 4843 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4844 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4845 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4846 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4847 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4848 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4849 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4850 | |
| 4851 | #ifdef CONFIG_COMPAT |
| 4852 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4853 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4854 | #endif |
| 4855 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4856 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4857 | } |
| 4858 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4859 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4860 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4861 | { |
| 4862 | struct io_sr_msg *sr = &req->sr_msg; |
| 4863 | struct io_buffer *kbuf; |
| 4864 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4865 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4866 | if (IS_ERR(kbuf)) |
| 4867 | return kbuf; |
| 4868 | |
| 4869 | sr->kbuf = kbuf; |
| 4870 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4871 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4874 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4875 | { |
| 4876 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4877 | } |
| 4878 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4879 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4880 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4881 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4882 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4883 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4884 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4885 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4886 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4887 | return -EINVAL; |
| 4888 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4889 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4890 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4891 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4892 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4893 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4894 | #ifdef CONFIG_COMPAT |
| 4895 | if (req->ctx->compat) |
| 4896 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4897 | #endif |
| 4898 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4899 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4900 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4901 | ret = io_recvmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4902 | if (!ret) |
| 4903 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4904 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4905 | } |
| 4906 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4907 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4908 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4909 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4910 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4911 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4912 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4913 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4914 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4915 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4916 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4917 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4918 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4919 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4920 | kmsg = req->async_data; |
| 4921 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4922 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4923 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4924 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4925 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4926 | } |
| 4927 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4928 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4929 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4930 | if (IS_ERR(kbuf)) |
| 4931 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4932 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4933 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4934 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4935 | 1, req->sr_msg.len); |
| 4936 | } |
| 4937 | |
| 4938 | flags = req->sr_msg.msg_flags; |
| 4939 | if (flags & MSG_DONTWAIT) |
| 4940 | req->flags |= REQ_F_NOWAIT; |
| 4941 | else if (force_nonblock) |
| 4942 | flags |= MSG_DONTWAIT; |
| 4943 | |
| 4944 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4945 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4946 | if (force_nonblock && ret == -EAGAIN) |
| 4947 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4948 | if (ret == -ERESTARTSYS) |
| 4949 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4950 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4951 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4952 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4953 | /* fast path, check for non-NULL to avoid function call */ |
| 4954 | if (kmsg->free_iov) |
| 4955 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4956 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4957 | if (ret < 0) |
| 4958 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4959 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4960 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4961 | } |
| 4962 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4963 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4964 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4965 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4966 | struct io_sr_msg *sr = &req->sr_msg; |
| 4967 | struct msghdr msg; |
| 4968 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4969 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4970 | struct iovec iov; |
| 4971 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4972 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4973 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4974 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4975 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4976 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4977 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4978 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4979 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4980 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4981 | if (IS_ERR(kbuf)) |
| 4982 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4983 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4984 | } |
| 4985 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4986 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4987 | if (unlikely(ret)) |
| 4988 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4989 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4990 | msg.msg_name = NULL; |
| 4991 | msg.msg_control = NULL; |
| 4992 | msg.msg_controllen = 0; |
| 4993 | msg.msg_namelen = 0; |
| 4994 | msg.msg_iocb = NULL; |
| 4995 | msg.msg_flags = 0; |
| 4996 | |
| 4997 | flags = req->sr_msg.msg_flags; |
| 4998 | if (flags & MSG_DONTWAIT) |
| 4999 | req->flags |= REQ_F_NOWAIT; |
| 5000 | else if (force_nonblock) |
| 5001 | flags |= MSG_DONTWAIT; |
| 5002 | |
| 5003 | ret = sock_recvmsg(sock, &msg, flags); |
| 5004 | if (force_nonblock && ret == -EAGAIN) |
| 5005 | return -EAGAIN; |
| 5006 | if (ret == -ERESTARTSYS) |
| 5007 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5008 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5009 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5010 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5011 | if (ret < 0) |
| 5012 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5013 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5014 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5017 | 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] | 5018 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5019 | struct io_accept *accept = &req->accept; |
| 5020 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5021 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5022 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 5023 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5024 | return -EINVAL; |
| 5025 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5026 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5027 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5028 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5029 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5030 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5031 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5032 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5033 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5034 | { |
| 5035 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5036 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5037 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5038 | int ret; |
| 5039 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5040 | if (req->file->f_flags & O_NONBLOCK) |
| 5041 | req->flags |= REQ_F_NOWAIT; |
| 5042 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5043 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5044 | accept->addr_len, accept->flags, |
| 5045 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5046 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5047 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5048 | if (ret < 0) { |
| 5049 | if (ret == -ERESTARTSYS) |
| 5050 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5051 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5052 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5053 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5054 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5055 | } |
| 5056 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5057 | 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] | 5058 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5059 | struct io_connect *conn = &req->connect; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5060 | struct io_async_connect *io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5061 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5062 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5063 | return -EINVAL; |
| 5064 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5065 | return -EINVAL; |
| 5066 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5067 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5068 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 5069 | |
| 5070 | if (!io) |
| 5071 | return 0; |
| 5072 | |
| 5073 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5074 | &io->address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5075 | } |
| 5076 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5077 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5078 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5079 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5080 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5081 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5082 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
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 | if (req->async_data) { |
| 5085 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5086 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5087 | ret = move_addr_to_kernel(req->connect.addr, |
| 5088 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5089 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5090 | if (ret) |
| 5091 | goto out; |
| 5092 | io = &__io; |
| 5093 | } |
| 5094 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5095 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5096 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5097 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5098 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5099 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5100 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5101 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5102 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5103 | ret = -ENOMEM; |
| 5104 | goto out; |
| 5105 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5106 | io = req->async_data; |
| 5107 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5108 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5109 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5110 | if (ret == -ERESTARTSYS) |
| 5111 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5112 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5113 | if (ret < 0) |
| 5114 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5115 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5116 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5117 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5118 | #else /* !CONFIG_NET */ |
| 5119 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5120 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5121 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5122 | } |
| 5123 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5124 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5125 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5126 | return -EOPNOTSUPP; |
| 5127 | } |
| 5128 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5129 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5130 | { |
| 5131 | return -EOPNOTSUPP; |
| 5132 | } |
| 5133 | |
| 5134 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 5135 | const struct io_uring_sqe *sqe) |
| 5136 | { |
| 5137 | return -EOPNOTSUPP; |
| 5138 | } |
| 5139 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5140 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5141 | { |
| 5142 | return -EOPNOTSUPP; |
| 5143 | } |
| 5144 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5145 | static int io_recv(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 | |
| 5150 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5151 | { |
| 5152 | return -EOPNOTSUPP; |
| 5153 | } |
| 5154 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5155 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5156 | { |
| 5157 | return -EOPNOTSUPP; |
| 5158 | } |
| 5159 | |
| 5160 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5161 | { |
| 5162 | return -EOPNOTSUPP; |
| 5163 | } |
| 5164 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5165 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5166 | { |
| 5167 | return -EOPNOTSUPP; |
| 5168 | } |
| 5169 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5170 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5171 | struct io_poll_table { |
| 5172 | struct poll_table_struct pt; |
| 5173 | struct io_kiocb *req; |
| 5174 | int error; |
| 5175 | }; |
| 5176 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5177 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5178 | __poll_t mask, task_work_func_t func) |
| 5179 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5180 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5181 | |
| 5182 | /* for instances that support it check for an event match first: */ |
| 5183 | if (mask && !(mask & poll->events)) |
| 5184 | return 0; |
| 5185 | |
| 5186 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5187 | |
| 5188 | list_del_init(&poll->wait.entry); |
| 5189 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5190 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 5191 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5192 | percpu_ref_get(&req->ctx->refs); |
| 5193 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5194 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5195 | * If this fails, then the task is exiting. When a task exits, the |
| 5196 | * work gets canceled, so just cancel this request as well instead |
| 5197 | * of executing it. We can't safely execute it anyway, as we may not |
| 5198 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5199 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5200 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5201 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5202 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 5203 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5204 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5205 | return 1; |
| 5206 | } |
| 5207 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5208 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5209 | __acquires(&req->ctx->completion_lock) |
| 5210 | { |
| 5211 | struct io_ring_ctx *ctx = req->ctx; |
| 5212 | |
| 5213 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5214 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5215 | |
| 5216 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5217 | } |
| 5218 | |
| 5219 | spin_lock_irq(&ctx->completion_lock); |
| 5220 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5221 | add_wait_queue(poll->head, &poll->wait); |
| 5222 | return true; |
| 5223 | } |
| 5224 | |
| 5225 | return false; |
| 5226 | } |
| 5227 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5228 | 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] | 5229 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5230 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5231 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5232 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5233 | return req->apoll->double_poll; |
| 5234 | } |
| 5235 | |
| 5236 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5237 | { |
| 5238 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5239 | return &req->poll; |
| 5240 | return &req->apoll->poll; |
| 5241 | } |
| 5242 | |
| 5243 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5244 | { |
| 5245 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5246 | |
| 5247 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5248 | |
| 5249 | if (poll && poll->head) { |
| 5250 | struct wait_queue_head *head = poll->head; |
| 5251 | |
| 5252 | spin_lock(&head->lock); |
| 5253 | list_del_init(&poll->wait.entry); |
| 5254 | if (poll->wait.private) |
| 5255 | refcount_dec(&req->refs); |
| 5256 | poll->head = NULL; |
| 5257 | spin_unlock(&head->lock); |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5262 | { |
| 5263 | struct io_ring_ctx *ctx = req->ctx; |
| 5264 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5265 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5266 | req->poll.done = true; |
| 5267 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5268 | io_commit_cqring(ctx); |
| 5269 | } |
| 5270 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5271 | static void io_poll_task_func(struct callback_head *cb) |
| 5272 | { |
| 5273 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5274 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5275 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5276 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5277 | if (io_poll_rewait(req, &req->poll)) { |
| 5278 | spin_unlock_irq(&ctx->completion_lock); |
| 5279 | } else { |
| 5280 | hash_del(&req->hash_node); |
| 5281 | io_poll_complete(req, req->result, 0); |
| 5282 | spin_unlock_irq(&ctx->completion_lock); |
| 5283 | |
| 5284 | nxt = io_put_req_find_next(req); |
| 5285 | io_cqring_ev_posted(ctx); |
| 5286 | if (nxt) |
| 5287 | __io_req_task_submit(nxt); |
| 5288 | } |
| 5289 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5290 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5294 | int sync, void *key) |
| 5295 | { |
| 5296 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5297 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5298 | __poll_t mask = key_to_poll(key); |
| 5299 | |
| 5300 | /* for instances that support it check for an event match first: */ |
| 5301 | if (mask && !(mask & poll->events)) |
| 5302 | return 0; |
| 5303 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5304 | list_del_init(&wait->entry); |
| 5305 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5306 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5307 | bool done; |
| 5308 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5309 | spin_lock(&poll->head->lock); |
| 5310 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5311 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5312 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5313 | /* make sure double remove sees this as being gone */ |
| 5314 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5315 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5316 | if (!done) { |
| 5317 | /* use wait func handler, so it matches the rq type */ |
| 5318 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5319 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5320 | } |
| 5321 | refcount_dec(&req->refs); |
| 5322 | return 1; |
| 5323 | } |
| 5324 | |
| 5325 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5326 | wait_queue_func_t wake_func) |
| 5327 | { |
| 5328 | poll->head = NULL; |
| 5329 | poll->done = false; |
| 5330 | poll->canceled = false; |
| 5331 | poll->events = events; |
| 5332 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5333 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5334 | } |
| 5335 | |
| 5336 | 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] | 5337 | struct wait_queue_head *head, |
| 5338 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5339 | { |
| 5340 | struct io_kiocb *req = pt->req; |
| 5341 | |
| 5342 | /* |
| 5343 | * If poll->head is already set, it's because the file being polled |
| 5344 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5345 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5346 | */ |
| 5347 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5348 | struct io_poll_iocb *poll_one = poll; |
| 5349 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5350 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5351 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5352 | pt->error = -EINVAL; |
| 5353 | return; |
| 5354 | } |
| 5355 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5356 | if (!poll) { |
| 5357 | pt->error = -ENOMEM; |
| 5358 | return; |
| 5359 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5360 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5361 | refcount_inc(&req->refs); |
| 5362 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5363 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | pt->error = 0; |
| 5367 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5368 | |
| 5369 | if (poll->events & EPOLLEXCLUSIVE) |
| 5370 | add_wait_queue_exclusive(head, &poll->wait); |
| 5371 | else |
| 5372 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5376 | struct poll_table_struct *p) |
| 5377 | { |
| 5378 | 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] | 5379 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5380 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5381 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5382 | } |
| 5383 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5384 | static void io_async_task_func(struct callback_head *cb) |
| 5385 | { |
| 5386 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5387 | struct async_poll *apoll = req->apoll; |
| 5388 | struct io_ring_ctx *ctx = req->ctx; |
| 5389 | |
| 5390 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5391 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5392 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5393 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5394 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5395 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5396 | } |
| 5397 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5398 | /* 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] | 5399 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5400 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5401 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5402 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5403 | spin_unlock_irq(&ctx->completion_lock); |
| 5404 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5405 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5406 | __io_req_task_submit(req); |
| 5407 | else |
| 5408 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5409 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5410 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5411 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5412 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5416 | void *key) |
| 5417 | { |
| 5418 | struct io_kiocb *req = wait->private; |
| 5419 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5420 | |
| 5421 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5422 | key_to_poll(key)); |
| 5423 | |
| 5424 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5425 | } |
| 5426 | |
| 5427 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5428 | { |
| 5429 | struct io_ring_ctx *ctx = req->ctx; |
| 5430 | struct hlist_head *list; |
| 5431 | |
| 5432 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5433 | hlist_add_head(&req->hash_node, list); |
| 5434 | } |
| 5435 | |
| 5436 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5437 | struct io_poll_iocb *poll, |
| 5438 | struct io_poll_table *ipt, __poll_t mask, |
| 5439 | wait_queue_func_t wake_func) |
| 5440 | __acquires(&ctx->completion_lock) |
| 5441 | { |
| 5442 | struct io_ring_ctx *ctx = req->ctx; |
| 5443 | bool cancel = false; |
| 5444 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5445 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5446 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5447 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5448 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5449 | |
| 5450 | ipt->pt._key = mask; |
| 5451 | ipt->req = req; |
| 5452 | ipt->error = -EINVAL; |
| 5453 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5454 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5455 | |
| 5456 | spin_lock_irq(&ctx->completion_lock); |
| 5457 | if (likely(poll->head)) { |
| 5458 | spin_lock(&poll->head->lock); |
| 5459 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5460 | if (ipt->error) |
| 5461 | cancel = true; |
| 5462 | ipt->error = 0; |
| 5463 | mask = 0; |
| 5464 | } |
| 5465 | if (mask || ipt->error) |
| 5466 | list_del_init(&poll->wait.entry); |
| 5467 | else if (cancel) |
| 5468 | WRITE_ONCE(poll->canceled, true); |
| 5469 | else if (!poll->done) /* actually waiting for an event */ |
| 5470 | io_poll_req_insert(req); |
| 5471 | spin_unlock(&poll->head->lock); |
| 5472 | } |
| 5473 | |
| 5474 | return mask; |
| 5475 | } |
| 5476 | |
| 5477 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5478 | { |
| 5479 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5480 | struct io_ring_ctx *ctx = req->ctx; |
| 5481 | struct async_poll *apoll; |
| 5482 | struct io_poll_table ipt; |
| 5483 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5484 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5485 | |
| 5486 | if (!req->file || !file_can_poll(req->file)) |
| 5487 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5488 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5489 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5490 | if (def->pollin) |
| 5491 | rw = READ; |
| 5492 | else if (def->pollout) |
| 5493 | rw = WRITE; |
| 5494 | else |
| 5495 | return false; |
| 5496 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5497 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5498 | return false; |
| 5499 | |
| 5500 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5501 | if (unlikely(!apoll)) |
| 5502 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5503 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5504 | |
| 5505 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5506 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5507 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5508 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5509 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5510 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5511 | if (def->pollout) |
| 5512 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5513 | |
| 5514 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5515 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5516 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5517 | mask &= ~POLLIN; |
| 5518 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5519 | mask |= POLLERR | POLLPRI; |
| 5520 | |
| 5521 | ipt.pt._qproc = io_async_queue_proc; |
| 5522 | |
| 5523 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5524 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5525 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5526 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5527 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5528 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5529 | kfree(apoll); |
| 5530 | return false; |
| 5531 | } |
| 5532 | spin_unlock_irq(&ctx->completion_lock); |
| 5533 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5534 | apoll->poll.events); |
| 5535 | return true; |
| 5536 | } |
| 5537 | |
| 5538 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5539 | struct io_poll_iocb *poll) |
| 5540 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5541 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5542 | |
| 5543 | spin_lock(&poll->head->lock); |
| 5544 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5545 | if (!list_empty(&poll->wait.entry)) { |
| 5546 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5547 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5548 | } |
| 5549 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5550 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5551 | return do_complete; |
| 5552 | } |
| 5553 | |
| 5554 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5555 | { |
| 5556 | bool do_complete; |
| 5557 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5558 | io_poll_remove_double(req); |
| 5559 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5560 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5561 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5562 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5563 | struct async_poll *apoll = req->apoll; |
| 5564 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5565 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5566 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5567 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5568 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5569 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5570 | kfree(apoll); |
| 5571 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5572 | } |
| 5573 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5574 | if (do_complete) { |
| 5575 | io_cqring_fill_event(req, -ECANCELED); |
| 5576 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5577 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5578 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5582 | } |
| 5583 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5584 | /* |
| 5585 | * Returns true if we found and killed one or more poll requests |
| 5586 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5587 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5588 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5589 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5590 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5591 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5592 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5593 | |
| 5594 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5595 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5596 | struct hlist_head *list; |
| 5597 | |
| 5598 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5599 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5600 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5601 | posted += io_poll_remove_one(req); |
| 5602 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5603 | } |
| 5604 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5605 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5606 | if (posted) |
| 5607 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5608 | |
| 5609 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5610 | } |
| 5611 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5612 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5613 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5614 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5615 | struct io_kiocb *req; |
| 5616 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5617 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5618 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5619 | if (sqe_addr != req->user_data) |
| 5620 | continue; |
| 5621 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5622 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5623 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | return -ENOENT; |
| 5627 | } |
| 5628 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5629 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5630 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5631 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5632 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5633 | return -EINVAL; |
| 5634 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5635 | sqe->poll_events) |
| 5636 | return -EINVAL; |
| 5637 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5638 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5639 | return 0; |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * Find a running poll command that matches one specified in sqe->addr, |
| 5644 | * and remove it if found. |
| 5645 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5646 | 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] | 5647 | { |
| 5648 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5649 | int ret; |
| 5650 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5651 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5652 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5653 | spin_unlock_irq(&ctx->completion_lock); |
| 5654 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5655 | if (ret < 0) |
| 5656 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5657 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5658 | return 0; |
| 5659 | } |
| 5660 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5661 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5662 | void *key) |
| 5663 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5664 | struct io_kiocb *req = wait->private; |
| 5665 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5666 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5667 | 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] | 5668 | } |
| 5669 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5670 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5671 | struct poll_table_struct *p) |
| 5672 | { |
| 5673 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5674 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5675 | __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] | 5676 | } |
| 5677 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5678 | 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] | 5679 | { |
| 5680 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5681 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5682 | |
| 5683 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5684 | return -EINVAL; |
| 5685 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5686 | return -EINVAL; |
| 5687 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5688 | events = READ_ONCE(sqe->poll32_events); |
| 5689 | #ifdef __BIG_ENDIAN |
| 5690 | events = swahw32(events); |
| 5691 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5692 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5693 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5694 | return 0; |
| 5695 | } |
| 5696 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5697 | 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] | 5698 | { |
| 5699 | struct io_poll_iocb *poll = &req->poll; |
| 5700 | struct io_ring_ctx *ctx = req->ctx; |
| 5701 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5702 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5703 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5704 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5705 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5706 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5707 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5708 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5709 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5710 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5711 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5712 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5713 | spin_unlock_irq(&ctx->completion_lock); |
| 5714 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5715 | if (mask) { |
| 5716 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5717 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5718 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5719 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5720 | } |
| 5721 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5722 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5723 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5724 | struct io_timeout_data *data = container_of(timer, |
| 5725 | struct io_timeout_data, timer); |
| 5726 | struct io_kiocb *req = data->req; |
| 5727 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5728 | unsigned long flags; |
| 5729 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5730 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5731 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5732 | atomic_set(&req->ctx->cq_timeouts, |
| 5733 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5734 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5735 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5736 | io_commit_cqring(ctx); |
| 5737 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5738 | |
| 5739 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5740 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5741 | io_put_req(req); |
| 5742 | return HRTIMER_NORESTART; |
| 5743 | } |
| 5744 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5745 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5746 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5747 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5748 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5749 | struct io_kiocb *req; |
| 5750 | int ret = -ENOENT; |
| 5751 | |
| 5752 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5753 | if (user_data == req->user_data) { |
| 5754 | ret = 0; |
| 5755 | break; |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5760 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5761 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5762 | io = req->async_data; |
| 5763 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5764 | if (ret == -1) |
| 5765 | return ERR_PTR(-EALREADY); |
| 5766 | list_del_init(&req->timeout.list); |
| 5767 | return req; |
| 5768 | } |
| 5769 | |
| 5770 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5771 | { |
| 5772 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5773 | |
| 5774 | if (IS_ERR(req)) |
| 5775 | return PTR_ERR(req); |
| 5776 | |
| 5777 | req_set_fail_links(req); |
| 5778 | io_cqring_fill_event(req, -ECANCELED); |
| 5779 | io_put_req_deferred(req, 1); |
| 5780 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5781 | } |
| 5782 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5783 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5784 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5785 | { |
| 5786 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5787 | struct io_timeout_data *data; |
| 5788 | |
| 5789 | if (IS_ERR(req)) |
| 5790 | return PTR_ERR(req); |
| 5791 | |
| 5792 | req->timeout.off = 0; /* noseq */ |
| 5793 | data = req->async_data; |
| 5794 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5795 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5796 | data->timer.function = io_timeout_fn; |
| 5797 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5798 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5799 | } |
| 5800 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5801 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5802 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5803 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5804 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5805 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5806 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5807 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5808 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5809 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5810 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5811 | return -EINVAL; |
| 5812 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5813 | tr->addr = READ_ONCE(sqe->addr); |
| 5814 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5815 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5816 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5817 | return -EINVAL; |
| 5818 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5819 | return -EFAULT; |
| 5820 | } else if (tr->flags) { |
| 5821 | /* timeout removal doesn't support flags */ |
| 5822 | return -EINVAL; |
| 5823 | } |
| 5824 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5825 | return 0; |
| 5826 | } |
| 5827 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5828 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5829 | { |
| 5830 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5831 | : HRTIMER_MODE_REL; |
| 5832 | } |
| 5833 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5834 | /* |
| 5835 | * Remove or update an existing timeout command |
| 5836 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5837 | 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] | 5838 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5839 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5840 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5841 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5842 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5843 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5844 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5845 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5846 | else |
| 5847 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5848 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5849 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5850 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5851 | io_commit_cqring(ctx); |
| 5852 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5853 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5854 | if (ret < 0) |
| 5855 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5856 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5857 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5858 | } |
| 5859 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5860 | 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] | 5861 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5862 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5863 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5864 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5865 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5866 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5867 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5868 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5869 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5870 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5871 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5872 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5873 | flags = READ_ONCE(sqe->timeout_flags); |
| 5874 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5875 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5876 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5877 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5878 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5879 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5880 | return -ENOMEM; |
| 5881 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5882 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5883 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5884 | |
| 5885 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5886 | return -EFAULT; |
| 5887 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5888 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5889 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5890 | return 0; |
| 5891 | } |
| 5892 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5893 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5894 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5895 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5896 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5897 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5898 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5899 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5900 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5901 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5902 | /* |
| 5903 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5904 | * timeout event to be satisfied. If it isn't set, then this is |
| 5905 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5906 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5907 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5908 | entry = ctx->timeout_list.prev; |
| 5909 | goto add; |
| 5910 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5911 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5912 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5913 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5914 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5915 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5916 | * This is safe because ->completion_lock is held, and submissions |
| 5917 | * and completions are never mixed in the same ->completion_lock section. |
| 5918 | */ |
| 5919 | ctx->cq_last_tm_flush = tail; |
| 5920 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5921 | /* |
| 5922 | * Insertion sort, ensuring the first entry in the list is always |
| 5923 | * the one we need first. |
| 5924 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5925 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5926 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5927 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5928 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5929 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5930 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5931 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5932 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5933 | break; |
| 5934 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5935 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5936 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5937 | data->timer.function = io_timeout_fn; |
| 5938 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5939 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5940 | return 0; |
| 5941 | } |
| 5942 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5943 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5944 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5945 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5946 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5947 | return req->user_data == (unsigned long) data; |
| 5948 | } |
| 5949 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5950 | 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] | 5951 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5952 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5953 | int ret = 0; |
| 5954 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5955 | 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] | 5956 | switch (cancel_ret) { |
| 5957 | case IO_WQ_CANCEL_OK: |
| 5958 | ret = 0; |
| 5959 | break; |
| 5960 | case IO_WQ_CANCEL_RUNNING: |
| 5961 | ret = -EALREADY; |
| 5962 | break; |
| 5963 | case IO_WQ_CANCEL_NOTFOUND: |
| 5964 | ret = -ENOENT; |
| 5965 | break; |
| 5966 | } |
| 5967 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5968 | return ret; |
| 5969 | } |
| 5970 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5971 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5972 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5973 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5974 | { |
| 5975 | unsigned long flags; |
| 5976 | int ret; |
| 5977 | |
| 5978 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5979 | if (ret != -ENOENT) { |
| 5980 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5981 | goto done; |
| 5982 | } |
| 5983 | |
| 5984 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5985 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5986 | if (ret != -ENOENT) |
| 5987 | goto done; |
| 5988 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5989 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5990 | if (!ret) |
| 5991 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5992 | io_cqring_fill_event(req, ret); |
| 5993 | io_commit_cqring(ctx); |
| 5994 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5995 | io_cqring_ev_posted(ctx); |
| 5996 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5997 | if (ret < 0) |
| 5998 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5999 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6000 | } |
| 6001 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6002 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6003 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6004 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6005 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6006 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6007 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6008 | return -EINVAL; |
| 6009 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6010 | return -EINVAL; |
| 6011 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6012 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6013 | return 0; |
| 6014 | } |
| 6015 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6016 | 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] | 6017 | { |
| 6018 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6019 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6020 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6021 | return 0; |
| 6022 | } |
| 6023 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6024 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6025 | const struct io_uring_sqe *sqe) |
| 6026 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 6027 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 6028 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6029 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6030 | return -EINVAL; |
| 6031 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6032 | return -EINVAL; |
| 6033 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6034 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6035 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6036 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6037 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6038 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6039 | return 0; |
| 6040 | } |
| 6041 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6042 | 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] | 6043 | { |
| 6044 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6045 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6046 | int ret; |
| 6047 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6048 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6049 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6050 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6051 | up.offset = req->rsrc_update.offset; |
| 6052 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6053 | |
| 6054 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6055 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6056 | mutex_unlock(&ctx->uring_lock); |
| 6057 | |
| 6058 | if (ret < 0) |
| 6059 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6060 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6061 | return 0; |
| 6062 | } |
| 6063 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6064 | 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] | 6065 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6066 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6067 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6068 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6069 | case IORING_OP_READV: |
| 6070 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6071 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6072 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6073 | case IORING_OP_WRITEV: |
| 6074 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6075 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6076 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6077 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6078 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6079 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6080 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6081 | case IORING_OP_FSYNC: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6082 | return io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6083 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6084 | return io_prep_sfr(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6085 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6086 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6087 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6088 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6089 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6090 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6091 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6092 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6093 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6094 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6095 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6096 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6097 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6098 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6099 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6100 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6101 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6102 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6103 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6104 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6105 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6106 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6107 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6108 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6109 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6110 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6111 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6112 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6113 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6114 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6115 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6116 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6117 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6118 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6119 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6120 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6121 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6122 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6123 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6124 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6125 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6126 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6127 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6128 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6129 | case IORING_OP_SHUTDOWN: |
| 6130 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6131 | case IORING_OP_RENAMEAT: |
| 6132 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6133 | case IORING_OP_UNLINKAT: |
| 6134 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6135 | } |
| 6136 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6137 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6138 | req->opcode); |
| 6139 | return-EINVAL; |
| 6140 | } |
| 6141 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6142 | static int io_req_defer_prep(struct io_kiocb *req, |
| 6143 | const struct io_uring_sqe *sqe) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6144 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6145 | if (!sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6146 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6147 | if (io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6148 | return -EAGAIN; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6149 | return io_req_prep(req, sqe); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6150 | } |
| 6151 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6152 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6153 | { |
| 6154 | struct io_kiocb *pos; |
| 6155 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6156 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6157 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6158 | io_for_each_link(pos, req) |
| 6159 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6160 | |
| 6161 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6162 | return total_submitted - nr_reqs; |
| 6163 | } |
| 6164 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6165 | 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] | 6166 | { |
| 6167 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6168 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6169 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6170 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6171 | |
| 6172 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6173 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6174 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6175 | return 0; |
| 6176 | |
| 6177 | seq = io_get_sequence(req); |
| 6178 | /* Still a chance to pass the sequence check */ |
| 6179 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6180 | return 0; |
| 6181 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6182 | if (!req->async_data) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6183 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6184 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6185 | return ret; |
| 6186 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6187 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6188 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6189 | if (!de) |
| 6190 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6191 | |
| 6192 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6193 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6194 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6195 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6196 | io_queue_async_work(req); |
| 6197 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6201 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6202 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6203 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6204 | spin_unlock_irq(&ctx->completion_lock); |
| 6205 | return -EIOCBQUEUED; |
| 6206 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6207 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6208 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6209 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6210 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6211 | switch (req->opcode) { |
| 6212 | case IORING_OP_READV: |
| 6213 | case IORING_OP_READ_FIXED: |
| 6214 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6215 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6216 | break; |
| 6217 | case IORING_OP_RECVMSG: |
| 6218 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6219 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6220 | break; |
| 6221 | } |
| 6222 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6223 | } |
| 6224 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6225 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6226 | switch (req->opcode) { |
| 6227 | case IORING_OP_READV: |
| 6228 | case IORING_OP_READ_FIXED: |
| 6229 | case IORING_OP_READ: |
| 6230 | case IORING_OP_WRITEV: |
| 6231 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6232 | case IORING_OP_WRITE: { |
| 6233 | struct io_async_rw *io = req->async_data; |
| 6234 | if (io->free_iovec) |
| 6235 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6236 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6237 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6238 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6239 | case IORING_OP_SENDMSG: { |
| 6240 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6241 | |
| 6242 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6243 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6244 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6245 | case IORING_OP_SPLICE: |
| 6246 | case IORING_OP_TEE: |
| 6247 | io_put_file(req, req->splice.file_in, |
| 6248 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6249 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6250 | case IORING_OP_OPENAT: |
| 6251 | case IORING_OP_OPENAT2: |
| 6252 | if (req->open.filename) |
| 6253 | putname(req->open.filename); |
| 6254 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6255 | case IORING_OP_RENAMEAT: |
| 6256 | putname(req->rename.oldpath); |
| 6257 | putname(req->rename.newpath); |
| 6258 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6259 | case IORING_OP_UNLINKAT: |
| 6260 | putname(req->unlink.filename); |
| 6261 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6262 | } |
| 6263 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6264 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6265 | } |
| 6266 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6267 | 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] | 6268 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6269 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6270 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6271 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6272 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6273 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6274 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6275 | break; |
| 6276 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6277 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6278 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6279 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6280 | break; |
| 6281 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6282 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6283 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6284 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6285 | break; |
| 6286 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6287 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6288 | break; |
| 6289 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6290 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6291 | break; |
| 6292 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6293 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6294 | break; |
| 6295 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6296 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6297 | break; |
| 6298 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6299 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6300 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6301 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6302 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6303 | break; |
| 6304 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6305 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6306 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6307 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6308 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6309 | break; |
| 6310 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6311 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6312 | break; |
| 6313 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6314 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6315 | break; |
| 6316 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6317 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6318 | break; |
| 6319 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6320 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6321 | break; |
| 6322 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6323 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6324 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6325 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6326 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6327 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6328 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6329 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6330 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6331 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6332 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6333 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6334 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6335 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6336 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6337 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6338 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6339 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6340 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6341 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6342 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6343 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6344 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6345 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6346 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6347 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6348 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6349 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6350 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6351 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6352 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6353 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6354 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6355 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6356 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6357 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6358 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6359 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6360 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6361 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6362 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6363 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6364 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6365 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6366 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6367 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6368 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6369 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6370 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6371 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6372 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6373 | default: |
| 6374 | ret = -EINVAL; |
| 6375 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6379 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6380 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6381 | /* If the op doesn't have a file, we're not polling for it */ |
| 6382 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6383 | const bool in_async = io_wq_current_is_worker(); |
| 6384 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6385 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6386 | if (in_async) |
| 6387 | mutex_lock(&ctx->uring_lock); |
| 6388 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6389 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6390 | |
| 6391 | if (in_async) |
| 6392 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | return 0; |
| 6396 | } |
| 6397 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6398 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6399 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6400 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6401 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6402 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6403 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6404 | timeout = io_prep_linked_timeout(req); |
| 6405 | if (timeout) |
| 6406 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6407 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6408 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6409 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6410 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6411 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6412 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6413 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6414 | /* |
| 6415 | * We can get EAGAIN for polled IO even though we're |
| 6416 | * forcing a sync submission from here, since we can't |
| 6417 | * wait for request slots on the block side. |
| 6418 | */ |
| 6419 | if (ret != -EAGAIN) |
| 6420 | break; |
| 6421 | cond_resched(); |
| 6422 | } while (1); |
| 6423 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6424 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6425 | if (ret) { |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6426 | struct io_ring_ctx *lock_ctx = NULL; |
Xiaoguang Wang | dad1b12 | 2020-12-06 22:22:42 +0000 | [diff] [blame] | 6427 | |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6428 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 6429 | lock_ctx = req->ctx; |
| 6430 | |
| 6431 | /* |
| 6432 | * io_iopoll_complete() does not hold completion_lock to |
| 6433 | * complete polled io, so here for polled io, we can not call |
| 6434 | * io_req_complete() directly, otherwise there maybe concurrent |
| 6435 | * access to cqring, defer_list, etc, which is not safe. Given |
| 6436 | * that io_iopoll_complete() is always called under uring_lock, |
| 6437 | * so here for polled io, we also get uring_lock to complete |
| 6438 | * it. |
| 6439 | */ |
| 6440 | if (lock_ctx) |
| 6441 | mutex_lock(&lock_ctx->uring_lock); |
| 6442 | |
| 6443 | req_set_fail_links(req); |
| 6444 | io_req_complete(req, ret); |
| 6445 | |
| 6446 | if (lock_ctx) |
| 6447 | mutex_unlock(&lock_ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6448 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6449 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6450 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6451 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6452 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6453 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6454 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6455 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6456 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6457 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6458 | } |
| 6459 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6460 | static struct file *io_file_get(struct io_submit_state *state, |
| 6461 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6462 | { |
| 6463 | struct io_ring_ctx *ctx = req->ctx; |
| 6464 | struct file *file; |
| 6465 | |
| 6466 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6467 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6468 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6469 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6470 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6471 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6472 | } else { |
| 6473 | trace_io_uring_file_get(ctx, fd); |
| 6474 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6475 | } |
| 6476 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6477 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6478 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6479 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6480 | } |
| 6481 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6482 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6483 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6484 | struct io_timeout_data *data = container_of(timer, |
| 6485 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6486 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6487 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6488 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6489 | |
| 6490 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6491 | prev = req->timeout.head; |
| 6492 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6493 | |
| 6494 | /* |
| 6495 | * We don't expect the list to be empty, that will only happen if we |
| 6496 | * race with the completion of the linked work. |
| 6497 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6498 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6499 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6500 | else |
| 6501 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6502 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6503 | |
| 6504 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6505 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6506 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6507 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6508 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6509 | io_req_complete_post(req, -ETIME, 0); |
| 6510 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6511 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6512 | return HRTIMER_NORESTART; |
| 6513 | } |
| 6514 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6515 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6516 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6517 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6518 | * If the back reference is NULL, then our linked request finished |
| 6519 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6520 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6521 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6522 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6523 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6524 | data->timer.function = io_link_timeout_fn; |
| 6525 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6526 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6527 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6528 | } |
| 6529 | |
| 6530 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6531 | { |
| 6532 | struct io_ring_ctx *ctx = req->ctx; |
| 6533 | |
| 6534 | spin_lock_irq(&ctx->completion_lock); |
| 6535 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6536 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6537 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6538 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6539 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6540 | } |
| 6541 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6542 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6543 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6544 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6545 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6546 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6547 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6548 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6549 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6550 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6551 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6552 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6553 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6554 | } |
| 6555 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6556 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6557 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6558 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6559 | const struct cred *old_creds = NULL; |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6560 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6561 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6562 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6563 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6564 | req->work.identity->creds != current_cred()) |
| 6565 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6566 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6567 | 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] | 6568 | |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6569 | if (old_creds) |
| 6570 | revert_creds(old_creds); |
| 6571 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6572 | /* |
| 6573 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6574 | * doesn't support non-blocking read/write attempts |
| 6575 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6576 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6577 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6578 | /* |
| 6579 | * Queued up for async execution, worker will release |
| 6580 | * submit reference when the iocb is actually submitted. |
| 6581 | */ |
| 6582 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6583 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6584 | } else if (likely(!ret)) { |
| 6585 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6586 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6587 | struct io_ring_ctx *ctx = req->ctx; |
| 6588 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 6589 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6590 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6591 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6592 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6593 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6594 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6595 | } |
| 6596 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6597 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6598 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6599 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6600 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6601 | if (linked_timeout) |
| 6602 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6603 | } |
| 6604 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6605 | 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] | 6606 | { |
| 6607 | int ret; |
| 6608 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6609 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6610 | if (ret) { |
| 6611 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6612 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6613 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6614 | io_put_req(req); |
| 6615 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6616 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6617 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6618 | if (!req->async_data) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6619 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6620 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6621 | goto fail_req; |
| 6622 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6623 | io_queue_async_work(req); |
| 6624 | } else { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6625 | if (sqe) { |
| 6626 | ret = io_req_prep(req, sqe); |
| 6627 | if (unlikely(ret)) |
| 6628 | goto fail_req; |
| 6629 | } |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6630 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6631 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6632 | } |
| 6633 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6634 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6635 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6636 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6637 | io_put_req(req); |
| 6638 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6639 | } else |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6640 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6641 | } |
| 6642 | |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6643 | struct io_submit_link { |
| 6644 | struct io_kiocb *head; |
| 6645 | struct io_kiocb *last; |
| 6646 | }; |
| 6647 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6648 | 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] | 6649 | struct io_submit_link *link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6650 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6651 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6652 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6653 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6654 | /* |
| 6655 | * If we already have a head request, queue this one for async |
| 6656 | * submittal once the head completes. If we don't have a head but |
| 6657 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6658 | * submitted sync once the chain is complete. If none of those |
| 6659 | * conditions are true (normal request), then just queue it. |
| 6660 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6661 | if (link->head) { |
| 6662 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6663 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6664 | /* |
| 6665 | * Taking sequential execution of a link, draining both sides |
| 6666 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6667 | * requests in the link. So, it drains the head and the |
| 6668 | * next after the link request. The last one is done via |
| 6669 | * drain_next flag to persist the effect across calls. |
| 6670 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6671 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6672 | head->flags |= REQ_F_IO_DRAIN; |
| 6673 | ctx->drain_next = 1; |
| 6674 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6675 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6676 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6677 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6678 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6679 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6680 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6681 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6682 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6683 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6684 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6685 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6686 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6687 | io_queue_link_head(head); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6688 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6689 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6690 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6691 | if (unlikely(ctx->drain_next)) { |
| 6692 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6693 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6694 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6695 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6696 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6697 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6698 | req->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6699 | link->head = req; |
| 6700 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6701 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6702 | io_queue_sqe(req, sqe); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6703 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6704 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6705 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6706 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6707 | } |
| 6708 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6709 | /* |
| 6710 | * Batched submission is done, ensure local IO is flushed out. |
| 6711 | */ |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6712 | static void io_submit_state_end(struct io_submit_state *state, |
| 6713 | struct io_ring_ctx *ctx) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6714 | { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6715 | if (state->comp.nr) |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6716 | io_submit_flush_completions(&state->comp, ctx); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6717 | if (state->plug_started) |
| 6718 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6719 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | /* |
| 6723 | * Start submission side cache. |
| 6724 | */ |
| 6725 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6726 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6727 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6728 | state->plug_started = false; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6729 | state->ios_left = max_ios; |
| 6730 | } |
| 6731 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6732 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6733 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6734 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6735 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6736 | /* |
| 6737 | * Ensure any loads from the SQEs are done at this point, |
| 6738 | * since once we write the new head, the application could |
| 6739 | * write new data to them. |
| 6740 | */ |
| 6741 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6742 | } |
| 6743 | |
| 6744 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6745 | * 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] | 6746 | * that is mapped by userspace. This means that care needs to be taken to |
| 6747 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6748 | * being a good citizen. If members of the sqe are validated and then later |
| 6749 | * used, it's important that those reads are done through READ_ONCE() to |
| 6750 | * prevent a re-load down the line. |
| 6751 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6752 | 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] | 6753 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6754 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6755 | unsigned head; |
| 6756 | |
| 6757 | /* |
| 6758 | * The cached sq head (or cq tail) serves two purposes: |
| 6759 | * |
| 6760 | * 1) allows us to batch the cost of updating the user visible |
| 6761 | * head updates. |
| 6762 | * 2) allows the kernel side to track the head on its own, even |
| 6763 | * though the application is the one updating it. |
| 6764 | */ |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6765 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6766 | if (likely(head < ctx->sq_entries)) |
| 6767 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6768 | |
| 6769 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6770 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6771 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6772 | return NULL; |
| 6773 | } |
| 6774 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6775 | /* |
| 6776 | * Check SQE restrictions (opcode and flags). |
| 6777 | * |
| 6778 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6779 | */ |
| 6780 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6781 | struct io_kiocb *req, |
| 6782 | unsigned int sqe_flags) |
| 6783 | { |
| 6784 | if (!ctx->restricted) |
| 6785 | return true; |
| 6786 | |
| 6787 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6788 | return false; |
| 6789 | |
| 6790 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6791 | ctx->restrictions.sqe_flags_required) |
| 6792 | return false; |
| 6793 | |
| 6794 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6795 | ctx->restrictions.sqe_flags_required)) |
| 6796 | return false; |
| 6797 | |
| 6798 | return true; |
| 6799 | } |
| 6800 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6801 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6802 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6803 | IOSQE_BUFFER_SELECT) |
| 6804 | |
| 6805 | 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] | 6806 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6807 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6808 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6809 | unsigned int sqe_flags; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6810 | int id, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6811 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6812 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6813 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6814 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6815 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6816 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6817 | req->file = NULL; |
| 6818 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6819 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6820 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6821 | /* one is dropped after submission, the other at completion */ |
| 6822 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6823 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6824 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6825 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6826 | /* enforce forwards compatibility on users */ |
| 6827 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6828 | return -EINVAL; |
| 6829 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6830 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6831 | return -EINVAL; |
| 6832 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6833 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6834 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6835 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6836 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6837 | return -EACCES; |
| 6838 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6839 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6840 | !io_op_defs[req->opcode].buffer_select) |
| 6841 | return -EOPNOTSUPP; |
| 6842 | |
| 6843 | id = READ_ONCE(sqe->personality); |
| 6844 | if (id) { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6845 | struct io_identity *iod; |
| 6846 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6847 | iod = idr_find(&ctx->personality_idr, id); |
| 6848 | if (unlikely(!iod)) |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6849 | return -EINVAL; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6850 | refcount_inc(&iod->count); |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6851 | |
| 6852 | __io_req_init_async(req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6853 | get_cred(iod->creds); |
| 6854 | req->work.identity = iod; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6855 | req->work.flags |= IO_WQ_WORK_CREDS; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6856 | } |
| 6857 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6858 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6859 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6860 | /* |
| 6861 | * Plug now if we have more than 1 IO left after this, and the target |
| 6862 | * is potentially a read/write to block based storage. |
| 6863 | */ |
| 6864 | if (!state->plug_started && state->ios_left > 1 && |
| 6865 | io_op_defs[req->opcode].plug) { |
| 6866 | blk_start_plug(&state->plug); |
| 6867 | state->plug_started = true; |
| 6868 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6869 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6870 | if (io_op_defs[req->opcode].needs_file) { |
| 6871 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6872 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6873 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6874 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6875 | ret = -EBADF; |
| 6876 | } |
| 6877 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6878 | state->ios_left--; |
| 6879 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6880 | } |
| 6881 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6882 | 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] | 6883 | { |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6884 | struct io_submit_link link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6885 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6886 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6887 | /* 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] | 6888 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6889 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6890 | return -EBUSY; |
| 6891 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6892 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6893 | /* make sure SQ entry isn't read before tail */ |
| 6894 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6895 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6896 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6897 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6898 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6899 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6900 | refcount_add(nr, ¤t->usage); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6901 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6902 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6903 | link.head = NULL; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6904 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6905 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6906 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6907 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6908 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6909 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6910 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6911 | if (unlikely(!req)) { |
| 6912 | if (!submitted) |
| 6913 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6914 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6915 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6916 | sqe = io_get_sqe(ctx); |
| 6917 | if (unlikely(!sqe)) { |
| 6918 | kmem_cache_free(req_cachep, req); |
| 6919 | break; |
| 6920 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6921 | /* will complete beyond this point, count as submitted */ |
| 6922 | submitted++; |
| 6923 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6924 | err = io_init_req(ctx, req, sqe); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6925 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6926 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6927 | io_put_req(req); |
| 6928 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6929 | break; |
| 6930 | } |
| 6931 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6932 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 2d7e935 | 2021-01-19 13:32:37 +0000 | [diff] [blame] | 6933 | true, ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6934 | err = io_submit_sqe(req, sqe, &link); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6935 | if (err) |
| 6936 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6937 | } |
| 6938 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6939 | if (unlikely(submitted != nr)) { |
| 6940 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6941 | struct io_uring_task *tctx = current->io_uring; |
| 6942 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6943 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6944 | percpu_ref_put_many(&ctx->refs, unused); |
| 6945 | percpu_counter_sub(&tctx->inflight, unused); |
| 6946 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6947 | } |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6948 | if (link.head) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6949 | io_queue_link_head(link.head); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6950 | io_submit_state_end(&ctx->submit_state, ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6951 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6952 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6953 | io_commit_sqring(ctx); |
| 6954 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6955 | return submitted; |
| 6956 | } |
| 6957 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6958 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6959 | { |
| 6960 | /* Tell userspace we may need a wakeup call */ |
| 6961 | spin_lock_irq(&ctx->completion_lock); |
| 6962 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6963 | spin_unlock_irq(&ctx->completion_lock); |
| 6964 | } |
| 6965 | |
| 6966 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6967 | { |
| 6968 | spin_lock_irq(&ctx->completion_lock); |
| 6969 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6970 | spin_unlock_irq(&ctx->completion_lock); |
| 6971 | } |
| 6972 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6973 | 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] | 6974 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6975 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6976 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6977 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6978 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6979 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6980 | if (cap_entries && to_submit > 8) |
| 6981 | to_submit = 8; |
| 6982 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6983 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6984 | unsigned nr_events = 0; |
| 6985 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6986 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6987 | if (!list_empty(&ctx->iopoll_list)) |
| 6988 | io_do_iopoll(ctx, &nr_events, 0); |
| 6989 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6990 | if (to_submit && !ctx->sqo_dead && |
| 6991 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6992 | ret = io_submit_sqes(ctx, to_submit); |
| 6993 | mutex_unlock(&ctx->uring_lock); |
| 6994 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6995 | |
| 6996 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6997 | wake_up(&ctx->sqo_sq_wait); |
| 6998 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6999 | return ret; |
| 7000 | } |
| 7001 | |
| 7002 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7003 | { |
| 7004 | struct io_ring_ctx *ctx; |
| 7005 | unsigned sq_thread_idle = 0; |
| 7006 | |
| 7007 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7008 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 7009 | sq_thread_idle = ctx->sq_thread_idle; |
| 7010 | } |
| 7011 | |
| 7012 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7013 | } |
| 7014 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7015 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7016 | { |
| 7017 | struct io_ring_ctx *ctx; |
| 7018 | |
| 7019 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7020 | 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] | 7021 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7022 | complete(&ctx->sq_thread_comp); |
| 7023 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7024 | |
| 7025 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7026 | } |
| 7027 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7028 | static int io_sq_thread(void *data) |
| 7029 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7030 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7031 | struct files_struct *old_files = current->files; |
| 7032 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7033 | const struct cred *old_cred = NULL; |
| 7034 | struct io_sq_data *sqd = data; |
| 7035 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7036 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7037 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7038 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7039 | task_lock(current); |
| 7040 | current->files = NULL; |
| 7041 | current->nsproxy = NULL; |
| 7042 | task_unlock(current); |
| 7043 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7044 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7045 | int ret; |
| 7046 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7047 | |
| 7048 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7049 | * Any changes to the sqd lists are synchronized through the |
| 7050 | * kthread parking. This synchronizes the thread vs users, |
| 7051 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7052 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7053 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7054 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7055 | /* |
| 7056 | * When sq thread is unparked, in case the previous park operation |
| 7057 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7058 | * to be stopped, so here needs to have a check. |
| 7059 | */ |
| 7060 | if (kthread_should_stop()) |
| 7061 | break; |
| 7062 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7063 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7064 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7065 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7066 | timeout = jiffies + sqd->sq_thread_idle; |
| 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 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7070 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7071 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7072 | if (current->cred != ctx->creds) { |
| 7073 | if (old_cred) |
| 7074 | revert_creds(old_cred); |
| 7075 | old_cred = override_creds(ctx->creds); |
| 7076 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7077 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7078 | #ifdef CONFIG_AUDIT |
| 7079 | current->loginuid = ctx->loginuid; |
| 7080 | current->sessionid = ctx->sessionid; |
| 7081 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7082 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7083 | ret = __io_sq_thread(ctx, cap_entries); |
| 7084 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7085 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7086 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7087 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7088 | } |
| 7089 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7090 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7091 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7092 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7093 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7094 | if (sqt_spin) |
| 7095 | timeout = jiffies + sqd->sq_thread_idle; |
| 7096 | continue; |
| 7097 | } |
| 7098 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7099 | needs_sched = true; |
| 7100 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7101 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7102 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7103 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7104 | needs_sched = false; |
| 7105 | break; |
| 7106 | } |
| 7107 | if (io_sqring_entries(ctx)) { |
| 7108 | needs_sched = false; |
| 7109 | break; |
| 7110 | } |
| 7111 | } |
| 7112 | |
Hao Xu | 8b28fdf | 2021-01-31 22:39:04 +0800 | [diff] [blame] | 7113 | if (needs_sched && !kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7114 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7115 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7116 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7117 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7118 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7119 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7120 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7121 | |
| 7122 | finish_wait(&sqd->wait, &wait); |
| 7123 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7124 | } |
| 7125 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7126 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7127 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7128 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7129 | if (cur_css) |
| 7130 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7131 | if (old_cred) |
| 7132 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7133 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7134 | task_lock(current); |
| 7135 | current->files = old_files; |
| 7136 | current->nsproxy = old_nsproxy; |
| 7137 | task_unlock(current); |
| 7138 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7139 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7140 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7141 | return 0; |
| 7142 | } |
| 7143 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7144 | struct io_wait_queue { |
| 7145 | struct wait_queue_entry wq; |
| 7146 | struct io_ring_ctx *ctx; |
| 7147 | unsigned to_wait; |
| 7148 | unsigned nr_timeouts; |
| 7149 | }; |
| 7150 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7151 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7152 | { |
| 7153 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7154 | |
| 7155 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7156 | * 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] | 7157 | * started waiting. For timeouts, we always want to return to userspace, |
| 7158 | * regardless of event count. |
| 7159 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7160 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7161 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7162 | } |
| 7163 | |
| 7164 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7165 | int wake_flags, void *key) |
| 7166 | { |
| 7167 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7168 | wq); |
| 7169 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7170 | /* |
| 7171 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7172 | * the task, and the next invocation will do it. |
| 7173 | */ |
| 7174 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7175 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7176 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7177 | } |
| 7178 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7179 | static int io_run_task_work_sig(void) |
| 7180 | { |
| 7181 | if (io_run_task_work()) |
| 7182 | return 1; |
| 7183 | if (!signal_pending(current)) |
| 7184 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7185 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7186 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7187 | return -EINTR; |
| 7188 | } |
| 7189 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7190 | /* when returns >0, the caller should retry */ |
| 7191 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7192 | struct io_wait_queue *iowq, |
| 7193 | signed long *timeout) |
| 7194 | { |
| 7195 | int ret; |
| 7196 | |
| 7197 | /* make sure we run task_work before checking for signals */ |
| 7198 | ret = io_run_task_work_sig(); |
| 7199 | if (ret || io_should_wake(iowq)) |
| 7200 | return ret; |
| 7201 | /* let the caller flush overflows, retry */ |
| 7202 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7203 | return 1; |
| 7204 | |
| 7205 | *timeout = schedule_timeout(*timeout); |
| 7206 | return !*timeout ? -ETIME : 1; |
| 7207 | } |
| 7208 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7209 | /* |
| 7210 | * Wait until events become available, if we don't already have some. The |
| 7211 | * application must reap them itself, as they reside on the shared cq ring. |
| 7212 | */ |
| 7213 | 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] | 7214 | const sigset_t __user *sig, size_t sigsz, |
| 7215 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7216 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7217 | struct io_wait_queue iowq = { |
| 7218 | .wq = { |
| 7219 | .private = current, |
| 7220 | .func = io_wake_function, |
| 7221 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7222 | }, |
| 7223 | .ctx = ctx, |
| 7224 | .to_wait = min_events, |
| 7225 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7226 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7227 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7228 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7229 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7230 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7231 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7232 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7233 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7234 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7235 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7236 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7237 | |
| 7238 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7239 | #ifdef CONFIG_COMPAT |
| 7240 | if (in_compat_syscall()) |
| 7241 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7242 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7243 | else |
| 7244 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7245 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7246 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7247 | if (ret) |
| 7248 | return ret; |
| 7249 | } |
| 7250 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7251 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7252 | struct timespec64 ts; |
| 7253 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7254 | if (get_timespec64(&ts, uts)) |
| 7255 | return -EFAULT; |
| 7256 | timeout = timespec64_to_jiffies(&ts); |
| 7257 | } |
| 7258 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7259 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7260 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7261 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7262 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7263 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7264 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7265 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7266 | finish_wait(&ctx->wait, &iowq.wq); |
| 7267 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7268 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7269 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7270 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7271 | 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] | 7272 | } |
| 7273 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7274 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7275 | { |
| 7276 | #if defined(CONFIG_UNIX) |
| 7277 | if (ctx->ring_sock) { |
| 7278 | struct sock *sock = ctx->ring_sock->sk; |
| 7279 | struct sk_buff *skb; |
| 7280 | |
| 7281 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7282 | kfree_skb(skb); |
| 7283 | } |
| 7284 | #else |
| 7285 | int i; |
| 7286 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7287 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7288 | struct file *file; |
| 7289 | |
| 7290 | file = io_file_from_index(ctx, i); |
| 7291 | if (file) |
| 7292 | fput(file); |
| 7293 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7294 | #endif |
| 7295 | } |
| 7296 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7297 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7298 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7299 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7300 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7301 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7302 | complete(&data->done); |
| 7303 | } |
| 7304 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7305 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
| 7306 | { |
| 7307 | spin_lock_bh(&ctx->rsrc_ref_lock); |
| 7308 | } |
| 7309 | |
| 7310 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
| 7311 | { |
| 7312 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7313 | } |
| 7314 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7315 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 7316 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7317 | struct fixed_rsrc_ref_node *ref_node) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7318 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7319 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7320 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7321 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7322 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7323 | percpu_ref_get(&rsrc_data->refs); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7326 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 7327 | struct io_ring_ctx *ctx, |
| 7328 | struct fixed_rsrc_ref_node *backup_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7329 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7330 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7331 | int ret; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7332 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7333 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7334 | ref_node = data->node; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7335 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7336 | if (ref_node) |
| 7337 | percpu_ref_kill(&ref_node->refs); |
| 7338 | |
| 7339 | percpu_ref_kill(&data->refs); |
| 7340 | |
| 7341 | /* wait for all refs nodes to complete */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7342 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7343 | do { |
| 7344 | ret = wait_for_completion_interruptible(&data->done); |
| 7345 | if (!ret) |
| 7346 | break; |
| 7347 | ret = io_run_task_work_sig(); |
| 7348 | if (ret < 0) { |
| 7349 | percpu_ref_resurrect(&data->refs); |
| 7350 | reinit_completion(&data->done); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7351 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7352 | return ret; |
| 7353 | } |
| 7354 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7355 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7356 | destroy_fixed_rsrc_ref_node(backup_node); |
| 7357 | return 0; |
| 7358 | } |
| 7359 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7360 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7361 | { |
| 7362 | struct fixed_rsrc_data *data; |
| 7363 | |
| 7364 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7365 | if (!data) |
| 7366 | return NULL; |
| 7367 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7368 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7369 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7370 | kfree(data); |
| 7371 | return NULL; |
| 7372 | } |
| 7373 | data->ctx = ctx; |
| 7374 | init_completion(&data->done); |
| 7375 | return data; |
| 7376 | } |
| 7377 | |
| 7378 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7379 | { |
| 7380 | percpu_ref_exit(&data->refs); |
| 7381 | kfree(data->table); |
| 7382 | kfree(data); |
| 7383 | } |
| 7384 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7385 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7386 | { |
| 7387 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7388 | struct fixed_rsrc_ref_node *backup_node; |
| 7389 | unsigned nr_tables, i; |
| 7390 | int ret; |
| 7391 | |
| 7392 | if (!data) |
| 7393 | return -ENXIO; |
| 7394 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 7395 | if (!backup_node) |
| 7396 | return -ENOMEM; |
| 7397 | init_fixed_file_ref_node(ctx, backup_node); |
| 7398 | |
| 7399 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); |
| 7400 | if (ret) |
| 7401 | return ret; |
| 7402 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7403 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7404 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7405 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7406 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7407 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7408 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7409 | ctx->nr_user_files = 0; |
| 7410 | return 0; |
| 7411 | } |
| 7412 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7413 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7414 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7415 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7416 | /* |
| 7417 | * The park is a bit of a work-around, without it we get |
| 7418 | * warning spews on shutdown with SQPOLL set and affinity |
| 7419 | * set to a single CPU. |
| 7420 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7421 | if (sqd->thread) { |
| 7422 | kthread_park(sqd->thread); |
| 7423 | kthread_stop(sqd->thread); |
| 7424 | } |
| 7425 | |
| 7426 | kfree(sqd); |
| 7427 | } |
| 7428 | } |
| 7429 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7430 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7431 | { |
| 7432 | struct io_ring_ctx *ctx_attach; |
| 7433 | struct io_sq_data *sqd; |
| 7434 | struct fd f; |
| 7435 | |
| 7436 | f = fdget(p->wq_fd); |
| 7437 | if (!f.file) |
| 7438 | return ERR_PTR(-ENXIO); |
| 7439 | if (f.file->f_op != &io_uring_fops) { |
| 7440 | fdput(f); |
| 7441 | return ERR_PTR(-EINVAL); |
| 7442 | } |
| 7443 | |
| 7444 | ctx_attach = f.file->private_data; |
| 7445 | sqd = ctx_attach->sq_data; |
| 7446 | if (!sqd) { |
| 7447 | fdput(f); |
| 7448 | return ERR_PTR(-EINVAL); |
| 7449 | } |
| 7450 | |
| 7451 | refcount_inc(&sqd->refs); |
| 7452 | fdput(f); |
| 7453 | return sqd; |
| 7454 | } |
| 7455 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7456 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7457 | { |
| 7458 | struct io_sq_data *sqd; |
| 7459 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7460 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7461 | return io_attach_sq_data(p); |
| 7462 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7463 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7464 | if (!sqd) |
| 7465 | return ERR_PTR(-ENOMEM); |
| 7466 | |
| 7467 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7468 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7469 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7470 | mutex_init(&sqd->ctx_lock); |
| 7471 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7472 | init_waitqueue_head(&sqd->wait); |
| 7473 | return sqd; |
| 7474 | } |
| 7475 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7476 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7477 | __releases(&sqd->lock) |
| 7478 | { |
| 7479 | if (!sqd->thread) |
| 7480 | return; |
| 7481 | kthread_unpark(sqd->thread); |
| 7482 | mutex_unlock(&sqd->lock); |
| 7483 | } |
| 7484 | |
| 7485 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7486 | __acquires(&sqd->lock) |
| 7487 | { |
| 7488 | if (!sqd->thread) |
| 7489 | return; |
| 7490 | mutex_lock(&sqd->lock); |
| 7491 | kthread_park(sqd->thread); |
| 7492 | } |
| 7493 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7494 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7495 | { |
| 7496 | struct io_sq_data *sqd = ctx->sq_data; |
| 7497 | |
| 7498 | if (sqd) { |
| 7499 | if (sqd->thread) { |
| 7500 | /* |
| 7501 | * We may arrive here from the error branch in |
| 7502 | * io_sq_offload_create() where the kthread is created |
| 7503 | * without being waked up, thus wake it up now to make |
| 7504 | * sure the wait will complete. |
| 7505 | */ |
| 7506 | wake_up_process(sqd->thread); |
| 7507 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7508 | |
| 7509 | io_sq_thread_park(sqd); |
| 7510 | } |
| 7511 | |
| 7512 | mutex_lock(&sqd->ctx_lock); |
| 7513 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7514 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7515 | mutex_unlock(&sqd->ctx_lock); |
| 7516 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7517 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7518 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7519 | |
| 7520 | io_put_sq_data(sqd); |
| 7521 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7522 | } |
| 7523 | } |
| 7524 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7525 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7526 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7527 | io_sq_thread_stop(ctx); |
| 7528 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7529 | if (ctx->io_wq) { |
| 7530 | io_wq_destroy(ctx->io_wq); |
| 7531 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7532 | } |
| 7533 | } |
| 7534 | |
| 7535 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7536 | /* |
| 7537 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7538 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7539 | * loops in the file referencing. |
| 7540 | */ |
| 7541 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7542 | { |
| 7543 | struct sock *sk = ctx->ring_sock->sk; |
| 7544 | struct scm_fp_list *fpl; |
| 7545 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7546 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7547 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7548 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7549 | if (!fpl) |
| 7550 | return -ENOMEM; |
| 7551 | |
| 7552 | skb = alloc_skb(0, GFP_KERNEL); |
| 7553 | if (!skb) { |
| 7554 | kfree(fpl); |
| 7555 | return -ENOMEM; |
| 7556 | } |
| 7557 | |
| 7558 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7559 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7560 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7561 | fpl->user = get_uid(ctx->user); |
| 7562 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7563 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7564 | |
| 7565 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7566 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7567 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7568 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7569 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7570 | } |
| 7571 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7572 | if (nr_files) { |
| 7573 | fpl->max = SCM_MAX_FD; |
| 7574 | fpl->count = nr_files; |
| 7575 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7576 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7577 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7578 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7579 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7580 | for (i = 0; i < nr_files; i++) |
| 7581 | fput(fpl->fp[i]); |
| 7582 | } else { |
| 7583 | kfree_skb(skb); |
| 7584 | kfree(fpl); |
| 7585 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7586 | |
| 7587 | return 0; |
| 7588 | } |
| 7589 | |
| 7590 | /* |
| 7591 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7592 | * causes regular reference counting to break down. We rely on the UNIX |
| 7593 | * garbage collection to take care of this problem for us. |
| 7594 | */ |
| 7595 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7596 | { |
| 7597 | unsigned left, total; |
| 7598 | int ret = 0; |
| 7599 | |
| 7600 | total = 0; |
| 7601 | left = ctx->nr_user_files; |
| 7602 | while (left) { |
| 7603 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7604 | |
| 7605 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7606 | if (ret) |
| 7607 | break; |
| 7608 | left -= this_files; |
| 7609 | total += this_files; |
| 7610 | } |
| 7611 | |
| 7612 | if (!ret) |
| 7613 | return 0; |
| 7614 | |
| 7615 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7616 | struct file *file = io_file_from_index(ctx, total); |
| 7617 | |
| 7618 | if (file) |
| 7619 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7620 | total++; |
| 7621 | } |
| 7622 | |
| 7623 | return ret; |
| 7624 | } |
| 7625 | #else |
| 7626 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7627 | { |
| 7628 | return 0; |
| 7629 | } |
| 7630 | #endif |
| 7631 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7632 | 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] | 7633 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7634 | { |
| 7635 | int i; |
| 7636 | |
| 7637 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7638 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7639 | unsigned this_files; |
| 7640 | |
| 7641 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7642 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7643 | GFP_KERNEL); |
| 7644 | if (!table->files) |
| 7645 | break; |
| 7646 | nr_files -= this_files; |
| 7647 | } |
| 7648 | |
| 7649 | if (i == nr_tables) |
| 7650 | return 0; |
| 7651 | |
| 7652 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7653 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7654 | kfree(table->files); |
| 7655 | } |
| 7656 | return 1; |
| 7657 | } |
| 7658 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7659 | 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] | 7660 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7661 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7662 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7663 | struct sock *sock = ctx->ring_sock->sk; |
| 7664 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7665 | struct sk_buff *skb; |
| 7666 | int i; |
| 7667 | |
| 7668 | __skb_queue_head_init(&list); |
| 7669 | |
| 7670 | /* |
| 7671 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7672 | * remove this entry and rearrange the file array. |
| 7673 | */ |
| 7674 | skb = skb_dequeue(head); |
| 7675 | while (skb) { |
| 7676 | struct scm_fp_list *fp; |
| 7677 | |
| 7678 | fp = UNIXCB(skb).fp; |
| 7679 | for (i = 0; i < fp->count; i++) { |
| 7680 | int left; |
| 7681 | |
| 7682 | if (fp->fp[i] != file) |
| 7683 | continue; |
| 7684 | |
| 7685 | unix_notinflight(fp->user, fp->fp[i]); |
| 7686 | left = fp->count - 1 - i; |
| 7687 | if (left) { |
| 7688 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7689 | left * sizeof(struct file *)); |
| 7690 | } |
| 7691 | fp->count--; |
| 7692 | if (!fp->count) { |
| 7693 | kfree_skb(skb); |
| 7694 | skb = NULL; |
| 7695 | } else { |
| 7696 | __skb_queue_tail(&list, skb); |
| 7697 | } |
| 7698 | fput(file); |
| 7699 | file = NULL; |
| 7700 | break; |
| 7701 | } |
| 7702 | |
| 7703 | if (!file) |
| 7704 | break; |
| 7705 | |
| 7706 | __skb_queue_tail(&list, skb); |
| 7707 | |
| 7708 | skb = skb_dequeue(head); |
| 7709 | } |
| 7710 | |
| 7711 | if (skb_peek(&list)) { |
| 7712 | spin_lock_irq(&head->lock); |
| 7713 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7714 | __skb_queue_tail(head, skb); |
| 7715 | spin_unlock_irq(&head->lock); |
| 7716 | } |
| 7717 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7718 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7719 | #endif |
| 7720 | } |
| 7721 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7722 | 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] | 7723 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7724 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7725 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7726 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7727 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7728 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7729 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7730 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7731 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7732 | } |
| 7733 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7734 | percpu_ref_exit(&ref_node->refs); |
| 7735 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7736 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7737 | } |
| 7738 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7739 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7740 | { |
| 7741 | struct io_ring_ctx *ctx; |
| 7742 | struct llist_node *node; |
| 7743 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7744 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7745 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7746 | |
| 7747 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7748 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7749 | struct llist_node *next = node->next; |
| 7750 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7751 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7752 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7753 | node = next; |
| 7754 | } |
| 7755 | } |
| 7756 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7757 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7758 | unsigned i) |
| 7759 | { |
| 7760 | struct fixed_rsrc_table *table; |
| 7761 | |
| 7762 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7763 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7764 | } |
| 7765 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7766 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7767 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7768 | struct fixed_rsrc_ref_node *ref_node; |
| 7769 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7770 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7771 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7772 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7773 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7774 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7775 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7776 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7777 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7778 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7779 | ref_node->done = true; |
| 7780 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7781 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7782 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7783 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7784 | /* recycle ref nodes in order */ |
| 7785 | if (!ref_node->done) |
| 7786 | break; |
| 7787 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7788 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7789 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7790 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7791 | |
| 7792 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7793 | delay = 0; |
| 7794 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7795 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7796 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7797 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7798 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7799 | } |
| 7800 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7801 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7802 | struct io_ring_ctx *ctx) |
| 7803 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7804 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7805 | |
| 7806 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7807 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7808 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7809 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7810 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7811 | 0, GFP_KERNEL)) { |
| 7812 | kfree(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 | } |
| 7815 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7816 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7817 | ref_node->done = false; |
| 7818 | return ref_node; |
| 7819 | } |
| 7820 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7821 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7822 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7823 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7824 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7825 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7826 | } |
| 7827 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7828 | 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] | 7829 | { |
| 7830 | percpu_ref_exit(&ref_node->refs); |
| 7831 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7832 | } |
| 7833 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7834 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7835 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7836 | unsigned nr_args) |
| 7837 | { |
| 7838 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7839 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7840 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7841 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7842 | struct fixed_rsrc_ref_node *ref_node; |
| 7843 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7844 | |
| 7845 | if (ctx->file_data) |
| 7846 | return -EBUSY; |
| 7847 | if (!nr_args) |
| 7848 | return -EINVAL; |
| 7849 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7850 | return -EMFILE; |
| 7851 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7852 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7853 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7854 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7855 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7856 | |
| 7857 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7858 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7859 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7860 | if (!file_data->table) |
| 7861 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7862 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7863 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7864 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7865 | |
| 7866 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7867 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7868 | ret = -EFAULT; |
| 7869 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7870 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7871 | /* allow sparse sets */ |
| 7872 | if (fd == -1) |
| 7873 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7874 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7875 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7876 | ret = -EBADF; |
| 7877 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7878 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7879 | |
| 7880 | /* |
| 7881 | * Don't allow io_uring instances to be registered. If UNIX |
| 7882 | * isn't enabled, then this causes a reference cycle and this |
| 7883 | * instance can never get freed. If UNIX is enabled we'll |
| 7884 | * handle it just fine, but there's still no point in allowing |
| 7885 | * a ring fd as it doesn't support regular read/write anyway. |
| 7886 | */ |
| 7887 | if (file->f_op == &io_uring_fops) { |
| 7888 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7889 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7890 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7891 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7892 | } |
| 7893 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7894 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7895 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7896 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7897 | return ret; |
| 7898 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7899 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7900 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7901 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7902 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7903 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7904 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7905 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7906 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7907 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7908 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7909 | out_fput: |
| 7910 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7911 | file = io_file_from_index(ctx, i); |
| 7912 | if (file) |
| 7913 | fput(file); |
| 7914 | } |
| 7915 | for (i = 0; i < nr_tables; i++) |
| 7916 | kfree(file_data->table[i].files); |
| 7917 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7918 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7919 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7920 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7921 | return ret; |
| 7922 | } |
| 7923 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7924 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7925 | int index) |
| 7926 | { |
| 7927 | #if defined(CONFIG_UNIX) |
| 7928 | struct sock *sock = ctx->ring_sock->sk; |
| 7929 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7930 | struct sk_buff *skb; |
| 7931 | |
| 7932 | /* |
| 7933 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7934 | * file set. If there's no room, fall back to allocating a new skb |
| 7935 | * and filling it in. |
| 7936 | */ |
| 7937 | spin_lock_irq(&head->lock); |
| 7938 | skb = skb_peek(head); |
| 7939 | if (skb) { |
| 7940 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7941 | |
| 7942 | if (fpl->count < SCM_MAX_FD) { |
| 7943 | __skb_unlink(skb, head); |
| 7944 | spin_unlock_irq(&head->lock); |
| 7945 | fpl->fp[fpl->count] = get_file(file); |
| 7946 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7947 | fpl->count++; |
| 7948 | spin_lock_irq(&head->lock); |
| 7949 | __skb_queue_head(head, skb); |
| 7950 | } else { |
| 7951 | skb = NULL; |
| 7952 | } |
| 7953 | } |
| 7954 | spin_unlock_irq(&head->lock); |
| 7955 | |
| 7956 | if (skb) { |
| 7957 | fput(file); |
| 7958 | return 0; |
| 7959 | } |
| 7960 | |
| 7961 | return __io_sqe_files_scm(ctx, 1, index); |
| 7962 | #else |
| 7963 | return 0; |
| 7964 | #endif |
| 7965 | } |
| 7966 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7967 | 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] | 7968 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7969 | struct io_rsrc_put *prsrc; |
| 7970 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7971 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7972 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7973 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7974 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7975 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7976 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7977 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7978 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7979 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7980 | } |
| 7981 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7982 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7983 | struct file *file) |
| 7984 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7985 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7988 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7989 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7990 | unsigned nr_args) |
| 7991 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7992 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7993 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7994 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7995 | __s32 __user *fds; |
| 7996 | int fd, i, err; |
| 7997 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7998 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7999 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8000 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8001 | return -EOVERFLOW; |
| 8002 | if (done > ctx->nr_user_files) |
| 8003 | return -EINVAL; |
| 8004 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8005 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 8006 | if (!ref_node) |
| 8007 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8008 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8009 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8010 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8011 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8012 | err = 0; |
| 8013 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 8014 | err = -EFAULT; |
| 8015 | break; |
| 8016 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8017 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8018 | continue; |
| 8019 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8020 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8021 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 8022 | |
| 8023 | if (*file_slot) { |
| 8024 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8025 | if (err) |
| 8026 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8027 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8028 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8029 | } |
| 8030 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8031 | file = fget(fd); |
| 8032 | if (!file) { |
| 8033 | err = -EBADF; |
| 8034 | break; |
| 8035 | } |
| 8036 | /* |
| 8037 | * Don't allow io_uring instances to be registered. If |
| 8038 | * UNIX isn't enabled, then this causes a reference |
| 8039 | * cycle and this instance can never get freed. If UNIX |
| 8040 | * is enabled we'll handle it just fine, but there's |
| 8041 | * still no point in allowing a ring fd as it doesn't |
| 8042 | * support regular read/write anyway. |
| 8043 | */ |
| 8044 | if (file->f_op == &io_uring_fops) { |
| 8045 | fput(file); |
| 8046 | err = -EBADF; |
| 8047 | break; |
| 8048 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8049 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8050 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8051 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8052 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8053 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8054 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8055 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8056 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8057 | } |
| 8058 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8059 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8060 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 8061 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8062 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8063 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8064 | |
| 8065 | return done ? done : err; |
| 8066 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8067 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8068 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8069 | unsigned nr_args) |
| 8070 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8071 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8072 | |
| 8073 | if (!ctx->file_data) |
| 8074 | return -ENXIO; |
| 8075 | if (!nr_args) |
| 8076 | return -EINVAL; |
| 8077 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8078 | return -EFAULT; |
| 8079 | if (up.resv) |
| 8080 | return -EINVAL; |
| 8081 | |
| 8082 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8083 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8084 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8085 | 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] | 8086 | { |
| 8087 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8088 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8089 | req = io_put_req_find_next(req); |
| 8090 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8091 | } |
| 8092 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8093 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8094 | struct io_uring_params *p) |
| 8095 | { |
| 8096 | struct io_wq_data data; |
| 8097 | struct fd f; |
| 8098 | struct io_ring_ctx *ctx_attach; |
| 8099 | unsigned int concurrency; |
| 8100 | int ret = 0; |
| 8101 | |
| 8102 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8103 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8104 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8105 | |
| 8106 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8107 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8108 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8109 | |
| 8110 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8111 | if (IS_ERR(ctx->io_wq)) { |
| 8112 | ret = PTR_ERR(ctx->io_wq); |
| 8113 | ctx->io_wq = NULL; |
| 8114 | } |
| 8115 | return ret; |
| 8116 | } |
| 8117 | |
| 8118 | f = fdget(p->wq_fd); |
| 8119 | if (!f.file) |
| 8120 | return -EBADF; |
| 8121 | |
| 8122 | if (f.file->f_op != &io_uring_fops) { |
| 8123 | ret = -EINVAL; |
| 8124 | goto out_fput; |
| 8125 | } |
| 8126 | |
| 8127 | ctx_attach = f.file->private_data; |
| 8128 | /* @io_wq is protected by holding the fd */ |
| 8129 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8130 | ret = -EINVAL; |
| 8131 | goto out_fput; |
| 8132 | } |
| 8133 | |
| 8134 | ctx->io_wq = ctx_attach->io_wq; |
| 8135 | out_fput: |
| 8136 | fdput(f); |
| 8137 | return ret; |
| 8138 | } |
| 8139 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8140 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8141 | { |
| 8142 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8143 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8144 | |
| 8145 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8146 | if (unlikely(!tctx)) |
| 8147 | return -ENOMEM; |
| 8148 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8149 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8150 | if (unlikely(ret)) { |
| 8151 | kfree(tctx); |
| 8152 | return ret; |
| 8153 | } |
| 8154 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8155 | xa_init(&tctx->xa); |
| 8156 | init_waitqueue_head(&tctx->wait); |
| 8157 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8158 | atomic_set(&tctx->in_idle, 0); |
| 8159 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8160 | io_init_identity(&tctx->__identity); |
| 8161 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8162 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8163 | spin_lock_init(&tctx->task_lock); |
| 8164 | INIT_WQ_LIST(&tctx->task_list); |
| 8165 | tctx->task_state = 0; |
| 8166 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8167 | return 0; |
| 8168 | } |
| 8169 | |
| 8170 | void __io_uring_free(struct task_struct *tsk) |
| 8171 | { |
| 8172 | struct io_uring_task *tctx = tsk->io_uring; |
| 8173 | |
| 8174 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8175 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8176 | if (tctx->identity != &tctx->__identity) |
| 8177 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8178 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8179 | kfree(tctx); |
| 8180 | tsk->io_uring = NULL; |
| 8181 | } |
| 8182 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8183 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8184 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8185 | { |
| 8186 | int ret; |
| 8187 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8188 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8189 | struct io_sq_data *sqd; |
| 8190 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8191 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8192 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8193 | goto err; |
| 8194 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8195 | sqd = io_get_sq_data(p); |
| 8196 | if (IS_ERR(sqd)) { |
| 8197 | ret = PTR_ERR(sqd); |
| 8198 | goto err; |
| 8199 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8200 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8201 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8202 | io_sq_thread_park(sqd); |
| 8203 | mutex_lock(&sqd->ctx_lock); |
| 8204 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8205 | mutex_unlock(&sqd->ctx_lock); |
| 8206 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8207 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8208 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8209 | if (!ctx->sq_thread_idle) |
| 8210 | ctx->sq_thread_idle = HZ; |
| 8211 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8212 | if (sqd->thread) |
| 8213 | goto done; |
| 8214 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8215 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8216 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8217 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8218 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8219 | if (cpu >= nr_cpu_ids) |
| 8220 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8221 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8222 | goto err; |
| 8223 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8224 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8225 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8226 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8227 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8228 | "io_uring-sq"); |
| 8229 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8230 | if (IS_ERR(sqd->thread)) { |
| 8231 | ret = PTR_ERR(sqd->thread); |
| 8232 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8233 | goto err; |
| 8234 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8235 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8236 | if (ret) |
| 8237 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8238 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8239 | /* Can't have SQ_AFF without SQPOLL */ |
| 8240 | ret = -EINVAL; |
| 8241 | goto err; |
| 8242 | } |
| 8243 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8244 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8245 | ret = io_init_wq_offload(ctx, p); |
| 8246 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8247 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8248 | |
| 8249 | return 0; |
| 8250 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8251 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8252 | return ret; |
| 8253 | } |
| 8254 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8255 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8256 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8257 | struct io_sq_data *sqd = ctx->sq_data; |
| 8258 | |
| 8259 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8260 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8261 | } |
| 8262 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8263 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8264 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8265 | { |
| 8266 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8267 | } |
| 8268 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8269 | static inline int __io_account_mem(struct user_struct *user, |
| 8270 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8271 | { |
| 8272 | unsigned long page_limit, cur_pages, new_pages; |
| 8273 | |
| 8274 | /* Don't allow more pages than we can safely lock */ |
| 8275 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8276 | |
| 8277 | do { |
| 8278 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8279 | new_pages = cur_pages + nr_pages; |
| 8280 | if (new_pages > page_limit) |
| 8281 | return -ENOMEM; |
| 8282 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8283 | new_pages) != cur_pages); |
| 8284 | |
| 8285 | return 0; |
| 8286 | } |
| 8287 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8288 | 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] | 8289 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8290 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8291 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8292 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8293 | if (ctx->mm_account) |
| 8294 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8295 | } |
| 8296 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8297 | 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] | 8298 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8299 | int ret; |
| 8300 | |
| 8301 | if (ctx->limit_mem) { |
| 8302 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8303 | if (ret) |
| 8304 | return ret; |
| 8305 | } |
| 8306 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8307 | if (ctx->mm_account) |
| 8308 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8309 | |
| 8310 | return 0; |
| 8311 | } |
| 8312 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8313 | static void io_mem_free(void *ptr) |
| 8314 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8315 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8316 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8317 | if (!ptr) |
| 8318 | return; |
| 8319 | |
| 8320 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8321 | if (put_page_testzero(page)) |
| 8322 | free_compound_page(page); |
| 8323 | } |
| 8324 | |
| 8325 | static void *io_mem_alloc(size_t size) |
| 8326 | { |
| 8327 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8328 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8329 | |
| 8330 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8331 | } |
| 8332 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8333 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8334 | size_t *sq_offset) |
| 8335 | { |
| 8336 | struct io_rings *rings; |
| 8337 | size_t off, sq_array_size; |
| 8338 | |
| 8339 | off = struct_size(rings, cqes, cq_entries); |
| 8340 | if (off == SIZE_MAX) |
| 8341 | return SIZE_MAX; |
| 8342 | |
| 8343 | #ifdef CONFIG_SMP |
| 8344 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8345 | if (off == 0) |
| 8346 | return SIZE_MAX; |
| 8347 | #endif |
| 8348 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8349 | if (sq_offset) |
| 8350 | *sq_offset = off; |
| 8351 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8352 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8353 | if (sq_array_size == SIZE_MAX) |
| 8354 | return SIZE_MAX; |
| 8355 | |
| 8356 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8357 | return SIZE_MAX; |
| 8358 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8359 | return off; |
| 8360 | } |
| 8361 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8362 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8363 | { |
| 8364 | int i, j; |
| 8365 | |
| 8366 | if (!ctx->user_bufs) |
| 8367 | return -ENXIO; |
| 8368 | |
| 8369 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8370 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8371 | |
| 8372 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8373 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8374 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8375 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8376 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8377 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8378 | imu->nr_bvecs = 0; |
| 8379 | } |
| 8380 | |
| 8381 | kfree(ctx->user_bufs); |
| 8382 | ctx->user_bufs = NULL; |
| 8383 | ctx->nr_user_bufs = 0; |
| 8384 | return 0; |
| 8385 | } |
| 8386 | |
| 8387 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8388 | void __user *arg, unsigned index) |
| 8389 | { |
| 8390 | struct iovec __user *src; |
| 8391 | |
| 8392 | #ifdef CONFIG_COMPAT |
| 8393 | if (ctx->compat) { |
| 8394 | struct compat_iovec __user *ciovs; |
| 8395 | struct compat_iovec ciov; |
| 8396 | |
| 8397 | ciovs = (struct compat_iovec __user *) arg; |
| 8398 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8399 | return -EFAULT; |
| 8400 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8401 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8402 | dst->iov_len = ciov.iov_len; |
| 8403 | return 0; |
| 8404 | } |
| 8405 | #endif |
| 8406 | src = (struct iovec __user *) arg; |
| 8407 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8408 | return -EFAULT; |
| 8409 | return 0; |
| 8410 | } |
| 8411 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8412 | /* |
| 8413 | * Not super efficient, but this is just a registration time. And we do cache |
| 8414 | * the last compound head, so generally we'll only do a full search if we don't |
| 8415 | * match that one. |
| 8416 | * |
| 8417 | * We check if the given compound head page has already been accounted, to |
| 8418 | * avoid double accounting it. This allows us to account the full size of the |
| 8419 | * page, not just the constituent pages of a huge page. |
| 8420 | */ |
| 8421 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8422 | int nr_pages, struct page *hpage) |
| 8423 | { |
| 8424 | int i, j; |
| 8425 | |
| 8426 | /* check current page array */ |
| 8427 | for (i = 0; i < nr_pages; i++) { |
| 8428 | if (!PageCompound(pages[i])) |
| 8429 | continue; |
| 8430 | if (compound_head(pages[i]) == hpage) |
| 8431 | return true; |
| 8432 | } |
| 8433 | |
| 8434 | /* check previously registered pages */ |
| 8435 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8436 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8437 | |
| 8438 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8439 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8440 | continue; |
| 8441 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8442 | return true; |
| 8443 | } |
| 8444 | } |
| 8445 | |
| 8446 | return false; |
| 8447 | } |
| 8448 | |
| 8449 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8450 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8451 | struct page **last_hpage) |
| 8452 | { |
| 8453 | int i, ret; |
| 8454 | |
| 8455 | for (i = 0; i < nr_pages; i++) { |
| 8456 | if (!PageCompound(pages[i])) { |
| 8457 | imu->acct_pages++; |
| 8458 | } else { |
| 8459 | struct page *hpage; |
| 8460 | |
| 8461 | hpage = compound_head(pages[i]); |
| 8462 | if (hpage == *last_hpage) |
| 8463 | continue; |
| 8464 | *last_hpage = hpage; |
| 8465 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8466 | continue; |
| 8467 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8468 | } |
| 8469 | } |
| 8470 | |
| 8471 | if (!imu->acct_pages) |
| 8472 | return 0; |
| 8473 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8474 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8475 | if (ret) |
| 8476 | imu->acct_pages = 0; |
| 8477 | return ret; |
| 8478 | } |
| 8479 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8480 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8481 | struct io_mapped_ubuf *imu, |
| 8482 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8483 | { |
| 8484 | struct vm_area_struct **vmas = NULL; |
| 8485 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8486 | unsigned long off, start, end, ubuf; |
| 8487 | size_t size; |
| 8488 | int ret, pret, nr_pages, i; |
| 8489 | |
| 8490 | ubuf = (unsigned long) iov->iov_base; |
| 8491 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8492 | start = ubuf >> PAGE_SHIFT; |
| 8493 | nr_pages = end - start; |
| 8494 | |
| 8495 | ret = -ENOMEM; |
| 8496 | |
| 8497 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8498 | if (!pages) |
| 8499 | goto done; |
| 8500 | |
| 8501 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8502 | GFP_KERNEL); |
| 8503 | if (!vmas) |
| 8504 | goto done; |
| 8505 | |
| 8506 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8507 | GFP_KERNEL); |
| 8508 | if (!imu->bvec) |
| 8509 | goto done; |
| 8510 | |
| 8511 | ret = 0; |
| 8512 | mmap_read_lock(current->mm); |
| 8513 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8514 | pages, vmas); |
| 8515 | if (pret == nr_pages) { |
| 8516 | /* don't support file backed memory */ |
| 8517 | for (i = 0; i < nr_pages; i++) { |
| 8518 | struct vm_area_struct *vma = vmas[i]; |
| 8519 | |
| 8520 | if (vma->vm_file && |
| 8521 | !is_file_hugepages(vma->vm_file)) { |
| 8522 | ret = -EOPNOTSUPP; |
| 8523 | break; |
| 8524 | } |
| 8525 | } |
| 8526 | } else { |
| 8527 | ret = pret < 0 ? pret : -EFAULT; |
| 8528 | } |
| 8529 | mmap_read_unlock(current->mm); |
| 8530 | if (ret) { |
| 8531 | /* |
| 8532 | * if we did partial map, or found file backed vmas, |
| 8533 | * release any pages we did get |
| 8534 | */ |
| 8535 | if (pret > 0) |
| 8536 | unpin_user_pages(pages, pret); |
| 8537 | kvfree(imu->bvec); |
| 8538 | goto done; |
| 8539 | } |
| 8540 | |
| 8541 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8542 | if (ret) { |
| 8543 | unpin_user_pages(pages, pret); |
| 8544 | kvfree(imu->bvec); |
| 8545 | goto done; |
| 8546 | } |
| 8547 | |
| 8548 | off = ubuf & ~PAGE_MASK; |
| 8549 | size = iov->iov_len; |
| 8550 | for (i = 0; i < nr_pages; i++) { |
| 8551 | size_t vec_len; |
| 8552 | |
| 8553 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8554 | imu->bvec[i].bv_page = pages[i]; |
| 8555 | imu->bvec[i].bv_len = vec_len; |
| 8556 | imu->bvec[i].bv_offset = off; |
| 8557 | off = 0; |
| 8558 | size -= vec_len; |
| 8559 | } |
| 8560 | /* store original address for later verification */ |
| 8561 | imu->ubuf = ubuf; |
| 8562 | imu->len = iov->iov_len; |
| 8563 | imu->nr_bvecs = nr_pages; |
| 8564 | ret = 0; |
| 8565 | done: |
| 8566 | kvfree(pages); |
| 8567 | kvfree(vmas); |
| 8568 | return ret; |
| 8569 | } |
| 8570 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8571 | 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] | 8572 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8573 | if (ctx->user_bufs) |
| 8574 | return -EBUSY; |
| 8575 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8576 | return -EINVAL; |
| 8577 | |
| 8578 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8579 | GFP_KERNEL); |
| 8580 | if (!ctx->user_bufs) |
| 8581 | return -ENOMEM; |
| 8582 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8583 | return 0; |
| 8584 | } |
| 8585 | |
| 8586 | static int io_buffer_validate(struct iovec *iov) |
| 8587 | { |
| 8588 | /* |
| 8589 | * Don't impose further limits on the size and buffer |
| 8590 | * constraints here, we'll -EINVAL later when IO is |
| 8591 | * submitted if they are wrong. |
| 8592 | */ |
| 8593 | if (!iov->iov_base || !iov->iov_len) |
| 8594 | return -EFAULT; |
| 8595 | |
| 8596 | /* arbitrary limit, but we need something */ |
| 8597 | if (iov->iov_len > SZ_1G) |
| 8598 | return -EFAULT; |
| 8599 | |
| 8600 | return 0; |
| 8601 | } |
| 8602 | |
| 8603 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8604 | unsigned int nr_args) |
| 8605 | { |
| 8606 | int i, ret; |
| 8607 | struct iovec iov; |
| 8608 | struct page *last_hpage = NULL; |
| 8609 | |
| 8610 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8611 | if (ret) |
| 8612 | return ret; |
| 8613 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8614 | for (i = 0; i < nr_args; i++) { |
| 8615 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8616 | |
| 8617 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8618 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8619 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8620 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8621 | ret = io_buffer_validate(&iov); |
| 8622 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8623 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8624 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8625 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8626 | if (ret) |
| 8627 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8628 | |
| 8629 | ctx->nr_user_bufs++; |
| 8630 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8631 | |
| 8632 | if (ret) |
| 8633 | io_sqe_buffers_unregister(ctx); |
| 8634 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8635 | return ret; |
| 8636 | } |
| 8637 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8638 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8639 | { |
| 8640 | __s32 __user *fds = arg; |
| 8641 | int fd; |
| 8642 | |
| 8643 | if (ctx->cq_ev_fd) |
| 8644 | return -EBUSY; |
| 8645 | |
| 8646 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8647 | return -EFAULT; |
| 8648 | |
| 8649 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8650 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8651 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8652 | ctx->cq_ev_fd = NULL; |
| 8653 | return ret; |
| 8654 | } |
| 8655 | |
| 8656 | return 0; |
| 8657 | } |
| 8658 | |
| 8659 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8660 | { |
| 8661 | if (ctx->cq_ev_fd) { |
| 8662 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8663 | ctx->cq_ev_fd = NULL; |
| 8664 | return 0; |
| 8665 | } |
| 8666 | |
| 8667 | return -ENXIO; |
| 8668 | } |
| 8669 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8670 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8671 | { |
| 8672 | struct io_ring_ctx *ctx = data; |
| 8673 | struct io_buffer *buf = p; |
| 8674 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8675 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8676 | return 0; |
| 8677 | } |
| 8678 | |
| 8679 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8680 | { |
| 8681 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8682 | idr_destroy(&ctx->io_buffer_idr); |
| 8683 | } |
| 8684 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8685 | 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] | 8686 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8687 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8688 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8689 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8690 | if (tsk && req->task != tsk) |
| 8691 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8692 | list_del(&req->compl.list); |
| 8693 | kmem_cache_free(req_cachep, req); |
| 8694 | } |
| 8695 | } |
| 8696 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8697 | 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] | 8698 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8699 | struct io_submit_state *submit_state = &ctx->submit_state; |
| 8700 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8701 | mutex_lock(&ctx->uring_lock); |
| 8702 | |
| 8703 | if (submit_state->free_reqs) |
| 8704 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8705 | submit_state->reqs); |
| 8706 | |
| 8707 | io_req_cache_free(&submit_state->comp.free_list, NULL); |
| 8708 | |
| 8709 | spin_lock_irq(&ctx->completion_lock); |
| 8710 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); |
| 8711 | spin_unlock_irq(&ctx->completion_lock); |
| 8712 | |
| 8713 | mutex_unlock(&ctx->uring_lock); |
| 8714 | } |
| 8715 | |
| 8716 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8717 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8718 | /* |
| 8719 | * Some may use context even when all refs and requests have been put, |
| 8720 | * and they are free to do so while still holding uring_lock, see |
| 8721 | * __io_req_task_submit(). Wait for them to finish. |
| 8722 | */ |
| 8723 | mutex_lock(&ctx->uring_lock); |
| 8724 | mutex_unlock(&ctx->uring_lock); |
| 8725 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8726 | io_finish_async(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8727 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8728 | |
| 8729 | if (ctx->sqo_task) { |
| 8730 | put_task_struct(ctx->sqo_task); |
| 8731 | ctx->sqo_task = NULL; |
| 8732 | mmdrop(ctx->mm_account); |
| 8733 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8734 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8735 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8736 | #ifdef CONFIG_BLK_CGROUP |
| 8737 | if (ctx->sqo_blkcg_css) |
| 8738 | css_put(ctx->sqo_blkcg_css); |
| 8739 | #endif |
| 8740 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8741 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8742 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8743 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8744 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8745 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8746 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8747 | if (ctx->ring_sock) { |
| 8748 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8749 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8750 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8751 | #endif |
| 8752 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8753 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8754 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8755 | |
| 8756 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8757 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8758 | put_cred(ctx->creds); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8759 | io_req_caches_free(ctx, NULL); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8760 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8761 | kfree(ctx); |
| 8762 | } |
| 8763 | |
| 8764 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8765 | { |
| 8766 | struct io_ring_ctx *ctx = file->private_data; |
| 8767 | __poll_t mask = 0; |
| 8768 | |
| 8769 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8770 | /* |
| 8771 | * synchronizes with barrier from wq_has_sleeper call in |
| 8772 | * io_commit_cqring |
| 8773 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8774 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8775 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8776 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8777 | |
| 8778 | /* |
| 8779 | * Don't flush cqring overflow list here, just do a simple check. |
| 8780 | * Otherwise there could possible be ABBA deadlock: |
| 8781 | * CPU0 CPU1 |
| 8782 | * ---- ---- |
| 8783 | * lock(&ctx->uring_lock); |
| 8784 | * lock(&ep->mtx); |
| 8785 | * lock(&ctx->uring_lock); |
| 8786 | * lock(&ep->mtx); |
| 8787 | * |
| 8788 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8789 | * pushs them to do the flush. |
| 8790 | */ |
| 8791 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8792 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8793 | |
| 8794 | return mask; |
| 8795 | } |
| 8796 | |
| 8797 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8798 | { |
| 8799 | struct io_ring_ctx *ctx = file->private_data; |
| 8800 | |
| 8801 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8802 | } |
| 8803 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8804 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8805 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8806 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8807 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8808 | iod = idr_remove(&ctx->personality_idr, id); |
| 8809 | if (iod) { |
| 8810 | put_cred(iod->creds); |
| 8811 | if (refcount_dec_and_test(&iod->count)) |
| 8812 | kfree(iod); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8813 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8814 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8815 | |
| 8816 | return -EINVAL; |
| 8817 | } |
| 8818 | |
| 8819 | static int io_remove_personalities(int id, void *p, void *data) |
| 8820 | { |
| 8821 | struct io_ring_ctx *ctx = data; |
| 8822 | |
| 8823 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8824 | return 0; |
| 8825 | } |
| 8826 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8827 | static void io_ring_exit_work(struct work_struct *work) |
| 8828 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8829 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8830 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8831 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8832 | /* |
| 8833 | * If we're doing polled IO and end up having requests being |
| 8834 | * submitted async (out-of-line), then completions can come in while |
| 8835 | * we're waiting for refs to drop. We need to reap these manually, |
| 8836 | * as nobody else will be looking for them. |
| 8837 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8838 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8839 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8840 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8841 | io_ring_ctx_free(ctx); |
| 8842 | } |
| 8843 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8844 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8845 | { |
| 8846 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8847 | |
| 8848 | return req->ctx == data; |
| 8849 | } |
| 8850 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8851 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8852 | { |
| 8853 | mutex_lock(&ctx->uring_lock); |
| 8854 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8855 | |
| 8856 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8857 | ctx->sqo_dead = 1; |
| 8858 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8859 | /* if force is set, the ring is going away. always drop after that */ |
| 8860 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8861 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8862 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8863 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8864 | mutex_unlock(&ctx->uring_lock); |
| 8865 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8866 | io_kill_timeouts(ctx, NULL, NULL); |
| 8867 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8868 | |
| 8869 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8870 | 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] | 8871 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8872 | /* 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] | 8873 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8874 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8875 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8876 | /* |
| 8877 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8878 | * if we're exiting a ton of rings at the same time. It just adds |
| 8879 | * noise and overhead, there's no discernable change in runtime |
| 8880 | * over using system_wq. |
| 8881 | */ |
| 8882 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8883 | } |
| 8884 | |
| 8885 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8886 | { |
| 8887 | struct io_ring_ctx *ctx = file->private_data; |
| 8888 | |
| 8889 | file->private_data = NULL; |
| 8890 | io_ring_ctx_wait_and_kill(ctx); |
| 8891 | return 0; |
| 8892 | } |
| 8893 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8894 | struct io_task_cancel { |
| 8895 | struct task_struct *task; |
| 8896 | struct files_struct *files; |
| 8897 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8898 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8899 | 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] | 8900 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8901 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8902 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8903 | bool ret; |
| 8904 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8905 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8906 | unsigned long flags; |
| 8907 | struct io_ring_ctx *ctx = req->ctx; |
| 8908 | |
| 8909 | /* protect against races with linked timeouts */ |
| 8910 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8911 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8912 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8913 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8914 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8915 | } |
| 8916 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8917 | } |
| 8918 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8919 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8920 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8921 | struct files_struct *files) |
| 8922 | { |
| 8923 | struct io_defer_entry *de = NULL; |
| 8924 | LIST_HEAD(list); |
| 8925 | |
| 8926 | spin_lock_irq(&ctx->completion_lock); |
| 8927 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8928 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8929 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8930 | break; |
| 8931 | } |
| 8932 | } |
| 8933 | spin_unlock_irq(&ctx->completion_lock); |
| 8934 | |
| 8935 | while (!list_empty(&list)) { |
| 8936 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8937 | list_del_init(&de->list); |
| 8938 | req_set_fail_links(de->req); |
| 8939 | io_put_req(de->req); |
| 8940 | io_req_complete(de->req, -ECANCELED); |
| 8941 | kfree(de); |
| 8942 | } |
| 8943 | } |
| 8944 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8945 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8946 | struct task_struct *task, |
| 8947 | struct files_struct *files) |
| 8948 | { |
| 8949 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
| 8950 | |
| 8951 | while (1) { |
| 8952 | enum io_wq_cancel cret; |
| 8953 | bool ret = false; |
| 8954 | |
| 8955 | if (ctx->io_wq) { |
| 8956 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8957 | &cancel, true); |
| 8958 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8959 | } |
| 8960 | |
| 8961 | /* SQPOLL thread does its own polling */ |
| 8962 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8963 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8964 | io_iopoll_try_reap_events(ctx); |
| 8965 | ret = true; |
| 8966 | } |
| 8967 | } |
| 8968 | |
| 8969 | ret |= io_poll_remove_all(ctx, task, files); |
| 8970 | ret |= io_kill_timeouts(ctx, task, files); |
| 8971 | ret |= io_run_task_work(); |
| 8972 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8973 | if (!ret) |
| 8974 | break; |
| 8975 | cond_resched(); |
| 8976 | } |
| 8977 | } |
| 8978 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8979 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8980 | struct task_struct *task, |
| 8981 | struct files_struct *files) |
| 8982 | { |
| 8983 | struct io_kiocb *req; |
| 8984 | int cnt = 0; |
| 8985 | |
| 8986 | spin_lock_irq(&ctx->inflight_lock); |
| 8987 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8988 | cnt += io_match_task(req, task, files); |
| 8989 | spin_unlock_irq(&ctx->inflight_lock); |
| 8990 | return cnt; |
| 8991 | } |
| 8992 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8993 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8994 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8995 | struct files_struct *files) |
| 8996 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8997 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8998 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8999 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9000 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9001 | inflight = io_uring_count_inflight(ctx, task, files); |
| 9002 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9003 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9004 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9005 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9006 | |
| 9007 | if (ctx->sq_data) |
| 9008 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9009 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 9010 | TASK_UNINTERRUPTIBLE); |
| 9011 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 9012 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 9013 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9014 | if (ctx->sq_data) |
| 9015 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9016 | } |
| 9017 | } |
| 9018 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9019 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 9020 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9021 | mutex_lock(&ctx->uring_lock); |
| 9022 | ctx->sqo_dead = 1; |
| 9023 | mutex_unlock(&ctx->uring_lock); |
| 9024 | |
| 9025 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 9026 | if (ctx->rings) |
| 9027 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9028 | } |
| 9029 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9030 | /* |
| 9031 | * We need to iteratively cancel requests, in case a request has dependent |
| 9032 | * hard links. These persist even for failure of cancelations, hence keep |
| 9033 | * looping until none are found. |
| 9034 | */ |
| 9035 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 9036 | struct files_struct *files) |
| 9037 | { |
| 9038 | struct task_struct *task = current; |
| 9039 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9040 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9041 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9042 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9043 | atomic_inc(&task->io_uring->in_idle); |
| 9044 | io_sq_thread_park(ctx->sq_data); |
| 9045 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9046 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 9047 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9048 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 9049 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 9050 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9051 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9052 | |
| 9053 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 9054 | atomic_dec(&task->io_uring->in_idle); |
| 9055 | /* |
| 9056 | * If the files that are going away are the ones in the thread |
| 9057 | * identity, clear them out. |
| 9058 | */ |
| 9059 | if (task->io_uring->identity->files == files) |
| 9060 | task->io_uring->identity->files = NULL; |
| 9061 | io_sq_thread_unpark(ctx->sq_data); |
| 9062 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9063 | } |
| 9064 | |
| 9065 | /* |
| 9066 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9067 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9068 | 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] | 9069 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9070 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9071 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9072 | |
| 9073 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9074 | ret = io_uring_alloc_task_context(current); |
| 9075 | if (unlikely(ret)) |
| 9076 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9077 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9078 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9079 | if (tctx->last != file) { |
| 9080 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9081 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9082 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9083 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9084 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 9085 | file, GFP_KERNEL)); |
| 9086 | if (ret) { |
| 9087 | fput(file); |
| 9088 | return ret; |
| 9089 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 9090 | |
| 9091 | /* one and only SQPOLL file note, held by sqo_task */ |
| 9092 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 9093 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9094 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9095 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9096 | } |
| 9097 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9098 | /* |
| 9099 | * This is race safe in that the task itself is doing this, hence it |
| 9100 | * cannot be going through the exit/cancel paths at the same time. |
| 9101 | * This cannot be modified while exit/cancel is running. |
| 9102 | */ |
| 9103 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9104 | tctx->sqpoll = true; |
| 9105 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9106 | return 0; |
| 9107 | } |
| 9108 | |
| 9109 | /* |
| 9110 | * Remove this io_uring_file -> task mapping. |
| 9111 | */ |
| 9112 | static void io_uring_del_task_file(struct file *file) |
| 9113 | { |
| 9114 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9115 | |
| 9116 | if (tctx->last == file) |
| 9117 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9118 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9119 | if (file) |
| 9120 | fput(file); |
| 9121 | } |
| 9122 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9123 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9124 | { |
| 9125 | struct file *file; |
| 9126 | unsigned long index; |
| 9127 | |
| 9128 | xa_for_each(&tctx->xa, index, file) |
| 9129 | io_uring_del_task_file(file); |
| 9130 | } |
| 9131 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9132 | void __io_uring_files_cancel(struct files_struct *files) |
| 9133 | { |
| 9134 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9135 | struct file *file; |
| 9136 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9137 | |
| 9138 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9139 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9140 | xa_for_each(&tctx->xa, index, file) |
| 9141 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9142 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9143 | |
| 9144 | if (files) |
| 9145 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9146 | } |
| 9147 | |
| 9148 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9149 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9150 | return percpu_counter_sum(&tctx->inflight); |
| 9151 | } |
| 9152 | |
| 9153 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 9154 | { |
| 9155 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9156 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9157 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9158 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9159 | if (!ctx->sq_data) |
| 9160 | return; |
| 9161 | tctx = ctx->sq_data->thread->io_uring; |
| 9162 | io_disable_sqo_submit(ctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9163 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9164 | atomic_inc(&tctx->in_idle); |
| 9165 | do { |
| 9166 | /* read completions before cancelations */ |
| 9167 | inflight = tctx_inflight(tctx); |
| 9168 | if (!inflight) |
| 9169 | break; |
| 9170 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9171 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9172 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9173 | /* |
| 9174 | * If we've seen completions, retry without waiting. This |
| 9175 | * avoids a race where a completion comes in before we did |
| 9176 | * prepare_to_wait(). |
| 9177 | */ |
| 9178 | if (inflight == tctx_inflight(tctx)) |
| 9179 | schedule(); |
| 9180 | finish_wait(&tctx->wait, &wait); |
| 9181 | } while (1); |
| 9182 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9183 | } |
| 9184 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9185 | /* |
| 9186 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9187 | * requests. |
| 9188 | */ |
| 9189 | void __io_uring_task_cancel(void) |
| 9190 | { |
| 9191 | struct io_uring_task *tctx = current->io_uring; |
| 9192 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9193 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9194 | |
| 9195 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9196 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9197 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9198 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9199 | if (tctx->sqpoll) { |
| 9200 | struct file *file; |
| 9201 | unsigned long index; |
| 9202 | |
| 9203 | xa_for_each(&tctx->xa, index, file) |
| 9204 | io_uring_cancel_sqpoll(file->private_data); |
| 9205 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9206 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9207 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9208 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9209 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9210 | if (!inflight) |
| 9211 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9212 | __io_uring_files_cancel(NULL); |
| 9213 | |
| 9214 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9215 | |
| 9216 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9217 | * If we've seen completions, retry without waiting. This |
| 9218 | * avoids a race where a completion comes in before we did |
| 9219 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9220 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9221 | if (inflight == tctx_inflight(tctx)) |
| 9222 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9223 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9224 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9225 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9226 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9227 | |
| 9228 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9229 | } |
| 9230 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9231 | static int io_uring_flush(struct file *file, void *data) |
| 9232 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9233 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9234 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9235 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9236 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9237 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9238 | io_req_caches_free(ctx, current); |
| 9239 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9240 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9241 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9242 | return 0; |
| 9243 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9244 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9245 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9246 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9247 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9248 | /* |
| 9249 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9250 | * task file note. If the task is exiting, drop regardless of count. |
| 9251 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9252 | if (atomic_long_read(&file->f_count) != 2) |
| 9253 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9254 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9255 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9256 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 9257 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9258 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9259 | /* sqo_dead check is for when this happens after cancellation */ |
| 9260 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9261 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9262 | |
| 9263 | io_disable_sqo_submit(ctx); |
| 9264 | } |
| 9265 | |
| 9266 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9267 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9268 | return 0; |
| 9269 | } |
| 9270 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9271 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9272 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9273 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9274 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9275 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9276 | struct page *page; |
| 9277 | void *ptr; |
| 9278 | |
| 9279 | switch (offset) { |
| 9280 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9281 | case IORING_OFF_CQ_RING: |
| 9282 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9283 | break; |
| 9284 | case IORING_OFF_SQES: |
| 9285 | ptr = ctx->sq_sqes; |
| 9286 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9287 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9288 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9289 | } |
| 9290 | |
| 9291 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9292 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9293 | return ERR_PTR(-EINVAL); |
| 9294 | |
| 9295 | return ptr; |
| 9296 | } |
| 9297 | |
| 9298 | #ifdef CONFIG_MMU |
| 9299 | |
| 9300 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9301 | { |
| 9302 | size_t sz = vma->vm_end - vma->vm_start; |
| 9303 | unsigned long pfn; |
| 9304 | void *ptr; |
| 9305 | |
| 9306 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9307 | if (IS_ERR(ptr)) |
| 9308 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9309 | |
| 9310 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9311 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9312 | } |
| 9313 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9314 | #else /* !CONFIG_MMU */ |
| 9315 | |
| 9316 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9317 | { |
| 9318 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9319 | } |
| 9320 | |
| 9321 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9322 | { |
| 9323 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9324 | } |
| 9325 | |
| 9326 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9327 | unsigned long addr, unsigned long len, |
| 9328 | unsigned long pgoff, unsigned long flags) |
| 9329 | { |
| 9330 | void *ptr; |
| 9331 | |
| 9332 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9333 | if (IS_ERR(ptr)) |
| 9334 | return PTR_ERR(ptr); |
| 9335 | |
| 9336 | return (unsigned long) ptr; |
| 9337 | } |
| 9338 | |
| 9339 | #endif /* !CONFIG_MMU */ |
| 9340 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9341 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9342 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9343 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9344 | DEFINE_WAIT(wait); |
| 9345 | |
| 9346 | do { |
| 9347 | if (!io_sqring_full(ctx)) |
| 9348 | break; |
| 9349 | |
| 9350 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9351 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9352 | if (unlikely(ctx->sqo_dead)) { |
| 9353 | ret = -EOWNERDEAD; |
| 9354 | goto out; |
| 9355 | } |
| 9356 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9357 | if (!io_sqring_full(ctx)) |
| 9358 | break; |
| 9359 | |
| 9360 | schedule(); |
| 9361 | } while (!signal_pending(current)); |
| 9362 | |
| 9363 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9364 | out: |
| 9365 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9366 | } |
| 9367 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9368 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9369 | struct __kernel_timespec __user **ts, |
| 9370 | const sigset_t __user **sig) |
| 9371 | { |
| 9372 | struct io_uring_getevents_arg arg; |
| 9373 | |
| 9374 | /* |
| 9375 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9376 | * is just a pointer to the sigset_t. |
| 9377 | */ |
| 9378 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9379 | *sig = (const sigset_t __user *) argp; |
| 9380 | *ts = NULL; |
| 9381 | return 0; |
| 9382 | } |
| 9383 | |
| 9384 | /* |
| 9385 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9386 | * timespec and sigset_t pointers if good. |
| 9387 | */ |
| 9388 | if (*argsz != sizeof(arg)) |
| 9389 | return -EINVAL; |
| 9390 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9391 | return -EFAULT; |
| 9392 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9393 | *argsz = arg.sigmask_sz; |
| 9394 | *ts = u64_to_user_ptr(arg.ts); |
| 9395 | return 0; |
| 9396 | } |
| 9397 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9398 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9399 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9400 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9401 | { |
| 9402 | struct io_ring_ctx *ctx; |
| 9403 | long ret = -EBADF; |
| 9404 | int submitted = 0; |
| 9405 | struct fd f; |
| 9406 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9407 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9408 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9409 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9410 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9411 | return -EINVAL; |
| 9412 | |
| 9413 | f = fdget(fd); |
| 9414 | if (!f.file) |
| 9415 | return -EBADF; |
| 9416 | |
| 9417 | ret = -EOPNOTSUPP; |
| 9418 | if (f.file->f_op != &io_uring_fops) |
| 9419 | goto out_fput; |
| 9420 | |
| 9421 | ret = -ENXIO; |
| 9422 | ctx = f.file->private_data; |
| 9423 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9424 | goto out_fput; |
| 9425 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9426 | ret = -EBADFD; |
| 9427 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9428 | goto out; |
| 9429 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9430 | /* |
| 9431 | * For SQ polling, the thread will do all submissions and completions. |
| 9432 | * Just return the requested submit count, and wake the thread if |
| 9433 | * we were asked to. |
| 9434 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9435 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9436 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9437 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9438 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9439 | ret = -EOWNERDEAD; |
| 9440 | if (unlikely(ctx->sqo_dead)) |
| 9441 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9442 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9443 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9444 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9445 | ret = io_sqpoll_wait_sq(ctx); |
| 9446 | if (ret) |
| 9447 | goto out; |
| 9448 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9449 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9450 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9451 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9452 | if (unlikely(ret)) |
| 9453 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9454 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9455 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9456 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9457 | |
| 9458 | if (submitted != to_submit) |
| 9459 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9460 | } |
| 9461 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9462 | const sigset_t __user *sig; |
| 9463 | struct __kernel_timespec __user *ts; |
| 9464 | |
| 9465 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9466 | if (unlikely(ret)) |
| 9467 | goto out; |
| 9468 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9469 | min_complete = min(min_complete, ctx->cq_entries); |
| 9470 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9471 | /* |
| 9472 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9473 | * space applications don't need to do io completion events |
| 9474 | * polling again, they can rely on io_sq_thread to do polling |
| 9475 | * work, which can reduce cpu usage and uring_lock contention. |
| 9476 | */ |
| 9477 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9478 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9479 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9480 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9481 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9482 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9483 | } |
| 9484 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9485 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9486 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9487 | out_fput: |
| 9488 | fdput(f); |
| 9489 | return submitted ? submitted : ret; |
| 9490 | } |
| 9491 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9492 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9493 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9494 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9495 | struct io_identity *iod = p; |
| 9496 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9497 | struct seq_file *m = data; |
| 9498 | struct user_namespace *uns = seq_user_ns(m); |
| 9499 | struct group_info *gi; |
| 9500 | kernel_cap_t cap; |
| 9501 | unsigned __capi; |
| 9502 | int g; |
| 9503 | |
| 9504 | seq_printf(m, "%5d\n", id); |
| 9505 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9506 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9507 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9508 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9509 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9510 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9511 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9512 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9513 | seq_puts(m, "\n\tGroups:\t"); |
| 9514 | gi = cred->group_info; |
| 9515 | for (g = 0; g < gi->ngroups; g++) { |
| 9516 | seq_put_decimal_ull(m, g ? " " : "", |
| 9517 | from_kgid_munged(uns, gi->gid[g])); |
| 9518 | } |
| 9519 | seq_puts(m, "\n\tCapEff:\t"); |
| 9520 | cap = cred->cap_effective; |
| 9521 | CAP_FOR_EACH_U32(__capi) |
| 9522 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9523 | seq_putc(m, '\n'); |
| 9524 | return 0; |
| 9525 | } |
| 9526 | |
| 9527 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9528 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9529 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9530 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9531 | int i; |
| 9532 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9533 | /* |
| 9534 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9535 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9536 | * cases. If we fail to get the lock, we just don't iterate any |
| 9537 | * structures that could be going away outside the io_uring mutex. |
| 9538 | */ |
| 9539 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9540 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9541 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9542 | sq = ctx->sq_data; |
| 9543 | |
| 9544 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9545 | 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] | 9546 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9547 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9548 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9549 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9550 | if (f) |
| 9551 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9552 | else |
| 9553 | seq_printf(m, "%5u: <none>\n", i); |
| 9554 | } |
| 9555 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9556 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9557 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9558 | |
| 9559 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9560 | (unsigned int) buf->len); |
| 9561 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9562 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9563 | seq_printf(m, "Personalities:\n"); |
| 9564 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9565 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9566 | seq_printf(m, "PollList:\n"); |
| 9567 | spin_lock_irq(&ctx->completion_lock); |
| 9568 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9569 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9570 | struct io_kiocb *req; |
| 9571 | |
| 9572 | hlist_for_each_entry(req, list, hash_node) |
| 9573 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9574 | req->task->task_works != NULL); |
| 9575 | } |
| 9576 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9577 | if (has_lock) |
| 9578 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9579 | } |
| 9580 | |
| 9581 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9582 | { |
| 9583 | struct io_ring_ctx *ctx = f->private_data; |
| 9584 | |
| 9585 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9586 | __io_uring_show_fdinfo(ctx, m); |
| 9587 | percpu_ref_put(&ctx->refs); |
| 9588 | } |
| 9589 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9590 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9591 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9592 | static const struct file_operations io_uring_fops = { |
| 9593 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9594 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9595 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9596 | #ifndef CONFIG_MMU |
| 9597 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9598 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9599 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9600 | .poll = io_uring_poll, |
| 9601 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9602 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9603 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9604 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9605 | }; |
| 9606 | |
| 9607 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9608 | struct io_uring_params *p) |
| 9609 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9610 | struct io_rings *rings; |
| 9611 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9612 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9613 | /* make sure these are sane, as we already accounted them */ |
| 9614 | ctx->sq_entries = p->sq_entries; |
| 9615 | ctx->cq_entries = p->cq_entries; |
| 9616 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9617 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9618 | if (size == SIZE_MAX) |
| 9619 | return -EOVERFLOW; |
| 9620 | |
| 9621 | rings = io_mem_alloc(size); |
| 9622 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9623 | return -ENOMEM; |
| 9624 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9625 | ctx->rings = rings; |
| 9626 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9627 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9628 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9629 | rings->sq_ring_entries = p->sq_entries; |
| 9630 | rings->cq_ring_entries = p->cq_entries; |
| 9631 | ctx->sq_mask = rings->sq_ring_mask; |
| 9632 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9633 | |
| 9634 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9635 | if (size == SIZE_MAX) { |
| 9636 | io_mem_free(ctx->rings); |
| 9637 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9638 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9639 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9640 | |
| 9641 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9642 | if (!ctx->sq_sqes) { |
| 9643 | io_mem_free(ctx->rings); |
| 9644 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9645 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9646 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9647 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9648 | return 0; |
| 9649 | } |
| 9650 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9651 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9652 | { |
| 9653 | int ret, fd; |
| 9654 | |
| 9655 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9656 | if (fd < 0) |
| 9657 | return fd; |
| 9658 | |
| 9659 | ret = io_uring_add_task_file(ctx, file); |
| 9660 | if (ret) { |
| 9661 | put_unused_fd(fd); |
| 9662 | return ret; |
| 9663 | } |
| 9664 | fd_install(fd, file); |
| 9665 | return fd; |
| 9666 | } |
| 9667 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9668 | /* |
| 9669 | * Allocate an anonymous fd, this is what constitutes the application |
| 9670 | * visible backing of an io_uring instance. The application mmaps this |
| 9671 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9672 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9673 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9674 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9675 | { |
| 9676 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9677 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9678 | int ret; |
| 9679 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9680 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9681 | &ctx->ring_sock); |
| 9682 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9683 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9684 | #endif |
| 9685 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9686 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9687 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9688 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9689 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9690 | sock_release(ctx->ring_sock); |
| 9691 | ctx->ring_sock = NULL; |
| 9692 | } else { |
| 9693 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9694 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9695 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9696 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9697 | } |
| 9698 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9699 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9700 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9701 | { |
| 9702 | struct user_struct *user = NULL; |
| 9703 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9704 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9705 | int ret; |
| 9706 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9707 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9708 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9709 | if (entries > IORING_MAX_ENTRIES) { |
| 9710 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9711 | return -EINVAL; |
| 9712 | entries = IORING_MAX_ENTRIES; |
| 9713 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9714 | |
| 9715 | /* |
| 9716 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9717 | * application to drive a higher depth than the size of the SQ ring, |
| 9718 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9719 | * some flexibility in overcommitting a bit. If the application has |
| 9720 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9721 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9722 | */ |
| 9723 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9724 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9725 | /* |
| 9726 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9727 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9728 | * any cq vs sq ring sizing. |
| 9729 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9730 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9731 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9732 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9733 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9734 | return -EINVAL; |
| 9735 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9736 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9737 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9738 | if (p->cq_entries < p->sq_entries) |
| 9739 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9740 | } else { |
| 9741 | p->cq_entries = 2 * p->sq_entries; |
| 9742 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9743 | |
| 9744 | user = get_uid(current_user()); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9745 | |
| 9746 | ctx = io_ring_ctx_alloc(p); |
| 9747 | if (!ctx) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9748 | free_uid(user); |
| 9749 | return -ENOMEM; |
| 9750 | } |
| 9751 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9752 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9753 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9754 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9755 | #ifdef CONFIG_AUDIT |
| 9756 | ctx->loginuid = current->loginuid; |
| 9757 | ctx->sessionid = current->sessionid; |
| 9758 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9759 | ctx->sqo_task = get_task_struct(current); |
| 9760 | |
| 9761 | /* |
| 9762 | * This is just grabbed for accounting purposes. When a process exits, |
| 9763 | * the mm is exited and dropped before the files, hence we need to hang |
| 9764 | * on to this mm purely for the purposes of being able to unaccount |
| 9765 | * memory (locked/pinned vm). It's not used for anything else. |
| 9766 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9767 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9768 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9769 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9770 | #ifdef CONFIG_BLK_CGROUP |
| 9771 | /* |
| 9772 | * The sq thread will belong to the original cgroup it was inited in. |
| 9773 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9774 | * issued bios will be associated with the closest cgroup later in the |
| 9775 | * block layer. |
| 9776 | */ |
| 9777 | rcu_read_lock(); |
| 9778 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9779 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9780 | rcu_read_unlock(); |
| 9781 | if (!ret) { |
| 9782 | /* don't init against a dying cgroup, have the user try again */ |
| 9783 | ctx->sqo_blkcg_css = NULL; |
| 9784 | ret = -ENODEV; |
| 9785 | goto err; |
| 9786 | } |
| 9787 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9788 | ret = io_allocate_scq_urings(ctx, p); |
| 9789 | if (ret) |
| 9790 | goto err; |
| 9791 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9792 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9793 | if (ret) |
| 9794 | goto err; |
| 9795 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9796 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9797 | io_sq_offload_start(ctx); |
| 9798 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9799 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9800 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9801 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9802 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9803 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9804 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9805 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9806 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9807 | |
| 9808 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9809 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9810 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9811 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9812 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9813 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9814 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9815 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9816 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9817 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9818 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9819 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9820 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9821 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9822 | |
| 9823 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9824 | ret = -EFAULT; |
| 9825 | goto err; |
| 9826 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9827 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9828 | file = io_uring_get_file(ctx); |
| 9829 | if (IS_ERR(file)) { |
| 9830 | ret = PTR_ERR(file); |
| 9831 | goto err; |
| 9832 | } |
| 9833 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9834 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9835 | * Install ring fd as the very last thing, so we don't risk someone |
| 9836 | * having closed it before we finish setup |
| 9837 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9838 | ret = io_uring_install_fd(ctx, file); |
| 9839 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9840 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9841 | /* fput will clean it up */ |
| 9842 | fput(file); |
| 9843 | return ret; |
| 9844 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9845 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9846 | 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] | 9847 | return ret; |
| 9848 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9849 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9850 | io_ring_ctx_wait_and_kill(ctx); |
| 9851 | return ret; |
| 9852 | } |
| 9853 | |
| 9854 | /* |
| 9855 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9856 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9857 | * params structure passed in. |
| 9858 | */ |
| 9859 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9860 | { |
| 9861 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9862 | int i; |
| 9863 | |
| 9864 | if (copy_from_user(&p, params, sizeof(p))) |
| 9865 | return -EFAULT; |
| 9866 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9867 | if (p.resv[i]) |
| 9868 | return -EINVAL; |
| 9869 | } |
| 9870 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9871 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9872 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9873 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9874 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9875 | return -EINVAL; |
| 9876 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9877 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9878 | } |
| 9879 | |
| 9880 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9881 | struct io_uring_params __user *, params) |
| 9882 | { |
| 9883 | return io_uring_setup(entries, params); |
| 9884 | } |
| 9885 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9886 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9887 | { |
| 9888 | struct io_uring_probe *p; |
| 9889 | size_t size; |
| 9890 | int i, ret; |
| 9891 | |
| 9892 | size = struct_size(p, ops, nr_args); |
| 9893 | if (size == SIZE_MAX) |
| 9894 | return -EOVERFLOW; |
| 9895 | p = kzalloc(size, GFP_KERNEL); |
| 9896 | if (!p) |
| 9897 | return -ENOMEM; |
| 9898 | |
| 9899 | ret = -EFAULT; |
| 9900 | if (copy_from_user(p, arg, size)) |
| 9901 | goto out; |
| 9902 | ret = -EINVAL; |
| 9903 | if (memchr_inv(p, 0, size)) |
| 9904 | goto out; |
| 9905 | |
| 9906 | p->last_op = IORING_OP_LAST - 1; |
| 9907 | if (nr_args > IORING_OP_LAST) |
| 9908 | nr_args = IORING_OP_LAST; |
| 9909 | |
| 9910 | for (i = 0; i < nr_args; i++) { |
| 9911 | p->ops[i].op = i; |
| 9912 | if (!io_op_defs[i].not_supported) |
| 9913 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9914 | } |
| 9915 | p->ops_len = i; |
| 9916 | |
| 9917 | ret = 0; |
| 9918 | if (copy_to_user(arg, p, size)) |
| 9919 | ret = -EFAULT; |
| 9920 | out: |
| 9921 | kfree(p); |
| 9922 | return ret; |
| 9923 | } |
| 9924 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9925 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9926 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9927 | struct io_identity *id; |
| 9928 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9929 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9930 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9931 | if (unlikely(!id)) |
| 9932 | return -ENOMEM; |
| 9933 | |
| 9934 | io_init_identity(id); |
| 9935 | id->creds = get_current_cred(); |
| 9936 | |
| 9937 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9938 | if (ret < 0) { |
| 9939 | put_cred(id->creds); |
| 9940 | kfree(id); |
| 9941 | } |
| 9942 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9943 | } |
| 9944 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9945 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9946 | unsigned int nr_args) |
| 9947 | { |
| 9948 | struct io_uring_restriction *res; |
| 9949 | size_t size; |
| 9950 | int i, ret; |
| 9951 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9952 | /* Restrictions allowed only if rings started disabled */ |
| 9953 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9954 | return -EBADFD; |
| 9955 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9956 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9957 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9958 | return -EBUSY; |
| 9959 | |
| 9960 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9961 | return -EINVAL; |
| 9962 | |
| 9963 | size = array_size(nr_args, sizeof(*res)); |
| 9964 | if (size == SIZE_MAX) |
| 9965 | return -EOVERFLOW; |
| 9966 | |
| 9967 | res = memdup_user(arg, size); |
| 9968 | if (IS_ERR(res)) |
| 9969 | return PTR_ERR(res); |
| 9970 | |
| 9971 | ret = 0; |
| 9972 | |
| 9973 | for (i = 0; i < nr_args; i++) { |
| 9974 | switch (res[i].opcode) { |
| 9975 | case IORING_RESTRICTION_REGISTER_OP: |
| 9976 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9977 | ret = -EINVAL; |
| 9978 | goto out; |
| 9979 | } |
| 9980 | |
| 9981 | __set_bit(res[i].register_op, |
| 9982 | ctx->restrictions.register_op); |
| 9983 | break; |
| 9984 | case IORING_RESTRICTION_SQE_OP: |
| 9985 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9986 | ret = -EINVAL; |
| 9987 | goto out; |
| 9988 | } |
| 9989 | |
| 9990 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9991 | break; |
| 9992 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9993 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9994 | break; |
| 9995 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9996 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9997 | break; |
| 9998 | default: |
| 9999 | ret = -EINVAL; |
| 10000 | goto out; |
| 10001 | } |
| 10002 | } |
| 10003 | |
| 10004 | out: |
| 10005 | /* Reset all restrictions if an error happened */ |
| 10006 | if (ret != 0) |
| 10007 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10008 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10009 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10010 | |
| 10011 | kfree(res); |
| 10012 | return ret; |
| 10013 | } |
| 10014 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10015 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10016 | { |
| 10017 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10018 | return -EBADFD; |
| 10019 | |
| 10020 | if (ctx->restrictions.registered) |
| 10021 | ctx->restricted = 1; |
| 10022 | |
| 10023 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10024 | |
| 10025 | io_sq_offload_start(ctx); |
| 10026 | |
| 10027 | return 0; |
| 10028 | } |
| 10029 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10030 | static bool io_register_op_must_quiesce(int op) |
| 10031 | { |
| 10032 | switch (op) { |
| 10033 | case IORING_UNREGISTER_FILES: |
| 10034 | case IORING_REGISTER_FILES_UPDATE: |
| 10035 | case IORING_REGISTER_PROBE: |
| 10036 | case IORING_REGISTER_PERSONALITY: |
| 10037 | case IORING_UNREGISTER_PERSONALITY: |
| 10038 | return false; |
| 10039 | default: |
| 10040 | return true; |
| 10041 | } |
| 10042 | } |
| 10043 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10044 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10045 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10046 | __releases(ctx->uring_lock) |
| 10047 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10048 | { |
| 10049 | int ret; |
| 10050 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10051 | /* |
| 10052 | * We're inside the ring mutex, if the ref is already dying, then |
| 10053 | * someone else killed the ctx or is already going through |
| 10054 | * io_uring_register(). |
| 10055 | */ |
| 10056 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10057 | return -ENXIO; |
| 10058 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10059 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10060 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10061 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10062 | /* |
| 10063 | * Drop uring mutex before waiting for references to exit. If |
| 10064 | * another thread is currently inside io_uring_enter() it might |
| 10065 | * need to grab the uring_lock to make progress. If we hold it |
| 10066 | * here across the drain wait, then we can deadlock. It's safe |
| 10067 | * to drop the mutex here, since no new references will come in |
| 10068 | * after we've killed the percpu ref. |
| 10069 | */ |
| 10070 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10071 | do { |
| 10072 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10073 | if (!ret) |
| 10074 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 10075 | ret = io_run_task_work_sig(); |
| 10076 | if (ret < 0) |
| 10077 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10078 | } while (1); |
| 10079 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10080 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10081 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10082 | if (ret) { |
| 10083 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10084 | goto out_quiesce; |
| 10085 | } |
| 10086 | } |
| 10087 | |
| 10088 | if (ctx->restricted) { |
| 10089 | if (opcode >= IORING_REGISTER_LAST) { |
| 10090 | ret = -EINVAL; |
| 10091 | goto out; |
| 10092 | } |
| 10093 | |
| 10094 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10095 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10096 | goto out; |
| 10097 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10098 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10099 | |
| 10100 | switch (opcode) { |
| 10101 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10102 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10103 | break; |
| 10104 | case IORING_UNREGISTER_BUFFERS: |
| 10105 | ret = -EINVAL; |
| 10106 | if (arg || nr_args) |
| 10107 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10108 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10109 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10110 | case IORING_REGISTER_FILES: |
| 10111 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10112 | break; |
| 10113 | case IORING_UNREGISTER_FILES: |
| 10114 | ret = -EINVAL; |
| 10115 | if (arg || nr_args) |
| 10116 | break; |
| 10117 | ret = io_sqe_files_unregister(ctx); |
| 10118 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10119 | case IORING_REGISTER_FILES_UPDATE: |
| 10120 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10121 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10122 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10123 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10124 | ret = -EINVAL; |
| 10125 | if (nr_args != 1) |
| 10126 | break; |
| 10127 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10128 | if (ret) |
| 10129 | break; |
| 10130 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10131 | ctx->eventfd_async = 1; |
| 10132 | else |
| 10133 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10134 | break; |
| 10135 | case IORING_UNREGISTER_EVENTFD: |
| 10136 | ret = -EINVAL; |
| 10137 | if (arg || nr_args) |
| 10138 | break; |
| 10139 | ret = io_eventfd_unregister(ctx); |
| 10140 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10141 | case IORING_REGISTER_PROBE: |
| 10142 | ret = -EINVAL; |
| 10143 | if (!arg || nr_args > 256) |
| 10144 | break; |
| 10145 | ret = io_probe(ctx, arg, nr_args); |
| 10146 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10147 | case IORING_REGISTER_PERSONALITY: |
| 10148 | ret = -EINVAL; |
| 10149 | if (arg || nr_args) |
| 10150 | break; |
| 10151 | ret = io_register_personality(ctx); |
| 10152 | break; |
| 10153 | case IORING_UNREGISTER_PERSONALITY: |
| 10154 | ret = -EINVAL; |
| 10155 | if (arg) |
| 10156 | break; |
| 10157 | ret = io_unregister_personality(ctx, nr_args); |
| 10158 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10159 | case IORING_REGISTER_ENABLE_RINGS: |
| 10160 | ret = -EINVAL; |
| 10161 | if (arg || nr_args) |
| 10162 | break; |
| 10163 | ret = io_register_enable_rings(ctx); |
| 10164 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10165 | case IORING_REGISTER_RESTRICTIONS: |
| 10166 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10167 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10168 | default: |
| 10169 | ret = -EINVAL; |
| 10170 | break; |
| 10171 | } |
| 10172 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10173 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10174 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10175 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10176 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10177 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10178 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10179 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10180 | return ret; |
| 10181 | } |
| 10182 | |
| 10183 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10184 | void __user *, arg, unsigned int, nr_args) |
| 10185 | { |
| 10186 | struct io_ring_ctx *ctx; |
| 10187 | long ret = -EBADF; |
| 10188 | struct fd f; |
| 10189 | |
| 10190 | f = fdget(fd); |
| 10191 | if (!f.file) |
| 10192 | return -EBADF; |
| 10193 | |
| 10194 | ret = -EOPNOTSUPP; |
| 10195 | if (f.file->f_op != &io_uring_fops) |
| 10196 | goto out_fput; |
| 10197 | |
| 10198 | ctx = f.file->private_data; |
| 10199 | |
| 10200 | mutex_lock(&ctx->uring_lock); |
| 10201 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10202 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10203 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10204 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10205 | out_fput: |
| 10206 | fdput(f); |
| 10207 | return ret; |
| 10208 | } |
| 10209 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10210 | static int __init io_uring_init(void) |
| 10211 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10212 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10213 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10214 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10215 | } while (0) |
| 10216 | |
| 10217 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10218 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10219 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10220 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10221 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10222 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10223 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10224 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10225 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10226 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10227 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10228 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10229 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10230 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10231 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10232 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10233 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10234 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10235 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10236 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10237 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10238 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10239 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10240 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10241 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10242 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10243 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10244 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10245 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10246 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10247 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10248 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10249 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10250 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 10251 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 10252 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10253 | return 0; |
| 10254 | }; |
| 10255 | __initcall(io_uring_init); |