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 | |
| 2189 | spin_lock(&tctx->task_lock); |
| 2190 | list = tctx->task_list; |
| 2191 | INIT_WQ_LIST(&tctx->task_list); |
| 2192 | spin_unlock(&tctx->task_lock); |
| 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; |
| 2239 | int ret; |
| 2240 | |
| 2241 | WARN_ON_ONCE(!tctx); |
| 2242 | |
| 2243 | spin_lock(&tctx->task_lock); |
| 2244 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
| 2245 | spin_unlock(&tctx->task_lock); |
| 2246 | |
| 2247 | /* task_work already pending, we're done */ |
| 2248 | if (test_bit(0, &tctx->task_state) || |
| 2249 | test_and_set_bit(0, &tctx->task_state)) |
| 2250 | return 0; |
| 2251 | |
| 2252 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 2253 | return 0; |
| 2254 | |
| 2255 | /* |
| 2256 | * Slow path - we failed, find and delete work. if the work is not |
| 2257 | * in the list, it got run and we're fine. |
| 2258 | */ |
| 2259 | ret = 0; |
| 2260 | spin_lock(&tctx->task_lock); |
| 2261 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 2262 | if (&req->io_task_work.node == node) { |
| 2263 | wq_list_del(&tctx->task_list, node, prev); |
| 2264 | ret = 1; |
| 2265 | break; |
| 2266 | } |
| 2267 | } |
| 2268 | spin_unlock(&tctx->task_lock); |
| 2269 | clear_bit(0, &tctx->task_state); |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2273 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2274 | { |
| 2275 | struct task_struct *tsk = req->task; |
| 2276 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2277 | enum task_work_notify_mode notify; |
| 2278 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2279 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2280 | if (tsk->flags & PF_EXITING) |
| 2281 | return -ESRCH; |
| 2282 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2283 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2284 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2285 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2286 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2287 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2288 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2289 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2290 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2291 | notify = TWA_SIGNAL; |
| 2292 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2293 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2294 | if (!ret) |
| 2295 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2296 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2297 | return ret; |
| 2298 | } |
| 2299 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2300 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2301 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2302 | { |
| 2303 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); |
| 2304 | |
| 2305 | init_task_work(&req->task_work, cb); |
| 2306 | task_work_add(tsk, &req->task_work, TWA_NONE); |
| 2307 | wake_up_process(tsk); |
| 2308 | } |
| 2309 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2310 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2311 | { |
| 2312 | struct io_ring_ctx *ctx = req->ctx; |
| 2313 | |
| 2314 | spin_lock_irq(&ctx->completion_lock); |
| 2315 | io_cqring_fill_event(req, error); |
| 2316 | io_commit_cqring(ctx); |
| 2317 | spin_unlock_irq(&ctx->completion_lock); |
| 2318 | |
| 2319 | io_cqring_ev_posted(ctx); |
| 2320 | req_set_fail_links(req); |
| 2321 | io_double_put_req(req); |
| 2322 | } |
| 2323 | |
| 2324 | static void io_req_task_cancel(struct callback_head *cb) |
| 2325 | { |
| 2326 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2327 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2328 | |
| 2329 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2330 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2334 | { |
| 2335 | struct io_ring_ctx *ctx = req->ctx; |
| 2336 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2337 | /* 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] | 2338 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | dc0eced | 2021-02-12 18:41:15 +0000 | [diff] [blame] | 2339 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
| 2340 | !io_sq_thread_acquire_mm_files(ctx, req)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2341 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2342 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2343 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2344 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2345 | } |
| 2346 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2347 | static void io_req_task_submit(struct callback_head *cb) |
| 2348 | { |
| 2349 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2350 | |
| 2351 | __io_req_task_submit(req); |
| 2352 | } |
| 2353 | |
| 2354 | static void io_req_task_queue(struct io_kiocb *req) |
| 2355 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2356 | int ret; |
| 2357 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2358 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2359 | ret = io_req_task_work_add(req); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2360 | if (unlikely(ret)) { |
| 2361 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2362 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2363 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2364 | } |
| 2365 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2366 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2367 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2368 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2369 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2370 | if (nxt) |
| 2371 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2372 | } |
| 2373 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2374 | static void io_free_req(struct io_kiocb *req) |
| 2375 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2376 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2377 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2378 | } |
| 2379 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2380 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2381 | struct task_struct *task; |
| 2382 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2383 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2384 | }; |
| 2385 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2386 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2387 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2388 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2389 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2390 | rb->task = NULL; |
| 2391 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2392 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2393 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2394 | struct req_batch *rb) |
| 2395 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2396 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2397 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2398 | if (rb->ctx_refs) |
| 2399 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2400 | } |
| 2401 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2402 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2403 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2404 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2405 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2406 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2407 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2408 | if (rb->task) |
| 2409 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2410 | rb->task = req->task; |
| 2411 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2412 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2413 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2414 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2415 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2416 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2417 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2418 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2419 | else |
| 2420 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2421 | } |
| 2422 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2423 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2424 | struct io_ring_ctx *ctx) |
| 2425 | { |
| 2426 | int i, nr = cs->nr; |
| 2427 | struct io_kiocb *req; |
| 2428 | struct req_batch rb; |
| 2429 | |
| 2430 | io_init_req_batch(&rb); |
| 2431 | spin_lock_irq(&ctx->completion_lock); |
| 2432 | for (i = 0; i < nr; i++) { |
| 2433 | req = cs->reqs[i]; |
| 2434 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2435 | } |
| 2436 | io_commit_cqring(ctx); |
| 2437 | spin_unlock_irq(&ctx->completion_lock); |
| 2438 | |
| 2439 | io_cqring_ev_posted(ctx); |
| 2440 | for (i = 0; i < nr; i++) { |
| 2441 | req = cs->reqs[i]; |
| 2442 | |
| 2443 | /* submission and completion refs */ |
| 2444 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2445 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | io_req_free_batch_finish(ctx, &rb); |
| 2449 | cs->nr = 0; |
| 2450 | } |
| 2451 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2452 | /* |
| 2453 | * Drop reference to request, return next in chain (if there is one) if this |
| 2454 | * was the last reference to this request. |
| 2455 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2456 | 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] | 2457 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2458 | struct io_kiocb *nxt = NULL; |
| 2459 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2460 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2461 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2462 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2463 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2464 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2467 | static void io_put_req(struct io_kiocb *req) |
| 2468 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2469 | if (refcount_dec_and_test(&req->refs)) |
| 2470 | io_free_req(req); |
| 2471 | } |
| 2472 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2473 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2474 | { |
| 2475 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2476 | |
| 2477 | io_free_req(req); |
| 2478 | } |
| 2479 | |
| 2480 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2481 | { |
| 2482 | int ret; |
| 2483 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2484 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2485 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2486 | if (unlikely(ret)) |
| 2487 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2491 | { |
| 2492 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2493 | io_free_req_deferred(req); |
| 2494 | } |
| 2495 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2496 | static void io_double_put_req(struct io_kiocb *req) |
| 2497 | { |
| 2498 | /* drop both submit and complete references */ |
| 2499 | if (refcount_sub_and_test(2, &req->refs)) |
| 2500 | io_free_req(req); |
| 2501 | } |
| 2502 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2503 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2504 | { |
| 2505 | /* See comment at the top of this file */ |
| 2506 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2507 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2508 | } |
| 2509 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2510 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2511 | { |
| 2512 | struct io_rings *rings = ctx->rings; |
| 2513 | |
| 2514 | /* make sure SQ entry isn't read before tail */ |
| 2515 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2516 | } |
| 2517 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2518 | 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] | 2519 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2520 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2521 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2522 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2523 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2524 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2525 | kfree(kbuf); |
| 2526 | return cflags; |
| 2527 | } |
| 2528 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2529 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2530 | { |
| 2531 | struct io_buffer *kbuf; |
| 2532 | |
| 2533 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2534 | return io_put_kbuf(req, kbuf); |
| 2535 | } |
| 2536 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2537 | static inline bool io_run_task_work(void) |
| 2538 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2539 | /* |
| 2540 | * Not safe to run on exiting task, and the task_work handling will |
| 2541 | * not add work to such a task. |
| 2542 | */ |
| 2543 | if (unlikely(current->flags & PF_EXITING)) |
| 2544 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2545 | if (current->task_works) { |
| 2546 | __set_current_state(TASK_RUNNING); |
| 2547 | task_work_run(); |
| 2548 | return true; |
| 2549 | } |
| 2550 | |
| 2551 | return false; |
| 2552 | } |
| 2553 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2554 | /* |
| 2555 | * Find and free completed poll iocbs |
| 2556 | */ |
| 2557 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2558 | struct list_head *done) |
| 2559 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2560 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2561 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2562 | |
| 2563 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2564 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2565 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2566 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2567 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2568 | int cflags = 0; |
| 2569 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2570 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2571 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2572 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2573 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2574 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2575 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2576 | continue; |
| 2577 | } |
| 2578 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2579 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2580 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2581 | |
| 2582 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2583 | (*nr_events)++; |
| 2584 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2585 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2586 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2587 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2588 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2589 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2590 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2591 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2594 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2595 | long min) |
| 2596 | { |
| 2597 | struct io_kiocb *req, *tmp; |
| 2598 | LIST_HEAD(done); |
| 2599 | bool spin; |
| 2600 | int ret; |
| 2601 | |
| 2602 | /* |
| 2603 | * Only spin for completions if we don't have multiple devices hanging |
| 2604 | * off our complete list, and we're under the requested amount. |
| 2605 | */ |
| 2606 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2607 | |
| 2608 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2609 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2610 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2611 | |
| 2612 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2613 | * Move completed and retryable entries to our local lists. |
| 2614 | * If we find a request that requires polling, break out |
| 2615 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2616 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2617 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2618 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2619 | continue; |
| 2620 | } |
| 2621 | if (!list_empty(&done)) |
| 2622 | break; |
| 2623 | |
| 2624 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2625 | if (ret < 0) |
| 2626 | break; |
| 2627 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2628 | /* iopoll may have completed current req */ |
| 2629 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2630 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2631 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2632 | if (ret && spin) |
| 2633 | spin = false; |
| 2634 | ret = 0; |
| 2635 | } |
| 2636 | |
| 2637 | if (!list_empty(&done)) |
| 2638 | io_iopoll_complete(ctx, nr_events, &done); |
| 2639 | |
| 2640 | return ret; |
| 2641 | } |
| 2642 | |
| 2643 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2644 | * 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] | 2645 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2646 | * as a non-spinning completion check. |
| 2647 | */ |
| 2648 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2649 | long min) |
| 2650 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2651 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2652 | int ret; |
| 2653 | |
| 2654 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2655 | if (ret < 0) |
| 2656 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2657 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | return 1; |
| 2662 | } |
| 2663 | |
| 2664 | /* |
| 2665 | * We can't just wait for polled events to come to us, we have to actively |
| 2666 | * find and complete them. |
| 2667 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2668 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2669 | { |
| 2670 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2671 | return; |
| 2672 | |
| 2673 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2674 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2675 | unsigned int nr_events = 0; |
| 2676 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2677 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2678 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2679 | /* let it sleep and repeat later if can't complete a request */ |
| 2680 | if (nr_events == 0) |
| 2681 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2682 | /* |
| 2683 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2684 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2685 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2686 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2687 | if (need_resched()) { |
| 2688 | mutex_unlock(&ctx->uring_lock); |
| 2689 | cond_resched(); |
| 2690 | mutex_lock(&ctx->uring_lock); |
| 2691 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2692 | } |
| 2693 | mutex_unlock(&ctx->uring_lock); |
| 2694 | } |
| 2695 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2696 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2697 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2698 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2699 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2700 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2701 | /* |
| 2702 | * We disallow the app entering submit/complete with polling, but we |
| 2703 | * still need to lock the ring to prevent racing with polled issue |
| 2704 | * that got punted to a workqueue. |
| 2705 | */ |
| 2706 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2707 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2708 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2709 | * Don't enter poll loop if we already have events pending. |
| 2710 | * If we do, we can potentially be spinning for commands that |
| 2711 | * already triggered a CQE (eg in error). |
| 2712 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2713 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2714 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2715 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2716 | break; |
| 2717 | |
| 2718 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2719 | * If a submit got punted to a workqueue, we can have the |
| 2720 | * application entering polling for a command before it gets |
| 2721 | * issued. That app will hold the uring_lock for the duration |
| 2722 | * of the poll right here, so we need to take a breather every |
| 2723 | * now and then to ensure that the issue has a chance to add |
| 2724 | * the poll to the issued list. Otherwise we can spin here |
| 2725 | * forever, while the workqueue is stuck trying to acquire the |
| 2726 | * very same mutex. |
| 2727 | */ |
| 2728 | if (!(++iters & 7)) { |
| 2729 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2730 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2731 | mutex_lock(&ctx->uring_lock); |
| 2732 | } |
| 2733 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2734 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2735 | if (ret <= 0) |
| 2736 | break; |
| 2737 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2738 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2739 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2740 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2741 | return ret; |
| 2742 | } |
| 2743 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2744 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2745 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2746 | /* |
| 2747 | * Tell lockdep we inherited freeze protection from submission |
| 2748 | * thread. |
| 2749 | */ |
| 2750 | if (req->flags & REQ_F_ISREG) { |
| 2751 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2752 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2753 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2754 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2755 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2758 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2759 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2760 | { |
| 2761 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2762 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2763 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2764 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2765 | /* already prepared */ |
| 2766 | if (req->async_data) |
| 2767 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2768 | |
| 2769 | switch (req->opcode) { |
| 2770 | case IORING_OP_READV: |
| 2771 | case IORING_OP_READ_FIXED: |
| 2772 | case IORING_OP_READ: |
| 2773 | rw = READ; |
| 2774 | break; |
| 2775 | case IORING_OP_WRITEV: |
| 2776 | case IORING_OP_WRITE_FIXED: |
| 2777 | case IORING_OP_WRITE: |
| 2778 | rw = WRITE; |
| 2779 | break; |
| 2780 | default: |
| 2781 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2782 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2783 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2784 | } |
| 2785 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2786 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2787 | if (ret < 0) |
| 2788 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2789 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2790 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2791 | #endif |
| 2792 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2793 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2794 | { |
| 2795 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2796 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2797 | int ret; |
| 2798 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2799 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2800 | return false; |
| 2801 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2802 | return false; |
| 2803 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2804 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2805 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2806 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2807 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2808 | if (!ret && io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2809 | refcount_inc(&req->refs); |
| 2810 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2811 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2812 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2813 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2814 | #endif |
| 2815 | return false; |
| 2816 | } |
| 2817 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2818 | 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] | 2819 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2820 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2821 | int cflags = 0; |
| 2822 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2823 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2824 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2825 | if (res != req->result) |
| 2826 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2827 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2828 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2829 | kiocb_end_write(req); |
| 2830 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2831 | cflags = io_put_rw_kbuf(req); |
| 2832 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2836 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2837 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2838 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2839 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2842 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2843 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2844 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2845 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2846 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2847 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2848 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2849 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2850 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2851 | |
| 2852 | WRITE_ONCE(req->result, res); |
| 2853 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2854 | smp_wmb(); |
| 2855 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2860 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2861 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2862 | * accessing the kiocb cookie. |
| 2863 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2864 | 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] | 2865 | { |
| 2866 | struct io_ring_ctx *ctx = req->ctx; |
| 2867 | |
| 2868 | /* |
| 2869 | * Track whether we have multiple files in our lists. This will impact |
| 2870 | * how we do polling eventually, not spinning if we're on potentially |
| 2871 | * different devices. |
| 2872 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2873 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2874 | ctx->poll_multi_file = false; |
| 2875 | } else if (!ctx->poll_multi_file) { |
| 2876 | struct io_kiocb *list_req; |
| 2877 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2878 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2879 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2880 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2881 | ctx->poll_multi_file = true; |
| 2882 | } |
| 2883 | |
| 2884 | /* |
| 2885 | * For fast devices, IO may have already completed. If it has, add |
| 2886 | * it to the front so we find it first. |
| 2887 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2888 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2889 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2890 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2891 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2892 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2893 | /* |
| 2894 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2895 | * task context or in io worker task context. If current task context is |
| 2896 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2897 | */ |
| 2898 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2899 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2900 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2903 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2904 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2905 | if (state->file_refs) { |
| 2906 | fput_many(state->file, state->file_refs); |
| 2907 | state->file_refs = 0; |
| 2908 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /* |
| 2912 | * Get as many references to a file as we have IOs left in this submission, |
| 2913 | * assuming most submissions are for one file, or at least that each file |
| 2914 | * has more than one submission. |
| 2915 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2916 | 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] | 2917 | { |
| 2918 | if (!state) |
| 2919 | return fget(fd); |
| 2920 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2921 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2922 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2923 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2924 | return state->file; |
| 2925 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2926 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2927 | } |
| 2928 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2929 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2930 | return NULL; |
| 2931 | |
| 2932 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2933 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2934 | return state->file; |
| 2935 | } |
| 2936 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2937 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2938 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2939 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2940 | } |
| 2941 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2942 | /* |
| 2943 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2944 | * any file. For now, just ensure that anything potentially problematic is done |
| 2945 | * inline. |
| 2946 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2947 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2948 | { |
| 2949 | umode_t mode = file_inode(file)->i_mode; |
| 2950 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2951 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2952 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2953 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2954 | return true; |
| 2955 | return false; |
| 2956 | } |
| 2957 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2958 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2959 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2960 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2961 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2962 | file->f_op != &io_uring_fops) |
| 2963 | return true; |
| 2964 | return false; |
| 2965 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2966 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2967 | /* any ->read/write should understand O_NONBLOCK */ |
| 2968 | if (file->f_flags & O_NONBLOCK) |
| 2969 | return true; |
| 2970 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2971 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2972 | return false; |
| 2973 | |
| 2974 | if (rw == READ) |
| 2975 | return file->f_op->read_iter != NULL; |
| 2976 | |
| 2977 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2980 | 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] | 2981 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2982 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2983 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2984 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2985 | unsigned ioprio; |
| 2986 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2987 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2988 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2989 | req->flags |= REQ_F_ISREG; |
| 2990 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2991 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2992 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2993 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2994 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2995 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2996 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2997 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2998 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2999 | if (unlikely(ret)) |
| 3000 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3001 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3002 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 3003 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 3004 | req->flags |= REQ_F_NOWAIT; |
| 3005 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3006 | ioprio = READ_ONCE(sqe->ioprio); |
| 3007 | if (ioprio) { |
| 3008 | ret = ioprio_check_cap(ioprio); |
| 3009 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3010 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3011 | |
| 3012 | kiocb->ki_ioprio = ioprio; |
| 3013 | } else |
| 3014 | kiocb->ki_ioprio = get_current_ioprio(); |
| 3015 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3016 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3017 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 3018 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3019 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3020 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3021 | kiocb->ki_flags |= IOCB_HIPRI; |
| 3022 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3023 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3024 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3025 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3026 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3027 | kiocb->ki_complete = io_complete_rw; |
| 3028 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3029 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3030 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3031 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3032 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3033 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3037 | { |
| 3038 | switch (ret) { |
| 3039 | case -EIOCBQUEUED: |
| 3040 | break; |
| 3041 | case -ERESTARTSYS: |
| 3042 | case -ERESTARTNOINTR: |
| 3043 | case -ERESTARTNOHAND: |
| 3044 | case -ERESTART_RESTARTBLOCK: |
| 3045 | /* |
| 3046 | * We can't just restart the syscall, since previously |
| 3047 | * submitted sqes may already be in progress. Just fail this |
| 3048 | * IO with EINTR. |
| 3049 | */ |
| 3050 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3051 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3052 | default: |
| 3053 | kiocb->ki_complete(kiocb, ret, 0); |
| 3054 | } |
| 3055 | } |
| 3056 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3057 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3058 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3059 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3060 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3061 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3062 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3063 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3064 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3065 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3066 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3067 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3068 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3069 | } |
| 3070 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3071 | if (req->flags & REQ_F_CUR_POS) |
| 3072 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 3073 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3074 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3075 | else |
| 3076 | io_rw_done(kiocb, ret); |
| 3077 | } |
| 3078 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3079 | 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] | 3080 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3081 | struct io_ring_ctx *ctx = req->ctx; |
| 3082 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3083 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 3084 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3085 | size_t offset; |
| 3086 | u64 buf_addr; |
| 3087 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3088 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3089 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3090 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3091 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3092 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3093 | |
| 3094 | /* overflow */ |
| 3095 | if (buf_addr + len < buf_addr) |
| 3096 | return -EFAULT; |
| 3097 | /* not inside the mapped region */ |
| 3098 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3099 | return -EFAULT; |
| 3100 | |
| 3101 | /* |
| 3102 | * May not be a start of buffer, set size appropriately |
| 3103 | * and advance us to the beginning. |
| 3104 | */ |
| 3105 | offset = buf_addr - imu->ubuf; |
| 3106 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3107 | |
| 3108 | if (offset) { |
| 3109 | /* |
| 3110 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3111 | * using the latter parts of a big fixed buffer - it iterates |
| 3112 | * over each segment manually. We can cheat a bit here, because |
| 3113 | * we know that: |
| 3114 | * |
| 3115 | * 1) it's a BVEC iter, we set it up |
| 3116 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3117 | * first and last bvec |
| 3118 | * |
| 3119 | * So just find our index, and adjust the iterator afterwards. |
| 3120 | * If the offset is within the first bvec (or the whole first |
| 3121 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3122 | * since we can just skip the first segment, which may not |
| 3123 | * be PAGE_SIZE aligned. |
| 3124 | */ |
| 3125 | const struct bio_vec *bvec = imu->bvec; |
| 3126 | |
| 3127 | if (offset <= bvec->bv_len) { |
| 3128 | iov_iter_advance(iter, offset); |
| 3129 | } else { |
| 3130 | unsigned long seg_skip; |
| 3131 | |
| 3132 | /* skip first vec */ |
| 3133 | offset -= bvec->bv_len; |
| 3134 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3135 | |
| 3136 | iter->bvec = bvec + seg_skip; |
| 3137 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3138 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3139 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3143 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3146 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3147 | { |
| 3148 | if (needs_lock) |
| 3149 | mutex_unlock(&ctx->uring_lock); |
| 3150 | } |
| 3151 | |
| 3152 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3153 | { |
| 3154 | /* |
| 3155 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3156 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3157 | * The only exception is when we've detached the request and issue it |
| 3158 | * from an async worker thread, grab the lock for that case. |
| 3159 | */ |
| 3160 | if (needs_lock) |
| 3161 | mutex_lock(&ctx->uring_lock); |
| 3162 | } |
| 3163 | |
| 3164 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3165 | int bgid, struct io_buffer *kbuf, |
| 3166 | bool needs_lock) |
| 3167 | { |
| 3168 | struct io_buffer *head; |
| 3169 | |
| 3170 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3171 | return kbuf; |
| 3172 | |
| 3173 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3174 | |
| 3175 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3176 | |
| 3177 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3178 | if (head) { |
| 3179 | if (!list_empty(&head->list)) { |
| 3180 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3181 | list); |
| 3182 | list_del(&kbuf->list); |
| 3183 | } else { |
| 3184 | kbuf = head; |
| 3185 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3186 | } |
| 3187 | if (*len > kbuf->len) |
| 3188 | *len = kbuf->len; |
| 3189 | } else { |
| 3190 | kbuf = ERR_PTR(-ENOBUFS); |
| 3191 | } |
| 3192 | |
| 3193 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3194 | |
| 3195 | return kbuf; |
| 3196 | } |
| 3197 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3198 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3199 | bool needs_lock) |
| 3200 | { |
| 3201 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3202 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3203 | |
| 3204 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3205 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3206 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3207 | if (IS_ERR(kbuf)) |
| 3208 | return kbuf; |
| 3209 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3210 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3211 | return u64_to_user_ptr(kbuf->addr); |
| 3212 | } |
| 3213 | |
| 3214 | #ifdef CONFIG_COMPAT |
| 3215 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3216 | bool needs_lock) |
| 3217 | { |
| 3218 | struct compat_iovec __user *uiov; |
| 3219 | compat_ssize_t clen; |
| 3220 | void __user *buf; |
| 3221 | ssize_t len; |
| 3222 | |
| 3223 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3224 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3225 | return -EFAULT; |
| 3226 | if (__get_user(clen, &uiov->iov_len)) |
| 3227 | return -EFAULT; |
| 3228 | if (clen < 0) |
| 3229 | return -EINVAL; |
| 3230 | |
| 3231 | len = clen; |
| 3232 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3233 | if (IS_ERR(buf)) |
| 3234 | return PTR_ERR(buf); |
| 3235 | iov[0].iov_base = buf; |
| 3236 | iov[0].iov_len = (compat_size_t) len; |
| 3237 | return 0; |
| 3238 | } |
| 3239 | #endif |
| 3240 | |
| 3241 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3242 | bool needs_lock) |
| 3243 | { |
| 3244 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3245 | void __user *buf; |
| 3246 | ssize_t len; |
| 3247 | |
| 3248 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3249 | return -EFAULT; |
| 3250 | |
| 3251 | len = iov[0].iov_len; |
| 3252 | if (len < 0) |
| 3253 | return -EINVAL; |
| 3254 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3255 | if (IS_ERR(buf)) |
| 3256 | return PTR_ERR(buf); |
| 3257 | iov[0].iov_base = buf; |
| 3258 | iov[0].iov_len = len; |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
| 3262 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3263 | bool needs_lock) |
| 3264 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3265 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3266 | struct io_buffer *kbuf; |
| 3267 | |
| 3268 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3269 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3270 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3271 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3272 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3273 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3274 | return -EINVAL; |
| 3275 | |
| 3276 | #ifdef CONFIG_COMPAT |
| 3277 | if (req->ctx->compat) |
| 3278 | return io_compat_import(req, iov, needs_lock); |
| 3279 | #endif |
| 3280 | |
| 3281 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3282 | } |
| 3283 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3284 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3285 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3286 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3287 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3288 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3289 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3290 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3291 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3292 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3293 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3294 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3295 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3296 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3297 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3298 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3299 | return -EINVAL; |
| 3300 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3301 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3302 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3303 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3304 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3305 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3306 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3309 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3310 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3311 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3314 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3315 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3316 | if (!ret) |
| 3317 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3318 | *iovec = NULL; |
| 3319 | return ret; |
| 3320 | } |
| 3321 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3322 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3323 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3326 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3327 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3328 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3329 | } |
| 3330 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3331 | /* |
| 3332 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3333 | * by looping over ->read() or ->write() manually. |
| 3334 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3335 | 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] | 3336 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3337 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3338 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3339 | ssize_t ret = 0; |
| 3340 | |
| 3341 | /* |
| 3342 | * Don't support polled IO through this interface, and we can't |
| 3343 | * support non-blocking either. For the latter, this just causes |
| 3344 | * the kiocb to be handled from an async context. |
| 3345 | */ |
| 3346 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3347 | return -EOPNOTSUPP; |
| 3348 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3349 | return -EAGAIN; |
| 3350 | |
| 3351 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3352 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3353 | ssize_t nr; |
| 3354 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3355 | if (!iov_iter_is_bvec(iter)) { |
| 3356 | iovec = iov_iter_iovec(iter); |
| 3357 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3358 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3359 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3360 | } |
| 3361 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3362 | if (rw == READ) { |
| 3363 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3364 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3365 | } else { |
| 3366 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3367 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | if (nr < 0) { |
| 3371 | if (!ret) |
| 3372 | ret = nr; |
| 3373 | break; |
| 3374 | } |
| 3375 | ret += nr; |
| 3376 | if (nr != iovec.iov_len) |
| 3377 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3378 | req->rw.len -= nr; |
| 3379 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3380 | iov_iter_advance(iter, nr); |
| 3381 | } |
| 3382 | |
| 3383 | return ret; |
| 3384 | } |
| 3385 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3386 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3387 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3388 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3389 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3390 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3391 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3392 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3393 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3394 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3395 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3396 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3397 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3398 | unsigned iov_off = 0; |
| 3399 | |
| 3400 | rw->iter.iov = rw->fast_iov; |
| 3401 | if (iter->iov != fast_iov) { |
| 3402 | iov_off = iter->iov - fast_iov; |
| 3403 | rw->iter.iov += iov_off; |
| 3404 | } |
| 3405 | if (rw->fast_iov != fast_iov) |
| 3406 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3407 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3408 | } else { |
| 3409 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3413 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3414 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3415 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3416 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3417 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3418 | } |
| 3419 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3420 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3421 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3422 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3423 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3424 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3425 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3428 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3429 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3430 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3431 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3432 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3433 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3434 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3435 | if (__io_alloc_async_data(req)) { |
| 3436 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3437 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3438 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3439 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3440 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3441 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3442 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3445 | 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] | 3446 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3447 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3448 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3449 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3450 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3451 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3452 | if (unlikely(ret < 0)) |
| 3453 | return ret; |
| 3454 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3455 | iorw->bytes_done = 0; |
| 3456 | iorw->free_iovec = iov; |
| 3457 | if (iov) |
| 3458 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3459 | return 0; |
| 3460 | } |
| 3461 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3462 | 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] | 3463 | { |
| 3464 | ssize_t ret; |
| 3465 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3466 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3467 | if (ret) |
| 3468 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3469 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3470 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3471 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3472 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3473 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3474 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3475 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3476 | return io_rw_prep_async(req, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3479 | /* |
| 3480 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3481 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3482 | * This gets called when the page is unlocked, and we generally expect that to |
| 3483 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3484 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3485 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3486 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3487 | * slow path. |
| 3488 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3489 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3490 | int sync, void *arg) |
| 3491 | { |
| 3492 | struct wait_page_queue *wpq; |
| 3493 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3494 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3495 | |
| 3496 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3497 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3498 | if (!wake_page_match(wpq, key)) |
| 3499 | return 0; |
| 3500 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3501 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3502 | list_del_init(&wait->entry); |
| 3503 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3504 | /* submit ref gets dropped, acquire a new one */ |
| 3505 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3506 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3507 | return 1; |
| 3508 | } |
| 3509 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3510 | /* |
| 3511 | * This controls whether a given IO request should be armed for async page |
| 3512 | * based retry. If we return false here, the request is handed to the async |
| 3513 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3514 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3515 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3516 | * will register a callback when the page is unlocked at IO completion. Through |
| 3517 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3518 | * That retry will attempt the buffered read again. The retry will generally |
| 3519 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3520 | * async worker threads for a blocking retry. |
| 3521 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3522 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3523 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3524 | struct io_async_rw *rw = req->async_data; |
| 3525 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3526 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3527 | |
| 3528 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3529 | if (req->flags & REQ_F_NOWAIT) |
| 3530 | return false; |
| 3531 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3532 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3533 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3534 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3535 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3536 | /* |
| 3537 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3538 | * support callback based unlocks |
| 3539 | */ |
| 3540 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3541 | return false; |
| 3542 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3543 | wait->wait.func = io_async_buf_func; |
| 3544 | wait->wait.private = req; |
| 3545 | wait->wait.flags = 0; |
| 3546 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3547 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3548 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3549 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3550 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3554 | { |
| 3555 | if (req->file->f_op->read_iter) |
| 3556 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3557 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3558 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3559 | else |
| 3560 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3561 | } |
| 3562 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3563 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3564 | { |
| 3565 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3566 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3567 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3568 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3569 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3570 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3571 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3572 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3573 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3574 | iovec = NULL; |
| 3575 | } else { |
| 3576 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3577 | if (ret < 0) |
| 3578 | return ret; |
| 3579 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3580 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3581 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3582 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3583 | /* Ensure we clear previously set non-block flag */ |
| 3584 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3585 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3586 | else |
| 3587 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3588 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3589 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3590 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3591 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3592 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3593 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3594 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3595 | 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] | 3596 | if (unlikely(ret)) { |
| 3597 | kfree(iovec); |
| 3598 | return ret; |
| 3599 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3600 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3601 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3602 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3603 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3604 | /* it's faster to check here then delegate to kfree */ |
| 3605 | if (iovec) |
| 3606 | kfree(iovec); |
| 3607 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3608 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3609 | /* IOPOLL retry should happen for io-wq threads */ |
| 3610 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3611 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3612 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3613 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3614 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3615 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3616 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3617 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3618 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3619 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3620 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3621 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3622 | } |
| 3623 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3624 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3625 | if (ret2) |
| 3626 | return ret2; |
| 3627 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3628 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3629 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3630 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3631 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3632 | do { |
| 3633 | io_size -= ret; |
| 3634 | rw->bytes_done += ret; |
| 3635 | /* if we can retry, do so with the callbacks armed */ |
| 3636 | if (!io_rw_should_retry(req)) { |
| 3637 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3638 | return -EAGAIN; |
| 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3643 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3644 | * desired page gets unlocked. We can also get a partial read |
| 3645 | * here, and if we do, then just retry at the new offset. |
| 3646 | */ |
| 3647 | ret = io_iter_do_read(req, iter); |
| 3648 | if (ret == -EIOCBQUEUED) |
| 3649 | return 0; |
| 3650 | /* we got some bytes, but not all. retry. */ |
| 3651 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3652 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3653 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3654 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3657 | 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] | 3658 | { |
| 3659 | ssize_t ret; |
| 3660 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3661 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3662 | if (ret) |
| 3663 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3664 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3665 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3666 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3667 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3668 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3669 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3670 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3671 | return io_rw_prep_async(req, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3672 | } |
| 3673 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3674 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3675 | { |
| 3676 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3677 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3678 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3679 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3680 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3681 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3682 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3683 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3684 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3685 | iovec = NULL; |
| 3686 | } else { |
| 3687 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3688 | if (ret < 0) |
| 3689 | return ret; |
| 3690 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3691 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3692 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3693 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3694 | /* Ensure we clear previously set non-block flag */ |
| 3695 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3696 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3697 | else |
| 3698 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3699 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3700 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3701 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3702 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3703 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3704 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3705 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3706 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3707 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3708 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3709 | 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] | 3710 | if (unlikely(ret)) |
| 3711 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3712 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3713 | /* |
| 3714 | * Open-code file_start_write here to grab freeze protection, |
| 3715 | * which will be released by another thread in |
| 3716 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3717 | * released so that it doesn't complain about the held lock when |
| 3718 | * we return to userspace. |
| 3719 | */ |
| 3720 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3721 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3722 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3723 | SB_FREEZE_WRITE); |
| 3724 | } |
| 3725 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3726 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3727 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3728 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3729 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3730 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3731 | else |
| 3732 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3733 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3734 | /* |
| 3735 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3736 | * retry them without IOCB_NOWAIT. |
| 3737 | */ |
| 3738 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3739 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3740 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3741 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3742 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3743 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3744 | /* IOPOLL retry should happen for io-wq threads */ |
| 3745 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3746 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3747 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3748 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3749 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3750 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3751 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3752 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3753 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3754 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3755 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3756 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3757 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3758 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3759 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3760 | return ret; |
| 3761 | } |
| 3762 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3763 | static int io_renameat_prep(struct io_kiocb *req, |
| 3764 | const struct io_uring_sqe *sqe) |
| 3765 | { |
| 3766 | struct io_rename *ren = &req->rename; |
| 3767 | const char __user *oldf, *newf; |
| 3768 | |
| 3769 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3770 | return -EBADF; |
| 3771 | |
| 3772 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3773 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3774 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3775 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3776 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3777 | |
| 3778 | ren->oldpath = getname(oldf); |
| 3779 | if (IS_ERR(ren->oldpath)) |
| 3780 | return PTR_ERR(ren->oldpath); |
| 3781 | |
| 3782 | ren->newpath = getname(newf); |
| 3783 | if (IS_ERR(ren->newpath)) { |
| 3784 | putname(ren->oldpath); |
| 3785 | return PTR_ERR(ren->newpath); |
| 3786 | } |
| 3787 | |
| 3788 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3789 | return 0; |
| 3790 | } |
| 3791 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3792 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3793 | { |
| 3794 | struct io_rename *ren = &req->rename; |
| 3795 | int ret; |
| 3796 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3797 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3798 | return -EAGAIN; |
| 3799 | |
| 3800 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3801 | ren->newpath, ren->flags); |
| 3802 | |
| 3803 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3804 | if (ret < 0) |
| 3805 | req_set_fail_links(req); |
| 3806 | io_req_complete(req, ret); |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3810 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3811 | const struct io_uring_sqe *sqe) |
| 3812 | { |
| 3813 | struct io_unlink *un = &req->unlink; |
| 3814 | const char __user *fname; |
| 3815 | |
| 3816 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3817 | return -EBADF; |
| 3818 | |
| 3819 | un->dfd = READ_ONCE(sqe->fd); |
| 3820 | |
| 3821 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3822 | if (un->flags & ~AT_REMOVEDIR) |
| 3823 | return -EINVAL; |
| 3824 | |
| 3825 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3826 | un->filename = getname(fname); |
| 3827 | if (IS_ERR(un->filename)) |
| 3828 | return PTR_ERR(un->filename); |
| 3829 | |
| 3830 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3831 | return 0; |
| 3832 | } |
| 3833 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3834 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3835 | { |
| 3836 | struct io_unlink *un = &req->unlink; |
| 3837 | int ret; |
| 3838 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3839 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3840 | return -EAGAIN; |
| 3841 | |
| 3842 | if (un->flags & AT_REMOVEDIR) |
| 3843 | ret = do_rmdir(un->dfd, un->filename); |
| 3844 | else |
| 3845 | ret = do_unlinkat(un->dfd, un->filename); |
| 3846 | |
| 3847 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3848 | if (ret < 0) |
| 3849 | req_set_fail_links(req); |
| 3850 | io_req_complete(req, ret); |
| 3851 | return 0; |
| 3852 | } |
| 3853 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3854 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3855 | const struct io_uring_sqe *sqe) |
| 3856 | { |
| 3857 | #if defined(CONFIG_NET) |
| 3858 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3859 | return -EINVAL; |
| 3860 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3861 | sqe->buf_index) |
| 3862 | return -EINVAL; |
| 3863 | |
| 3864 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3865 | return 0; |
| 3866 | #else |
| 3867 | return -EOPNOTSUPP; |
| 3868 | #endif |
| 3869 | } |
| 3870 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3871 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3872 | { |
| 3873 | #if defined(CONFIG_NET) |
| 3874 | struct socket *sock; |
| 3875 | int ret; |
| 3876 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3877 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3878 | return -EAGAIN; |
| 3879 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3880 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3881 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3882 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3883 | |
| 3884 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3885 | if (ret < 0) |
| 3886 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3887 | io_req_complete(req, ret); |
| 3888 | return 0; |
| 3889 | #else |
| 3890 | return -EOPNOTSUPP; |
| 3891 | #endif |
| 3892 | } |
| 3893 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3894 | static int __io_splice_prep(struct io_kiocb *req, |
| 3895 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3896 | { |
| 3897 | struct io_splice* sp = &req->splice; |
| 3898 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3899 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3900 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3901 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3902 | |
| 3903 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3904 | sp->len = READ_ONCE(sqe->len); |
| 3905 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3906 | |
| 3907 | if (unlikely(sp->flags & ~valid_flags)) |
| 3908 | return -EINVAL; |
| 3909 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3910 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3911 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3912 | if (!sp->file_in) |
| 3913 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3914 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3915 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3916 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3917 | /* |
| 3918 | * Splice operation will be punted aync, and here need to |
| 3919 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3920 | */ |
| 3921 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3922 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3923 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3924 | |
| 3925 | return 0; |
| 3926 | } |
| 3927 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3928 | static int io_tee_prep(struct io_kiocb *req, |
| 3929 | const struct io_uring_sqe *sqe) |
| 3930 | { |
| 3931 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3932 | return -EINVAL; |
| 3933 | return __io_splice_prep(req, sqe); |
| 3934 | } |
| 3935 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3936 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3937 | { |
| 3938 | struct io_splice *sp = &req->splice; |
| 3939 | struct file *in = sp->file_in; |
| 3940 | struct file *out = sp->file_out; |
| 3941 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3942 | long ret = 0; |
| 3943 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3944 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3945 | return -EAGAIN; |
| 3946 | if (sp->len) |
| 3947 | ret = do_tee(in, out, sp->len, flags); |
| 3948 | |
| 3949 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3950 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3951 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3952 | if (ret != sp->len) |
| 3953 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3954 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3955 | return 0; |
| 3956 | } |
| 3957 | |
| 3958 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3959 | { |
| 3960 | struct io_splice* sp = &req->splice; |
| 3961 | |
| 3962 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3963 | sp->off_out = READ_ONCE(sqe->off); |
| 3964 | return __io_splice_prep(req, sqe); |
| 3965 | } |
| 3966 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3967 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3968 | { |
| 3969 | struct io_splice *sp = &req->splice; |
| 3970 | struct file *in = sp->file_in; |
| 3971 | struct file *out = sp->file_out; |
| 3972 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3973 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3974 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3975 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3976 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3977 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3978 | |
| 3979 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3980 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3981 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3982 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3983 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3984 | |
| 3985 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3986 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3987 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3988 | if (ret != sp->len) |
| 3989 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3990 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3991 | return 0; |
| 3992 | } |
| 3993 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3994 | /* |
| 3995 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3996 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3997 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3998 | { |
| 3999 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4000 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4001 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4002 | return -EINVAL; |
| 4003 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4004 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4005 | return 0; |
| 4006 | } |
| 4007 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4008 | 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] | 4009 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4010 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4011 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4012 | if (!req->file) |
| 4013 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4014 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4015 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4016 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4017 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4018 | return -EINVAL; |
| 4019 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4020 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4021 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4022 | return -EINVAL; |
| 4023 | |
| 4024 | req->sync.off = READ_ONCE(sqe->off); |
| 4025 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4026 | return 0; |
| 4027 | } |
| 4028 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4029 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4030 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4031 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4032 | int ret; |
| 4033 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4034 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4035 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4036 | return -EAGAIN; |
| 4037 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4038 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4039 | end > 0 ? end : LLONG_MAX, |
| 4040 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4041 | if (ret < 0) |
| 4042 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4043 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4044 | return 0; |
| 4045 | } |
| 4046 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4047 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4048 | const struct io_uring_sqe *sqe) |
| 4049 | { |
| 4050 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 4051 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4052 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4053 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4054 | |
| 4055 | req->sync.off = READ_ONCE(sqe->off); |
| 4056 | req->sync.len = READ_ONCE(sqe->addr); |
| 4057 | req->sync.mode = READ_ONCE(sqe->len); |
| 4058 | return 0; |
| 4059 | } |
| 4060 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4061 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4062 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4063 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4064 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4065 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4066 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4067 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4068 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4069 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4070 | if (ret < 0) |
| 4071 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4072 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4073 | return 0; |
| 4074 | } |
| 4075 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4076 | 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] | 4077 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4078 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4079 | int ret; |
| 4080 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4081 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4082 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4083 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4084 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4085 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4086 | /* open.how should be already initialised */ |
| 4087 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4088 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4089 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4090 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4091 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4092 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4093 | if (IS_ERR(req->open.filename)) { |
| 4094 | ret = PTR_ERR(req->open.filename); |
| 4095 | req->open.filename = NULL; |
| 4096 | return ret; |
| 4097 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4098 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4099 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4100 | return 0; |
| 4101 | } |
| 4102 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4103 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4104 | { |
| 4105 | u64 flags, mode; |
| 4106 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4107 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4108 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4109 | mode = READ_ONCE(sqe->len); |
| 4110 | flags = READ_ONCE(sqe->open_flags); |
| 4111 | req->open.how = build_open_how(flags, mode); |
| 4112 | return __io_openat_prep(req, sqe); |
| 4113 | } |
| 4114 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4115 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4116 | { |
| 4117 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4118 | size_t len; |
| 4119 | int ret; |
| 4120 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4121 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4122 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4123 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4124 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4125 | if (len < OPEN_HOW_SIZE_VER0) |
| 4126 | return -EINVAL; |
| 4127 | |
| 4128 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4129 | len); |
| 4130 | if (ret) |
| 4131 | return ret; |
| 4132 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4133 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4136 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4137 | { |
| 4138 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4139 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4140 | bool nonblock_set; |
| 4141 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4142 | int ret; |
| 4143 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4144 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4145 | if (ret) |
| 4146 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4147 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4148 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4149 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4150 | /* |
| 4151 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4152 | * it'll always -EAGAIN |
| 4153 | */ |
| 4154 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4155 | return -EAGAIN; |
| 4156 | op.lookup_flags |= LOOKUP_CACHED; |
| 4157 | op.open_flag |= O_NONBLOCK; |
| 4158 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4159 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4160 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4161 | if (ret < 0) |
| 4162 | goto err; |
| 4163 | |
| 4164 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4165 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4166 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 4167 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4168 | /* |
| 4169 | * We could hang on to this 'fd', but seems like marginal |
| 4170 | * gain for something that is now known to be a slower path. |
| 4171 | * So just put it, and we'll get a new one when we retry. |
| 4172 | */ |
| 4173 | put_unused_fd(ret); |
| 4174 | return -EAGAIN; |
| 4175 | } |
| 4176 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4177 | if (IS_ERR(file)) { |
| 4178 | put_unused_fd(ret); |
| 4179 | ret = PTR_ERR(file); |
| 4180 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4181 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4182 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4183 | fsnotify_open(file); |
| 4184 | fd_install(ret, file); |
| 4185 | } |
| 4186 | err: |
| 4187 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4188 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4189 | if (ret < 0) |
| 4190 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4191 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4192 | return 0; |
| 4193 | } |
| 4194 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4195 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4196 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4197 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4198 | } |
| 4199 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4200 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4201 | const struct io_uring_sqe *sqe) |
| 4202 | { |
| 4203 | struct io_provide_buf *p = &req->pbuf; |
| 4204 | u64 tmp; |
| 4205 | |
| 4206 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4207 | return -EINVAL; |
| 4208 | |
| 4209 | tmp = READ_ONCE(sqe->fd); |
| 4210 | if (!tmp || tmp > USHRT_MAX) |
| 4211 | return -EINVAL; |
| 4212 | |
| 4213 | memset(p, 0, sizeof(*p)); |
| 4214 | p->nbufs = tmp; |
| 4215 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
| 4219 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4220 | int bgid, unsigned nbufs) |
| 4221 | { |
| 4222 | unsigned i = 0; |
| 4223 | |
| 4224 | /* shouldn't happen */ |
| 4225 | if (!nbufs) |
| 4226 | return 0; |
| 4227 | |
| 4228 | /* the head kbuf is the list itself */ |
| 4229 | while (!list_empty(&buf->list)) { |
| 4230 | struct io_buffer *nxt; |
| 4231 | |
| 4232 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4233 | list_del(&nxt->list); |
| 4234 | kfree(nxt); |
| 4235 | if (++i == nbufs) |
| 4236 | return i; |
| 4237 | } |
| 4238 | i++; |
| 4239 | kfree(buf); |
| 4240 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4241 | |
| 4242 | return i; |
| 4243 | } |
| 4244 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4245 | 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] | 4246 | { |
| 4247 | struct io_provide_buf *p = &req->pbuf; |
| 4248 | struct io_ring_ctx *ctx = req->ctx; |
| 4249 | struct io_buffer *head; |
| 4250 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4251 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4252 | |
| 4253 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4254 | |
| 4255 | lockdep_assert_held(&ctx->uring_lock); |
| 4256 | |
| 4257 | ret = -ENOENT; |
| 4258 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4259 | if (head) |
| 4260 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4261 | if (ret < 0) |
| 4262 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4263 | |
| 4264 | /* need to hold the lock to complete IOPOLL requests */ |
| 4265 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4266 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4267 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4268 | } else { |
| 4269 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4270 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4271 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4272 | return 0; |
| 4273 | } |
| 4274 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4275 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4276 | const struct io_uring_sqe *sqe) |
| 4277 | { |
| 4278 | struct io_provide_buf *p = &req->pbuf; |
| 4279 | u64 tmp; |
| 4280 | |
| 4281 | if (sqe->ioprio || sqe->rw_flags) |
| 4282 | return -EINVAL; |
| 4283 | |
| 4284 | tmp = READ_ONCE(sqe->fd); |
| 4285 | if (!tmp || tmp > USHRT_MAX) |
| 4286 | return -E2BIG; |
| 4287 | p->nbufs = tmp; |
| 4288 | p->addr = READ_ONCE(sqe->addr); |
| 4289 | p->len = READ_ONCE(sqe->len); |
| 4290 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4291 | 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] | 4292 | return -EFAULT; |
| 4293 | |
| 4294 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4295 | tmp = READ_ONCE(sqe->off); |
| 4296 | if (tmp > USHRT_MAX) |
| 4297 | return -E2BIG; |
| 4298 | p->bid = tmp; |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4303 | { |
| 4304 | struct io_buffer *buf; |
| 4305 | u64 addr = pbuf->addr; |
| 4306 | int i, bid = pbuf->bid; |
| 4307 | |
| 4308 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4309 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4310 | if (!buf) |
| 4311 | break; |
| 4312 | |
| 4313 | buf->addr = addr; |
| 4314 | buf->len = pbuf->len; |
| 4315 | buf->bid = bid; |
| 4316 | addr += pbuf->len; |
| 4317 | bid++; |
| 4318 | if (!*head) { |
| 4319 | INIT_LIST_HEAD(&buf->list); |
| 4320 | *head = buf; |
| 4321 | } else { |
| 4322 | list_add_tail(&buf->list, &(*head)->list); |
| 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | return i ? i : -ENOMEM; |
| 4327 | } |
| 4328 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4329 | 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] | 4330 | { |
| 4331 | struct io_provide_buf *p = &req->pbuf; |
| 4332 | struct io_ring_ctx *ctx = req->ctx; |
| 4333 | struct io_buffer *head, *list; |
| 4334 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4335 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4336 | |
| 4337 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4338 | |
| 4339 | lockdep_assert_held(&ctx->uring_lock); |
| 4340 | |
| 4341 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4342 | |
| 4343 | ret = io_add_buffers(p, &head); |
| 4344 | if (ret < 0) |
| 4345 | goto out; |
| 4346 | |
| 4347 | if (!list) { |
| 4348 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4349 | GFP_KERNEL); |
| 4350 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4351 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4352 | goto out; |
| 4353 | } |
| 4354 | } |
| 4355 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4356 | if (ret < 0) |
| 4357 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4358 | |
| 4359 | /* need to hold the lock to complete IOPOLL requests */ |
| 4360 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4361 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4362 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4363 | } else { |
| 4364 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4365 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4366 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4367 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4370 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4371 | const struct io_uring_sqe *sqe) |
| 4372 | { |
| 4373 | #if defined(CONFIG_EPOLL) |
| 4374 | if (sqe->ioprio || sqe->buf_index) |
| 4375 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4376 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4377 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4378 | |
| 4379 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4380 | req->epoll.op = READ_ONCE(sqe->len); |
| 4381 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4382 | |
| 4383 | if (ep_op_has_event(req->epoll.op)) { |
| 4384 | struct epoll_event __user *ev; |
| 4385 | |
| 4386 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4387 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4388 | return -EFAULT; |
| 4389 | } |
| 4390 | |
| 4391 | return 0; |
| 4392 | #else |
| 4393 | return -EOPNOTSUPP; |
| 4394 | #endif |
| 4395 | } |
| 4396 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4397 | 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] | 4398 | { |
| 4399 | #if defined(CONFIG_EPOLL) |
| 4400 | struct io_epoll *ie = &req->epoll; |
| 4401 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4402 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4403 | |
| 4404 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4405 | if (force_nonblock && ret == -EAGAIN) |
| 4406 | return -EAGAIN; |
| 4407 | |
| 4408 | if (ret < 0) |
| 4409 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4410 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4411 | return 0; |
| 4412 | #else |
| 4413 | return -EOPNOTSUPP; |
| 4414 | #endif |
| 4415 | } |
| 4416 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4417 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4418 | { |
| 4419 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4420 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4421 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4422 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4423 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4424 | |
| 4425 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4426 | req->madvise.len = READ_ONCE(sqe->len); |
| 4427 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4428 | return 0; |
| 4429 | #else |
| 4430 | return -EOPNOTSUPP; |
| 4431 | #endif |
| 4432 | } |
| 4433 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4434 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4435 | { |
| 4436 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4437 | struct io_madvise *ma = &req->madvise; |
| 4438 | int ret; |
| 4439 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4440 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4441 | return -EAGAIN; |
| 4442 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4443 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4444 | if (ret < 0) |
| 4445 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4446 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4447 | return 0; |
| 4448 | #else |
| 4449 | return -EOPNOTSUPP; |
| 4450 | #endif |
| 4451 | } |
| 4452 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4453 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4454 | { |
| 4455 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4456 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4457 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4458 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4459 | |
| 4460 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4461 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4462 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4463 | return 0; |
| 4464 | } |
| 4465 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4466 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4467 | { |
| 4468 | struct io_fadvise *fa = &req->fadvise; |
| 4469 | int ret; |
| 4470 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4471 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4472 | switch (fa->advice) { |
| 4473 | case POSIX_FADV_NORMAL: |
| 4474 | case POSIX_FADV_RANDOM: |
| 4475 | case POSIX_FADV_SEQUENTIAL: |
| 4476 | break; |
| 4477 | default: |
| 4478 | return -EAGAIN; |
| 4479 | } |
| 4480 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4481 | |
| 4482 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4483 | if (ret < 0) |
| 4484 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4485 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4486 | return 0; |
| 4487 | } |
| 4488 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4489 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4490 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4491 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4492 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4493 | if (sqe->ioprio || sqe->buf_index) |
| 4494 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4495 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4496 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4497 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4498 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4499 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4500 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4501 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4502 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4503 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4504 | return 0; |
| 4505 | } |
| 4506 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4507 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4508 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4509 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4510 | int ret; |
| 4511 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4512 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4513 | /* only need file table for an actual valid fd */ |
| 4514 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4515 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4516 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4517 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4518 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4519 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4520 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4521 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4522 | if (ret < 0) |
| 4523 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4524 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4525 | return 0; |
| 4526 | } |
| 4527 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4528 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4529 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4530 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4531 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4532 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4533 | sqe->rw_flags || sqe->buf_index) |
| 4534 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4535 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4536 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4537 | |
| 4538 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4539 | return 0; |
| 4540 | } |
| 4541 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4542 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4543 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4544 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4545 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4546 | struct fdtable *fdt; |
| 4547 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4548 | int ret; |
| 4549 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4550 | file = NULL; |
| 4551 | ret = -EBADF; |
| 4552 | spin_lock(&files->file_lock); |
| 4553 | fdt = files_fdtable(files); |
| 4554 | if (close->fd >= fdt->max_fds) { |
| 4555 | spin_unlock(&files->file_lock); |
| 4556 | goto err; |
| 4557 | } |
| 4558 | file = fdt->fd[close->fd]; |
| 4559 | if (!file) { |
| 4560 | spin_unlock(&files->file_lock); |
| 4561 | goto err; |
| 4562 | } |
| 4563 | |
| 4564 | if (file->f_op == &io_uring_fops) { |
| 4565 | spin_unlock(&files->file_lock); |
| 4566 | file = NULL; |
| 4567 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4568 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4569 | |
| 4570 | /* 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] | 4571 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4572 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4573 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4574 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4575 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4576 | ret = __close_fd_get_file(close->fd, &file); |
| 4577 | spin_unlock(&files->file_lock); |
| 4578 | if (ret < 0) { |
| 4579 | if (ret == -ENOENT) |
| 4580 | ret = -EBADF; |
| 4581 | goto err; |
| 4582 | } |
| 4583 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4584 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4585 | ret = filp_close(file, current->files); |
| 4586 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4587 | if (ret < 0) |
| 4588 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4589 | if (file) |
| 4590 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4591 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4592 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4593 | } |
| 4594 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4595 | 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] | 4596 | { |
| 4597 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4598 | |
| 4599 | if (!req->file) |
| 4600 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4601 | |
| 4602 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4603 | return -EINVAL; |
| 4604 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4605 | return -EINVAL; |
| 4606 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4607 | req->sync.off = READ_ONCE(sqe->off); |
| 4608 | req->sync.len = READ_ONCE(sqe->len); |
| 4609 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4610 | return 0; |
| 4611 | } |
| 4612 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4613 | 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] | 4614 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4615 | int ret; |
| 4616 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4617 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4618 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4619 | return -EAGAIN; |
| 4620 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4621 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4622 | req->sync.flags); |
| 4623 | if (ret < 0) |
| 4624 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4625 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4626 | return 0; |
| 4627 | } |
| 4628 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4629 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4630 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4631 | struct io_async_msghdr *kmsg) |
| 4632 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4633 | struct io_async_msghdr *async_msg = req->async_data; |
| 4634 | |
| 4635 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4636 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4637 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4638 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4639 | return -ENOMEM; |
| 4640 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4641 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4642 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4643 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4644 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4645 | /* if were using fast_iov, set it to the new one */ |
| 4646 | if (!async_msg->free_iov) |
| 4647 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4648 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4649 | return -EAGAIN; |
| 4650 | } |
| 4651 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4652 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4653 | struct io_async_msghdr *iomsg) |
| 4654 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4655 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4656 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4657 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4658 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4659 | } |
| 4660 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4661 | 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] | 4662 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4663 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4664 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4665 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4666 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4667 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4668 | return -EINVAL; |
| 4669 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4670 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4671 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4672 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4673 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4674 | #ifdef CONFIG_COMPAT |
| 4675 | if (req->ctx->compat) |
| 4676 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4677 | #endif |
| 4678 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4679 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4680 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4681 | ret = io_sendmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4682 | if (!ret) |
| 4683 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4684 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4687 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4688 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4689 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4690 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4691 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4692 | int ret; |
| 4693 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4694 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4695 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4696 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4697 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4698 | kmsg = req->async_data; |
| 4699 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4700 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4701 | if (ret) |
| 4702 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4703 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4706 | flags = req->sr_msg.msg_flags; |
| 4707 | if (flags & MSG_DONTWAIT) |
| 4708 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4709 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4710 | flags |= MSG_DONTWAIT; |
| 4711 | |
| 4712 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4713 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4714 | return io_setup_async_msg(req, kmsg); |
| 4715 | if (ret == -ERESTARTSYS) |
| 4716 | ret = -EINTR; |
| 4717 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4718 | /* fast path, check for non-NULL to avoid function call */ |
| 4719 | if (kmsg->free_iov) |
| 4720 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4721 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4722 | if (ret < 0) |
| 4723 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4724 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4725 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4728 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4729 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4730 | struct io_sr_msg *sr = &req->sr_msg; |
| 4731 | struct msghdr msg; |
| 4732 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4733 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4734 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4735 | int ret; |
| 4736 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4737 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4738 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4739 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4740 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4741 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4742 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4743 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4744 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4745 | msg.msg_name = NULL; |
| 4746 | msg.msg_control = NULL; |
| 4747 | msg.msg_controllen = 0; |
| 4748 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4749 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4750 | flags = req->sr_msg.msg_flags; |
| 4751 | if (flags & MSG_DONTWAIT) |
| 4752 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4753 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4754 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4755 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4756 | msg.msg_flags = flags; |
| 4757 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4758 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4759 | return -EAGAIN; |
| 4760 | if (ret == -ERESTARTSYS) |
| 4761 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4762 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4763 | if (ret < 0) |
| 4764 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4765 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4766 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4769 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4770 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4771 | { |
| 4772 | struct io_sr_msg *sr = &req->sr_msg; |
| 4773 | struct iovec __user *uiov; |
| 4774 | size_t iov_len; |
| 4775 | int ret; |
| 4776 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4777 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4778 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4779 | if (ret) |
| 4780 | return ret; |
| 4781 | |
| 4782 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4783 | if (iov_len > 1) |
| 4784 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4785 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4786 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4787 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4788 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4789 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4790 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4791 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4792 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4793 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4794 | if (ret > 0) |
| 4795 | ret = 0; |
| 4796 | } |
| 4797 | |
| 4798 | return ret; |
| 4799 | } |
| 4800 | |
| 4801 | #ifdef CONFIG_COMPAT |
| 4802 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4803 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4804 | { |
| 4805 | struct compat_msghdr __user *msg_compat; |
| 4806 | struct io_sr_msg *sr = &req->sr_msg; |
| 4807 | struct compat_iovec __user *uiov; |
| 4808 | compat_uptr_t ptr; |
| 4809 | compat_size_t len; |
| 4810 | int ret; |
| 4811 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4812 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4813 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4814 | &ptr, &len); |
| 4815 | if (ret) |
| 4816 | return ret; |
| 4817 | |
| 4818 | uiov = compat_ptr(ptr); |
| 4819 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4820 | compat_ssize_t clen; |
| 4821 | |
| 4822 | if (len > 1) |
| 4823 | return -EINVAL; |
| 4824 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4825 | return -EFAULT; |
| 4826 | if (__get_user(clen, &uiov->iov_len)) |
| 4827 | return -EFAULT; |
| 4828 | if (clen < 0) |
| 4829 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4830 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4831 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4832 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4833 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4834 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4835 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4836 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4837 | if (ret < 0) |
| 4838 | return ret; |
| 4839 | } |
| 4840 | |
| 4841 | return 0; |
| 4842 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4843 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4844 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4845 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4846 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4847 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4848 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4849 | |
| 4850 | #ifdef CONFIG_COMPAT |
| 4851 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4852 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4853 | #endif |
| 4854 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4855 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4858 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4859 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4860 | { |
| 4861 | struct io_sr_msg *sr = &req->sr_msg; |
| 4862 | struct io_buffer *kbuf; |
| 4863 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4864 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4865 | if (IS_ERR(kbuf)) |
| 4866 | return kbuf; |
| 4867 | |
| 4868 | sr->kbuf = kbuf; |
| 4869 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4870 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4871 | } |
| 4872 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4873 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4874 | { |
| 4875 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4876 | } |
| 4877 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4878 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4879 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4880 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4881 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4882 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4883 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4884 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4885 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4886 | return -EINVAL; |
| 4887 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4888 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4889 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4890 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4891 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4892 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4893 | #ifdef CONFIG_COMPAT |
| 4894 | if (req->ctx->compat) |
| 4895 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4896 | #endif |
| 4897 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4898 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4899 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4900 | ret = io_recvmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4901 | if (!ret) |
| 4902 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4903 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4904 | } |
| 4905 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4906 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4907 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4908 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4909 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4910 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4911 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4912 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4913 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4914 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4915 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4916 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4917 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4918 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4919 | kmsg = req->async_data; |
| 4920 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4921 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4922 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4923 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4924 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4925 | } |
| 4926 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4927 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4928 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4929 | if (IS_ERR(kbuf)) |
| 4930 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4931 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4932 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4933 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4934 | 1, req->sr_msg.len); |
| 4935 | } |
| 4936 | |
| 4937 | flags = req->sr_msg.msg_flags; |
| 4938 | if (flags & MSG_DONTWAIT) |
| 4939 | req->flags |= REQ_F_NOWAIT; |
| 4940 | else if (force_nonblock) |
| 4941 | flags |= MSG_DONTWAIT; |
| 4942 | |
| 4943 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4944 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4945 | if (force_nonblock && ret == -EAGAIN) |
| 4946 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4947 | if (ret == -ERESTARTSYS) |
| 4948 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4949 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4950 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4951 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4952 | /* fast path, check for non-NULL to avoid function call */ |
| 4953 | if (kmsg->free_iov) |
| 4954 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4955 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4956 | if (ret < 0) |
| 4957 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4958 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4959 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4960 | } |
| 4961 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4962 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4963 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4964 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4965 | struct io_sr_msg *sr = &req->sr_msg; |
| 4966 | struct msghdr msg; |
| 4967 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4968 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4969 | struct iovec iov; |
| 4970 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4971 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4972 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4973 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4974 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4975 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4976 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4977 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4978 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4979 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4980 | if (IS_ERR(kbuf)) |
| 4981 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4982 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4983 | } |
| 4984 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4985 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4986 | if (unlikely(ret)) |
| 4987 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4988 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4989 | msg.msg_name = NULL; |
| 4990 | msg.msg_control = NULL; |
| 4991 | msg.msg_controllen = 0; |
| 4992 | msg.msg_namelen = 0; |
| 4993 | msg.msg_iocb = NULL; |
| 4994 | msg.msg_flags = 0; |
| 4995 | |
| 4996 | flags = req->sr_msg.msg_flags; |
| 4997 | if (flags & MSG_DONTWAIT) |
| 4998 | req->flags |= REQ_F_NOWAIT; |
| 4999 | else if (force_nonblock) |
| 5000 | flags |= MSG_DONTWAIT; |
| 5001 | |
| 5002 | ret = sock_recvmsg(sock, &msg, flags); |
| 5003 | if (force_nonblock && ret == -EAGAIN) |
| 5004 | return -EAGAIN; |
| 5005 | if (ret == -ERESTARTSYS) |
| 5006 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5007 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5008 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5009 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5010 | if (ret < 0) |
| 5011 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5012 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5013 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5014 | } |
| 5015 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5016 | 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] | 5017 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5018 | struct io_accept *accept = &req->accept; |
| 5019 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5020 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5021 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 5022 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5023 | return -EINVAL; |
| 5024 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5025 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5026 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5027 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5028 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5029 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5030 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5031 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5032 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5033 | { |
| 5034 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5035 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5036 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5037 | int ret; |
| 5038 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5039 | if (req->file->f_flags & O_NONBLOCK) |
| 5040 | req->flags |= REQ_F_NOWAIT; |
| 5041 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5042 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5043 | accept->addr_len, accept->flags, |
| 5044 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5045 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5046 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5047 | if (ret < 0) { |
| 5048 | if (ret == -ERESTARTSYS) |
| 5049 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5050 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5051 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5052 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5053 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5056 | 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] | 5057 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5058 | struct io_connect *conn = &req->connect; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5059 | struct io_async_connect *io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5060 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5061 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5062 | return -EINVAL; |
| 5063 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5064 | return -EINVAL; |
| 5065 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5066 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5067 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 5068 | |
| 5069 | if (!io) |
| 5070 | return 0; |
| 5071 | |
| 5072 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5073 | &io->address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5074 | } |
| 5075 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5076 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5077 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5078 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5079 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5080 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5081 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5082 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5083 | if (req->async_data) { |
| 5084 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5085 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5086 | ret = move_addr_to_kernel(req->connect.addr, |
| 5087 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5088 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5089 | if (ret) |
| 5090 | goto out; |
| 5091 | io = &__io; |
| 5092 | } |
| 5093 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5094 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5095 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5096 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5097 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5098 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5099 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5100 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5101 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5102 | ret = -ENOMEM; |
| 5103 | goto out; |
| 5104 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5105 | io = req->async_data; |
| 5106 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5107 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5108 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5109 | if (ret == -ERESTARTSYS) |
| 5110 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5111 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5112 | if (ret < 0) |
| 5113 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5114 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5115 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5116 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5117 | #else /* !CONFIG_NET */ |
| 5118 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5119 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5120 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5121 | } |
| 5122 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5123 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5124 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5125 | return -EOPNOTSUPP; |
| 5126 | } |
| 5127 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5128 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5129 | { |
| 5130 | return -EOPNOTSUPP; |
| 5131 | } |
| 5132 | |
| 5133 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 5134 | const struct io_uring_sqe *sqe) |
| 5135 | { |
| 5136 | return -EOPNOTSUPP; |
| 5137 | } |
| 5138 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5139 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5140 | { |
| 5141 | return -EOPNOTSUPP; |
| 5142 | } |
| 5143 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5144 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5145 | { |
| 5146 | return -EOPNOTSUPP; |
| 5147 | } |
| 5148 | |
| 5149 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5150 | { |
| 5151 | return -EOPNOTSUPP; |
| 5152 | } |
| 5153 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5154 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5155 | { |
| 5156 | return -EOPNOTSUPP; |
| 5157 | } |
| 5158 | |
| 5159 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5160 | { |
| 5161 | return -EOPNOTSUPP; |
| 5162 | } |
| 5163 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5164 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5165 | { |
| 5166 | return -EOPNOTSUPP; |
| 5167 | } |
| 5168 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5169 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5170 | struct io_poll_table { |
| 5171 | struct poll_table_struct pt; |
| 5172 | struct io_kiocb *req; |
| 5173 | int error; |
| 5174 | }; |
| 5175 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5176 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5177 | __poll_t mask, task_work_func_t func) |
| 5178 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5179 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5180 | |
| 5181 | /* for instances that support it check for an event match first: */ |
| 5182 | if (mask && !(mask & poll->events)) |
| 5183 | return 0; |
| 5184 | |
| 5185 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5186 | |
| 5187 | list_del_init(&poll->wait.entry); |
| 5188 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5189 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 5190 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5191 | percpu_ref_get(&req->ctx->refs); |
| 5192 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5193 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5194 | * If this fails, then the task is exiting. When a task exits, the |
| 5195 | * work gets canceled, so just cancel this request as well instead |
| 5196 | * of executing it. We can't safely execute it anyway, as we may not |
| 5197 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5198 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5199 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5200 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5201 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 5202 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5203 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5204 | return 1; |
| 5205 | } |
| 5206 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5207 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5208 | __acquires(&req->ctx->completion_lock) |
| 5209 | { |
| 5210 | struct io_ring_ctx *ctx = req->ctx; |
| 5211 | |
| 5212 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5213 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5214 | |
| 5215 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5216 | } |
| 5217 | |
| 5218 | spin_lock_irq(&ctx->completion_lock); |
| 5219 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5220 | add_wait_queue(poll->head, &poll->wait); |
| 5221 | return true; |
| 5222 | } |
| 5223 | |
| 5224 | return false; |
| 5225 | } |
| 5226 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5227 | 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] | 5228 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5229 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5230 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5231 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5232 | return req->apoll->double_poll; |
| 5233 | } |
| 5234 | |
| 5235 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5236 | { |
| 5237 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5238 | return &req->poll; |
| 5239 | return &req->apoll->poll; |
| 5240 | } |
| 5241 | |
| 5242 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5243 | { |
| 5244 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5245 | |
| 5246 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5247 | |
| 5248 | if (poll && poll->head) { |
| 5249 | struct wait_queue_head *head = poll->head; |
| 5250 | |
| 5251 | spin_lock(&head->lock); |
| 5252 | list_del_init(&poll->wait.entry); |
| 5253 | if (poll->wait.private) |
| 5254 | refcount_dec(&req->refs); |
| 5255 | poll->head = NULL; |
| 5256 | spin_unlock(&head->lock); |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5261 | { |
| 5262 | struct io_ring_ctx *ctx = req->ctx; |
| 5263 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5264 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5265 | req->poll.done = true; |
| 5266 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5267 | io_commit_cqring(ctx); |
| 5268 | } |
| 5269 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5270 | static void io_poll_task_func(struct callback_head *cb) |
| 5271 | { |
| 5272 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5273 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5274 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5275 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5276 | if (io_poll_rewait(req, &req->poll)) { |
| 5277 | spin_unlock_irq(&ctx->completion_lock); |
| 5278 | } else { |
| 5279 | hash_del(&req->hash_node); |
| 5280 | io_poll_complete(req, req->result, 0); |
| 5281 | spin_unlock_irq(&ctx->completion_lock); |
| 5282 | |
| 5283 | nxt = io_put_req_find_next(req); |
| 5284 | io_cqring_ev_posted(ctx); |
| 5285 | if (nxt) |
| 5286 | __io_req_task_submit(nxt); |
| 5287 | } |
| 5288 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5289 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5293 | int sync, void *key) |
| 5294 | { |
| 5295 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5296 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5297 | __poll_t mask = key_to_poll(key); |
| 5298 | |
| 5299 | /* for instances that support it check for an event match first: */ |
| 5300 | if (mask && !(mask & poll->events)) |
| 5301 | return 0; |
| 5302 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5303 | list_del_init(&wait->entry); |
| 5304 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5305 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5306 | bool done; |
| 5307 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5308 | spin_lock(&poll->head->lock); |
| 5309 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5310 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5311 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5312 | /* make sure double remove sees this as being gone */ |
| 5313 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5314 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5315 | if (!done) { |
| 5316 | /* use wait func handler, so it matches the rq type */ |
| 5317 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5318 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5319 | } |
| 5320 | refcount_dec(&req->refs); |
| 5321 | return 1; |
| 5322 | } |
| 5323 | |
| 5324 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5325 | wait_queue_func_t wake_func) |
| 5326 | { |
| 5327 | poll->head = NULL; |
| 5328 | poll->done = false; |
| 5329 | poll->canceled = false; |
| 5330 | poll->events = events; |
| 5331 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5332 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5333 | } |
| 5334 | |
| 5335 | 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] | 5336 | struct wait_queue_head *head, |
| 5337 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5338 | { |
| 5339 | struct io_kiocb *req = pt->req; |
| 5340 | |
| 5341 | /* |
| 5342 | * If poll->head is already set, it's because the file being polled |
| 5343 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5344 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5345 | */ |
| 5346 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5347 | struct io_poll_iocb *poll_one = poll; |
| 5348 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5349 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5350 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5351 | pt->error = -EINVAL; |
| 5352 | return; |
| 5353 | } |
| 5354 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5355 | if (!poll) { |
| 5356 | pt->error = -ENOMEM; |
| 5357 | return; |
| 5358 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5359 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5360 | refcount_inc(&req->refs); |
| 5361 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5362 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | pt->error = 0; |
| 5366 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5367 | |
| 5368 | if (poll->events & EPOLLEXCLUSIVE) |
| 5369 | add_wait_queue_exclusive(head, &poll->wait); |
| 5370 | else |
| 5371 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5375 | struct poll_table_struct *p) |
| 5376 | { |
| 5377 | 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] | 5378 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5379 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5380 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5381 | } |
| 5382 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5383 | static void io_async_task_func(struct callback_head *cb) |
| 5384 | { |
| 5385 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5386 | struct async_poll *apoll = req->apoll; |
| 5387 | struct io_ring_ctx *ctx = req->ctx; |
| 5388 | |
| 5389 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5390 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5391 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5392 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5393 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5394 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5395 | } |
| 5396 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5397 | /* 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] | 5398 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5399 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5400 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5401 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5402 | spin_unlock_irq(&ctx->completion_lock); |
| 5403 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5404 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5405 | __io_req_task_submit(req); |
| 5406 | else |
| 5407 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5408 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5409 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5410 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5411 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5415 | void *key) |
| 5416 | { |
| 5417 | struct io_kiocb *req = wait->private; |
| 5418 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5419 | |
| 5420 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5421 | key_to_poll(key)); |
| 5422 | |
| 5423 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5424 | } |
| 5425 | |
| 5426 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5427 | { |
| 5428 | struct io_ring_ctx *ctx = req->ctx; |
| 5429 | struct hlist_head *list; |
| 5430 | |
| 5431 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5432 | hlist_add_head(&req->hash_node, list); |
| 5433 | } |
| 5434 | |
| 5435 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5436 | struct io_poll_iocb *poll, |
| 5437 | struct io_poll_table *ipt, __poll_t mask, |
| 5438 | wait_queue_func_t wake_func) |
| 5439 | __acquires(&ctx->completion_lock) |
| 5440 | { |
| 5441 | struct io_ring_ctx *ctx = req->ctx; |
| 5442 | bool cancel = false; |
| 5443 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5444 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5445 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5446 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5447 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5448 | |
| 5449 | ipt->pt._key = mask; |
| 5450 | ipt->req = req; |
| 5451 | ipt->error = -EINVAL; |
| 5452 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5453 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5454 | |
| 5455 | spin_lock_irq(&ctx->completion_lock); |
| 5456 | if (likely(poll->head)) { |
| 5457 | spin_lock(&poll->head->lock); |
| 5458 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5459 | if (ipt->error) |
| 5460 | cancel = true; |
| 5461 | ipt->error = 0; |
| 5462 | mask = 0; |
| 5463 | } |
| 5464 | if (mask || ipt->error) |
| 5465 | list_del_init(&poll->wait.entry); |
| 5466 | else if (cancel) |
| 5467 | WRITE_ONCE(poll->canceled, true); |
| 5468 | else if (!poll->done) /* actually waiting for an event */ |
| 5469 | io_poll_req_insert(req); |
| 5470 | spin_unlock(&poll->head->lock); |
| 5471 | } |
| 5472 | |
| 5473 | return mask; |
| 5474 | } |
| 5475 | |
| 5476 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5477 | { |
| 5478 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5479 | struct io_ring_ctx *ctx = req->ctx; |
| 5480 | struct async_poll *apoll; |
| 5481 | struct io_poll_table ipt; |
| 5482 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5483 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5484 | |
| 5485 | if (!req->file || !file_can_poll(req->file)) |
| 5486 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5487 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5488 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5489 | if (def->pollin) |
| 5490 | rw = READ; |
| 5491 | else if (def->pollout) |
| 5492 | rw = WRITE; |
| 5493 | else |
| 5494 | return false; |
| 5495 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5496 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5497 | return false; |
| 5498 | |
| 5499 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5500 | if (unlikely(!apoll)) |
| 5501 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5502 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5503 | |
| 5504 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5505 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5506 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5507 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5508 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5509 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5510 | if (def->pollout) |
| 5511 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5512 | |
| 5513 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5514 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5515 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5516 | mask &= ~POLLIN; |
| 5517 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5518 | mask |= POLLERR | POLLPRI; |
| 5519 | |
| 5520 | ipt.pt._qproc = io_async_queue_proc; |
| 5521 | |
| 5522 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5523 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5524 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5525 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5526 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5527 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5528 | kfree(apoll); |
| 5529 | return false; |
| 5530 | } |
| 5531 | spin_unlock_irq(&ctx->completion_lock); |
| 5532 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5533 | apoll->poll.events); |
| 5534 | return true; |
| 5535 | } |
| 5536 | |
| 5537 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5538 | struct io_poll_iocb *poll) |
| 5539 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5540 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5541 | |
| 5542 | spin_lock(&poll->head->lock); |
| 5543 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5544 | if (!list_empty(&poll->wait.entry)) { |
| 5545 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5546 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5547 | } |
| 5548 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5549 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5550 | return do_complete; |
| 5551 | } |
| 5552 | |
| 5553 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5554 | { |
| 5555 | bool do_complete; |
| 5556 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5557 | io_poll_remove_double(req); |
| 5558 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5559 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5560 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5561 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5562 | struct async_poll *apoll = req->apoll; |
| 5563 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5564 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5565 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5566 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5567 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5568 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5569 | kfree(apoll); |
| 5570 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5571 | } |
| 5572 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5573 | if (do_complete) { |
| 5574 | io_cqring_fill_event(req, -ECANCELED); |
| 5575 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5576 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5577 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5581 | } |
| 5582 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5583 | /* |
| 5584 | * Returns true if we found and killed one or more poll requests |
| 5585 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5586 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5587 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5588 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5589 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5590 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5591 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5592 | |
| 5593 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5594 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5595 | struct hlist_head *list; |
| 5596 | |
| 5597 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5598 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5599 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5600 | posted += io_poll_remove_one(req); |
| 5601 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5602 | } |
| 5603 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5604 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5605 | if (posted) |
| 5606 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5607 | |
| 5608 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5609 | } |
| 5610 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5611 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5612 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5613 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5614 | struct io_kiocb *req; |
| 5615 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5616 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5617 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5618 | if (sqe_addr != req->user_data) |
| 5619 | continue; |
| 5620 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5621 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5622 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | return -ENOENT; |
| 5626 | } |
| 5627 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5628 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5629 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5630 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5631 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5632 | return -EINVAL; |
| 5633 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5634 | sqe->poll_events) |
| 5635 | return -EINVAL; |
| 5636 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5637 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5638 | return 0; |
| 5639 | } |
| 5640 | |
| 5641 | /* |
| 5642 | * Find a running poll command that matches one specified in sqe->addr, |
| 5643 | * and remove it if found. |
| 5644 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5645 | 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] | 5646 | { |
| 5647 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5648 | int ret; |
| 5649 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5650 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5651 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5652 | spin_unlock_irq(&ctx->completion_lock); |
| 5653 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5654 | if (ret < 0) |
| 5655 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5656 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5657 | return 0; |
| 5658 | } |
| 5659 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5660 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5661 | void *key) |
| 5662 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5663 | struct io_kiocb *req = wait->private; |
| 5664 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5665 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5666 | 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] | 5667 | } |
| 5668 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5669 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5670 | struct poll_table_struct *p) |
| 5671 | { |
| 5672 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5673 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5674 | __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] | 5675 | } |
| 5676 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5677 | 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] | 5678 | { |
| 5679 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5680 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5681 | |
| 5682 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5683 | return -EINVAL; |
| 5684 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5685 | return -EINVAL; |
| 5686 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5687 | events = READ_ONCE(sqe->poll32_events); |
| 5688 | #ifdef __BIG_ENDIAN |
| 5689 | events = swahw32(events); |
| 5690 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5691 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5692 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5693 | return 0; |
| 5694 | } |
| 5695 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5696 | 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] | 5697 | { |
| 5698 | struct io_poll_iocb *poll = &req->poll; |
| 5699 | struct io_ring_ctx *ctx = req->ctx; |
| 5700 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5701 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5702 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5703 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5704 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5705 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5706 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5707 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5708 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5709 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5710 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5711 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5712 | spin_unlock_irq(&ctx->completion_lock); |
| 5713 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5714 | if (mask) { |
| 5715 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5716 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5717 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5718 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5719 | } |
| 5720 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5721 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5722 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5723 | struct io_timeout_data *data = container_of(timer, |
| 5724 | struct io_timeout_data, timer); |
| 5725 | struct io_kiocb *req = data->req; |
| 5726 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5727 | unsigned long flags; |
| 5728 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5729 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5730 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5731 | atomic_set(&req->ctx->cq_timeouts, |
| 5732 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5733 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5734 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5735 | io_commit_cqring(ctx); |
| 5736 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5737 | |
| 5738 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5739 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5740 | io_put_req(req); |
| 5741 | return HRTIMER_NORESTART; |
| 5742 | } |
| 5743 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5744 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5745 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5746 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5747 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5748 | struct io_kiocb *req; |
| 5749 | int ret = -ENOENT; |
| 5750 | |
| 5751 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5752 | if (user_data == req->user_data) { |
| 5753 | ret = 0; |
| 5754 | break; |
| 5755 | } |
| 5756 | } |
| 5757 | |
| 5758 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5759 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5760 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5761 | io = req->async_data; |
| 5762 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5763 | if (ret == -1) |
| 5764 | return ERR_PTR(-EALREADY); |
| 5765 | list_del_init(&req->timeout.list); |
| 5766 | return req; |
| 5767 | } |
| 5768 | |
| 5769 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5770 | { |
| 5771 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5772 | |
| 5773 | if (IS_ERR(req)) |
| 5774 | return PTR_ERR(req); |
| 5775 | |
| 5776 | req_set_fail_links(req); |
| 5777 | io_cqring_fill_event(req, -ECANCELED); |
| 5778 | io_put_req_deferred(req, 1); |
| 5779 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5780 | } |
| 5781 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5782 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5783 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5784 | { |
| 5785 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5786 | struct io_timeout_data *data; |
| 5787 | |
| 5788 | if (IS_ERR(req)) |
| 5789 | return PTR_ERR(req); |
| 5790 | |
| 5791 | req->timeout.off = 0; /* noseq */ |
| 5792 | data = req->async_data; |
| 5793 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5794 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5795 | data->timer.function = io_timeout_fn; |
| 5796 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5797 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5798 | } |
| 5799 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5800 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5801 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5802 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5803 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5804 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5805 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5806 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5807 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5808 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5809 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5810 | return -EINVAL; |
| 5811 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5812 | tr->addr = READ_ONCE(sqe->addr); |
| 5813 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5814 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5815 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5816 | return -EINVAL; |
| 5817 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5818 | return -EFAULT; |
| 5819 | } else if (tr->flags) { |
| 5820 | /* timeout removal doesn't support flags */ |
| 5821 | return -EINVAL; |
| 5822 | } |
| 5823 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5824 | return 0; |
| 5825 | } |
| 5826 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5827 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5828 | { |
| 5829 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5830 | : HRTIMER_MODE_REL; |
| 5831 | } |
| 5832 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5833 | /* |
| 5834 | * Remove or update an existing timeout command |
| 5835 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5836 | 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] | 5837 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5838 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5839 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5840 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5841 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5842 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5843 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5844 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5845 | else |
| 5846 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5847 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5848 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5849 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5850 | io_commit_cqring(ctx); |
| 5851 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5852 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5853 | if (ret < 0) |
| 5854 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5855 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5856 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5857 | } |
| 5858 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5859 | 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] | 5860 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5861 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5862 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5863 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5864 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5865 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5866 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5867 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5868 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5869 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5870 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5871 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5872 | flags = READ_ONCE(sqe->timeout_flags); |
| 5873 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5874 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5875 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5876 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5877 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5878 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5879 | return -ENOMEM; |
| 5880 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5881 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5882 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5883 | |
| 5884 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5885 | return -EFAULT; |
| 5886 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5887 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5888 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5889 | return 0; |
| 5890 | } |
| 5891 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5892 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5893 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5894 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5895 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5896 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5897 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5898 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5899 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5900 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5901 | /* |
| 5902 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5903 | * timeout event to be satisfied. If it isn't set, then this is |
| 5904 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5905 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5906 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5907 | entry = ctx->timeout_list.prev; |
| 5908 | goto add; |
| 5909 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5910 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5911 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5912 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5913 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5914 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5915 | * This is safe because ->completion_lock is held, and submissions |
| 5916 | * and completions are never mixed in the same ->completion_lock section. |
| 5917 | */ |
| 5918 | ctx->cq_last_tm_flush = tail; |
| 5919 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5920 | /* |
| 5921 | * Insertion sort, ensuring the first entry in the list is always |
| 5922 | * the one we need first. |
| 5923 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5924 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5925 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5926 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5927 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5928 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5929 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5930 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5931 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5932 | break; |
| 5933 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5934 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5935 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5936 | data->timer.function = io_timeout_fn; |
| 5937 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5938 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5939 | return 0; |
| 5940 | } |
| 5941 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5942 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5943 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5944 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5945 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5946 | return req->user_data == (unsigned long) data; |
| 5947 | } |
| 5948 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5949 | 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] | 5950 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5951 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5952 | int ret = 0; |
| 5953 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5954 | 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] | 5955 | switch (cancel_ret) { |
| 5956 | case IO_WQ_CANCEL_OK: |
| 5957 | ret = 0; |
| 5958 | break; |
| 5959 | case IO_WQ_CANCEL_RUNNING: |
| 5960 | ret = -EALREADY; |
| 5961 | break; |
| 5962 | case IO_WQ_CANCEL_NOTFOUND: |
| 5963 | ret = -ENOENT; |
| 5964 | break; |
| 5965 | } |
| 5966 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5967 | return ret; |
| 5968 | } |
| 5969 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5970 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5971 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5972 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5973 | { |
| 5974 | unsigned long flags; |
| 5975 | int ret; |
| 5976 | |
| 5977 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5978 | if (ret != -ENOENT) { |
| 5979 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5980 | goto done; |
| 5981 | } |
| 5982 | |
| 5983 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5984 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5985 | if (ret != -ENOENT) |
| 5986 | goto done; |
| 5987 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5988 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5989 | if (!ret) |
| 5990 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5991 | io_cqring_fill_event(req, ret); |
| 5992 | io_commit_cqring(ctx); |
| 5993 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5994 | io_cqring_ev_posted(ctx); |
| 5995 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5996 | if (ret < 0) |
| 5997 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5998 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5999 | } |
| 6000 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6001 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6002 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6003 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6004 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6005 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6006 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6007 | return -EINVAL; |
| 6008 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6009 | return -EINVAL; |
| 6010 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6011 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6015 | 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] | 6016 | { |
| 6017 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6018 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6019 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6020 | return 0; |
| 6021 | } |
| 6022 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6023 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6024 | const struct io_uring_sqe *sqe) |
| 6025 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 6026 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 6027 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6028 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6029 | return -EINVAL; |
| 6030 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6031 | return -EINVAL; |
| 6032 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6033 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6034 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6035 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6036 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6037 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6038 | return 0; |
| 6039 | } |
| 6040 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6041 | 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] | 6042 | { |
| 6043 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6044 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6045 | int ret; |
| 6046 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6047 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6048 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6049 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6050 | up.offset = req->rsrc_update.offset; |
| 6051 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6052 | |
| 6053 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6054 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6055 | mutex_unlock(&ctx->uring_lock); |
| 6056 | |
| 6057 | if (ret < 0) |
| 6058 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6059 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6060 | return 0; |
| 6061 | } |
| 6062 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6063 | 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] | 6064 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6065 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6066 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6067 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6068 | case IORING_OP_READV: |
| 6069 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6070 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6071 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6072 | case IORING_OP_WRITEV: |
| 6073 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6074 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6075 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6076 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6077 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6078 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6079 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6080 | case IORING_OP_FSYNC: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6081 | return io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6082 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6083 | return io_prep_sfr(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6084 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6085 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6086 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6087 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6088 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6089 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6090 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6091 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6092 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6093 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6094 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6095 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6096 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6097 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6098 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6099 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6100 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6101 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6102 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6103 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6104 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6105 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6106 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6107 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6108 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6109 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6110 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6111 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6112 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6113 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6114 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6115 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6116 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6117 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6118 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6119 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6120 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6121 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6122 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6123 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6124 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6125 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6126 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6127 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6128 | case IORING_OP_SHUTDOWN: |
| 6129 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6130 | case IORING_OP_RENAMEAT: |
| 6131 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6132 | case IORING_OP_UNLINKAT: |
| 6133 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6134 | } |
| 6135 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6136 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6137 | req->opcode); |
| 6138 | return-EINVAL; |
| 6139 | } |
| 6140 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6141 | static int io_req_defer_prep(struct io_kiocb *req, |
| 6142 | const struct io_uring_sqe *sqe) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6143 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6144 | if (!sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6145 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6146 | if (io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6147 | return -EAGAIN; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6148 | return io_req_prep(req, sqe); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6149 | } |
| 6150 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6151 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6152 | { |
| 6153 | struct io_kiocb *pos; |
| 6154 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6155 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6156 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6157 | io_for_each_link(pos, req) |
| 6158 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6159 | |
| 6160 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6161 | return total_submitted - nr_reqs; |
| 6162 | } |
| 6163 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6164 | 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] | 6165 | { |
| 6166 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6167 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6168 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6169 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6170 | |
| 6171 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6172 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6173 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6174 | return 0; |
| 6175 | |
| 6176 | seq = io_get_sequence(req); |
| 6177 | /* Still a chance to pass the sequence check */ |
| 6178 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6179 | return 0; |
| 6180 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6181 | if (!req->async_data) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6182 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6183 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6184 | return ret; |
| 6185 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6186 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6187 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6188 | if (!de) |
| 6189 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6190 | |
| 6191 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6192 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6193 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6194 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6195 | io_queue_async_work(req); |
| 6196 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6200 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6201 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6202 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6203 | spin_unlock_irq(&ctx->completion_lock); |
| 6204 | return -EIOCBQUEUED; |
| 6205 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6206 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6207 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6208 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6209 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6210 | switch (req->opcode) { |
| 6211 | case IORING_OP_READV: |
| 6212 | case IORING_OP_READ_FIXED: |
| 6213 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6214 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6215 | break; |
| 6216 | case IORING_OP_RECVMSG: |
| 6217 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6218 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6219 | break; |
| 6220 | } |
| 6221 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6222 | } |
| 6223 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6224 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6225 | switch (req->opcode) { |
| 6226 | case IORING_OP_READV: |
| 6227 | case IORING_OP_READ_FIXED: |
| 6228 | case IORING_OP_READ: |
| 6229 | case IORING_OP_WRITEV: |
| 6230 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6231 | case IORING_OP_WRITE: { |
| 6232 | struct io_async_rw *io = req->async_data; |
| 6233 | if (io->free_iovec) |
| 6234 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6235 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6236 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6237 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6238 | case IORING_OP_SENDMSG: { |
| 6239 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6240 | |
| 6241 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6242 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6243 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6244 | case IORING_OP_SPLICE: |
| 6245 | case IORING_OP_TEE: |
| 6246 | io_put_file(req, req->splice.file_in, |
| 6247 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6248 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6249 | case IORING_OP_OPENAT: |
| 6250 | case IORING_OP_OPENAT2: |
| 6251 | if (req->open.filename) |
| 6252 | putname(req->open.filename); |
| 6253 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6254 | case IORING_OP_RENAMEAT: |
| 6255 | putname(req->rename.oldpath); |
| 6256 | putname(req->rename.newpath); |
| 6257 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6258 | case IORING_OP_UNLINKAT: |
| 6259 | putname(req->unlink.filename); |
| 6260 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6261 | } |
| 6262 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6263 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6264 | } |
| 6265 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6266 | 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] | 6267 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6268 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6269 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6270 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6271 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6272 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6273 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6274 | break; |
| 6275 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6276 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6277 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6278 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6279 | break; |
| 6280 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6281 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6282 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6283 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6284 | break; |
| 6285 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6286 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6287 | break; |
| 6288 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6289 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6290 | break; |
| 6291 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6292 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6293 | break; |
| 6294 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6295 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6296 | break; |
| 6297 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6298 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6299 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6300 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6301 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6302 | break; |
| 6303 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6304 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6305 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6306 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6307 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6308 | break; |
| 6309 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6310 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6311 | break; |
| 6312 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6313 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6314 | break; |
| 6315 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6316 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6317 | break; |
| 6318 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6319 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6320 | break; |
| 6321 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6322 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6323 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6324 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6325 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6326 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6327 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6328 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6329 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6330 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6331 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6332 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6333 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6334 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6335 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6336 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6337 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6338 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6339 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6340 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6341 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6342 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6343 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6344 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6345 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6346 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6347 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6348 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6349 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6350 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6351 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6352 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6353 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6354 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6355 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6356 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6357 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6358 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6359 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6360 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6361 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6362 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6363 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6364 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6365 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6366 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6367 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6368 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6369 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6370 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6371 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6372 | default: |
| 6373 | ret = -EINVAL; |
| 6374 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6378 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6379 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6380 | /* If the op doesn't have a file, we're not polling for it */ |
| 6381 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6382 | const bool in_async = io_wq_current_is_worker(); |
| 6383 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6384 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6385 | if (in_async) |
| 6386 | mutex_lock(&ctx->uring_lock); |
| 6387 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6388 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6389 | |
| 6390 | if (in_async) |
| 6391 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | return 0; |
| 6395 | } |
| 6396 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6397 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6398 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6399 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6400 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6401 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6402 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6403 | timeout = io_prep_linked_timeout(req); |
| 6404 | if (timeout) |
| 6405 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6406 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6407 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6408 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6409 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6410 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6411 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6412 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6413 | /* |
| 6414 | * We can get EAGAIN for polled IO even though we're |
| 6415 | * forcing a sync submission from here, since we can't |
| 6416 | * wait for request slots on the block side. |
| 6417 | */ |
| 6418 | if (ret != -EAGAIN) |
| 6419 | break; |
| 6420 | cond_resched(); |
| 6421 | } while (1); |
| 6422 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6423 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6424 | if (ret) { |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6425 | struct io_ring_ctx *lock_ctx = NULL; |
Xiaoguang Wang | dad1b12 | 2020-12-06 22:22:42 +0000 | [diff] [blame] | 6426 | |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6427 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 6428 | lock_ctx = req->ctx; |
| 6429 | |
| 6430 | /* |
| 6431 | * io_iopoll_complete() does not hold completion_lock to |
| 6432 | * complete polled io, so here for polled io, we can not call |
| 6433 | * io_req_complete() directly, otherwise there maybe concurrent |
| 6434 | * access to cqring, defer_list, etc, which is not safe. Given |
| 6435 | * that io_iopoll_complete() is always called under uring_lock, |
| 6436 | * so here for polled io, we also get uring_lock to complete |
| 6437 | * it. |
| 6438 | */ |
| 6439 | if (lock_ctx) |
| 6440 | mutex_lock(&lock_ctx->uring_lock); |
| 6441 | |
| 6442 | req_set_fail_links(req); |
| 6443 | io_req_complete(req, ret); |
| 6444 | |
| 6445 | if (lock_ctx) |
| 6446 | mutex_unlock(&lock_ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6447 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6448 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6449 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6450 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6451 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6452 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6453 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6454 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6455 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6456 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6457 | } |
| 6458 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6459 | static struct file *io_file_get(struct io_submit_state *state, |
| 6460 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6461 | { |
| 6462 | struct io_ring_ctx *ctx = req->ctx; |
| 6463 | struct file *file; |
| 6464 | |
| 6465 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6466 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6467 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6468 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6469 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6470 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6471 | } else { |
| 6472 | trace_io_uring_file_get(ctx, fd); |
| 6473 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6474 | } |
| 6475 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6476 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6477 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6478 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6479 | } |
| 6480 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6481 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6482 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6483 | struct io_timeout_data *data = container_of(timer, |
| 6484 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6485 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6486 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6487 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6488 | |
| 6489 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6490 | prev = req->timeout.head; |
| 6491 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6492 | |
| 6493 | /* |
| 6494 | * We don't expect the list to be empty, that will only happen if we |
| 6495 | * race with the completion of the linked work. |
| 6496 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6497 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6498 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6499 | else |
| 6500 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6501 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6502 | |
| 6503 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6504 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6505 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6506 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6507 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6508 | io_req_complete_post(req, -ETIME, 0); |
| 6509 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6510 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6511 | return HRTIMER_NORESTART; |
| 6512 | } |
| 6513 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6514 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6515 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6516 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6517 | * If the back reference is NULL, then our linked request finished |
| 6518 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6519 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6520 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6521 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6522 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6523 | data->timer.function = io_link_timeout_fn; |
| 6524 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6525 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6526 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6530 | { |
| 6531 | struct io_ring_ctx *ctx = req->ctx; |
| 6532 | |
| 6533 | spin_lock_irq(&ctx->completion_lock); |
| 6534 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6535 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6536 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6537 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6538 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6539 | } |
| 6540 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6541 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6542 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6543 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6544 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6545 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6546 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6547 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6548 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6549 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6550 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6551 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6552 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6553 | } |
| 6554 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6555 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6556 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6557 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6558 | const struct cred *old_creds = NULL; |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6559 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6560 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6561 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6562 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6563 | req->work.identity->creds != current_cred()) |
| 6564 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6565 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6566 | 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] | 6567 | |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6568 | if (old_creds) |
| 6569 | revert_creds(old_creds); |
| 6570 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6571 | /* |
| 6572 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6573 | * doesn't support non-blocking read/write attempts |
| 6574 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6575 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6576 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6577 | /* |
| 6578 | * Queued up for async execution, worker will release |
| 6579 | * submit reference when the iocb is actually submitted. |
| 6580 | */ |
| 6581 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6582 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6583 | } else if (likely(!ret)) { |
| 6584 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6585 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6586 | struct io_ring_ctx *ctx = req->ctx; |
| 6587 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 6588 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6589 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6590 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6591 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6592 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6593 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6594 | } |
| 6595 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6596 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6597 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6598 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6599 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6600 | if (linked_timeout) |
| 6601 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6602 | } |
| 6603 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6604 | 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] | 6605 | { |
| 6606 | int ret; |
| 6607 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6608 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6609 | if (ret) { |
| 6610 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6611 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6612 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6613 | io_put_req(req); |
| 6614 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6615 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6616 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6617 | if (!req->async_data) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6618 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6619 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6620 | goto fail_req; |
| 6621 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6622 | io_queue_async_work(req); |
| 6623 | } else { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6624 | if (sqe) { |
| 6625 | ret = io_req_prep(req, sqe); |
| 6626 | if (unlikely(ret)) |
| 6627 | goto fail_req; |
| 6628 | } |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6629 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6630 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6631 | } |
| 6632 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6633 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6634 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6635 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6636 | io_put_req(req); |
| 6637 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6638 | } else |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6639 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6640 | } |
| 6641 | |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6642 | struct io_submit_link { |
| 6643 | struct io_kiocb *head; |
| 6644 | struct io_kiocb *last; |
| 6645 | }; |
| 6646 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6647 | 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] | 6648 | struct io_submit_link *link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6649 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6650 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6651 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6652 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6653 | /* |
| 6654 | * If we already have a head request, queue this one for async |
| 6655 | * submittal once the head completes. If we don't have a head but |
| 6656 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6657 | * submitted sync once the chain is complete. If none of those |
| 6658 | * conditions are true (normal request), then just queue it. |
| 6659 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6660 | if (link->head) { |
| 6661 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6662 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6663 | /* |
| 6664 | * Taking sequential execution of a link, draining both sides |
| 6665 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6666 | * requests in the link. So, it drains the head and the |
| 6667 | * next after the link request. The last one is done via |
| 6668 | * drain_next flag to persist the effect across calls. |
| 6669 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6670 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6671 | head->flags |= REQ_F_IO_DRAIN; |
| 6672 | ctx->drain_next = 1; |
| 6673 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6674 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6675 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6676 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6677 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6678 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6679 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6680 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6681 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6682 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6683 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6684 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6685 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6686 | io_queue_link_head(head); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6687 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6688 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6689 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6690 | if (unlikely(ctx->drain_next)) { |
| 6691 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6692 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6693 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6694 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6695 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6696 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6697 | req->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6698 | link->head = req; |
| 6699 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6700 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6701 | io_queue_sqe(req, sqe); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6702 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6703 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6704 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6705 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6706 | } |
| 6707 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6708 | /* |
| 6709 | * Batched submission is done, ensure local IO is flushed out. |
| 6710 | */ |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6711 | static void io_submit_state_end(struct io_submit_state *state, |
| 6712 | struct io_ring_ctx *ctx) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6713 | { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6714 | if (state->comp.nr) |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6715 | io_submit_flush_completions(&state->comp, ctx); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6716 | if (state->plug_started) |
| 6717 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6718 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6719 | } |
| 6720 | |
| 6721 | /* |
| 6722 | * Start submission side cache. |
| 6723 | */ |
| 6724 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6725 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6726 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6727 | state->plug_started = false; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6728 | state->ios_left = max_ios; |
| 6729 | } |
| 6730 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6731 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6732 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6733 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6734 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6735 | /* |
| 6736 | * Ensure any loads from the SQEs are done at this point, |
| 6737 | * since once we write the new head, the application could |
| 6738 | * write new data to them. |
| 6739 | */ |
| 6740 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6741 | } |
| 6742 | |
| 6743 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6744 | * 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] | 6745 | * that is mapped by userspace. This means that care needs to be taken to |
| 6746 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6747 | * being a good citizen. If members of the sqe are validated and then later |
| 6748 | * used, it's important that those reads are done through READ_ONCE() to |
| 6749 | * prevent a re-load down the line. |
| 6750 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6751 | 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] | 6752 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6753 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6754 | unsigned head; |
| 6755 | |
| 6756 | /* |
| 6757 | * The cached sq head (or cq tail) serves two purposes: |
| 6758 | * |
| 6759 | * 1) allows us to batch the cost of updating the user visible |
| 6760 | * head updates. |
| 6761 | * 2) allows the kernel side to track the head on its own, even |
| 6762 | * though the application is the one updating it. |
| 6763 | */ |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6764 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6765 | if (likely(head < ctx->sq_entries)) |
| 6766 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6767 | |
| 6768 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6769 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6770 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6771 | return NULL; |
| 6772 | } |
| 6773 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6774 | /* |
| 6775 | * Check SQE restrictions (opcode and flags). |
| 6776 | * |
| 6777 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6778 | */ |
| 6779 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6780 | struct io_kiocb *req, |
| 6781 | unsigned int sqe_flags) |
| 6782 | { |
| 6783 | if (!ctx->restricted) |
| 6784 | return true; |
| 6785 | |
| 6786 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6787 | return false; |
| 6788 | |
| 6789 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6790 | ctx->restrictions.sqe_flags_required) |
| 6791 | return false; |
| 6792 | |
| 6793 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6794 | ctx->restrictions.sqe_flags_required)) |
| 6795 | return false; |
| 6796 | |
| 6797 | return true; |
| 6798 | } |
| 6799 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6800 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6801 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6802 | IOSQE_BUFFER_SELECT) |
| 6803 | |
| 6804 | 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] | 6805 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6806 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6807 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6808 | unsigned int sqe_flags; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6809 | int id, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6810 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6811 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6812 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6813 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6814 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6815 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6816 | req->file = NULL; |
| 6817 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6818 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6819 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6820 | /* one is dropped after submission, the other at completion */ |
| 6821 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6822 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6823 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6824 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6825 | /* enforce forwards compatibility on users */ |
| 6826 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6827 | return -EINVAL; |
| 6828 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6829 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6830 | return -EINVAL; |
| 6831 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6832 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6833 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6834 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6835 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6836 | return -EACCES; |
| 6837 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6838 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6839 | !io_op_defs[req->opcode].buffer_select) |
| 6840 | return -EOPNOTSUPP; |
| 6841 | |
| 6842 | id = READ_ONCE(sqe->personality); |
| 6843 | if (id) { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6844 | struct io_identity *iod; |
| 6845 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6846 | iod = idr_find(&ctx->personality_idr, id); |
| 6847 | if (unlikely(!iod)) |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6848 | return -EINVAL; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6849 | refcount_inc(&iod->count); |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6850 | |
| 6851 | __io_req_init_async(req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6852 | get_cred(iod->creds); |
| 6853 | req->work.identity = iod; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6854 | req->work.flags |= IO_WQ_WORK_CREDS; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6855 | } |
| 6856 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6857 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6858 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6859 | /* |
| 6860 | * Plug now if we have more than 1 IO left after this, and the target |
| 6861 | * is potentially a read/write to block based storage. |
| 6862 | */ |
| 6863 | if (!state->plug_started && state->ios_left > 1 && |
| 6864 | io_op_defs[req->opcode].plug) { |
| 6865 | blk_start_plug(&state->plug); |
| 6866 | state->plug_started = true; |
| 6867 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6868 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6869 | if (io_op_defs[req->opcode].needs_file) { |
| 6870 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6871 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6872 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6873 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6874 | ret = -EBADF; |
| 6875 | } |
| 6876 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6877 | state->ios_left--; |
| 6878 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6879 | } |
| 6880 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6881 | 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] | 6882 | { |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6883 | struct io_submit_link link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6884 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6885 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6886 | /* 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] | 6887 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6888 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6889 | return -EBUSY; |
| 6890 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6891 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6892 | /* make sure SQ entry isn't read before tail */ |
| 6893 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6894 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6895 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6896 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6897 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6898 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6899 | refcount_add(nr, ¤t->usage); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6900 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6901 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6902 | link.head = NULL; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6903 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6904 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6905 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6906 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6907 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6908 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6909 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6910 | if (unlikely(!req)) { |
| 6911 | if (!submitted) |
| 6912 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6913 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6914 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6915 | sqe = io_get_sqe(ctx); |
| 6916 | if (unlikely(!sqe)) { |
| 6917 | kmem_cache_free(req_cachep, req); |
| 6918 | break; |
| 6919 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6920 | /* will complete beyond this point, count as submitted */ |
| 6921 | submitted++; |
| 6922 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6923 | err = io_init_req(ctx, req, sqe); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6924 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6925 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6926 | io_put_req(req); |
| 6927 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6928 | break; |
| 6929 | } |
| 6930 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6931 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 2d7e935 | 2021-01-19 13:32:37 +0000 | [diff] [blame] | 6932 | true, ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6933 | err = io_submit_sqe(req, sqe, &link); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6934 | if (err) |
| 6935 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6936 | } |
| 6937 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6938 | if (unlikely(submitted != nr)) { |
| 6939 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6940 | struct io_uring_task *tctx = current->io_uring; |
| 6941 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6942 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6943 | percpu_ref_put_many(&ctx->refs, unused); |
| 6944 | percpu_counter_sub(&tctx->inflight, unused); |
| 6945 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6946 | } |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6947 | if (link.head) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6948 | io_queue_link_head(link.head); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6949 | io_submit_state_end(&ctx->submit_state, ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6950 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6951 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6952 | io_commit_sqring(ctx); |
| 6953 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6954 | return submitted; |
| 6955 | } |
| 6956 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6957 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6958 | { |
| 6959 | /* Tell userspace we may need a wakeup call */ |
| 6960 | spin_lock_irq(&ctx->completion_lock); |
| 6961 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6962 | spin_unlock_irq(&ctx->completion_lock); |
| 6963 | } |
| 6964 | |
| 6965 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6966 | { |
| 6967 | spin_lock_irq(&ctx->completion_lock); |
| 6968 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6969 | spin_unlock_irq(&ctx->completion_lock); |
| 6970 | } |
| 6971 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6972 | 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] | 6973 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6974 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6975 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6976 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6977 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6978 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6979 | if (cap_entries && to_submit > 8) |
| 6980 | to_submit = 8; |
| 6981 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6982 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6983 | unsigned nr_events = 0; |
| 6984 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6985 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6986 | if (!list_empty(&ctx->iopoll_list)) |
| 6987 | io_do_iopoll(ctx, &nr_events, 0); |
| 6988 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6989 | if (to_submit && !ctx->sqo_dead && |
| 6990 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6991 | ret = io_submit_sqes(ctx, to_submit); |
| 6992 | mutex_unlock(&ctx->uring_lock); |
| 6993 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6994 | |
| 6995 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6996 | wake_up(&ctx->sqo_sq_wait); |
| 6997 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6998 | return ret; |
| 6999 | } |
| 7000 | |
| 7001 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7002 | { |
| 7003 | struct io_ring_ctx *ctx; |
| 7004 | unsigned sq_thread_idle = 0; |
| 7005 | |
| 7006 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7007 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 7008 | sq_thread_idle = ctx->sq_thread_idle; |
| 7009 | } |
| 7010 | |
| 7011 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7012 | } |
| 7013 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7014 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7015 | { |
| 7016 | struct io_ring_ctx *ctx; |
| 7017 | |
| 7018 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7019 | 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] | 7020 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7021 | complete(&ctx->sq_thread_comp); |
| 7022 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7023 | |
| 7024 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7025 | } |
| 7026 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7027 | static int io_sq_thread(void *data) |
| 7028 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7029 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7030 | struct files_struct *old_files = current->files; |
| 7031 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7032 | const struct cred *old_cred = NULL; |
| 7033 | struct io_sq_data *sqd = data; |
| 7034 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7035 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7036 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7037 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7038 | task_lock(current); |
| 7039 | current->files = NULL; |
| 7040 | current->nsproxy = NULL; |
| 7041 | task_unlock(current); |
| 7042 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7043 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7044 | int ret; |
| 7045 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7046 | |
| 7047 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7048 | * Any changes to the sqd lists are synchronized through the |
| 7049 | * kthread parking. This synchronizes the thread vs users, |
| 7050 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7051 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7052 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7053 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7054 | /* |
| 7055 | * When sq thread is unparked, in case the previous park operation |
| 7056 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7057 | * to be stopped, so here needs to have a check. |
| 7058 | */ |
| 7059 | if (kthread_should_stop()) |
| 7060 | break; |
| 7061 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7062 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7063 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7064 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7065 | timeout = jiffies + sqd->sq_thread_idle; |
| 7066 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7067 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7068 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7069 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7070 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7071 | if (current->cred != ctx->creds) { |
| 7072 | if (old_cred) |
| 7073 | revert_creds(old_cred); |
| 7074 | old_cred = override_creds(ctx->creds); |
| 7075 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7076 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7077 | #ifdef CONFIG_AUDIT |
| 7078 | current->loginuid = ctx->loginuid; |
| 7079 | current->sessionid = ctx->sessionid; |
| 7080 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7081 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7082 | ret = __io_sq_thread(ctx, cap_entries); |
| 7083 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7084 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7085 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7086 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7087 | } |
| 7088 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7089 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7090 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7091 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7092 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7093 | if (sqt_spin) |
| 7094 | timeout = jiffies + sqd->sq_thread_idle; |
| 7095 | continue; |
| 7096 | } |
| 7097 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7098 | needs_sched = true; |
| 7099 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7100 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7101 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7102 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7103 | needs_sched = false; |
| 7104 | break; |
| 7105 | } |
| 7106 | if (io_sqring_entries(ctx)) { |
| 7107 | needs_sched = false; |
| 7108 | break; |
| 7109 | } |
| 7110 | } |
| 7111 | |
Hao Xu | 8b28fdf | 2021-01-31 22:39:04 +0800 | [diff] [blame] | 7112 | if (needs_sched && !kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7113 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7114 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7115 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7116 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7117 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7118 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7119 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7120 | |
| 7121 | finish_wait(&sqd->wait, &wait); |
| 7122 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7123 | } |
| 7124 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7125 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7126 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7127 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7128 | if (cur_css) |
| 7129 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7130 | if (old_cred) |
| 7131 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7132 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7133 | task_lock(current); |
| 7134 | current->files = old_files; |
| 7135 | current->nsproxy = old_nsproxy; |
| 7136 | task_unlock(current); |
| 7137 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7138 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7139 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7140 | return 0; |
| 7141 | } |
| 7142 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7143 | struct io_wait_queue { |
| 7144 | struct wait_queue_entry wq; |
| 7145 | struct io_ring_ctx *ctx; |
| 7146 | unsigned to_wait; |
| 7147 | unsigned nr_timeouts; |
| 7148 | }; |
| 7149 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7150 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7151 | { |
| 7152 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7153 | |
| 7154 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7155 | * 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] | 7156 | * started waiting. For timeouts, we always want to return to userspace, |
| 7157 | * regardless of event count. |
| 7158 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7159 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7160 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7161 | } |
| 7162 | |
| 7163 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7164 | int wake_flags, void *key) |
| 7165 | { |
| 7166 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7167 | wq); |
| 7168 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7169 | /* |
| 7170 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7171 | * the task, and the next invocation will do it. |
| 7172 | */ |
| 7173 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7174 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7175 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7176 | } |
| 7177 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7178 | static int io_run_task_work_sig(void) |
| 7179 | { |
| 7180 | if (io_run_task_work()) |
| 7181 | return 1; |
| 7182 | if (!signal_pending(current)) |
| 7183 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7184 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7185 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7186 | return -EINTR; |
| 7187 | } |
| 7188 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7189 | /* when returns >0, the caller should retry */ |
| 7190 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7191 | struct io_wait_queue *iowq, |
| 7192 | signed long *timeout) |
| 7193 | { |
| 7194 | int ret; |
| 7195 | |
| 7196 | /* make sure we run task_work before checking for signals */ |
| 7197 | ret = io_run_task_work_sig(); |
| 7198 | if (ret || io_should_wake(iowq)) |
| 7199 | return ret; |
| 7200 | /* let the caller flush overflows, retry */ |
| 7201 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7202 | return 1; |
| 7203 | |
| 7204 | *timeout = schedule_timeout(*timeout); |
| 7205 | return !*timeout ? -ETIME : 1; |
| 7206 | } |
| 7207 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7208 | /* |
| 7209 | * Wait until events become available, if we don't already have some. The |
| 7210 | * application must reap them itself, as they reside on the shared cq ring. |
| 7211 | */ |
| 7212 | 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] | 7213 | const sigset_t __user *sig, size_t sigsz, |
| 7214 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7215 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7216 | struct io_wait_queue iowq = { |
| 7217 | .wq = { |
| 7218 | .private = current, |
| 7219 | .func = io_wake_function, |
| 7220 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7221 | }, |
| 7222 | .ctx = ctx, |
| 7223 | .to_wait = min_events, |
| 7224 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7225 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7226 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7227 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7228 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7229 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7230 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7231 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7232 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7233 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7234 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7235 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7236 | |
| 7237 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7238 | #ifdef CONFIG_COMPAT |
| 7239 | if (in_compat_syscall()) |
| 7240 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7241 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7242 | else |
| 7243 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7244 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7245 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7246 | if (ret) |
| 7247 | return ret; |
| 7248 | } |
| 7249 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7250 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7251 | struct timespec64 ts; |
| 7252 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7253 | if (get_timespec64(&ts, uts)) |
| 7254 | return -EFAULT; |
| 7255 | timeout = timespec64_to_jiffies(&ts); |
| 7256 | } |
| 7257 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7258 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7259 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7260 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7261 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7262 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7263 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7264 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7265 | finish_wait(&ctx->wait, &iowq.wq); |
| 7266 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7267 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7268 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7269 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7270 | 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] | 7271 | } |
| 7272 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7273 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7274 | { |
| 7275 | #if defined(CONFIG_UNIX) |
| 7276 | if (ctx->ring_sock) { |
| 7277 | struct sock *sock = ctx->ring_sock->sk; |
| 7278 | struct sk_buff *skb; |
| 7279 | |
| 7280 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7281 | kfree_skb(skb); |
| 7282 | } |
| 7283 | #else |
| 7284 | int i; |
| 7285 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7286 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7287 | struct file *file; |
| 7288 | |
| 7289 | file = io_file_from_index(ctx, i); |
| 7290 | if (file) |
| 7291 | fput(file); |
| 7292 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7293 | #endif |
| 7294 | } |
| 7295 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7296 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7297 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7298 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7299 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7300 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7301 | complete(&data->done); |
| 7302 | } |
| 7303 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7304 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
| 7305 | { |
| 7306 | spin_lock_bh(&ctx->rsrc_ref_lock); |
| 7307 | } |
| 7308 | |
| 7309 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
| 7310 | { |
| 7311 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7312 | } |
| 7313 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7314 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 7315 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7316 | struct fixed_rsrc_ref_node *ref_node) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7317 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7318 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7319 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7320 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7321 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7322 | percpu_ref_get(&rsrc_data->refs); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7323 | } |
| 7324 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7325 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 7326 | struct io_ring_ctx *ctx, |
| 7327 | struct fixed_rsrc_ref_node *backup_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7328 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7329 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7330 | int ret; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7331 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7332 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7333 | ref_node = data->node; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7334 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7335 | if (ref_node) |
| 7336 | percpu_ref_kill(&ref_node->refs); |
| 7337 | |
| 7338 | percpu_ref_kill(&data->refs); |
| 7339 | |
| 7340 | /* wait for all refs nodes to complete */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7341 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7342 | do { |
| 7343 | ret = wait_for_completion_interruptible(&data->done); |
| 7344 | if (!ret) |
| 7345 | break; |
| 7346 | ret = io_run_task_work_sig(); |
| 7347 | if (ret < 0) { |
| 7348 | percpu_ref_resurrect(&data->refs); |
| 7349 | reinit_completion(&data->done); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7350 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7351 | return ret; |
| 7352 | } |
| 7353 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7354 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7355 | destroy_fixed_rsrc_ref_node(backup_node); |
| 7356 | return 0; |
| 7357 | } |
| 7358 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7359 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7360 | { |
| 7361 | struct fixed_rsrc_data *data; |
| 7362 | |
| 7363 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7364 | if (!data) |
| 7365 | return NULL; |
| 7366 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7367 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7368 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7369 | kfree(data); |
| 7370 | return NULL; |
| 7371 | } |
| 7372 | data->ctx = ctx; |
| 7373 | init_completion(&data->done); |
| 7374 | return data; |
| 7375 | } |
| 7376 | |
| 7377 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7378 | { |
| 7379 | percpu_ref_exit(&data->refs); |
| 7380 | kfree(data->table); |
| 7381 | kfree(data); |
| 7382 | } |
| 7383 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7384 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7385 | { |
| 7386 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7387 | struct fixed_rsrc_ref_node *backup_node; |
| 7388 | unsigned nr_tables, i; |
| 7389 | int ret; |
| 7390 | |
| 7391 | if (!data) |
| 7392 | return -ENXIO; |
| 7393 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 7394 | if (!backup_node) |
| 7395 | return -ENOMEM; |
| 7396 | init_fixed_file_ref_node(ctx, backup_node); |
| 7397 | |
| 7398 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); |
| 7399 | if (ret) |
| 7400 | return ret; |
| 7401 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7402 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7403 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7404 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7405 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7406 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7407 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7408 | ctx->nr_user_files = 0; |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7412 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7413 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7414 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7415 | /* |
| 7416 | * The park is a bit of a work-around, without it we get |
| 7417 | * warning spews on shutdown with SQPOLL set and affinity |
| 7418 | * set to a single CPU. |
| 7419 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7420 | if (sqd->thread) { |
| 7421 | kthread_park(sqd->thread); |
| 7422 | kthread_stop(sqd->thread); |
| 7423 | } |
| 7424 | |
| 7425 | kfree(sqd); |
| 7426 | } |
| 7427 | } |
| 7428 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7429 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7430 | { |
| 7431 | struct io_ring_ctx *ctx_attach; |
| 7432 | struct io_sq_data *sqd; |
| 7433 | struct fd f; |
| 7434 | |
| 7435 | f = fdget(p->wq_fd); |
| 7436 | if (!f.file) |
| 7437 | return ERR_PTR(-ENXIO); |
| 7438 | if (f.file->f_op != &io_uring_fops) { |
| 7439 | fdput(f); |
| 7440 | return ERR_PTR(-EINVAL); |
| 7441 | } |
| 7442 | |
| 7443 | ctx_attach = f.file->private_data; |
| 7444 | sqd = ctx_attach->sq_data; |
| 7445 | if (!sqd) { |
| 7446 | fdput(f); |
| 7447 | return ERR_PTR(-EINVAL); |
| 7448 | } |
| 7449 | |
| 7450 | refcount_inc(&sqd->refs); |
| 7451 | fdput(f); |
| 7452 | return sqd; |
| 7453 | } |
| 7454 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7455 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7456 | { |
| 7457 | struct io_sq_data *sqd; |
| 7458 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7459 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7460 | return io_attach_sq_data(p); |
| 7461 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7462 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7463 | if (!sqd) |
| 7464 | return ERR_PTR(-ENOMEM); |
| 7465 | |
| 7466 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7467 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7468 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7469 | mutex_init(&sqd->ctx_lock); |
| 7470 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7471 | init_waitqueue_head(&sqd->wait); |
| 7472 | return sqd; |
| 7473 | } |
| 7474 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7475 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7476 | __releases(&sqd->lock) |
| 7477 | { |
| 7478 | if (!sqd->thread) |
| 7479 | return; |
| 7480 | kthread_unpark(sqd->thread); |
| 7481 | mutex_unlock(&sqd->lock); |
| 7482 | } |
| 7483 | |
| 7484 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7485 | __acquires(&sqd->lock) |
| 7486 | { |
| 7487 | if (!sqd->thread) |
| 7488 | return; |
| 7489 | mutex_lock(&sqd->lock); |
| 7490 | kthread_park(sqd->thread); |
| 7491 | } |
| 7492 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7493 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7494 | { |
| 7495 | struct io_sq_data *sqd = ctx->sq_data; |
| 7496 | |
| 7497 | if (sqd) { |
| 7498 | if (sqd->thread) { |
| 7499 | /* |
| 7500 | * We may arrive here from the error branch in |
| 7501 | * io_sq_offload_create() where the kthread is created |
| 7502 | * without being waked up, thus wake it up now to make |
| 7503 | * sure the wait will complete. |
| 7504 | */ |
| 7505 | wake_up_process(sqd->thread); |
| 7506 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7507 | |
| 7508 | io_sq_thread_park(sqd); |
| 7509 | } |
| 7510 | |
| 7511 | mutex_lock(&sqd->ctx_lock); |
| 7512 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7513 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7514 | mutex_unlock(&sqd->ctx_lock); |
| 7515 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7516 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7517 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7518 | |
| 7519 | io_put_sq_data(sqd); |
| 7520 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7521 | } |
| 7522 | } |
| 7523 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7524 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7525 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7526 | io_sq_thread_stop(ctx); |
| 7527 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7528 | if (ctx->io_wq) { |
| 7529 | io_wq_destroy(ctx->io_wq); |
| 7530 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | |
| 7534 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7535 | /* |
| 7536 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7537 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7538 | * loops in the file referencing. |
| 7539 | */ |
| 7540 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7541 | { |
| 7542 | struct sock *sk = ctx->ring_sock->sk; |
| 7543 | struct scm_fp_list *fpl; |
| 7544 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7545 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7546 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7547 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7548 | if (!fpl) |
| 7549 | return -ENOMEM; |
| 7550 | |
| 7551 | skb = alloc_skb(0, GFP_KERNEL); |
| 7552 | if (!skb) { |
| 7553 | kfree(fpl); |
| 7554 | return -ENOMEM; |
| 7555 | } |
| 7556 | |
| 7557 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7558 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7559 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7560 | fpl->user = get_uid(ctx->user); |
| 7561 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7562 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7563 | |
| 7564 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7565 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7566 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7567 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7568 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7569 | } |
| 7570 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7571 | if (nr_files) { |
| 7572 | fpl->max = SCM_MAX_FD; |
| 7573 | fpl->count = nr_files; |
| 7574 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7575 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7576 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7577 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7578 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7579 | for (i = 0; i < nr_files; i++) |
| 7580 | fput(fpl->fp[i]); |
| 7581 | } else { |
| 7582 | kfree_skb(skb); |
| 7583 | kfree(fpl); |
| 7584 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7585 | |
| 7586 | return 0; |
| 7587 | } |
| 7588 | |
| 7589 | /* |
| 7590 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7591 | * causes regular reference counting to break down. We rely on the UNIX |
| 7592 | * garbage collection to take care of this problem for us. |
| 7593 | */ |
| 7594 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7595 | { |
| 7596 | unsigned left, total; |
| 7597 | int ret = 0; |
| 7598 | |
| 7599 | total = 0; |
| 7600 | left = ctx->nr_user_files; |
| 7601 | while (left) { |
| 7602 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7603 | |
| 7604 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7605 | if (ret) |
| 7606 | break; |
| 7607 | left -= this_files; |
| 7608 | total += this_files; |
| 7609 | } |
| 7610 | |
| 7611 | if (!ret) |
| 7612 | return 0; |
| 7613 | |
| 7614 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7615 | struct file *file = io_file_from_index(ctx, total); |
| 7616 | |
| 7617 | if (file) |
| 7618 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7619 | total++; |
| 7620 | } |
| 7621 | |
| 7622 | return ret; |
| 7623 | } |
| 7624 | #else |
| 7625 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7626 | { |
| 7627 | return 0; |
| 7628 | } |
| 7629 | #endif |
| 7630 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7631 | 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] | 7632 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7633 | { |
| 7634 | int i; |
| 7635 | |
| 7636 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7637 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7638 | unsigned this_files; |
| 7639 | |
| 7640 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7641 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7642 | GFP_KERNEL); |
| 7643 | if (!table->files) |
| 7644 | break; |
| 7645 | nr_files -= this_files; |
| 7646 | } |
| 7647 | |
| 7648 | if (i == nr_tables) |
| 7649 | return 0; |
| 7650 | |
| 7651 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7652 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7653 | kfree(table->files); |
| 7654 | } |
| 7655 | return 1; |
| 7656 | } |
| 7657 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7658 | 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] | 7659 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7660 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7661 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7662 | struct sock *sock = ctx->ring_sock->sk; |
| 7663 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7664 | struct sk_buff *skb; |
| 7665 | int i; |
| 7666 | |
| 7667 | __skb_queue_head_init(&list); |
| 7668 | |
| 7669 | /* |
| 7670 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7671 | * remove this entry and rearrange the file array. |
| 7672 | */ |
| 7673 | skb = skb_dequeue(head); |
| 7674 | while (skb) { |
| 7675 | struct scm_fp_list *fp; |
| 7676 | |
| 7677 | fp = UNIXCB(skb).fp; |
| 7678 | for (i = 0; i < fp->count; i++) { |
| 7679 | int left; |
| 7680 | |
| 7681 | if (fp->fp[i] != file) |
| 7682 | continue; |
| 7683 | |
| 7684 | unix_notinflight(fp->user, fp->fp[i]); |
| 7685 | left = fp->count - 1 - i; |
| 7686 | if (left) { |
| 7687 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7688 | left * sizeof(struct file *)); |
| 7689 | } |
| 7690 | fp->count--; |
| 7691 | if (!fp->count) { |
| 7692 | kfree_skb(skb); |
| 7693 | skb = NULL; |
| 7694 | } else { |
| 7695 | __skb_queue_tail(&list, skb); |
| 7696 | } |
| 7697 | fput(file); |
| 7698 | file = NULL; |
| 7699 | break; |
| 7700 | } |
| 7701 | |
| 7702 | if (!file) |
| 7703 | break; |
| 7704 | |
| 7705 | __skb_queue_tail(&list, skb); |
| 7706 | |
| 7707 | skb = skb_dequeue(head); |
| 7708 | } |
| 7709 | |
| 7710 | if (skb_peek(&list)) { |
| 7711 | spin_lock_irq(&head->lock); |
| 7712 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7713 | __skb_queue_tail(head, skb); |
| 7714 | spin_unlock_irq(&head->lock); |
| 7715 | } |
| 7716 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7717 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7718 | #endif |
| 7719 | } |
| 7720 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7721 | 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] | 7722 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7723 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7724 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7725 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7726 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7727 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7728 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7729 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7730 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7731 | } |
| 7732 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7733 | percpu_ref_exit(&ref_node->refs); |
| 7734 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7735 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7736 | } |
| 7737 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7738 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7739 | { |
| 7740 | struct io_ring_ctx *ctx; |
| 7741 | struct llist_node *node; |
| 7742 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7743 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7744 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7745 | |
| 7746 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7747 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7748 | struct llist_node *next = node->next; |
| 7749 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7750 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7751 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7752 | node = next; |
| 7753 | } |
| 7754 | } |
| 7755 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7756 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7757 | unsigned i) |
| 7758 | { |
| 7759 | struct fixed_rsrc_table *table; |
| 7760 | |
| 7761 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7762 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7763 | } |
| 7764 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7765 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7766 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7767 | struct fixed_rsrc_ref_node *ref_node; |
| 7768 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7769 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7770 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7771 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7772 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7773 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7774 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7775 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7776 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7777 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7778 | ref_node->done = true; |
| 7779 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7780 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7781 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7782 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7783 | /* recycle ref nodes in order */ |
| 7784 | if (!ref_node->done) |
| 7785 | break; |
| 7786 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7787 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7788 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7789 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7790 | |
| 7791 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7792 | delay = 0; |
| 7793 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7794 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7795 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7796 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7797 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7798 | } |
| 7799 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7800 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7801 | struct io_ring_ctx *ctx) |
| 7802 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7803 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7804 | |
| 7805 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7806 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7807 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7808 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7809 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7810 | 0, GFP_KERNEL)) { |
| 7811 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7812 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7813 | } |
| 7814 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7815 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7816 | ref_node->done = false; |
| 7817 | return ref_node; |
| 7818 | } |
| 7819 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7820 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7821 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7822 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7823 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7824 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7825 | } |
| 7826 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7827 | 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] | 7828 | { |
| 7829 | percpu_ref_exit(&ref_node->refs); |
| 7830 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7831 | } |
| 7832 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7833 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7834 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7835 | unsigned nr_args) |
| 7836 | { |
| 7837 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7838 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7839 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7840 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7841 | struct fixed_rsrc_ref_node *ref_node; |
| 7842 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7843 | |
| 7844 | if (ctx->file_data) |
| 7845 | return -EBUSY; |
| 7846 | if (!nr_args) |
| 7847 | return -EINVAL; |
| 7848 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7849 | return -EMFILE; |
| 7850 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7851 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7852 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7853 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7854 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7855 | |
| 7856 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7857 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7858 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7859 | if (!file_data->table) |
| 7860 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7861 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7862 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7863 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7864 | |
| 7865 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7866 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7867 | ret = -EFAULT; |
| 7868 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7869 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7870 | /* allow sparse sets */ |
| 7871 | if (fd == -1) |
| 7872 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7873 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7874 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7875 | ret = -EBADF; |
| 7876 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7877 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7878 | |
| 7879 | /* |
| 7880 | * Don't allow io_uring instances to be registered. If UNIX |
| 7881 | * isn't enabled, then this causes a reference cycle and this |
| 7882 | * instance can never get freed. If UNIX is enabled we'll |
| 7883 | * handle it just fine, but there's still no point in allowing |
| 7884 | * a ring fd as it doesn't support regular read/write anyway. |
| 7885 | */ |
| 7886 | if (file->f_op == &io_uring_fops) { |
| 7887 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7888 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7889 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7890 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7891 | } |
| 7892 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7893 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7894 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7895 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7896 | return ret; |
| 7897 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7898 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7899 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7900 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7901 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7902 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7903 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7904 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7905 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7906 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7907 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7908 | out_fput: |
| 7909 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7910 | file = io_file_from_index(ctx, i); |
| 7911 | if (file) |
| 7912 | fput(file); |
| 7913 | } |
| 7914 | for (i = 0; i < nr_tables; i++) |
| 7915 | kfree(file_data->table[i].files); |
| 7916 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7917 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7918 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7919 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7920 | return ret; |
| 7921 | } |
| 7922 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7923 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7924 | int index) |
| 7925 | { |
| 7926 | #if defined(CONFIG_UNIX) |
| 7927 | struct sock *sock = ctx->ring_sock->sk; |
| 7928 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7929 | struct sk_buff *skb; |
| 7930 | |
| 7931 | /* |
| 7932 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7933 | * file set. If there's no room, fall back to allocating a new skb |
| 7934 | * and filling it in. |
| 7935 | */ |
| 7936 | spin_lock_irq(&head->lock); |
| 7937 | skb = skb_peek(head); |
| 7938 | if (skb) { |
| 7939 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7940 | |
| 7941 | if (fpl->count < SCM_MAX_FD) { |
| 7942 | __skb_unlink(skb, head); |
| 7943 | spin_unlock_irq(&head->lock); |
| 7944 | fpl->fp[fpl->count] = get_file(file); |
| 7945 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7946 | fpl->count++; |
| 7947 | spin_lock_irq(&head->lock); |
| 7948 | __skb_queue_head(head, skb); |
| 7949 | } else { |
| 7950 | skb = NULL; |
| 7951 | } |
| 7952 | } |
| 7953 | spin_unlock_irq(&head->lock); |
| 7954 | |
| 7955 | if (skb) { |
| 7956 | fput(file); |
| 7957 | return 0; |
| 7958 | } |
| 7959 | |
| 7960 | return __io_sqe_files_scm(ctx, 1, index); |
| 7961 | #else |
| 7962 | return 0; |
| 7963 | #endif |
| 7964 | } |
| 7965 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7966 | 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] | 7967 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7968 | struct io_rsrc_put *prsrc; |
| 7969 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7970 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7971 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7972 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7973 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7974 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7975 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7976 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7977 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7978 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7979 | } |
| 7980 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7981 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7982 | struct file *file) |
| 7983 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7984 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7987 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7988 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7989 | unsigned nr_args) |
| 7990 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7991 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7992 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7993 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7994 | __s32 __user *fds; |
| 7995 | int fd, i, err; |
| 7996 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7997 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7998 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7999 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8000 | return -EOVERFLOW; |
| 8001 | if (done > ctx->nr_user_files) |
| 8002 | return -EINVAL; |
| 8003 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8004 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 8005 | if (!ref_node) |
| 8006 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 8007 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8008 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8009 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8010 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8011 | err = 0; |
| 8012 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 8013 | err = -EFAULT; |
| 8014 | break; |
| 8015 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8016 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8017 | continue; |
| 8018 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8019 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8020 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 8021 | |
| 8022 | if (*file_slot) { |
| 8023 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8024 | if (err) |
| 8025 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8026 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8027 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8028 | } |
| 8029 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8030 | file = fget(fd); |
| 8031 | if (!file) { |
| 8032 | err = -EBADF; |
| 8033 | break; |
| 8034 | } |
| 8035 | /* |
| 8036 | * Don't allow io_uring instances to be registered. If |
| 8037 | * UNIX isn't enabled, then this causes a reference |
| 8038 | * cycle and this instance can never get freed. If UNIX |
| 8039 | * is enabled we'll handle it just fine, but there's |
| 8040 | * still no point in allowing a ring fd as it doesn't |
| 8041 | * support regular read/write anyway. |
| 8042 | */ |
| 8043 | if (file->f_op == &io_uring_fops) { |
| 8044 | fput(file); |
| 8045 | err = -EBADF; |
| 8046 | break; |
| 8047 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8048 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8049 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8050 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 8051 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8052 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8053 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8054 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8055 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8056 | } |
| 8057 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8058 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8059 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 8060 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8061 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8062 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8063 | |
| 8064 | return done ? done : err; |
| 8065 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8066 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8067 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8068 | unsigned nr_args) |
| 8069 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8070 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8071 | |
| 8072 | if (!ctx->file_data) |
| 8073 | return -ENXIO; |
| 8074 | if (!nr_args) |
| 8075 | return -EINVAL; |
| 8076 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8077 | return -EFAULT; |
| 8078 | if (up.resv) |
| 8079 | return -EINVAL; |
| 8080 | |
| 8081 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8082 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8083 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8084 | 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] | 8085 | { |
| 8086 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8087 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 8088 | req = io_put_req_find_next(req); |
| 8089 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8090 | } |
| 8091 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8092 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8093 | struct io_uring_params *p) |
| 8094 | { |
| 8095 | struct io_wq_data data; |
| 8096 | struct fd f; |
| 8097 | struct io_ring_ctx *ctx_attach; |
| 8098 | unsigned int concurrency; |
| 8099 | int ret = 0; |
| 8100 | |
| 8101 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8102 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8103 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8104 | |
| 8105 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8106 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8107 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8108 | |
| 8109 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8110 | if (IS_ERR(ctx->io_wq)) { |
| 8111 | ret = PTR_ERR(ctx->io_wq); |
| 8112 | ctx->io_wq = NULL; |
| 8113 | } |
| 8114 | return ret; |
| 8115 | } |
| 8116 | |
| 8117 | f = fdget(p->wq_fd); |
| 8118 | if (!f.file) |
| 8119 | return -EBADF; |
| 8120 | |
| 8121 | if (f.file->f_op != &io_uring_fops) { |
| 8122 | ret = -EINVAL; |
| 8123 | goto out_fput; |
| 8124 | } |
| 8125 | |
| 8126 | ctx_attach = f.file->private_data; |
| 8127 | /* @io_wq is protected by holding the fd */ |
| 8128 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8129 | ret = -EINVAL; |
| 8130 | goto out_fput; |
| 8131 | } |
| 8132 | |
| 8133 | ctx->io_wq = ctx_attach->io_wq; |
| 8134 | out_fput: |
| 8135 | fdput(f); |
| 8136 | return ret; |
| 8137 | } |
| 8138 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8139 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8140 | { |
| 8141 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8142 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8143 | |
| 8144 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8145 | if (unlikely(!tctx)) |
| 8146 | return -ENOMEM; |
| 8147 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8148 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8149 | if (unlikely(ret)) { |
| 8150 | kfree(tctx); |
| 8151 | return ret; |
| 8152 | } |
| 8153 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8154 | xa_init(&tctx->xa); |
| 8155 | init_waitqueue_head(&tctx->wait); |
| 8156 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8157 | atomic_set(&tctx->in_idle, 0); |
| 8158 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8159 | io_init_identity(&tctx->__identity); |
| 8160 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8161 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8162 | spin_lock_init(&tctx->task_lock); |
| 8163 | INIT_WQ_LIST(&tctx->task_list); |
| 8164 | tctx->task_state = 0; |
| 8165 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8166 | return 0; |
| 8167 | } |
| 8168 | |
| 8169 | void __io_uring_free(struct task_struct *tsk) |
| 8170 | { |
| 8171 | struct io_uring_task *tctx = tsk->io_uring; |
| 8172 | |
| 8173 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8174 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8175 | if (tctx->identity != &tctx->__identity) |
| 8176 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8177 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8178 | kfree(tctx); |
| 8179 | tsk->io_uring = NULL; |
| 8180 | } |
| 8181 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8182 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8183 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8184 | { |
| 8185 | int ret; |
| 8186 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8187 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8188 | struct io_sq_data *sqd; |
| 8189 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8190 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8191 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8192 | goto err; |
| 8193 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8194 | sqd = io_get_sq_data(p); |
| 8195 | if (IS_ERR(sqd)) { |
| 8196 | ret = PTR_ERR(sqd); |
| 8197 | goto err; |
| 8198 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8199 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8200 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8201 | io_sq_thread_park(sqd); |
| 8202 | mutex_lock(&sqd->ctx_lock); |
| 8203 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8204 | mutex_unlock(&sqd->ctx_lock); |
| 8205 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8206 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8207 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8208 | if (!ctx->sq_thread_idle) |
| 8209 | ctx->sq_thread_idle = HZ; |
| 8210 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8211 | if (sqd->thread) |
| 8212 | goto done; |
| 8213 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8214 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8215 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8216 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8217 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8218 | if (cpu >= nr_cpu_ids) |
| 8219 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8220 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8221 | goto err; |
| 8222 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8223 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8224 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8225 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8226 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8227 | "io_uring-sq"); |
| 8228 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8229 | if (IS_ERR(sqd->thread)) { |
| 8230 | ret = PTR_ERR(sqd->thread); |
| 8231 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8232 | goto err; |
| 8233 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8234 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8235 | if (ret) |
| 8236 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8237 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8238 | /* Can't have SQ_AFF without SQPOLL */ |
| 8239 | ret = -EINVAL; |
| 8240 | goto err; |
| 8241 | } |
| 8242 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8243 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8244 | ret = io_init_wq_offload(ctx, p); |
| 8245 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8246 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8247 | |
| 8248 | return 0; |
| 8249 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8250 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8251 | return ret; |
| 8252 | } |
| 8253 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8254 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8255 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8256 | struct io_sq_data *sqd = ctx->sq_data; |
| 8257 | |
| 8258 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8259 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8260 | } |
| 8261 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8262 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8263 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8264 | { |
| 8265 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8266 | } |
| 8267 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8268 | static inline int __io_account_mem(struct user_struct *user, |
| 8269 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8270 | { |
| 8271 | unsigned long page_limit, cur_pages, new_pages; |
| 8272 | |
| 8273 | /* Don't allow more pages than we can safely lock */ |
| 8274 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8275 | |
| 8276 | do { |
| 8277 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8278 | new_pages = cur_pages + nr_pages; |
| 8279 | if (new_pages > page_limit) |
| 8280 | return -ENOMEM; |
| 8281 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8282 | new_pages) != cur_pages); |
| 8283 | |
| 8284 | return 0; |
| 8285 | } |
| 8286 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8287 | 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] | 8288 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8289 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8290 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8291 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8292 | if (ctx->mm_account) |
| 8293 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8294 | } |
| 8295 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8296 | 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] | 8297 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8298 | int ret; |
| 8299 | |
| 8300 | if (ctx->limit_mem) { |
| 8301 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8302 | if (ret) |
| 8303 | return ret; |
| 8304 | } |
| 8305 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8306 | if (ctx->mm_account) |
| 8307 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8308 | |
| 8309 | return 0; |
| 8310 | } |
| 8311 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8312 | static void io_mem_free(void *ptr) |
| 8313 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8314 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8315 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8316 | if (!ptr) |
| 8317 | return; |
| 8318 | |
| 8319 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8320 | if (put_page_testzero(page)) |
| 8321 | free_compound_page(page); |
| 8322 | } |
| 8323 | |
| 8324 | static void *io_mem_alloc(size_t size) |
| 8325 | { |
| 8326 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8327 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8328 | |
| 8329 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8330 | } |
| 8331 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8332 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8333 | size_t *sq_offset) |
| 8334 | { |
| 8335 | struct io_rings *rings; |
| 8336 | size_t off, sq_array_size; |
| 8337 | |
| 8338 | off = struct_size(rings, cqes, cq_entries); |
| 8339 | if (off == SIZE_MAX) |
| 8340 | return SIZE_MAX; |
| 8341 | |
| 8342 | #ifdef CONFIG_SMP |
| 8343 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8344 | if (off == 0) |
| 8345 | return SIZE_MAX; |
| 8346 | #endif |
| 8347 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8348 | if (sq_offset) |
| 8349 | *sq_offset = off; |
| 8350 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8351 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8352 | if (sq_array_size == SIZE_MAX) |
| 8353 | return SIZE_MAX; |
| 8354 | |
| 8355 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8356 | return SIZE_MAX; |
| 8357 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8358 | return off; |
| 8359 | } |
| 8360 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8361 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8362 | { |
| 8363 | int i, j; |
| 8364 | |
| 8365 | if (!ctx->user_bufs) |
| 8366 | return -ENXIO; |
| 8367 | |
| 8368 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8369 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8370 | |
| 8371 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8372 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8373 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8374 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8375 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8376 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8377 | imu->nr_bvecs = 0; |
| 8378 | } |
| 8379 | |
| 8380 | kfree(ctx->user_bufs); |
| 8381 | ctx->user_bufs = NULL; |
| 8382 | ctx->nr_user_bufs = 0; |
| 8383 | return 0; |
| 8384 | } |
| 8385 | |
| 8386 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8387 | void __user *arg, unsigned index) |
| 8388 | { |
| 8389 | struct iovec __user *src; |
| 8390 | |
| 8391 | #ifdef CONFIG_COMPAT |
| 8392 | if (ctx->compat) { |
| 8393 | struct compat_iovec __user *ciovs; |
| 8394 | struct compat_iovec ciov; |
| 8395 | |
| 8396 | ciovs = (struct compat_iovec __user *) arg; |
| 8397 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8398 | return -EFAULT; |
| 8399 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8400 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8401 | dst->iov_len = ciov.iov_len; |
| 8402 | return 0; |
| 8403 | } |
| 8404 | #endif |
| 8405 | src = (struct iovec __user *) arg; |
| 8406 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8407 | return -EFAULT; |
| 8408 | return 0; |
| 8409 | } |
| 8410 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8411 | /* |
| 8412 | * Not super efficient, but this is just a registration time. And we do cache |
| 8413 | * the last compound head, so generally we'll only do a full search if we don't |
| 8414 | * match that one. |
| 8415 | * |
| 8416 | * We check if the given compound head page has already been accounted, to |
| 8417 | * avoid double accounting it. This allows us to account the full size of the |
| 8418 | * page, not just the constituent pages of a huge page. |
| 8419 | */ |
| 8420 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8421 | int nr_pages, struct page *hpage) |
| 8422 | { |
| 8423 | int i, j; |
| 8424 | |
| 8425 | /* check current page array */ |
| 8426 | for (i = 0; i < nr_pages; i++) { |
| 8427 | if (!PageCompound(pages[i])) |
| 8428 | continue; |
| 8429 | if (compound_head(pages[i]) == hpage) |
| 8430 | return true; |
| 8431 | } |
| 8432 | |
| 8433 | /* check previously registered pages */ |
| 8434 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8435 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8436 | |
| 8437 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8438 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8439 | continue; |
| 8440 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8441 | return true; |
| 8442 | } |
| 8443 | } |
| 8444 | |
| 8445 | return false; |
| 8446 | } |
| 8447 | |
| 8448 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8449 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8450 | struct page **last_hpage) |
| 8451 | { |
| 8452 | int i, ret; |
| 8453 | |
| 8454 | for (i = 0; i < nr_pages; i++) { |
| 8455 | if (!PageCompound(pages[i])) { |
| 8456 | imu->acct_pages++; |
| 8457 | } else { |
| 8458 | struct page *hpage; |
| 8459 | |
| 8460 | hpage = compound_head(pages[i]); |
| 8461 | if (hpage == *last_hpage) |
| 8462 | continue; |
| 8463 | *last_hpage = hpage; |
| 8464 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8465 | continue; |
| 8466 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8467 | } |
| 8468 | } |
| 8469 | |
| 8470 | if (!imu->acct_pages) |
| 8471 | return 0; |
| 8472 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8473 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8474 | if (ret) |
| 8475 | imu->acct_pages = 0; |
| 8476 | return ret; |
| 8477 | } |
| 8478 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8479 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8480 | struct io_mapped_ubuf *imu, |
| 8481 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8482 | { |
| 8483 | struct vm_area_struct **vmas = NULL; |
| 8484 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8485 | unsigned long off, start, end, ubuf; |
| 8486 | size_t size; |
| 8487 | int ret, pret, nr_pages, i; |
| 8488 | |
| 8489 | ubuf = (unsigned long) iov->iov_base; |
| 8490 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8491 | start = ubuf >> PAGE_SHIFT; |
| 8492 | nr_pages = end - start; |
| 8493 | |
| 8494 | ret = -ENOMEM; |
| 8495 | |
| 8496 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8497 | if (!pages) |
| 8498 | goto done; |
| 8499 | |
| 8500 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8501 | GFP_KERNEL); |
| 8502 | if (!vmas) |
| 8503 | goto done; |
| 8504 | |
| 8505 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8506 | GFP_KERNEL); |
| 8507 | if (!imu->bvec) |
| 8508 | goto done; |
| 8509 | |
| 8510 | ret = 0; |
| 8511 | mmap_read_lock(current->mm); |
| 8512 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8513 | pages, vmas); |
| 8514 | if (pret == nr_pages) { |
| 8515 | /* don't support file backed memory */ |
| 8516 | for (i = 0; i < nr_pages; i++) { |
| 8517 | struct vm_area_struct *vma = vmas[i]; |
| 8518 | |
| 8519 | if (vma->vm_file && |
| 8520 | !is_file_hugepages(vma->vm_file)) { |
| 8521 | ret = -EOPNOTSUPP; |
| 8522 | break; |
| 8523 | } |
| 8524 | } |
| 8525 | } else { |
| 8526 | ret = pret < 0 ? pret : -EFAULT; |
| 8527 | } |
| 8528 | mmap_read_unlock(current->mm); |
| 8529 | if (ret) { |
| 8530 | /* |
| 8531 | * if we did partial map, or found file backed vmas, |
| 8532 | * release any pages we did get |
| 8533 | */ |
| 8534 | if (pret > 0) |
| 8535 | unpin_user_pages(pages, pret); |
| 8536 | kvfree(imu->bvec); |
| 8537 | goto done; |
| 8538 | } |
| 8539 | |
| 8540 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8541 | if (ret) { |
| 8542 | unpin_user_pages(pages, pret); |
| 8543 | kvfree(imu->bvec); |
| 8544 | goto done; |
| 8545 | } |
| 8546 | |
| 8547 | off = ubuf & ~PAGE_MASK; |
| 8548 | size = iov->iov_len; |
| 8549 | for (i = 0; i < nr_pages; i++) { |
| 8550 | size_t vec_len; |
| 8551 | |
| 8552 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8553 | imu->bvec[i].bv_page = pages[i]; |
| 8554 | imu->bvec[i].bv_len = vec_len; |
| 8555 | imu->bvec[i].bv_offset = off; |
| 8556 | off = 0; |
| 8557 | size -= vec_len; |
| 8558 | } |
| 8559 | /* store original address for later verification */ |
| 8560 | imu->ubuf = ubuf; |
| 8561 | imu->len = iov->iov_len; |
| 8562 | imu->nr_bvecs = nr_pages; |
| 8563 | ret = 0; |
| 8564 | done: |
| 8565 | kvfree(pages); |
| 8566 | kvfree(vmas); |
| 8567 | return ret; |
| 8568 | } |
| 8569 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8570 | 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] | 8571 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8572 | if (ctx->user_bufs) |
| 8573 | return -EBUSY; |
| 8574 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8575 | return -EINVAL; |
| 8576 | |
| 8577 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8578 | GFP_KERNEL); |
| 8579 | if (!ctx->user_bufs) |
| 8580 | return -ENOMEM; |
| 8581 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8582 | return 0; |
| 8583 | } |
| 8584 | |
| 8585 | static int io_buffer_validate(struct iovec *iov) |
| 8586 | { |
| 8587 | /* |
| 8588 | * Don't impose further limits on the size and buffer |
| 8589 | * constraints here, we'll -EINVAL later when IO is |
| 8590 | * submitted if they are wrong. |
| 8591 | */ |
| 8592 | if (!iov->iov_base || !iov->iov_len) |
| 8593 | return -EFAULT; |
| 8594 | |
| 8595 | /* arbitrary limit, but we need something */ |
| 8596 | if (iov->iov_len > SZ_1G) |
| 8597 | return -EFAULT; |
| 8598 | |
| 8599 | return 0; |
| 8600 | } |
| 8601 | |
| 8602 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8603 | unsigned int nr_args) |
| 8604 | { |
| 8605 | int i, ret; |
| 8606 | struct iovec iov; |
| 8607 | struct page *last_hpage = NULL; |
| 8608 | |
| 8609 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8610 | if (ret) |
| 8611 | return ret; |
| 8612 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8613 | for (i = 0; i < nr_args; i++) { |
| 8614 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8615 | |
| 8616 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8617 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8618 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8619 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8620 | ret = io_buffer_validate(&iov); |
| 8621 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8622 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8623 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8624 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8625 | if (ret) |
| 8626 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8627 | |
| 8628 | ctx->nr_user_bufs++; |
| 8629 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8630 | |
| 8631 | if (ret) |
| 8632 | io_sqe_buffers_unregister(ctx); |
| 8633 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8634 | return ret; |
| 8635 | } |
| 8636 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8637 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8638 | { |
| 8639 | __s32 __user *fds = arg; |
| 8640 | int fd; |
| 8641 | |
| 8642 | if (ctx->cq_ev_fd) |
| 8643 | return -EBUSY; |
| 8644 | |
| 8645 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8646 | return -EFAULT; |
| 8647 | |
| 8648 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8649 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8650 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8651 | ctx->cq_ev_fd = NULL; |
| 8652 | return ret; |
| 8653 | } |
| 8654 | |
| 8655 | return 0; |
| 8656 | } |
| 8657 | |
| 8658 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8659 | { |
| 8660 | if (ctx->cq_ev_fd) { |
| 8661 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8662 | ctx->cq_ev_fd = NULL; |
| 8663 | return 0; |
| 8664 | } |
| 8665 | |
| 8666 | return -ENXIO; |
| 8667 | } |
| 8668 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8669 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8670 | { |
| 8671 | struct io_ring_ctx *ctx = data; |
| 8672 | struct io_buffer *buf = p; |
| 8673 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8674 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8675 | return 0; |
| 8676 | } |
| 8677 | |
| 8678 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8679 | { |
| 8680 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8681 | idr_destroy(&ctx->io_buffer_idr); |
| 8682 | } |
| 8683 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8684 | 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] | 8685 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8686 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8687 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8688 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8689 | if (tsk && req->task != tsk) |
| 8690 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8691 | list_del(&req->compl.list); |
| 8692 | kmem_cache_free(req_cachep, req); |
| 8693 | } |
| 8694 | } |
| 8695 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8696 | 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] | 8697 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8698 | struct io_submit_state *submit_state = &ctx->submit_state; |
| 8699 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8700 | mutex_lock(&ctx->uring_lock); |
| 8701 | |
| 8702 | if (submit_state->free_reqs) |
| 8703 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8704 | submit_state->reqs); |
| 8705 | |
| 8706 | io_req_cache_free(&submit_state->comp.free_list, NULL); |
| 8707 | |
| 8708 | spin_lock_irq(&ctx->completion_lock); |
| 8709 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); |
| 8710 | spin_unlock_irq(&ctx->completion_lock); |
| 8711 | |
| 8712 | mutex_unlock(&ctx->uring_lock); |
| 8713 | } |
| 8714 | |
| 8715 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8716 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8717 | /* |
| 8718 | * Some may use context even when all refs and requests have been put, |
| 8719 | * and they are free to do so while still holding uring_lock, see |
| 8720 | * __io_req_task_submit(). Wait for them to finish. |
| 8721 | */ |
| 8722 | mutex_lock(&ctx->uring_lock); |
| 8723 | mutex_unlock(&ctx->uring_lock); |
| 8724 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8725 | io_finish_async(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8726 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8727 | |
| 8728 | if (ctx->sqo_task) { |
| 8729 | put_task_struct(ctx->sqo_task); |
| 8730 | ctx->sqo_task = NULL; |
| 8731 | mmdrop(ctx->mm_account); |
| 8732 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8733 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8734 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8735 | #ifdef CONFIG_BLK_CGROUP |
| 8736 | if (ctx->sqo_blkcg_css) |
| 8737 | css_put(ctx->sqo_blkcg_css); |
| 8738 | #endif |
| 8739 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8740 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8741 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8742 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8743 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8744 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8745 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8746 | if (ctx->ring_sock) { |
| 8747 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8748 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8749 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8750 | #endif |
| 8751 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8752 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8753 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8754 | |
| 8755 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8756 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8757 | put_cred(ctx->creds); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8758 | io_req_caches_free(ctx, NULL); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8759 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8760 | kfree(ctx); |
| 8761 | } |
| 8762 | |
| 8763 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8764 | { |
| 8765 | struct io_ring_ctx *ctx = file->private_data; |
| 8766 | __poll_t mask = 0; |
| 8767 | |
| 8768 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8769 | /* |
| 8770 | * synchronizes with barrier from wq_has_sleeper call in |
| 8771 | * io_commit_cqring |
| 8772 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8773 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8774 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8775 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8776 | |
| 8777 | /* |
| 8778 | * Don't flush cqring overflow list here, just do a simple check. |
| 8779 | * Otherwise there could possible be ABBA deadlock: |
| 8780 | * CPU0 CPU1 |
| 8781 | * ---- ---- |
| 8782 | * lock(&ctx->uring_lock); |
| 8783 | * lock(&ep->mtx); |
| 8784 | * lock(&ctx->uring_lock); |
| 8785 | * lock(&ep->mtx); |
| 8786 | * |
| 8787 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8788 | * pushs them to do the flush. |
| 8789 | */ |
| 8790 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8791 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8792 | |
| 8793 | return mask; |
| 8794 | } |
| 8795 | |
| 8796 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8797 | { |
| 8798 | struct io_ring_ctx *ctx = file->private_data; |
| 8799 | |
| 8800 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8801 | } |
| 8802 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8803 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8804 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8805 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8806 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8807 | iod = idr_remove(&ctx->personality_idr, id); |
| 8808 | if (iod) { |
| 8809 | put_cred(iod->creds); |
| 8810 | if (refcount_dec_and_test(&iod->count)) |
| 8811 | kfree(iod); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8812 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8813 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8814 | |
| 8815 | return -EINVAL; |
| 8816 | } |
| 8817 | |
| 8818 | static int io_remove_personalities(int id, void *p, void *data) |
| 8819 | { |
| 8820 | struct io_ring_ctx *ctx = data; |
| 8821 | |
| 8822 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8823 | return 0; |
| 8824 | } |
| 8825 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8826 | static void io_ring_exit_work(struct work_struct *work) |
| 8827 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8828 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8829 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8830 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8831 | /* |
| 8832 | * If we're doing polled IO and end up having requests being |
| 8833 | * submitted async (out-of-line), then completions can come in while |
| 8834 | * we're waiting for refs to drop. We need to reap these manually, |
| 8835 | * as nobody else will be looking for them. |
| 8836 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8837 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8838 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8839 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8840 | io_ring_ctx_free(ctx); |
| 8841 | } |
| 8842 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8843 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8844 | { |
| 8845 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8846 | |
| 8847 | return req->ctx == data; |
| 8848 | } |
| 8849 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8850 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8851 | { |
| 8852 | mutex_lock(&ctx->uring_lock); |
| 8853 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8854 | |
| 8855 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8856 | ctx->sqo_dead = 1; |
| 8857 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8858 | /* if force is set, the ring is going away. always drop after that */ |
| 8859 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8860 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8861 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8862 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8863 | mutex_unlock(&ctx->uring_lock); |
| 8864 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8865 | io_kill_timeouts(ctx, NULL, NULL); |
| 8866 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8867 | |
| 8868 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8869 | 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] | 8870 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8871 | /* 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] | 8872 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8873 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8874 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8875 | /* |
| 8876 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8877 | * if we're exiting a ton of rings at the same time. It just adds |
| 8878 | * noise and overhead, there's no discernable change in runtime |
| 8879 | * over using system_wq. |
| 8880 | */ |
| 8881 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8882 | } |
| 8883 | |
| 8884 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8885 | { |
| 8886 | struct io_ring_ctx *ctx = file->private_data; |
| 8887 | |
| 8888 | file->private_data = NULL; |
| 8889 | io_ring_ctx_wait_and_kill(ctx); |
| 8890 | return 0; |
| 8891 | } |
| 8892 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8893 | struct io_task_cancel { |
| 8894 | struct task_struct *task; |
| 8895 | struct files_struct *files; |
| 8896 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8897 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8898 | 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] | 8899 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8900 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8901 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8902 | bool ret; |
| 8903 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8904 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8905 | unsigned long flags; |
| 8906 | struct io_ring_ctx *ctx = req->ctx; |
| 8907 | |
| 8908 | /* protect against races with linked timeouts */ |
| 8909 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8910 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8911 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8912 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8913 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8914 | } |
| 8915 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8916 | } |
| 8917 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8918 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8919 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8920 | struct files_struct *files) |
| 8921 | { |
| 8922 | struct io_defer_entry *de = NULL; |
| 8923 | LIST_HEAD(list); |
| 8924 | |
| 8925 | spin_lock_irq(&ctx->completion_lock); |
| 8926 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8927 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8928 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8929 | break; |
| 8930 | } |
| 8931 | } |
| 8932 | spin_unlock_irq(&ctx->completion_lock); |
| 8933 | |
| 8934 | while (!list_empty(&list)) { |
| 8935 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8936 | list_del_init(&de->list); |
| 8937 | req_set_fail_links(de->req); |
| 8938 | io_put_req(de->req); |
| 8939 | io_req_complete(de->req, -ECANCELED); |
| 8940 | kfree(de); |
| 8941 | } |
| 8942 | } |
| 8943 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8944 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8945 | struct task_struct *task, |
| 8946 | struct files_struct *files) |
| 8947 | { |
| 8948 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
| 8949 | |
| 8950 | while (1) { |
| 8951 | enum io_wq_cancel cret; |
| 8952 | bool ret = false; |
| 8953 | |
| 8954 | if (ctx->io_wq) { |
| 8955 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8956 | &cancel, true); |
| 8957 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8958 | } |
| 8959 | |
| 8960 | /* SQPOLL thread does its own polling */ |
| 8961 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8962 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8963 | io_iopoll_try_reap_events(ctx); |
| 8964 | ret = true; |
| 8965 | } |
| 8966 | } |
| 8967 | |
| 8968 | ret |= io_poll_remove_all(ctx, task, files); |
| 8969 | ret |= io_kill_timeouts(ctx, task, files); |
| 8970 | ret |= io_run_task_work(); |
| 8971 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8972 | if (!ret) |
| 8973 | break; |
| 8974 | cond_resched(); |
| 8975 | } |
| 8976 | } |
| 8977 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8978 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8979 | struct task_struct *task, |
| 8980 | struct files_struct *files) |
| 8981 | { |
| 8982 | struct io_kiocb *req; |
| 8983 | int cnt = 0; |
| 8984 | |
| 8985 | spin_lock_irq(&ctx->inflight_lock); |
| 8986 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8987 | cnt += io_match_task(req, task, files); |
| 8988 | spin_unlock_irq(&ctx->inflight_lock); |
| 8989 | return cnt; |
| 8990 | } |
| 8991 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8992 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8993 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8994 | struct files_struct *files) |
| 8995 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8996 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8997 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8998 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8999 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9000 | inflight = io_uring_count_inflight(ctx, task, files); |
| 9001 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9002 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9003 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9004 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9005 | |
| 9006 | if (ctx->sq_data) |
| 9007 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 9008 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 9009 | TASK_UNINTERRUPTIBLE); |
| 9010 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 9011 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 9012 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 9013 | if (ctx->sq_data) |
| 9014 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9015 | } |
| 9016 | } |
| 9017 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9018 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 9019 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9020 | mutex_lock(&ctx->uring_lock); |
| 9021 | ctx->sqo_dead = 1; |
| 9022 | mutex_unlock(&ctx->uring_lock); |
| 9023 | |
| 9024 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 9025 | if (ctx->rings) |
| 9026 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9029 | /* |
| 9030 | * We need to iteratively cancel requests, in case a request has dependent |
| 9031 | * hard links. These persist even for failure of cancelations, hence keep |
| 9032 | * looping until none are found. |
| 9033 | */ |
| 9034 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 9035 | struct files_struct *files) |
| 9036 | { |
| 9037 | struct task_struct *task = current; |
| 9038 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9039 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9040 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9041 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9042 | atomic_inc(&task->io_uring->in_idle); |
| 9043 | io_sq_thread_park(ctx->sq_data); |
| 9044 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9045 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 9046 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9047 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 9048 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 9049 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9050 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9051 | |
| 9052 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 9053 | atomic_dec(&task->io_uring->in_idle); |
| 9054 | /* |
| 9055 | * If the files that are going away are the ones in the thread |
| 9056 | * identity, clear them out. |
| 9057 | */ |
| 9058 | if (task->io_uring->identity->files == files) |
| 9059 | task->io_uring->identity->files = NULL; |
| 9060 | io_sq_thread_unpark(ctx->sq_data); |
| 9061 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | /* |
| 9065 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9066 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9067 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9068 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9069 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9070 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9071 | |
| 9072 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9073 | ret = io_uring_alloc_task_context(current); |
| 9074 | if (unlikely(ret)) |
| 9075 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9076 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9077 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9078 | if (tctx->last != file) { |
| 9079 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9080 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9081 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9082 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9083 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 9084 | file, GFP_KERNEL)); |
| 9085 | if (ret) { |
| 9086 | fput(file); |
| 9087 | return ret; |
| 9088 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 9089 | |
| 9090 | /* one and only SQPOLL file note, held by sqo_task */ |
| 9091 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 9092 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9093 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9094 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9095 | } |
| 9096 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9097 | /* |
| 9098 | * This is race safe in that the task itself is doing this, hence it |
| 9099 | * cannot be going through the exit/cancel paths at the same time. |
| 9100 | * This cannot be modified while exit/cancel is running. |
| 9101 | */ |
| 9102 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9103 | tctx->sqpoll = true; |
| 9104 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9105 | return 0; |
| 9106 | } |
| 9107 | |
| 9108 | /* |
| 9109 | * Remove this io_uring_file -> task mapping. |
| 9110 | */ |
| 9111 | static void io_uring_del_task_file(struct file *file) |
| 9112 | { |
| 9113 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9114 | |
| 9115 | if (tctx->last == file) |
| 9116 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9117 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9118 | if (file) |
| 9119 | fput(file); |
| 9120 | } |
| 9121 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9122 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9123 | { |
| 9124 | struct file *file; |
| 9125 | unsigned long index; |
| 9126 | |
| 9127 | xa_for_each(&tctx->xa, index, file) |
| 9128 | io_uring_del_task_file(file); |
| 9129 | } |
| 9130 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9131 | void __io_uring_files_cancel(struct files_struct *files) |
| 9132 | { |
| 9133 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9134 | struct file *file; |
| 9135 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9136 | |
| 9137 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9138 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9139 | xa_for_each(&tctx->xa, index, file) |
| 9140 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9141 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9142 | |
| 9143 | if (files) |
| 9144 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9145 | } |
| 9146 | |
| 9147 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9148 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9149 | return percpu_counter_sum(&tctx->inflight); |
| 9150 | } |
| 9151 | |
| 9152 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 9153 | { |
| 9154 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9155 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9156 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9157 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9158 | if (!ctx->sq_data) |
| 9159 | return; |
| 9160 | tctx = ctx->sq_data->thread->io_uring; |
| 9161 | io_disable_sqo_submit(ctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9162 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9163 | atomic_inc(&tctx->in_idle); |
| 9164 | do { |
| 9165 | /* read completions before cancelations */ |
| 9166 | inflight = tctx_inflight(tctx); |
| 9167 | if (!inflight) |
| 9168 | break; |
| 9169 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9170 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9171 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9172 | /* |
| 9173 | * If we've seen completions, retry without waiting. This |
| 9174 | * avoids a race where a completion comes in before we did |
| 9175 | * prepare_to_wait(). |
| 9176 | */ |
| 9177 | if (inflight == tctx_inflight(tctx)) |
| 9178 | schedule(); |
| 9179 | finish_wait(&tctx->wait, &wait); |
| 9180 | } while (1); |
| 9181 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9182 | } |
| 9183 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9184 | /* |
| 9185 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9186 | * requests. |
| 9187 | */ |
| 9188 | void __io_uring_task_cancel(void) |
| 9189 | { |
| 9190 | struct io_uring_task *tctx = current->io_uring; |
| 9191 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9192 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9193 | |
| 9194 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9195 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9196 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9197 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9198 | if (tctx->sqpoll) { |
| 9199 | struct file *file; |
| 9200 | unsigned long index; |
| 9201 | |
| 9202 | xa_for_each(&tctx->xa, index, file) |
| 9203 | io_uring_cancel_sqpoll(file->private_data); |
| 9204 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9205 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9206 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9207 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9208 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9209 | if (!inflight) |
| 9210 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9211 | __io_uring_files_cancel(NULL); |
| 9212 | |
| 9213 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9214 | |
| 9215 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9216 | * If we've seen completions, retry without waiting. This |
| 9217 | * avoids a race where a completion comes in before we did |
| 9218 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9219 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9220 | if (inflight == tctx_inflight(tctx)) |
| 9221 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9222 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9223 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9224 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9225 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9226 | |
| 9227 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9228 | } |
| 9229 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9230 | static int io_uring_flush(struct file *file, void *data) |
| 9231 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9232 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9233 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9234 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9235 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9236 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 9237 | io_req_caches_free(ctx, current); |
| 9238 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 9239 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9240 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9241 | return 0; |
| 9242 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9243 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9244 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9245 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9246 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9247 | /* |
| 9248 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9249 | * task file note. If the task is exiting, drop regardless of count. |
| 9250 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9251 | if (atomic_long_read(&file->f_count) != 2) |
| 9252 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9253 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9254 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9255 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 9256 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9257 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9258 | /* sqo_dead check is for when this happens after cancellation */ |
| 9259 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9260 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9261 | |
| 9262 | io_disable_sqo_submit(ctx); |
| 9263 | } |
| 9264 | |
| 9265 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9266 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9267 | return 0; |
| 9268 | } |
| 9269 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9270 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9271 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9272 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9273 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9274 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9275 | struct page *page; |
| 9276 | void *ptr; |
| 9277 | |
| 9278 | switch (offset) { |
| 9279 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9280 | case IORING_OFF_CQ_RING: |
| 9281 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9282 | break; |
| 9283 | case IORING_OFF_SQES: |
| 9284 | ptr = ctx->sq_sqes; |
| 9285 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9286 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9287 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9291 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9292 | return ERR_PTR(-EINVAL); |
| 9293 | |
| 9294 | return ptr; |
| 9295 | } |
| 9296 | |
| 9297 | #ifdef CONFIG_MMU |
| 9298 | |
| 9299 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9300 | { |
| 9301 | size_t sz = vma->vm_end - vma->vm_start; |
| 9302 | unsigned long pfn; |
| 9303 | void *ptr; |
| 9304 | |
| 9305 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9306 | if (IS_ERR(ptr)) |
| 9307 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9308 | |
| 9309 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9310 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9311 | } |
| 9312 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9313 | #else /* !CONFIG_MMU */ |
| 9314 | |
| 9315 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9316 | { |
| 9317 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9318 | } |
| 9319 | |
| 9320 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9321 | { |
| 9322 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9323 | } |
| 9324 | |
| 9325 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9326 | unsigned long addr, unsigned long len, |
| 9327 | unsigned long pgoff, unsigned long flags) |
| 9328 | { |
| 9329 | void *ptr; |
| 9330 | |
| 9331 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9332 | if (IS_ERR(ptr)) |
| 9333 | return PTR_ERR(ptr); |
| 9334 | |
| 9335 | return (unsigned long) ptr; |
| 9336 | } |
| 9337 | |
| 9338 | #endif /* !CONFIG_MMU */ |
| 9339 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9340 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9341 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9342 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9343 | DEFINE_WAIT(wait); |
| 9344 | |
| 9345 | do { |
| 9346 | if (!io_sqring_full(ctx)) |
| 9347 | break; |
| 9348 | |
| 9349 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9350 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9351 | if (unlikely(ctx->sqo_dead)) { |
| 9352 | ret = -EOWNERDEAD; |
| 9353 | goto out; |
| 9354 | } |
| 9355 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9356 | if (!io_sqring_full(ctx)) |
| 9357 | break; |
| 9358 | |
| 9359 | schedule(); |
| 9360 | } while (!signal_pending(current)); |
| 9361 | |
| 9362 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9363 | out: |
| 9364 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9365 | } |
| 9366 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9367 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9368 | struct __kernel_timespec __user **ts, |
| 9369 | const sigset_t __user **sig) |
| 9370 | { |
| 9371 | struct io_uring_getevents_arg arg; |
| 9372 | |
| 9373 | /* |
| 9374 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9375 | * is just a pointer to the sigset_t. |
| 9376 | */ |
| 9377 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9378 | *sig = (const sigset_t __user *) argp; |
| 9379 | *ts = NULL; |
| 9380 | return 0; |
| 9381 | } |
| 9382 | |
| 9383 | /* |
| 9384 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9385 | * timespec and sigset_t pointers if good. |
| 9386 | */ |
| 9387 | if (*argsz != sizeof(arg)) |
| 9388 | return -EINVAL; |
| 9389 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9390 | return -EFAULT; |
| 9391 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9392 | *argsz = arg.sigmask_sz; |
| 9393 | *ts = u64_to_user_ptr(arg.ts); |
| 9394 | return 0; |
| 9395 | } |
| 9396 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9397 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9398 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9399 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9400 | { |
| 9401 | struct io_ring_ctx *ctx; |
| 9402 | long ret = -EBADF; |
| 9403 | int submitted = 0; |
| 9404 | struct fd f; |
| 9405 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9406 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9407 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9408 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9409 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9410 | return -EINVAL; |
| 9411 | |
| 9412 | f = fdget(fd); |
| 9413 | if (!f.file) |
| 9414 | return -EBADF; |
| 9415 | |
| 9416 | ret = -EOPNOTSUPP; |
| 9417 | if (f.file->f_op != &io_uring_fops) |
| 9418 | goto out_fput; |
| 9419 | |
| 9420 | ret = -ENXIO; |
| 9421 | ctx = f.file->private_data; |
| 9422 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9423 | goto out_fput; |
| 9424 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9425 | ret = -EBADFD; |
| 9426 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9427 | goto out; |
| 9428 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9429 | /* |
| 9430 | * For SQ polling, the thread will do all submissions and completions. |
| 9431 | * Just return the requested submit count, and wake the thread if |
| 9432 | * we were asked to. |
| 9433 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9434 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9435 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9436 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9437 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9438 | ret = -EOWNERDEAD; |
| 9439 | if (unlikely(ctx->sqo_dead)) |
| 9440 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9441 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9442 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9443 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9444 | ret = io_sqpoll_wait_sq(ctx); |
| 9445 | if (ret) |
| 9446 | goto out; |
| 9447 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9448 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9449 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9450 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9451 | if (unlikely(ret)) |
| 9452 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9453 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9454 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9455 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9456 | |
| 9457 | if (submitted != to_submit) |
| 9458 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9459 | } |
| 9460 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9461 | const sigset_t __user *sig; |
| 9462 | struct __kernel_timespec __user *ts; |
| 9463 | |
| 9464 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9465 | if (unlikely(ret)) |
| 9466 | goto out; |
| 9467 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9468 | min_complete = min(min_complete, ctx->cq_entries); |
| 9469 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9470 | /* |
| 9471 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9472 | * space applications don't need to do io completion events |
| 9473 | * polling again, they can rely on io_sq_thread to do polling |
| 9474 | * work, which can reduce cpu usage and uring_lock contention. |
| 9475 | */ |
| 9476 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9477 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9478 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9479 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9480 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9481 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9482 | } |
| 9483 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9484 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9485 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9486 | out_fput: |
| 9487 | fdput(f); |
| 9488 | return submitted ? submitted : ret; |
| 9489 | } |
| 9490 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9491 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9492 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9493 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9494 | struct io_identity *iod = p; |
| 9495 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9496 | struct seq_file *m = data; |
| 9497 | struct user_namespace *uns = seq_user_ns(m); |
| 9498 | struct group_info *gi; |
| 9499 | kernel_cap_t cap; |
| 9500 | unsigned __capi; |
| 9501 | int g; |
| 9502 | |
| 9503 | seq_printf(m, "%5d\n", id); |
| 9504 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9505 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9506 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9507 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9508 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9509 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9510 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9511 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9512 | seq_puts(m, "\n\tGroups:\t"); |
| 9513 | gi = cred->group_info; |
| 9514 | for (g = 0; g < gi->ngroups; g++) { |
| 9515 | seq_put_decimal_ull(m, g ? " " : "", |
| 9516 | from_kgid_munged(uns, gi->gid[g])); |
| 9517 | } |
| 9518 | seq_puts(m, "\n\tCapEff:\t"); |
| 9519 | cap = cred->cap_effective; |
| 9520 | CAP_FOR_EACH_U32(__capi) |
| 9521 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9522 | seq_putc(m, '\n'); |
| 9523 | return 0; |
| 9524 | } |
| 9525 | |
| 9526 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9527 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9528 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9529 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9530 | int i; |
| 9531 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9532 | /* |
| 9533 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9534 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9535 | * cases. If we fail to get the lock, we just don't iterate any |
| 9536 | * structures that could be going away outside the io_uring mutex. |
| 9537 | */ |
| 9538 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9539 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9540 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9541 | sq = ctx->sq_data; |
| 9542 | |
| 9543 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9544 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9545 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9546 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9547 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9548 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9549 | if (f) |
| 9550 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9551 | else |
| 9552 | seq_printf(m, "%5u: <none>\n", i); |
| 9553 | } |
| 9554 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9555 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9556 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9557 | |
| 9558 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9559 | (unsigned int) buf->len); |
| 9560 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9561 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9562 | seq_printf(m, "Personalities:\n"); |
| 9563 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9564 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9565 | seq_printf(m, "PollList:\n"); |
| 9566 | spin_lock_irq(&ctx->completion_lock); |
| 9567 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9568 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9569 | struct io_kiocb *req; |
| 9570 | |
| 9571 | hlist_for_each_entry(req, list, hash_node) |
| 9572 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9573 | req->task->task_works != NULL); |
| 9574 | } |
| 9575 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9576 | if (has_lock) |
| 9577 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9581 | { |
| 9582 | struct io_ring_ctx *ctx = f->private_data; |
| 9583 | |
| 9584 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9585 | __io_uring_show_fdinfo(ctx, m); |
| 9586 | percpu_ref_put(&ctx->refs); |
| 9587 | } |
| 9588 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9589 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9590 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9591 | static const struct file_operations io_uring_fops = { |
| 9592 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9593 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9594 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9595 | #ifndef CONFIG_MMU |
| 9596 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9597 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9598 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9599 | .poll = io_uring_poll, |
| 9600 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9601 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9602 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9603 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9604 | }; |
| 9605 | |
| 9606 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9607 | struct io_uring_params *p) |
| 9608 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9609 | struct io_rings *rings; |
| 9610 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9611 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9612 | /* make sure these are sane, as we already accounted them */ |
| 9613 | ctx->sq_entries = p->sq_entries; |
| 9614 | ctx->cq_entries = p->cq_entries; |
| 9615 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9616 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9617 | if (size == SIZE_MAX) |
| 9618 | return -EOVERFLOW; |
| 9619 | |
| 9620 | rings = io_mem_alloc(size); |
| 9621 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9622 | return -ENOMEM; |
| 9623 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9624 | ctx->rings = rings; |
| 9625 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9626 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9627 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9628 | rings->sq_ring_entries = p->sq_entries; |
| 9629 | rings->cq_ring_entries = p->cq_entries; |
| 9630 | ctx->sq_mask = rings->sq_ring_mask; |
| 9631 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9632 | |
| 9633 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9634 | if (size == SIZE_MAX) { |
| 9635 | io_mem_free(ctx->rings); |
| 9636 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9637 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9638 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9639 | |
| 9640 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9641 | if (!ctx->sq_sqes) { |
| 9642 | io_mem_free(ctx->rings); |
| 9643 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9644 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9645 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9646 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9647 | return 0; |
| 9648 | } |
| 9649 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9650 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9651 | { |
| 9652 | int ret, fd; |
| 9653 | |
| 9654 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9655 | if (fd < 0) |
| 9656 | return fd; |
| 9657 | |
| 9658 | ret = io_uring_add_task_file(ctx, file); |
| 9659 | if (ret) { |
| 9660 | put_unused_fd(fd); |
| 9661 | return ret; |
| 9662 | } |
| 9663 | fd_install(fd, file); |
| 9664 | return fd; |
| 9665 | } |
| 9666 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9667 | /* |
| 9668 | * Allocate an anonymous fd, this is what constitutes the application |
| 9669 | * visible backing of an io_uring instance. The application mmaps this |
| 9670 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9671 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9672 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9673 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9674 | { |
| 9675 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9676 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9677 | int ret; |
| 9678 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9679 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9680 | &ctx->ring_sock); |
| 9681 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9682 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9683 | #endif |
| 9684 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9685 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9686 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9687 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9688 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9689 | sock_release(ctx->ring_sock); |
| 9690 | ctx->ring_sock = NULL; |
| 9691 | } else { |
| 9692 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9693 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9694 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9695 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9696 | } |
| 9697 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9698 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9699 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9700 | { |
| 9701 | struct user_struct *user = NULL; |
| 9702 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9703 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9704 | int ret; |
| 9705 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9706 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9707 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9708 | if (entries > IORING_MAX_ENTRIES) { |
| 9709 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9710 | return -EINVAL; |
| 9711 | entries = IORING_MAX_ENTRIES; |
| 9712 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9713 | |
| 9714 | /* |
| 9715 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9716 | * application to drive a higher depth than the size of the SQ ring, |
| 9717 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9718 | * some flexibility in overcommitting a bit. If the application has |
| 9719 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9720 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9721 | */ |
| 9722 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9723 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9724 | /* |
| 9725 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9726 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9727 | * any cq vs sq ring sizing. |
| 9728 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9729 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9730 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9731 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9732 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9733 | return -EINVAL; |
| 9734 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9735 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9736 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9737 | if (p->cq_entries < p->sq_entries) |
| 9738 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9739 | } else { |
| 9740 | p->cq_entries = 2 * p->sq_entries; |
| 9741 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9742 | |
| 9743 | user = get_uid(current_user()); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9744 | |
| 9745 | ctx = io_ring_ctx_alloc(p); |
| 9746 | if (!ctx) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9747 | free_uid(user); |
| 9748 | return -ENOMEM; |
| 9749 | } |
| 9750 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9751 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9752 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9753 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9754 | #ifdef CONFIG_AUDIT |
| 9755 | ctx->loginuid = current->loginuid; |
| 9756 | ctx->sessionid = current->sessionid; |
| 9757 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9758 | ctx->sqo_task = get_task_struct(current); |
| 9759 | |
| 9760 | /* |
| 9761 | * This is just grabbed for accounting purposes. When a process exits, |
| 9762 | * the mm is exited and dropped before the files, hence we need to hang |
| 9763 | * on to this mm purely for the purposes of being able to unaccount |
| 9764 | * memory (locked/pinned vm). It's not used for anything else. |
| 9765 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9766 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9767 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9768 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9769 | #ifdef CONFIG_BLK_CGROUP |
| 9770 | /* |
| 9771 | * The sq thread will belong to the original cgroup it was inited in. |
| 9772 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9773 | * issued bios will be associated with the closest cgroup later in the |
| 9774 | * block layer. |
| 9775 | */ |
| 9776 | rcu_read_lock(); |
| 9777 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9778 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9779 | rcu_read_unlock(); |
| 9780 | if (!ret) { |
| 9781 | /* don't init against a dying cgroup, have the user try again */ |
| 9782 | ctx->sqo_blkcg_css = NULL; |
| 9783 | ret = -ENODEV; |
| 9784 | goto err; |
| 9785 | } |
| 9786 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9787 | ret = io_allocate_scq_urings(ctx, p); |
| 9788 | if (ret) |
| 9789 | goto err; |
| 9790 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9791 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9792 | if (ret) |
| 9793 | goto err; |
| 9794 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9795 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9796 | io_sq_offload_start(ctx); |
| 9797 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9798 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9799 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9800 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9801 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9802 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9803 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9804 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9805 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9806 | |
| 9807 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9808 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9809 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9810 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9811 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9812 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9813 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9814 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9815 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9816 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9817 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9818 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9819 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9820 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9821 | |
| 9822 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9823 | ret = -EFAULT; |
| 9824 | goto err; |
| 9825 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9826 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9827 | file = io_uring_get_file(ctx); |
| 9828 | if (IS_ERR(file)) { |
| 9829 | ret = PTR_ERR(file); |
| 9830 | goto err; |
| 9831 | } |
| 9832 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9833 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9834 | * Install ring fd as the very last thing, so we don't risk someone |
| 9835 | * having closed it before we finish setup |
| 9836 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9837 | ret = io_uring_install_fd(ctx, file); |
| 9838 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9839 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9840 | /* fput will clean it up */ |
| 9841 | fput(file); |
| 9842 | return ret; |
| 9843 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9844 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9845 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9846 | return ret; |
| 9847 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9848 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9849 | io_ring_ctx_wait_and_kill(ctx); |
| 9850 | return ret; |
| 9851 | } |
| 9852 | |
| 9853 | /* |
| 9854 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9855 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9856 | * params structure passed in. |
| 9857 | */ |
| 9858 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9859 | { |
| 9860 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9861 | int i; |
| 9862 | |
| 9863 | if (copy_from_user(&p, params, sizeof(p))) |
| 9864 | return -EFAULT; |
| 9865 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9866 | if (p.resv[i]) |
| 9867 | return -EINVAL; |
| 9868 | } |
| 9869 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9870 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9871 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9872 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9873 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9874 | return -EINVAL; |
| 9875 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9876 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9877 | } |
| 9878 | |
| 9879 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9880 | struct io_uring_params __user *, params) |
| 9881 | { |
| 9882 | return io_uring_setup(entries, params); |
| 9883 | } |
| 9884 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9885 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9886 | { |
| 9887 | struct io_uring_probe *p; |
| 9888 | size_t size; |
| 9889 | int i, ret; |
| 9890 | |
| 9891 | size = struct_size(p, ops, nr_args); |
| 9892 | if (size == SIZE_MAX) |
| 9893 | return -EOVERFLOW; |
| 9894 | p = kzalloc(size, GFP_KERNEL); |
| 9895 | if (!p) |
| 9896 | return -ENOMEM; |
| 9897 | |
| 9898 | ret = -EFAULT; |
| 9899 | if (copy_from_user(p, arg, size)) |
| 9900 | goto out; |
| 9901 | ret = -EINVAL; |
| 9902 | if (memchr_inv(p, 0, size)) |
| 9903 | goto out; |
| 9904 | |
| 9905 | p->last_op = IORING_OP_LAST - 1; |
| 9906 | if (nr_args > IORING_OP_LAST) |
| 9907 | nr_args = IORING_OP_LAST; |
| 9908 | |
| 9909 | for (i = 0; i < nr_args; i++) { |
| 9910 | p->ops[i].op = i; |
| 9911 | if (!io_op_defs[i].not_supported) |
| 9912 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9913 | } |
| 9914 | p->ops_len = i; |
| 9915 | |
| 9916 | ret = 0; |
| 9917 | if (copy_to_user(arg, p, size)) |
| 9918 | ret = -EFAULT; |
| 9919 | out: |
| 9920 | kfree(p); |
| 9921 | return ret; |
| 9922 | } |
| 9923 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9924 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9925 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9926 | struct io_identity *id; |
| 9927 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9928 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9929 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9930 | if (unlikely(!id)) |
| 9931 | return -ENOMEM; |
| 9932 | |
| 9933 | io_init_identity(id); |
| 9934 | id->creds = get_current_cred(); |
| 9935 | |
| 9936 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9937 | if (ret < 0) { |
| 9938 | put_cred(id->creds); |
| 9939 | kfree(id); |
| 9940 | } |
| 9941 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9942 | } |
| 9943 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9944 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9945 | unsigned int nr_args) |
| 9946 | { |
| 9947 | struct io_uring_restriction *res; |
| 9948 | size_t size; |
| 9949 | int i, ret; |
| 9950 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9951 | /* Restrictions allowed only if rings started disabled */ |
| 9952 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9953 | return -EBADFD; |
| 9954 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9955 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9956 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9957 | return -EBUSY; |
| 9958 | |
| 9959 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9960 | return -EINVAL; |
| 9961 | |
| 9962 | size = array_size(nr_args, sizeof(*res)); |
| 9963 | if (size == SIZE_MAX) |
| 9964 | return -EOVERFLOW; |
| 9965 | |
| 9966 | res = memdup_user(arg, size); |
| 9967 | if (IS_ERR(res)) |
| 9968 | return PTR_ERR(res); |
| 9969 | |
| 9970 | ret = 0; |
| 9971 | |
| 9972 | for (i = 0; i < nr_args; i++) { |
| 9973 | switch (res[i].opcode) { |
| 9974 | case IORING_RESTRICTION_REGISTER_OP: |
| 9975 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9976 | ret = -EINVAL; |
| 9977 | goto out; |
| 9978 | } |
| 9979 | |
| 9980 | __set_bit(res[i].register_op, |
| 9981 | ctx->restrictions.register_op); |
| 9982 | break; |
| 9983 | case IORING_RESTRICTION_SQE_OP: |
| 9984 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9985 | ret = -EINVAL; |
| 9986 | goto out; |
| 9987 | } |
| 9988 | |
| 9989 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9990 | break; |
| 9991 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9992 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9993 | break; |
| 9994 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9995 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9996 | break; |
| 9997 | default: |
| 9998 | ret = -EINVAL; |
| 9999 | goto out; |
| 10000 | } |
| 10001 | } |
| 10002 | |
| 10003 | out: |
| 10004 | /* Reset all restrictions if an error happened */ |
| 10005 | if (ret != 0) |
| 10006 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10007 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10008 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10009 | |
| 10010 | kfree(res); |
| 10011 | return ret; |
| 10012 | } |
| 10013 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10014 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10015 | { |
| 10016 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10017 | return -EBADFD; |
| 10018 | |
| 10019 | if (ctx->restrictions.registered) |
| 10020 | ctx->restricted = 1; |
| 10021 | |
| 10022 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10023 | |
| 10024 | io_sq_offload_start(ctx); |
| 10025 | |
| 10026 | return 0; |
| 10027 | } |
| 10028 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10029 | static bool io_register_op_must_quiesce(int op) |
| 10030 | { |
| 10031 | switch (op) { |
| 10032 | case IORING_UNREGISTER_FILES: |
| 10033 | case IORING_REGISTER_FILES_UPDATE: |
| 10034 | case IORING_REGISTER_PROBE: |
| 10035 | case IORING_REGISTER_PERSONALITY: |
| 10036 | case IORING_UNREGISTER_PERSONALITY: |
| 10037 | return false; |
| 10038 | default: |
| 10039 | return true; |
| 10040 | } |
| 10041 | } |
| 10042 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10043 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10044 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10045 | __releases(ctx->uring_lock) |
| 10046 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10047 | { |
| 10048 | int ret; |
| 10049 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10050 | /* |
| 10051 | * We're inside the ring mutex, if the ref is already dying, then |
| 10052 | * someone else killed the ctx or is already going through |
| 10053 | * io_uring_register(). |
| 10054 | */ |
| 10055 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10056 | return -ENXIO; |
| 10057 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10058 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10059 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10060 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10061 | /* |
| 10062 | * Drop uring mutex before waiting for references to exit. If |
| 10063 | * another thread is currently inside io_uring_enter() it might |
| 10064 | * need to grab the uring_lock to make progress. If we hold it |
| 10065 | * here across the drain wait, then we can deadlock. It's safe |
| 10066 | * to drop the mutex here, since no new references will come in |
| 10067 | * after we've killed the percpu ref. |
| 10068 | */ |
| 10069 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10070 | do { |
| 10071 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10072 | if (!ret) |
| 10073 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 10074 | ret = io_run_task_work_sig(); |
| 10075 | if (ret < 0) |
| 10076 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10077 | } while (1); |
| 10078 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10079 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10080 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10081 | if (ret) { |
| 10082 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10083 | goto out_quiesce; |
| 10084 | } |
| 10085 | } |
| 10086 | |
| 10087 | if (ctx->restricted) { |
| 10088 | if (opcode >= IORING_REGISTER_LAST) { |
| 10089 | ret = -EINVAL; |
| 10090 | goto out; |
| 10091 | } |
| 10092 | |
| 10093 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10094 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10095 | goto out; |
| 10096 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10097 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10098 | |
| 10099 | switch (opcode) { |
| 10100 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10101 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10102 | break; |
| 10103 | case IORING_UNREGISTER_BUFFERS: |
| 10104 | ret = -EINVAL; |
| 10105 | if (arg || nr_args) |
| 10106 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10107 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10108 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10109 | case IORING_REGISTER_FILES: |
| 10110 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10111 | break; |
| 10112 | case IORING_UNREGISTER_FILES: |
| 10113 | ret = -EINVAL; |
| 10114 | if (arg || nr_args) |
| 10115 | break; |
| 10116 | ret = io_sqe_files_unregister(ctx); |
| 10117 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10118 | case IORING_REGISTER_FILES_UPDATE: |
| 10119 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10120 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10121 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10122 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10123 | ret = -EINVAL; |
| 10124 | if (nr_args != 1) |
| 10125 | break; |
| 10126 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10127 | if (ret) |
| 10128 | break; |
| 10129 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10130 | ctx->eventfd_async = 1; |
| 10131 | else |
| 10132 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10133 | break; |
| 10134 | case IORING_UNREGISTER_EVENTFD: |
| 10135 | ret = -EINVAL; |
| 10136 | if (arg || nr_args) |
| 10137 | break; |
| 10138 | ret = io_eventfd_unregister(ctx); |
| 10139 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10140 | case IORING_REGISTER_PROBE: |
| 10141 | ret = -EINVAL; |
| 10142 | if (!arg || nr_args > 256) |
| 10143 | break; |
| 10144 | ret = io_probe(ctx, arg, nr_args); |
| 10145 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10146 | case IORING_REGISTER_PERSONALITY: |
| 10147 | ret = -EINVAL; |
| 10148 | if (arg || nr_args) |
| 10149 | break; |
| 10150 | ret = io_register_personality(ctx); |
| 10151 | break; |
| 10152 | case IORING_UNREGISTER_PERSONALITY: |
| 10153 | ret = -EINVAL; |
| 10154 | if (arg) |
| 10155 | break; |
| 10156 | ret = io_unregister_personality(ctx, nr_args); |
| 10157 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10158 | case IORING_REGISTER_ENABLE_RINGS: |
| 10159 | ret = -EINVAL; |
| 10160 | if (arg || nr_args) |
| 10161 | break; |
| 10162 | ret = io_register_enable_rings(ctx); |
| 10163 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10164 | case IORING_REGISTER_RESTRICTIONS: |
| 10165 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10166 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10167 | default: |
| 10168 | ret = -EINVAL; |
| 10169 | break; |
| 10170 | } |
| 10171 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10172 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10173 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10174 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10175 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10176 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10177 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10178 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10179 | return ret; |
| 10180 | } |
| 10181 | |
| 10182 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10183 | void __user *, arg, unsigned int, nr_args) |
| 10184 | { |
| 10185 | struct io_ring_ctx *ctx; |
| 10186 | long ret = -EBADF; |
| 10187 | struct fd f; |
| 10188 | |
| 10189 | f = fdget(fd); |
| 10190 | if (!f.file) |
| 10191 | return -EBADF; |
| 10192 | |
| 10193 | ret = -EOPNOTSUPP; |
| 10194 | if (f.file->f_op != &io_uring_fops) |
| 10195 | goto out_fput; |
| 10196 | |
| 10197 | ctx = f.file->private_data; |
| 10198 | |
| 10199 | mutex_lock(&ctx->uring_lock); |
| 10200 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10201 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10202 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10203 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10204 | out_fput: |
| 10205 | fdput(f); |
| 10206 | return ret; |
| 10207 | } |
| 10208 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10209 | static int __init io_uring_init(void) |
| 10210 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10211 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10212 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10213 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10214 | } while (0) |
| 10215 | |
| 10216 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10217 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10218 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10219 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10220 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10221 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10222 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10223 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10224 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10225 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10226 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10227 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10228 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10229 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10230 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10231 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10232 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10233 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10234 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10235 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10236 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10237 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10238 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10239 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10240 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10241 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10242 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10243 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10244 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10245 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10246 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10247 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10248 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10249 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 10250 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 10251 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10252 | return 0; |
| 10253 | }; |
| 10254 | __initcall(io_uring_init); |