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> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 83 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 84 | #define CREATE_TRACE_POINTS |
| 85 | #include <trace/events/io_uring.h> |
| 86 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 87 | #include <uapi/linux/io_uring.h> |
| 88 | |
| 89 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 90 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 91 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 92 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 93 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 97 | */ |
| 98 | #define IORING_FILE_TABLE_SHIFT 9 |
| 99 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 100 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 101 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 102 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 103 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 104 | |
| 105 | struct io_uring { |
| 106 | u32 head ____cacheline_aligned_in_smp; |
| 107 | u32 tail ____cacheline_aligned_in_smp; |
| 108 | }; |
| 109 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 110 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 111 | * This data is shared with the application through the mmap at offsets |
| 112 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 113 | * |
| 114 | * The offsets to the member fields are published through struct |
| 115 | * io_sqring_offsets when calling io_uring_setup. |
| 116 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 117 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | /* |
| 119 | * Head and tail offsets into the ring; the offsets need to be |
| 120 | * masked to get valid indices. |
| 121 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 123 | * and the application controls tail of the sq ring and the head of the |
| 124 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 126 | struct io_uring sq, cq; |
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 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 129 | * ring_entries - 1) |
| 130 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 131 | u32 sq_ring_mask, cq_ring_mask; |
| 132 | /* Ring sizes (constant, power of 2) */ |
| 133 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Number of invalid entries dropped by the kernel due to |
| 136 | * invalid index stored in array |
| 137 | * |
| 138 | * Written by the kernel, shouldn't be modified by the |
| 139 | * application (i.e. get number of "new events" by comparing to |
| 140 | * cached value). |
| 141 | * |
| 142 | * After a new SQ head value was read by the application this |
| 143 | * counter includes all submissions that were dropped reaching |
| 144 | * the new SQ head (and possibly more). |
| 145 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 146 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 147 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 148 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | * |
| 150 | * Written by the kernel, shouldn't be modified by the |
| 151 | * application. |
| 152 | * |
| 153 | * The application needs a full memory barrier before checking |
| 154 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 155 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 156 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 157 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 158 | * Runtime CQ flags |
| 159 | * |
| 160 | * Written by the application, shouldn't be modified by the |
| 161 | * kernel. |
| 162 | */ |
| 163 | u32 cq_flags; |
| 164 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 165 | * Number of completion events lost because the queue was full; |
| 166 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 167 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 168 | * the completion queue. |
| 169 | * |
| 170 | * Written by the kernel, shouldn't be modified by the |
| 171 | * application (i.e. get number of "new events" by comparing to |
| 172 | * cached value). |
| 173 | * |
| 174 | * As completion events come in out of order this counter is not |
| 175 | * ordered with any other data. |
| 176 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 177 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Ring buffer of completion events. |
| 180 | * |
| 181 | * The kernel writes completion events fresh every time they are |
| 182 | * produced, so the application is allowed to modify pending |
| 183 | * entries. |
| 184 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 185 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 188 | struct io_mapped_ubuf { |
| 189 | u64 ubuf; |
| 190 | size_t len; |
| 191 | struct bio_vec *bvec; |
| 192 | unsigned int nr_bvecs; |
| 193 | }; |
| 194 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 195 | struct fixed_file_table { |
| 196 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 199 | struct fixed_file_ref_node { |
| 200 | struct percpu_ref refs; |
| 201 | struct list_head node; |
| 202 | struct list_head file_list; |
| 203 | struct fixed_file_data *file_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 204 | struct llist_node llist; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 205 | }; |
| 206 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 207 | struct fixed_file_data { |
| 208 | struct fixed_file_table *table; |
| 209 | struct io_ring_ctx *ctx; |
| 210 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 211 | struct percpu_ref *cur_refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 212 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 213 | struct completion done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 214 | struct list_head ref_list; |
| 215 | spinlock_t lock; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 218 | struct io_buffer { |
| 219 | struct list_head list; |
| 220 | __u64 addr; |
| 221 | __s32 len; |
| 222 | __u16 bid; |
| 223 | }; |
| 224 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 225 | struct io_restriction { |
| 226 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 227 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 228 | u8 sqe_flags_allowed; |
| 229 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 230 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 233 | struct io_ring_ctx { |
| 234 | struct { |
| 235 | struct percpu_ref refs; |
| 236 | } ____cacheline_aligned_in_smp; |
| 237 | |
| 238 | struct { |
| 239 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 240 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 241 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 242 | unsigned int cq_overflow_flushed: 1; |
| 243 | unsigned int drain_next: 1; |
| 244 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 245 | unsigned int restricted: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 247 | /* |
| 248 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 249 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 250 | * |
| 251 | * This indirection could e.g. be used to assign fixed |
| 252 | * io_uring_sqe entries to operations and only submit them to |
| 253 | * the queue when needed. |
| 254 | * |
| 255 | * The kernel modifies neither the indices array nor the entries |
| 256 | * array. |
| 257 | */ |
| 258 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 259 | unsigned cached_sq_head; |
| 260 | unsigned sq_entries; |
| 261 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 262 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 263 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 264 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 265 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 266 | |
| 267 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 268 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 269 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 270 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 271 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 272 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 273 | } ____cacheline_aligned_in_smp; |
| 274 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 275 | struct io_rings *rings; |
| 276 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 277 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 278 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 279 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * For SQPOLL usage - we hold a reference to the parent task, so we |
| 283 | * have access to the ->files |
| 284 | */ |
| 285 | struct task_struct *sqo_task; |
| 286 | |
| 287 | /* Only used for accounting purposes */ |
| 288 | struct mm_struct *mm_account; |
| 289 | |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 290 | struct wait_queue_head *sqo_wait; |
| 291 | struct wait_queue_head __sqo_wait; |
| 292 | struct wait_queue_entry sqo_wait_entry; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 293 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 294 | /* |
| 295 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 296 | * readers must ensure that ->refs is alive as long as the file* is |
| 297 | * used. Only updated through io_uring_register(2). |
| 298 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 299 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 300 | unsigned nr_user_files; |
| 301 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 302 | /* if used, fixed mapped user buffers */ |
| 303 | unsigned nr_user_bufs; |
| 304 | struct io_mapped_ubuf *user_bufs; |
| 305 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 306 | struct user_struct *user; |
| 307 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 308 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 309 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 310 | struct completion ref_comp; |
| 311 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 312 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 313 | /* if all else fails... */ |
| 314 | struct io_kiocb *fallback_req; |
| 315 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 316 | #if defined(CONFIG_UNIX) |
| 317 | struct socket *ring_sock; |
| 318 | #endif |
| 319 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 320 | struct idr io_buffer_idr; |
| 321 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 322 | struct idr personality_idr; |
| 323 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 324 | struct { |
| 325 | unsigned cached_cq_tail; |
| 326 | unsigned cq_entries; |
| 327 | unsigned cq_mask; |
| 328 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 329 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 330 | struct wait_queue_head cq_wait; |
| 331 | struct fasync_struct *cq_fasync; |
| 332 | struct eventfd_ctx *cq_ev_fd; |
| 333 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 334 | |
| 335 | struct { |
| 336 | struct mutex uring_lock; |
| 337 | wait_queue_head_t wait; |
| 338 | } ____cacheline_aligned_in_smp; |
| 339 | |
| 340 | struct { |
| 341 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 342 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 343 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 344 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 345 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 346 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 347 | * manipulate the list, hence no extra locking is needed there. |
| 348 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 349 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 350 | struct hlist_head *cancel_hash; |
| 351 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 352 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 353 | |
| 354 | spinlock_t inflight_lock; |
| 355 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 356 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 357 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 358 | struct delayed_work file_put_work; |
| 359 | struct llist_head file_put_llist; |
| 360 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 361 | struct work_struct exit_work; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 362 | struct io_restriction restrictions; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 365 | /* |
| 366 | * First field must be the file pointer in all the |
| 367 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 368 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 369 | struct io_poll_iocb { |
| 370 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 371 | union { |
| 372 | struct wait_queue_head *head; |
| 373 | u64 addr; |
| 374 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 375 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 376 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 377 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 378 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 379 | }; |
| 380 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 381 | struct io_close { |
| 382 | struct file *file; |
| 383 | struct file *put_file; |
| 384 | int fd; |
| 385 | }; |
| 386 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 387 | struct io_timeout_data { |
| 388 | struct io_kiocb *req; |
| 389 | struct hrtimer timer; |
| 390 | struct timespec64 ts; |
| 391 | enum hrtimer_mode mode; |
| 392 | }; |
| 393 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 394 | struct io_accept { |
| 395 | struct file *file; |
| 396 | struct sockaddr __user *addr; |
| 397 | int __user *addr_len; |
| 398 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 399 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | struct io_sync { |
| 403 | struct file *file; |
| 404 | loff_t len; |
| 405 | loff_t off; |
| 406 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 407 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 408 | }; |
| 409 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 410 | struct io_cancel { |
| 411 | struct file *file; |
| 412 | u64 addr; |
| 413 | }; |
| 414 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 415 | struct io_timeout { |
| 416 | struct file *file; |
| 417 | u64 addr; |
| 418 | int flags; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 419 | u32 off; |
| 420 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 421 | struct list_head list; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 424 | struct io_rw { |
| 425 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 426 | struct kiocb kiocb; |
| 427 | u64 addr; |
| 428 | u64 len; |
| 429 | }; |
| 430 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 431 | struct io_connect { |
| 432 | struct file *file; |
| 433 | struct sockaddr __user *addr; |
| 434 | int addr_len; |
| 435 | }; |
| 436 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 437 | struct io_sr_msg { |
| 438 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 439 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 440 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 441 | void __user *buf; |
| 442 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 443 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 444 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 445 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 446 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 449 | struct io_open { |
| 450 | struct file *file; |
| 451 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 452 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 453 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 454 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 455 | }; |
| 456 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 457 | struct io_files_update { |
| 458 | struct file *file; |
| 459 | u64 arg; |
| 460 | u32 nr_args; |
| 461 | u32 offset; |
| 462 | }; |
| 463 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 464 | struct io_fadvise { |
| 465 | struct file *file; |
| 466 | u64 offset; |
| 467 | u32 len; |
| 468 | u32 advice; |
| 469 | }; |
| 470 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 471 | struct io_madvise { |
| 472 | struct file *file; |
| 473 | u64 addr; |
| 474 | u32 len; |
| 475 | u32 advice; |
| 476 | }; |
| 477 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 478 | struct io_epoll { |
| 479 | struct file *file; |
| 480 | int epfd; |
| 481 | int op; |
| 482 | int fd; |
| 483 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 486 | struct io_splice { |
| 487 | struct file *file_out; |
| 488 | struct file *file_in; |
| 489 | loff_t off_out; |
| 490 | loff_t off_in; |
| 491 | u64 len; |
| 492 | unsigned int flags; |
| 493 | }; |
| 494 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 495 | struct io_provide_buf { |
| 496 | struct file *file; |
| 497 | __u64 addr; |
| 498 | __s32 len; |
| 499 | __u32 bgid; |
| 500 | __u16 nbufs; |
| 501 | __u16 bid; |
| 502 | }; |
| 503 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 504 | struct io_statx { |
| 505 | struct file *file; |
| 506 | int dfd; |
| 507 | unsigned int mask; |
| 508 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 509 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 510 | struct statx __user *buffer; |
| 511 | }; |
| 512 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 513 | struct io_completion { |
| 514 | struct file *file; |
| 515 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 516 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 517 | }; |
| 518 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 519 | struct io_async_connect { |
| 520 | struct sockaddr_storage address; |
| 521 | }; |
| 522 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 523 | struct io_async_msghdr { |
| 524 | struct iovec fast_iov[UIO_FASTIOV]; |
| 525 | struct iovec *iov; |
| 526 | struct sockaddr __user *uaddr; |
| 527 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 528 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 529 | }; |
| 530 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 531 | struct io_async_rw { |
| 532 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 533 | const struct iovec *free_iovec; |
| 534 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 535 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 536 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 537 | }; |
| 538 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 539 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 540 | union { |
| 541 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 542 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 543 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 544 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 545 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 546 | }; |
| 547 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 548 | enum { |
| 549 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 550 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 551 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 552 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 553 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 554 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 555 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 556 | REQ_F_LINK_HEAD_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 557 | REQ_F_FAIL_LINK_BIT, |
| 558 | REQ_F_INFLIGHT_BIT, |
| 559 | REQ_F_CUR_POS_BIT, |
| 560 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 561 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 562 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 563 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 564 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 565 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 566 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 567 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 568 | REQ_F_WORK_INITIALIZED_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 569 | |
| 570 | /* not a real bit, just to check we're not overflowing the space */ |
| 571 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 572 | }; |
| 573 | |
| 574 | enum { |
| 575 | /* ctx owns file */ |
| 576 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 577 | /* drain existing IO first */ |
| 578 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 579 | /* linked sqes */ |
| 580 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 581 | /* doesn't sever on completion < 0 */ |
| 582 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 583 | /* IOSQE_ASYNC */ |
| 584 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 585 | /* IOSQE_BUFFER_SELECT */ |
| 586 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 587 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 588 | /* head of a link */ |
| 589 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 590 | /* fail rest of links */ |
| 591 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 592 | /* on inflight list */ |
| 593 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 594 | /* read/write uses file position */ |
| 595 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 596 | /* must not punt to workers */ |
| 597 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 598 | /* has linked timeout */ |
| 599 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 600 | /* regular file */ |
| 601 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 602 | /* completion under lock */ |
| 603 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 604 | /* needs cleanup */ |
| 605 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 606 | /* already went through poll handler */ |
| 607 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 608 | /* buffer already selected */ |
| 609 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 610 | /* doesn't need file table for this request */ |
| 611 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 612 | /* io_wq_work is initialized */ |
| 613 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | struct async_poll { |
| 617 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 618 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 619 | }; |
| 620 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 621 | /* |
| 622 | * NOTE! Each of the iocb union members has the file pointer |
| 623 | * as the first entry in their struct definition. So you can |
| 624 | * access the file pointer through any of the sub-structs, |
| 625 | * or directly as just 'ki_filp' in this struct. |
| 626 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 627 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 628 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 629 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 630 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 631 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 632 | struct io_accept accept; |
| 633 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 634 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 635 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 636 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 637 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 638 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 639 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 640 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 641 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 642 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 643 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 644 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 645 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 646 | struct io_statx statx; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 647 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 648 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 649 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 650 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 651 | struct io_async_ctx *io; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 652 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 653 | /* polled IO has completed */ |
| 654 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 655 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 656 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 657 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 658 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 659 | struct io_ring_ctx *ctx; |
| 660 | unsigned int flags; |
| 661 | refcount_t refs; |
| 662 | struct task_struct *task; |
| 663 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 664 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 665 | struct list_head link_list; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 666 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 667 | /* |
| 668 | * 1. used with ctx->iopoll_list with reads/writes |
| 669 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 670 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 671 | struct list_head inflight_entry; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 672 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 673 | struct percpu_ref *fixed_file_refs; |
| 674 | struct callback_head task_work; |
| 675 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 676 | struct hlist_node hash_node; |
| 677 | struct async_poll *apoll; |
| 678 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 679 | }; |
| 680 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 681 | struct io_defer_entry { |
| 682 | struct list_head list; |
| 683 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 684 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 685 | }; |
| 686 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 687 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 688 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 689 | struct io_comp_state { |
| 690 | unsigned int nr; |
| 691 | struct list_head list; |
| 692 | struct io_ring_ctx *ctx; |
| 693 | }; |
| 694 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 695 | struct io_submit_state { |
| 696 | struct blk_plug plug; |
| 697 | |
| 698 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 699 | * io_kiocb alloc cache |
| 700 | */ |
| 701 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 702 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 703 | |
| 704 | /* |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 705 | * Batch completion logic |
| 706 | */ |
| 707 | struct io_comp_state comp; |
| 708 | |
| 709 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 710 | * File reference cache |
| 711 | */ |
| 712 | struct file *file; |
| 713 | unsigned int fd; |
| 714 | unsigned int has_refs; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 715 | unsigned int ios_left; |
| 716 | }; |
| 717 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 718 | struct io_op_def { |
| 719 | /* needs req->io allocated for deferral/async */ |
| 720 | unsigned async_ctx : 1; |
| 721 | /* needs current->mm setup, does mm access */ |
| 722 | unsigned needs_mm : 1; |
| 723 | /* needs req->file assigned */ |
| 724 | unsigned needs_file : 1; |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 725 | /* don't fail if file grab fails */ |
| 726 | unsigned needs_file_no_error : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 727 | /* hash wq insertion if file is a regular file */ |
| 728 | unsigned hash_reg_file : 1; |
| 729 | /* unbound wq insertion if file is a non-regular file */ |
| 730 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 731 | /* opcode is not supported by this kernel */ |
| 732 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 733 | /* needs file table */ |
| 734 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 735 | /* needs ->fs */ |
| 736 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 737 | /* set if opcode supports polled "wait" */ |
| 738 | unsigned pollin : 1; |
| 739 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 740 | /* op supports buffer selection */ |
| 741 | unsigned buffer_select : 1; |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 742 | unsigned needs_fsize : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 746 | [IORING_OP_NOP] = {}, |
| 747 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 748 | .async_ctx = 1, |
| 749 | .needs_mm = 1, |
| 750 | .needs_file = 1, |
| 751 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 752 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 753 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 754 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 755 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 756 | .async_ctx = 1, |
| 757 | .needs_mm = 1, |
| 758 | .needs_file = 1, |
| 759 | .hash_reg_file = 1, |
| 760 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 761 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 762 | .needs_fsize = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 763 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 764 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 765 | .needs_file = 1, |
| 766 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 767 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 768 | .needs_file = 1, |
| 769 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 770 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 771 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 772 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 773 | .needs_file = 1, |
| 774 | .hash_reg_file = 1, |
| 775 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 776 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 777 | .needs_fsize = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 778 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 779 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 780 | .needs_file = 1, |
| 781 | .unbound_nonreg_file = 1, |
| 782 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 783 | [IORING_OP_POLL_REMOVE] = {}, |
| 784 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 785 | .needs_file = 1, |
| 786 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 787 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 788 | .async_ctx = 1, |
| 789 | .needs_mm = 1, |
| 790 | .needs_file = 1, |
| 791 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 792 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 793 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 794 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 795 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 796 | .async_ctx = 1, |
| 797 | .needs_mm = 1, |
| 798 | .needs_file = 1, |
| 799 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 800 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 801 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 802 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 803 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 804 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 805 | .async_ctx = 1, |
| 806 | .needs_mm = 1, |
| 807 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 808 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 809 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 810 | .needs_mm = 1, |
| 811 | .needs_file = 1, |
| 812 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 813 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 814 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 815 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 816 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 817 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 818 | .async_ctx = 1, |
| 819 | .needs_mm = 1, |
| 820 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 821 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | .async_ctx = 1, |
| 823 | .needs_mm = 1, |
| 824 | .needs_file = 1, |
| 825 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 826 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 827 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 828 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 829 | .needs_file = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 830 | .needs_fsize = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 831 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 832 | [IORING_OP_OPENAT] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 833 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 834 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 835 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 836 | [IORING_OP_CLOSE] = { |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 837 | .needs_file = 1, |
| 838 | .needs_file_no_error = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 839 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 840 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 841 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 842 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 843 | .file_table = 1, |
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_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 846 | .needs_mm = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 847 | .needs_fs = 1, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 848 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 849 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 850 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 851 | .needs_mm = 1, |
| 852 | .needs_file = 1, |
| 853 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 854 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 855 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 856 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 857 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 858 | .needs_mm = 1, |
| 859 | .needs_file = 1, |
| 860 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 861 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 862 | .needs_fsize = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 863 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 864 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 865 | .needs_file = 1, |
| 866 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 867 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 868 | .needs_mm = 1, |
| 869 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 870 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 871 | .needs_mm = 1, |
| 872 | .needs_file = 1, |
| 873 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 874 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 875 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 876 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 877 | .needs_mm = 1, |
| 878 | .needs_file = 1, |
| 879 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 880 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 881 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 882 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 883 | [IORING_OP_OPENAT2] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 884 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 885 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 886 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 887 | [IORING_OP_EPOLL_CTL] = { |
| 888 | .unbound_nonreg_file = 1, |
| 889 | .file_table = 1, |
| 890 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 891 | [IORING_OP_SPLICE] = { |
| 892 | .needs_file = 1, |
| 893 | .hash_reg_file = 1, |
| 894 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 895 | }, |
| 896 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 897 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 898 | [IORING_OP_TEE] = { |
| 899 | .needs_file = 1, |
| 900 | .hash_reg_file = 1, |
| 901 | .unbound_nonreg_file = 1, |
| 902 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 903 | }; |
| 904 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 905 | enum io_mem_account { |
| 906 | ACCT_LOCKED, |
| 907 | ACCT_PINNED, |
| 908 | }; |
| 909 | |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 910 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 911 | struct io_comp_state *cs); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 912 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 913 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 914 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 915 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 916 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 917 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 918 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 919 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 920 | struct io_uring_files_update *ip, |
| 921 | unsigned nr_args); |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 922 | static int io_prep_work_files(struct io_kiocb *req); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 923 | static void __io_clean_op(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 924 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 925 | int fd, struct file **out_file, bool fixed); |
| 926 | static void __io_queue_sqe(struct io_kiocb *req, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 927 | const struct io_uring_sqe *sqe, |
| 928 | struct io_comp_state *cs); |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 929 | static void io_file_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 930 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 931 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 932 | struct iovec **iovec, struct iov_iter *iter, |
| 933 | bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 934 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 935 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 936 | struct iov_iter *iter, bool force); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 937 | |
| 938 | static struct kmem_cache *req_cachep; |
| 939 | |
| 940 | static const struct file_operations io_uring_fops; |
| 941 | |
| 942 | struct sock *io_uring_get_socket(struct file *file) |
| 943 | { |
| 944 | #if defined(CONFIG_UNIX) |
| 945 | if (file->f_op == &io_uring_fops) { |
| 946 | struct io_ring_ctx *ctx = file->private_data; |
| 947 | |
| 948 | return ctx->ring_sock->sk; |
| 949 | } |
| 950 | #endif |
| 951 | return NULL; |
| 952 | } |
| 953 | EXPORT_SYMBOL(io_uring_get_socket); |
| 954 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 955 | static inline void io_clean_op(struct io_kiocb *req) |
| 956 | { |
Pavel Begunkov | bb17534 | 2020-08-20 11:33:35 +0300 | [diff] [blame] | 957 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
| 958 | REQ_F_INFLIGHT)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 959 | __io_clean_op(req); |
| 960 | } |
| 961 | |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 962 | static void io_sq_thread_drop_mm(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 963 | { |
| 964 | struct mm_struct *mm = current->mm; |
| 965 | |
| 966 | if (mm) { |
| 967 | kthread_unuse_mm(mm); |
| 968 | mmput(mm); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 973 | { |
| 974 | if (!current->mm) { |
Pavel Begunkov | cbcf721 | 2020-07-18 11:31:21 +0300 | [diff] [blame] | 975 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) || |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 976 | !ctx->sqo_task->mm || |
| 977 | !mmget_not_zero(ctx->sqo_task->mm))) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 978 | return -EFAULT; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 979 | kthread_use_mm(ctx->sqo_task->mm); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, |
| 986 | struct io_kiocb *req) |
| 987 | { |
| 988 | if (!io_op_defs[req->opcode].needs_mm) |
| 989 | return 0; |
| 990 | return __io_sq_thread_acquire_mm(ctx); |
| 991 | } |
| 992 | |
| 993 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 994 | { |
| 995 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 996 | req->flags |= REQ_F_FAIL_LINK; |
| 997 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 998 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 999 | /* |
| 1000 | * Note: must call io_req_init_async() for the first time you |
| 1001 | * touch any members of io_wq_work. |
| 1002 | */ |
| 1003 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1004 | { |
| 1005 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1006 | return; |
| 1007 | |
| 1008 | memset(&req->work, 0, sizeof(req->work)); |
| 1009 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1010 | } |
| 1011 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 1012 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 1013 | { |
| 1014 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 1015 | } |
| 1016 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1017 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1018 | { |
| 1019 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1020 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1021 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1024 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1025 | { |
| 1026 | return !req->timeout.off; |
| 1027 | } |
| 1028 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1029 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1030 | { |
| 1031 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1032 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1033 | |
| 1034 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1035 | if (!ctx) |
| 1036 | return NULL; |
| 1037 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1038 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 1039 | if (!ctx->fallback_req) |
| 1040 | goto err; |
| 1041 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * Use 5 bits less than the max cq entries, that should give us around |
| 1044 | * 32 entries per hash list if totally full and uniformly spread. |
| 1045 | */ |
| 1046 | hash_bits = ilog2(p->cq_entries); |
| 1047 | hash_bits -= 5; |
| 1048 | if (hash_bits <= 0) |
| 1049 | hash_bits = 1; |
| 1050 | ctx->cancel_hash_bits = hash_bits; |
| 1051 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1052 | GFP_KERNEL); |
| 1053 | if (!ctx->cancel_hash) |
| 1054 | goto err; |
| 1055 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1056 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1057 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1058 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1059 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1060 | |
| 1061 | ctx->flags = p->flags; |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 1062 | init_waitqueue_head(&ctx->__sqo_wait); |
| 1063 | ctx->sqo_wait = &ctx->__sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1064 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1065 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1066 | init_completion(&ctx->ref_comp); |
| 1067 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1068 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1069 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1070 | mutex_init(&ctx->uring_lock); |
| 1071 | init_waitqueue_head(&ctx->wait); |
| 1072 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1073 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1074 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1075 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1076 | init_waitqueue_head(&ctx->inflight_wait); |
| 1077 | spin_lock_init(&ctx->inflight_lock); |
| 1078 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1079 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1080 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1081 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1082 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1083 | if (ctx->fallback_req) |
| 1084 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1085 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1086 | kfree(ctx); |
| 1087 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1090 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1091 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1092 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1093 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1094 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1095 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 1096 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1097 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1098 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1099 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1100 | } |
| 1101 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1102 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1103 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1104 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1105 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1106 | /* order cqe stores with ring update */ |
| 1107 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1108 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1109 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 1110 | wake_up_interruptible(&ctx->cq_wait); |
| 1111 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1115 | /* |
| 1116 | * Returns true if we need to defer file table putting. This can only happen |
| 1117 | * from the error path with REQ_F_COMP_LOCKED set. |
| 1118 | */ |
| 1119 | static bool io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1120 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1121 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1122 | return false; |
| 1123 | |
| 1124 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1125 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1126 | if (req->work.mm) { |
| 1127 | mmdrop(req->work.mm); |
| 1128 | req->work.mm = NULL; |
| 1129 | } |
| 1130 | if (req->work.creds) { |
| 1131 | put_cred(req->work.creds); |
| 1132 | req->work.creds = NULL; |
| 1133 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1134 | if (req->work.fs) { |
| 1135 | struct fs_struct *fs = req->work.fs; |
| 1136 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1137 | if (req->flags & REQ_F_COMP_LOCKED) |
| 1138 | return true; |
| 1139 | |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1140 | spin_lock(&req->work.fs->lock); |
| 1141 | if (--fs->users) |
| 1142 | fs = NULL; |
| 1143 | spin_unlock(&req->work.fs->lock); |
| 1144 | if (fs) |
| 1145 | free_fs_struct(fs); |
Pavel Begunkov | b65e0dd | 2020-07-25 14:41:58 +0300 | [diff] [blame] | 1146 | req->work.fs = NULL; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1147 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1148 | |
| 1149 | return false; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1150 | } |
| 1151 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1152 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1153 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1154 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1155 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1156 | io_req_init_async(req); |
| 1157 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1158 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 1159 | if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1160 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1161 | } else { |
| 1162 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1163 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1164 | } |
Pavel Begunkov | dca9cf8 | 2020-07-15 12:46:49 +0300 | [diff] [blame] | 1165 | if (!req->work.mm && def->needs_mm) { |
| 1166 | mmgrab(current->mm); |
| 1167 | req->work.mm = current->mm; |
| 1168 | } |
| 1169 | if (!req->work.creds) |
| 1170 | req->work.creds = get_current_cred(); |
| 1171 | if (!req->work.fs && def->needs_fs) { |
| 1172 | spin_lock(¤t->fs->lock); |
| 1173 | if (!current->fs->in_exec) { |
| 1174 | req->work.fs = current->fs; |
| 1175 | req->work.fs->users++; |
| 1176 | } else { |
| 1177 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1178 | } |
| 1179 | spin_unlock(¤t->fs->lock); |
| 1180 | } |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 1181 | if (def->needs_fsize) |
| 1182 | req->work.fsize = rlimit(RLIMIT_FSIZE); |
| 1183 | else |
| 1184 | req->work.fsize = RLIM_INFINITY; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1185 | } |
| 1186 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1187 | static void io_prep_async_link(struct io_kiocb *req) |
| 1188 | { |
| 1189 | struct io_kiocb *cur; |
| 1190 | |
| 1191 | io_prep_async_work(req); |
| 1192 | if (req->flags & REQ_F_LINK_HEAD) |
| 1193 | list_for_each_entry(cur, &req->link_list, link_list) |
| 1194 | io_prep_async_work(cur); |
| 1195 | } |
| 1196 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1197 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1198 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1199 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1200 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1201 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1202 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1203 | &req->work, req->flags); |
| 1204 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1205 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1206 | } |
| 1207 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1208 | static void io_queue_async_work(struct io_kiocb *req) |
| 1209 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1210 | struct io_kiocb *link; |
| 1211 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1212 | /* init ->work of the whole link before punting */ |
| 1213 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1214 | link = __io_queue_async_work(req); |
| 1215 | |
| 1216 | if (link) |
| 1217 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1218 | } |
| 1219 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1220 | static void io_kill_timeout(struct io_kiocb *req) |
| 1221 | { |
| 1222 | int ret; |
| 1223 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1224 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1225 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1226 | atomic_set(&req->ctx->cq_timeouts, |
| 1227 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1228 | list_del_init(&req->timeout.list); |
Pavel Begunkov | f0e20b8 | 2020-03-07 01:15:22 +0300 | [diff] [blame] | 1229 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1230 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1231 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1235 | static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk) |
| 1236 | { |
| 1237 | struct io_ring_ctx *ctx = req->ctx; |
| 1238 | |
| 1239 | if (!tsk || req->task == tsk) |
| 1240 | return true; |
| 1241 | if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread) |
| 1242 | return true; |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1246 | /* |
| 1247 | * Returns true if we found and killed one or more timeouts |
| 1248 | */ |
| 1249 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1250 | { |
| 1251 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1252 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1253 | |
| 1254 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1255 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1256 | if (io_task_match(req, tsk)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1257 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1258 | canceled++; |
| 1259 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1260 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1261 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1262 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1263 | } |
| 1264 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1265 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1266 | { |
| 1267 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1268 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1269 | struct io_defer_entry, list); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1270 | struct io_kiocb *link; |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1271 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1272 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1273 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1274 | list_del_init(&de->list); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1275 | /* punt-init is done before queueing for defer */ |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1276 | link = __io_queue_async_work(de->req); |
| 1277 | if (link) { |
| 1278 | __io_queue_linked_timeout(link); |
| 1279 | /* drop submission reference */ |
| 1280 | link->flags |= REQ_F_COMP_LOCKED; |
| 1281 | io_put_req(link); |
| 1282 | } |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1283 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1284 | } while (!list_empty(&ctx->defer_list)); |
| 1285 | } |
| 1286 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1287 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1288 | { |
| 1289 | while (!list_empty(&ctx->timeout_list)) { |
| 1290 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1291 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1292 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1293 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1294 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1295 | if (req->timeout.target_seq != ctx->cached_cq_tail |
| 1296 | - atomic_read(&ctx->cq_timeouts)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1297 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1298 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1299 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1300 | io_kill_timeout(req); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1304 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1305 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1306 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1307 | __io_commit_cqring(ctx); |
| 1308 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1309 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1310 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1311 | } |
| 1312 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1313 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1314 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1315 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1316 | unsigned tail; |
| 1317 | |
| 1318 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1319 | /* |
| 1320 | * writes to the cq entry need to come after reading head; the |
| 1321 | * control dependency is enough as we're using WRITE_ONCE to |
| 1322 | * fill the cq entry |
| 1323 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1324 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1325 | return NULL; |
| 1326 | |
| 1327 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1328 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1331 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1332 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1333 | if (!ctx->cq_ev_fd) |
| 1334 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1335 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1336 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1337 | if (!ctx->eventfd_async) |
| 1338 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1339 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1342 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1343 | { |
| 1344 | if (waitqueue_active(&ctx->wait)) |
| 1345 | wake_up(&ctx->wait); |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 1346 | if (waitqueue_active(ctx->sqo_wait)) |
| 1347 | wake_up(ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1348 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1349 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1352 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
| 1353 | { |
| 1354 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1355 | clear_bit(0, &ctx->sq_check_overflow); |
| 1356 | clear_bit(0, &ctx->cq_check_overflow); |
| 1357 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1358 | } |
| 1359 | } |
| 1360 | |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1361 | static inline bool io_match_files(struct io_kiocb *req, |
| 1362 | struct files_struct *files) |
| 1363 | { |
| 1364 | if (!files) |
| 1365 | return true; |
| 1366 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1367 | return req->work.files == files; |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1371 | /* Returns true if there are no backlogged entries after the flush */ |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1372 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1373 | struct task_struct *tsk, |
| 1374 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1375 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1376 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1377 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1378 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1379 | unsigned long flags; |
| 1380 | LIST_HEAD(list); |
| 1381 | |
| 1382 | if (!force) { |
| 1383 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1384 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1385 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1386 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1387 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1391 | |
| 1392 | /* if force is set, the ring is going away. always drop after that */ |
| 1393 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1394 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1395 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1396 | cqe = NULL; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1397 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
| 1398 | if (tsk && req->task != tsk) |
| 1399 | continue; |
| 1400 | if (!io_match_files(req, files)) |
| 1401 | continue; |
| 1402 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1403 | cqe = io_get_cqring(ctx); |
| 1404 | if (!cqe && !force) |
| 1405 | break; |
| 1406 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1407 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1408 | if (cqe) { |
| 1409 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1410 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1411 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1412 | } else { |
| 1413 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1414 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | io_commit_cqring(ctx); |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1419 | io_cqring_mark_overflow(ctx); |
| 1420 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1421 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1422 | io_cqring_ev_posted(ctx); |
| 1423 | |
| 1424 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1425 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1426 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1427 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1428 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1429 | |
| 1430 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1433 | 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] | 1434 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1435 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1436 | struct io_uring_cqe *cqe; |
| 1437 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1438 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1439 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1440 | /* |
| 1441 | * If we can't get a cq entry, userspace overflowed the |
| 1442 | * submission (by quite a lot). Increment the overflow count in |
| 1443 | * the ring. |
| 1444 | */ |
| 1445 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1446 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1447 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1448 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1449 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1450 | } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) { |
| 1451 | /* |
| 1452 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1453 | * then we cannot store the request for later flushing, we need |
| 1454 | * to drop it on the floor. |
| 1455 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1456 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1457 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1458 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1459 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1460 | set_bit(0, &ctx->sq_check_overflow); |
| 1461 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1462 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1463 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1464 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1465 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1466 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1467 | refcount_inc(&req->refs); |
| 1468 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1472 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1473 | { |
| 1474 | __io_cqring_fill_event(req, res, 0); |
| 1475 | } |
| 1476 | |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1477 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1478 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1479 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1480 | unsigned long flags; |
| 1481 | |
| 1482 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1483 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1484 | io_commit_cqring(ctx); |
| 1485 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1486 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1487 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1490 | static void io_submit_flush_completions(struct io_comp_state *cs) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1491 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1492 | struct io_ring_ctx *ctx = cs->ctx; |
| 1493 | |
| 1494 | spin_lock_irq(&ctx->completion_lock); |
| 1495 | while (!list_empty(&cs->list)) { |
| 1496 | struct io_kiocb *req; |
| 1497 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1498 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
| 1499 | list_del(&req->compl.list); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1500 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1501 | if (!(req->flags & REQ_F_LINK_HEAD)) { |
| 1502 | req->flags |= REQ_F_COMP_LOCKED; |
| 1503 | io_put_req(req); |
| 1504 | } else { |
| 1505 | spin_unlock_irq(&ctx->completion_lock); |
| 1506 | io_put_req(req); |
| 1507 | spin_lock_irq(&ctx->completion_lock); |
| 1508 | } |
| 1509 | } |
| 1510 | io_commit_cqring(ctx); |
| 1511 | spin_unlock_irq(&ctx->completion_lock); |
| 1512 | |
| 1513 | io_cqring_ev_posted(ctx); |
| 1514 | cs->nr = 0; |
| 1515 | } |
| 1516 | |
| 1517 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| 1518 | struct io_comp_state *cs) |
| 1519 | { |
| 1520 | if (!cs) { |
| 1521 | io_cqring_add_event(req, res, cflags); |
| 1522 | io_put_req(req); |
| 1523 | } else { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1524 | io_clean_op(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1525 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1526 | req->compl.cflags = cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1527 | list_add_tail(&req->compl.list, &cs->list); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1528 | if (++cs->nr >= 32) |
| 1529 | io_submit_flush_completions(cs); |
| 1530 | } |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void io_req_complete(struct io_kiocb *req, long res) |
| 1534 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1535 | __io_req_complete(req, res, 0, NULL); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1538 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1539 | { |
| 1540 | return req == (struct io_kiocb *) |
| 1541 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1542 | } |
| 1543 | |
| 1544 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1545 | { |
| 1546 | struct io_kiocb *req; |
| 1547 | |
| 1548 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1549 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1550 | return req; |
| 1551 | |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1555 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1556 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1557 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1558 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1559 | struct io_kiocb *req; |
| 1560 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1561 | if (!state->free_reqs) { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1562 | size_t sz; |
| 1563 | int ret; |
| 1564 | |
| 1565 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1566 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1567 | |
| 1568 | /* |
| 1569 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1570 | * retry single alloc to be on the safe side. |
| 1571 | */ |
| 1572 | if (unlikely(ret <= 0)) { |
| 1573 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1574 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1575 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1576 | ret = 1; |
| 1577 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1578 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1579 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1580 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1581 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1582 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1585 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1586 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1587 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1590 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1591 | bool fixed) |
| 1592 | { |
| 1593 | if (fixed) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 1594 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1595 | else |
| 1596 | fput(file); |
| 1597 | } |
| 1598 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1599 | static bool io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1600 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1601 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1602 | |
Jens Axboe | 5acbbc8 | 2020-07-08 15:15:26 -0600 | [diff] [blame] | 1603 | if (req->io) |
| 1604 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1605 | if (req->file) |
| 1606 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1607 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1608 | return io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1609 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1610 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1611 | static void __io_free_req_finish(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1612 | { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1613 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1614 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1615 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1616 | atomic_long_inc(&tctx->req_complete); |
| 1617 | if (tctx->in_idle) |
| 1618 | wake_up(&tctx->wait); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 1619 | put_task_struct(req->task); |
| 1620 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 1621 | if (likely(!io_is_fallback_req(req))) |
| 1622 | kmem_cache_free(req_cachep, req); |
| 1623 | else |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1624 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
| 1625 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1626 | } |
| 1627 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1628 | static void io_req_task_file_table_put(struct callback_head *cb) |
| 1629 | { |
| 1630 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1631 | struct fs_struct *fs = req->work.fs; |
| 1632 | |
| 1633 | spin_lock(&req->work.fs->lock); |
| 1634 | if (--fs->users) |
| 1635 | fs = NULL; |
| 1636 | spin_unlock(&req->work.fs->lock); |
| 1637 | if (fs) |
| 1638 | free_fs_struct(fs); |
| 1639 | req->work.fs = NULL; |
| 1640 | __io_free_req_finish(req); |
| 1641 | } |
| 1642 | |
| 1643 | static void __io_free_req(struct io_kiocb *req) |
| 1644 | { |
| 1645 | if (!io_dismantle_req(req)) { |
| 1646 | __io_free_req_finish(req); |
| 1647 | } else { |
| 1648 | int ret; |
| 1649 | |
| 1650 | init_task_work(&req->task_work, io_req_task_file_table_put); |
| 1651 | ret = task_work_add(req->task, &req->task_work, TWA_RESUME); |
| 1652 | if (unlikely(ret)) { |
| 1653 | struct task_struct *tsk; |
| 1654 | |
| 1655 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 1656 | task_work_add(tsk, &req->task_work, 0); |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1661 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1662 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1663 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1664 | int ret; |
| 1665 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1666 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1667 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1668 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1669 | io_commit_cqring(ctx); |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1670 | req->flags &= ~REQ_F_LINK_HEAD; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1671 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1672 | return true; |
| 1673 | } |
| 1674 | |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1678 | static bool __io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1679 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1680 | struct io_kiocb *link; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1681 | bool wake_ev; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1682 | |
| 1683 | if (list_empty(&req->link_list)) |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1684 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1685 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 1686 | if (link->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1687 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1688 | |
| 1689 | list_del_init(&link->link_list); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame] | 1690 | link->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1691 | wake_ev = io_link_cancel_timeout(link); |
| 1692 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1693 | return wake_ev; |
| 1694 | } |
| 1695 | |
| 1696 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1697 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1698 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1699 | bool wake_ev; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1700 | |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1701 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
| 1702 | unsigned long flags; |
| 1703 | |
| 1704 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1705 | wake_ev = __io_kill_linked_timeout(req); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1706 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1707 | } else { |
| 1708 | wake_ev = __io_kill_linked_timeout(req); |
| 1709 | } |
| 1710 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1711 | if (wake_ev) |
| 1712 | io_cqring_ev_posted(ctx); |
| 1713 | } |
| 1714 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1715 | static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1716 | { |
| 1717 | struct io_kiocb *nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1718 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1719 | /* |
| 1720 | * The list should never be empty when we are called here. But could |
| 1721 | * potentially happen if the chain is messed up, check to be on the |
| 1722 | * safe side. |
| 1723 | */ |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1724 | if (unlikely(list_empty(&req->link_list))) |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1725 | return NULL; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1726 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1727 | nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 1728 | list_del_init(&req->link_list); |
| 1729 | if (!list_empty(&nxt->link_list)) |
| 1730 | nxt->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1731 | return nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1735 | * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1736 | */ |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1737 | static void __io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1738 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1739 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1740 | |
| 1741 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1742 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1743 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1744 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1745 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1746 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1747 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1748 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame] | 1749 | link->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1750 | __io_double_put_req(link); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1751 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1752 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1753 | |
| 1754 | io_commit_cqring(ctx); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1755 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1756 | } |
| 1757 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1758 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1759 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1760 | struct io_ring_ctx *ctx = req->ctx; |
| 1761 | |
| 1762 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
| 1763 | unsigned long flags; |
| 1764 | |
| 1765 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1766 | __io_fail_links(req); |
| 1767 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1768 | } else { |
| 1769 | __io_fail_links(req); |
| 1770 | } |
| 1771 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1772 | io_cqring_ev_posted(ctx); |
| 1773 | } |
| 1774 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1775 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1776 | { |
Pavel Begunkov | 9b0d911 | 2020-06-28 12:52:34 +0300 | [diff] [blame] | 1777 | req->flags &= ~REQ_F_LINK_HEAD; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1778 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 1779 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1780 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1781 | /* |
| 1782 | * If LINK is set, we have dependent requests in this chain. If we |
| 1783 | * didn't fail this request, queue the first one up, moving any other |
| 1784 | * dependencies to the next request. In case of failure, fail the rest |
| 1785 | * of the chain. |
| 1786 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1787 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
| 1788 | return io_req_link_next(req); |
| 1789 | io_fail_links(req); |
| 1790 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1791 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1792 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1793 | static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
| 1794 | { |
| 1795 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) |
| 1796 | return NULL; |
| 1797 | return __io_req_find_next(req); |
| 1798 | } |
| 1799 | |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 1800 | static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb, |
| 1801 | bool twa_signal_ok) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1802 | { |
| 1803 | struct task_struct *tsk = req->task; |
| 1804 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1805 | int ret, notify; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1806 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 1807 | if (tsk->flags & PF_EXITING) |
| 1808 | return -ESRCH; |
| 1809 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1810 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1811 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1812 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1813 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1814 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1815 | */ |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1816 | notify = 0; |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 1817 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1818 | notify = TWA_SIGNAL; |
| 1819 | |
| 1820 | ret = task_work_add(tsk, cb, notify); |
| 1821 | if (!ret) |
| 1822 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1823 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1824 | return ret; |
| 1825 | } |
| 1826 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1827 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1828 | { |
| 1829 | struct io_ring_ctx *ctx = req->ctx; |
| 1830 | |
| 1831 | spin_lock_irq(&ctx->completion_lock); |
| 1832 | io_cqring_fill_event(req, error); |
| 1833 | io_commit_cqring(ctx); |
| 1834 | spin_unlock_irq(&ctx->completion_lock); |
| 1835 | |
| 1836 | io_cqring_ev_posted(ctx); |
| 1837 | req_set_fail_links(req); |
| 1838 | io_double_put_req(req); |
| 1839 | } |
| 1840 | |
| 1841 | static void io_req_task_cancel(struct callback_head *cb) |
| 1842 | { |
| 1843 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1844 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1845 | |
| 1846 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1847 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | static void __io_req_task_submit(struct io_kiocb *req) |
| 1851 | { |
| 1852 | struct io_ring_ctx *ctx = req->ctx; |
| 1853 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1854 | if (!__io_sq_thread_acquire_mm(ctx)) { |
| 1855 | mutex_lock(&ctx->uring_lock); |
| 1856 | __io_queue_sqe(req, NULL, NULL); |
| 1857 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1858 | } else { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1859 | __io_req_task_cancel(req, -EFAULT); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1860 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1861 | } |
| 1862 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1863 | static void io_req_task_submit(struct callback_head *cb) |
| 1864 | { |
| 1865 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 1866 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1867 | |
| 1868 | __io_req_task_submit(req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 1869 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static void io_req_task_queue(struct io_kiocb *req) |
| 1873 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1874 | int ret; |
| 1875 | |
| 1876 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 1877 | percpu_ref_get(&req->ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1878 | |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 1879 | ret = io_req_task_work_add(req, &req->task_work, true); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1880 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1881 | struct task_struct *tsk; |
| 1882 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1883 | init_task_work(&req->task_work, io_req_task_cancel); |
| 1884 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1885 | task_work_add(tsk, &req->task_work, 0); |
| 1886 | wake_up_process(tsk); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1887 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1888 | } |
| 1889 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 1890 | static void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1891 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1892 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1893 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 1894 | if (nxt) |
| 1895 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1896 | } |
| 1897 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1898 | static void io_free_req(struct io_kiocb *req) |
| 1899 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 1900 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1901 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1902 | } |
| 1903 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1904 | struct req_batch { |
| 1905 | void *reqs[IO_IOPOLL_BATCH]; |
| 1906 | int to_free; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1907 | |
| 1908 | struct task_struct *task; |
| 1909 | int task_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1910 | }; |
| 1911 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1912 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1913 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1914 | rb->to_free = 0; |
| 1915 | rb->task_refs = 0; |
| 1916 | rb->task = NULL; |
| 1917 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1918 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1919 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
| 1920 | struct req_batch *rb) |
| 1921 | { |
| 1922 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1923 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 1924 | rb->to_free = 0; |
| 1925 | } |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1926 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1927 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 1928 | struct req_batch *rb) |
| 1929 | { |
| 1930 | if (rb->to_free) |
| 1931 | __io_req_free_batch_flush(ctx, rb); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1932 | if (rb->task) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1933 | atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1934 | put_task_struct_many(rb->task, rb->task_refs); |
| 1935 | rb->task = NULL; |
| 1936 | } |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) |
| 1940 | { |
| 1941 | if (unlikely(io_is_fallback_req(req))) { |
| 1942 | io_free_req(req); |
| 1943 | return; |
| 1944 | } |
| 1945 | if (req->flags & REQ_F_LINK_HEAD) |
| 1946 | io_queue_next(req); |
| 1947 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 1948 | if (req->task != rb->task) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1949 | if (rb->task) { |
| 1950 | atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 1951 | put_task_struct_many(rb->task, rb->task_refs); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1952 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 1953 | rb->task = req->task; |
| 1954 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1955 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 1956 | rb->task_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1957 | |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1958 | WARN_ON_ONCE(io_dismantle_req(req)); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1959 | rb->reqs[rb->to_free++] = req; |
| 1960 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1961 | __io_req_free_batch_flush(req->ctx, rb); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1962 | } |
| 1963 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1964 | /* |
| 1965 | * Drop reference to request, return next in chain (if there is one) if this |
| 1966 | * was the last reference to this request. |
| 1967 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1968 | 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] | 1969 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1970 | struct io_kiocb *nxt = NULL; |
| 1971 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1972 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1973 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1974 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1975 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1976 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1979 | static void io_put_req(struct io_kiocb *req) |
| 1980 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1981 | if (refcount_dec_and_test(&req->refs)) |
| 1982 | io_free_req(req); |
| 1983 | } |
| 1984 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1985 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1986 | { |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 1987 | struct io_kiocb *nxt; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1988 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1989 | /* |
| 1990 | * A ref is owned by io-wq in which context we're. So, if that's the |
| 1991 | * last one, it's safe to steal next work. False negatives are Ok, |
| 1992 | * it just will be re-punted async in io_put_work() |
| 1993 | */ |
| 1994 | if (refcount_read(&req->refs) != 1) |
| 1995 | return NULL; |
| 1996 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1997 | nxt = io_req_find_next(req); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 1998 | return nxt ? &nxt->work : NULL; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1999 | } |
| 2000 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2001 | /* |
| 2002 | * Must only be used if we don't need to care about links, usually from |
| 2003 | * within the completion handling itself. |
| 2004 | */ |
| 2005 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2006 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2007 | /* drop both submit and complete references */ |
| 2008 | if (refcount_sub_and_test(2, &req->refs)) |
| 2009 | __io_free_req(req); |
| 2010 | } |
| 2011 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2012 | static void io_double_put_req(struct io_kiocb *req) |
| 2013 | { |
| 2014 | /* drop both submit and complete references */ |
| 2015 | if (refcount_sub_and_test(2, &req->refs)) |
| 2016 | io_free_req(req); |
| 2017 | } |
| 2018 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2019 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2020 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 2021 | struct io_rings *rings = ctx->rings; |
| 2022 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 2023 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 2024 | /* |
| 2025 | * noflush == true is from the waitqueue handler, just ensure |
| 2026 | * we wake up the task, and the next invocation will flush the |
| 2027 | * entries. We cannot safely to it from here. |
| 2028 | */ |
| 2029 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 2030 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2031 | |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 2032 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 2033 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2034 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2035 | /* See comment at the top of this file */ |
| 2036 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 2037 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2038 | } |
| 2039 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2040 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2041 | { |
| 2042 | struct io_rings *rings = ctx->rings; |
| 2043 | |
| 2044 | /* make sure SQ entry isn't read before tail */ |
| 2045 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2046 | } |
| 2047 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2048 | 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] | 2049 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2050 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2051 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2052 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2053 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2054 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2055 | kfree(kbuf); |
| 2056 | return cflags; |
| 2057 | } |
| 2058 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2059 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2060 | { |
| 2061 | struct io_buffer *kbuf; |
| 2062 | |
| 2063 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2064 | return io_put_kbuf(req, kbuf); |
| 2065 | } |
| 2066 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2067 | static inline bool io_run_task_work(void) |
| 2068 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2069 | /* |
| 2070 | * Not safe to run on exiting task, and the task_work handling will |
| 2071 | * not add work to such a task. |
| 2072 | */ |
| 2073 | if (unlikely(current->flags & PF_EXITING)) |
| 2074 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2075 | if (current->task_works) { |
| 2076 | __set_current_state(TASK_RUNNING); |
| 2077 | task_work_run(); |
| 2078 | return true; |
| 2079 | } |
| 2080 | |
| 2081 | return false; |
| 2082 | } |
| 2083 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2084 | static void io_iopoll_queue(struct list_head *again) |
| 2085 | { |
| 2086 | struct io_kiocb *req; |
| 2087 | |
| 2088 | do { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2089 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
| 2090 | list_del(&req->inflight_entry); |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 2091 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2092 | } while (!list_empty(again)); |
| 2093 | } |
| 2094 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2095 | /* |
| 2096 | * Find and free completed poll iocbs |
| 2097 | */ |
| 2098 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2099 | struct list_head *done) |
| 2100 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2101 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2102 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2103 | LIST_HEAD(again); |
| 2104 | |
| 2105 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2106 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2107 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2108 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2109 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2110 | int cflags = 0; |
| 2111 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2112 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2113 | if (READ_ONCE(req->result) == -EAGAIN) { |
Jens Axboe | 56450c2 | 2020-08-26 18:58:26 -0600 | [diff] [blame] | 2114 | req->result = 0; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2115 | req->iopoll_completed = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2116 | list_move_tail(&req->inflight_entry, &again); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2117 | continue; |
| 2118 | } |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2119 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2120 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2121 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2122 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2123 | |
| 2124 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2125 | (*nr_events)++; |
| 2126 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2127 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2128 | io_req_free_batch(&rb, req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2129 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2130 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2131 | io_commit_cqring(ctx); |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 2132 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 2133 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2134 | io_req_free_batch_finish(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2135 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2136 | if (!list_empty(&again)) |
| 2137 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2140 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2141 | long min) |
| 2142 | { |
| 2143 | struct io_kiocb *req, *tmp; |
| 2144 | LIST_HEAD(done); |
| 2145 | bool spin; |
| 2146 | int ret; |
| 2147 | |
| 2148 | /* |
| 2149 | * Only spin for completions if we don't have multiple devices hanging |
| 2150 | * off our complete list, and we're under the requested amount. |
| 2151 | */ |
| 2152 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2153 | |
| 2154 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2155 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2156 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2157 | |
| 2158 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2159 | * Move completed and retryable entries to our local lists. |
| 2160 | * If we find a request that requires polling, break out |
| 2161 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2162 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2163 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2164 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2165 | continue; |
| 2166 | } |
| 2167 | if (!list_empty(&done)) |
| 2168 | break; |
| 2169 | |
| 2170 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2171 | if (ret < 0) |
| 2172 | break; |
| 2173 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2174 | /* iopoll may have completed current req */ |
| 2175 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2176 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2177 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2178 | if (ret && spin) |
| 2179 | spin = false; |
| 2180 | ret = 0; |
| 2181 | } |
| 2182 | |
| 2183 | if (!list_empty(&done)) |
| 2184 | io_iopoll_complete(ctx, nr_events, &done); |
| 2185 | |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2190 | * 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] | 2191 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2192 | * as a non-spinning completion check. |
| 2193 | */ |
| 2194 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2195 | long min) |
| 2196 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2197 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2198 | int ret; |
| 2199 | |
| 2200 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2201 | if (ret < 0) |
| 2202 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2203 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2204 | return 0; |
| 2205 | } |
| 2206 | |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | /* |
| 2211 | * We can't just wait for polled events to come to us, we have to actively |
| 2212 | * find and complete them. |
| 2213 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2214 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2215 | { |
| 2216 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2217 | return; |
| 2218 | |
| 2219 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2220 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2221 | unsigned int nr_events = 0; |
| 2222 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2223 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2224 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2225 | /* let it sleep and repeat later if can't complete a request */ |
| 2226 | if (nr_events == 0) |
| 2227 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2228 | /* |
| 2229 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2230 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2231 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2232 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2233 | if (need_resched()) { |
| 2234 | mutex_unlock(&ctx->uring_lock); |
| 2235 | cond_resched(); |
| 2236 | mutex_lock(&ctx->uring_lock); |
| 2237 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2238 | } |
| 2239 | mutex_unlock(&ctx->uring_lock); |
| 2240 | } |
| 2241 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2242 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2243 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2244 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2245 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2246 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2247 | /* |
| 2248 | * We disallow the app entering submit/complete with polling, but we |
| 2249 | * still need to lock the ring to prevent racing with polled issue |
| 2250 | * that got punted to a workqueue. |
| 2251 | */ |
| 2252 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2253 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2254 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2255 | * Don't enter poll loop if we already have events pending. |
| 2256 | * If we do, we can potentially be spinning for commands that |
| 2257 | * already triggered a CQE (eg in error). |
| 2258 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2259 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2260 | break; |
| 2261 | |
| 2262 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2263 | * If a submit got punted to a workqueue, we can have the |
| 2264 | * application entering polling for a command before it gets |
| 2265 | * issued. That app will hold the uring_lock for the duration |
| 2266 | * of the poll right here, so we need to take a breather every |
| 2267 | * now and then to ensure that the issue has a chance to add |
| 2268 | * the poll to the issued list. Otherwise we can spin here |
| 2269 | * forever, while the workqueue is stuck trying to acquire the |
| 2270 | * very same mutex. |
| 2271 | */ |
| 2272 | if (!(++iters & 7)) { |
| 2273 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2274 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2275 | mutex_lock(&ctx->uring_lock); |
| 2276 | } |
| 2277 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2278 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2279 | if (ret <= 0) |
| 2280 | break; |
| 2281 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2282 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2283 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2284 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2285 | return ret; |
| 2286 | } |
| 2287 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2288 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2289 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2290 | /* |
| 2291 | * Tell lockdep we inherited freeze protection from submission |
| 2292 | * thread. |
| 2293 | */ |
| 2294 | if (req->flags & REQ_F_ISREG) { |
| 2295 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2296 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2297 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2298 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2299 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2302 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 2303 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2304 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2305 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2306 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2307 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2308 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2309 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2310 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2311 | if (res != req->result) |
| 2312 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2313 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2314 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2315 | __io_req_complete(req, res, cflags, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2316 | } |
| 2317 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2318 | #ifdef CONFIG_BLOCK |
| 2319 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2320 | { |
| 2321 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2322 | ssize_t ret = -ECANCELED; |
| 2323 | struct iov_iter iter; |
| 2324 | int rw; |
| 2325 | |
| 2326 | if (error) { |
| 2327 | ret = error; |
| 2328 | goto end_req; |
| 2329 | } |
| 2330 | |
| 2331 | switch (req->opcode) { |
| 2332 | case IORING_OP_READV: |
| 2333 | case IORING_OP_READ_FIXED: |
| 2334 | case IORING_OP_READ: |
| 2335 | rw = READ; |
| 2336 | break; |
| 2337 | case IORING_OP_WRITEV: |
| 2338 | case IORING_OP_WRITE_FIXED: |
| 2339 | case IORING_OP_WRITE: |
| 2340 | rw = WRITE; |
| 2341 | break; |
| 2342 | default: |
| 2343 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2344 | req->opcode); |
| 2345 | goto end_req; |
| 2346 | } |
| 2347 | |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2348 | if (!req->io) { |
| 2349 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2350 | if (ret < 0) |
| 2351 | goto end_req; |
| 2352 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
| 2353 | if (!ret) |
| 2354 | return true; |
| 2355 | kfree(iovec); |
| 2356 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2357 | return true; |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2358 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2359 | end_req: |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2360 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 2361 | io_req_complete(req, ret); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2362 | return false; |
| 2363 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2364 | #endif |
| 2365 | |
| 2366 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2367 | { |
| 2368 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2369 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2370 | int ret; |
| 2371 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2372 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2373 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2374 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2375 | return false; |
| 2376 | |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2377 | ret = io_sq_thread_acquire_mm(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2378 | |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2379 | if (io_resubmit_prep(req, ret)) { |
| 2380 | refcount_inc(&req->refs); |
| 2381 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2382 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2383 | } |
| 2384 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2385 | #endif |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2389 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2390 | struct io_comp_state *cs) |
| 2391 | { |
| 2392 | if (!io_rw_reissue(req, res)) |
| 2393 | io_complete_rw_common(&req->rw.kiocb, res, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2397 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2398 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2399 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2400 | __io_complete_rw(req, res, res2, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2403 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2404 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2405 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2406 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2407 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2408 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2409 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2410 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2411 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2412 | |
| 2413 | WRITE_ONCE(req->result, res); |
| 2414 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2415 | smp_wmb(); |
| 2416 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* |
| 2420 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2421 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2422 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2423 | * accessing the kiocb cookie. |
| 2424 | */ |
| 2425 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 2426 | { |
| 2427 | struct io_ring_ctx *ctx = req->ctx; |
| 2428 | |
| 2429 | /* |
| 2430 | * Track whether we have multiple files in our lists. This will impact |
| 2431 | * how we do polling eventually, not spinning if we're on potentially |
| 2432 | * different devices. |
| 2433 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2434 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2435 | ctx->poll_multi_file = false; |
| 2436 | } else if (!ctx->poll_multi_file) { |
| 2437 | struct io_kiocb *list_req; |
| 2438 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2439 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2440 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2441 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2442 | ctx->poll_multi_file = true; |
| 2443 | } |
| 2444 | |
| 2445 | /* |
| 2446 | * For fast devices, IO may have already completed. If it has, add |
| 2447 | * it to the front so we find it first. |
| 2448 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2449 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2450 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2451 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2452 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2453 | |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 2454 | if ((ctx->flags & IORING_SETUP_SQPOLL) && wq_has_sleeper(ctx->sqo_wait)) |
| 2455 | wake_up(ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2458 | static void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2459 | { |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2460 | if (state->has_refs) |
| 2461 | fput_many(state->file, state->has_refs); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2462 | state->file = NULL; |
| 2463 | } |
| 2464 | |
| 2465 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2466 | { |
| 2467 | if (state->file) |
| 2468 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * Get as many references to a file as we have IOs left in this submission, |
| 2473 | * assuming most submissions are for one file, or at least that each file |
| 2474 | * has more than one submission. |
| 2475 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2476 | 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] | 2477 | { |
| 2478 | if (!state) |
| 2479 | return fget(fd); |
| 2480 | |
| 2481 | if (state->file) { |
| 2482 | if (state->fd == fd) { |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2483 | state->has_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2484 | state->ios_left--; |
| 2485 | return state->file; |
| 2486 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2487 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2488 | } |
| 2489 | state->file = fget_many(fd, state->ios_left); |
| 2490 | if (!state->file) |
| 2491 | return NULL; |
| 2492 | |
| 2493 | state->fd = fd; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2494 | state->ios_left--; |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2495 | state->has_refs = state->ios_left; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2496 | return state->file; |
| 2497 | } |
| 2498 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2499 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2500 | { |
| 2501 | #ifdef CONFIG_BLOCK |
| 2502 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); |
| 2503 | #else |
| 2504 | return true; |
| 2505 | #endif |
| 2506 | } |
| 2507 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2508 | /* |
| 2509 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2510 | * any file. For now, just ensure that anything potentially problematic is done |
| 2511 | * inline. |
| 2512 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2513 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2514 | { |
| 2515 | umode_t mode = file_inode(file)->i_mode; |
| 2516 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2517 | if (S_ISBLK(mode)) { |
| 2518 | if (io_bdev_nowait(file->f_inode->i_bdev)) |
| 2519 | return true; |
| 2520 | return false; |
| 2521 | } |
| 2522 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2523 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2524 | if (S_ISREG(mode)) { |
| 2525 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
| 2526 | file->f_op != &io_uring_fops) |
| 2527 | return true; |
| 2528 | return false; |
| 2529 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2530 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2531 | /* any ->read/write should understand O_NONBLOCK */ |
| 2532 | if (file->f_flags & O_NONBLOCK) |
| 2533 | return true; |
| 2534 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2535 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2536 | return false; |
| 2537 | |
| 2538 | if (rw == READ) |
| 2539 | return file->f_op->read_iter != NULL; |
| 2540 | |
| 2541 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2544 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2545 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2546 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2547 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2548 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2549 | unsigned ioprio; |
| 2550 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2551 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2552 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2553 | req->flags |= REQ_F_ISREG; |
| 2554 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2555 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2556 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2557 | req->flags |= REQ_F_CUR_POS; |
| 2558 | kiocb->ki_pos = req->file->f_pos; |
| 2559 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2560 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2561 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2562 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2563 | if (unlikely(ret)) |
| 2564 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2565 | |
| 2566 | ioprio = READ_ONCE(sqe->ioprio); |
| 2567 | if (ioprio) { |
| 2568 | ret = ioprio_check_cap(ioprio); |
| 2569 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2570 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2571 | |
| 2572 | kiocb->ki_ioprio = ioprio; |
| 2573 | } else |
| 2574 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2575 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2576 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2577 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2578 | req->flags |= REQ_F_NOWAIT; |
| 2579 | |
| 2580 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2581 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2582 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2583 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2584 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2585 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2586 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2587 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2588 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2589 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2590 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2591 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2592 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2593 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2594 | kiocb->ki_complete = io_complete_rw; |
| 2595 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2596 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2597 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2598 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2599 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2600 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2604 | { |
| 2605 | switch (ret) { |
| 2606 | case -EIOCBQUEUED: |
| 2607 | break; |
| 2608 | case -ERESTARTSYS: |
| 2609 | case -ERESTARTNOINTR: |
| 2610 | case -ERESTARTNOHAND: |
| 2611 | case -ERESTART_RESTARTBLOCK: |
| 2612 | /* |
| 2613 | * We can't just restart the syscall, since previously |
| 2614 | * submitted sqes may already be in progress. Just fail this |
| 2615 | * IO with EINTR. |
| 2616 | */ |
| 2617 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2618 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2619 | default: |
| 2620 | kiocb->ki_complete(kiocb, ret, 0); |
| 2621 | } |
| 2622 | } |
| 2623 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2624 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| 2625 | struct io_comp_state *cs) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2626 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2627 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2628 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2629 | /* add previously done IO, if any */ |
| 2630 | if (req->io && req->io->rw.bytes_done > 0) { |
| 2631 | if (ret < 0) |
| 2632 | ret = req->io->rw.bytes_done; |
| 2633 | else |
| 2634 | ret += req->io->rw.bytes_done; |
| 2635 | } |
| 2636 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2637 | if (req->flags & REQ_F_CUR_POS) |
| 2638 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2639 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2640 | __io_complete_rw(req, ret, 0, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2641 | else |
| 2642 | io_rw_done(kiocb, ret); |
| 2643 | } |
| 2644 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2645 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2646 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2647 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2648 | struct io_ring_ctx *ctx = req->ctx; |
| 2649 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2650 | struct io_mapped_ubuf *imu; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2651 | u16 index, buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2652 | size_t offset; |
| 2653 | u64 buf_addr; |
| 2654 | |
| 2655 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2656 | if (unlikely(!ctx->user_bufs)) |
| 2657 | return -EFAULT; |
| 2658 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2659 | buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2660 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2661 | return -EFAULT; |
| 2662 | |
| 2663 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2664 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2665 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2666 | |
| 2667 | /* overflow */ |
| 2668 | if (buf_addr + len < buf_addr) |
| 2669 | return -EFAULT; |
| 2670 | /* not inside the mapped region */ |
| 2671 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2672 | return -EFAULT; |
| 2673 | |
| 2674 | /* |
| 2675 | * May not be a start of buffer, set size appropriately |
| 2676 | * and advance us to the beginning. |
| 2677 | */ |
| 2678 | offset = buf_addr - imu->ubuf; |
| 2679 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2680 | |
| 2681 | if (offset) { |
| 2682 | /* |
| 2683 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2684 | * using the latter parts of a big fixed buffer - it iterates |
| 2685 | * over each segment manually. We can cheat a bit here, because |
| 2686 | * we know that: |
| 2687 | * |
| 2688 | * 1) it's a BVEC iter, we set it up |
| 2689 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2690 | * first and last bvec |
| 2691 | * |
| 2692 | * So just find our index, and adjust the iterator afterwards. |
| 2693 | * If the offset is within the first bvec (or the whole first |
| 2694 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2695 | * since we can just skip the first segment, which may not |
| 2696 | * be PAGE_SIZE aligned. |
| 2697 | */ |
| 2698 | const struct bio_vec *bvec = imu->bvec; |
| 2699 | |
| 2700 | if (offset <= bvec->bv_len) { |
| 2701 | iov_iter_advance(iter, offset); |
| 2702 | } else { |
| 2703 | unsigned long seg_skip; |
| 2704 | |
| 2705 | /* skip first vec */ |
| 2706 | offset -= bvec->bv_len; |
| 2707 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2708 | |
| 2709 | iter->bvec = bvec + seg_skip; |
| 2710 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2711 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2712 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2716 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2719 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2720 | { |
| 2721 | if (needs_lock) |
| 2722 | mutex_unlock(&ctx->uring_lock); |
| 2723 | } |
| 2724 | |
| 2725 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2726 | { |
| 2727 | /* |
| 2728 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2729 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2730 | * The only exception is when we've detached the request and issue it |
| 2731 | * from an async worker thread, grab the lock for that case. |
| 2732 | */ |
| 2733 | if (needs_lock) |
| 2734 | mutex_lock(&ctx->uring_lock); |
| 2735 | } |
| 2736 | |
| 2737 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2738 | int bgid, struct io_buffer *kbuf, |
| 2739 | bool needs_lock) |
| 2740 | { |
| 2741 | struct io_buffer *head; |
| 2742 | |
| 2743 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2744 | return kbuf; |
| 2745 | |
| 2746 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2747 | |
| 2748 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2749 | |
| 2750 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2751 | if (head) { |
| 2752 | if (!list_empty(&head->list)) { |
| 2753 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2754 | list); |
| 2755 | list_del(&kbuf->list); |
| 2756 | } else { |
| 2757 | kbuf = head; |
| 2758 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2759 | } |
| 2760 | if (*len > kbuf->len) |
| 2761 | *len = kbuf->len; |
| 2762 | } else { |
| 2763 | kbuf = ERR_PTR(-ENOBUFS); |
| 2764 | } |
| 2765 | |
| 2766 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2767 | |
| 2768 | return kbuf; |
| 2769 | } |
| 2770 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2771 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2772 | bool needs_lock) |
| 2773 | { |
| 2774 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2775 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2776 | |
| 2777 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2778 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2779 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2780 | if (IS_ERR(kbuf)) |
| 2781 | return kbuf; |
| 2782 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2783 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2784 | return u64_to_user_ptr(kbuf->addr); |
| 2785 | } |
| 2786 | |
| 2787 | #ifdef CONFIG_COMPAT |
| 2788 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2789 | bool needs_lock) |
| 2790 | { |
| 2791 | struct compat_iovec __user *uiov; |
| 2792 | compat_ssize_t clen; |
| 2793 | void __user *buf; |
| 2794 | ssize_t len; |
| 2795 | |
| 2796 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2797 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2798 | return -EFAULT; |
| 2799 | if (__get_user(clen, &uiov->iov_len)) |
| 2800 | return -EFAULT; |
| 2801 | if (clen < 0) |
| 2802 | return -EINVAL; |
| 2803 | |
| 2804 | len = clen; |
| 2805 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2806 | if (IS_ERR(buf)) |
| 2807 | return PTR_ERR(buf); |
| 2808 | iov[0].iov_base = buf; |
| 2809 | iov[0].iov_len = (compat_size_t) len; |
| 2810 | return 0; |
| 2811 | } |
| 2812 | #endif |
| 2813 | |
| 2814 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2815 | bool needs_lock) |
| 2816 | { |
| 2817 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2818 | void __user *buf; |
| 2819 | ssize_t len; |
| 2820 | |
| 2821 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2822 | return -EFAULT; |
| 2823 | |
| 2824 | len = iov[0].iov_len; |
| 2825 | if (len < 0) |
| 2826 | return -EINVAL; |
| 2827 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2828 | if (IS_ERR(buf)) |
| 2829 | return PTR_ERR(buf); |
| 2830 | iov[0].iov_base = buf; |
| 2831 | iov[0].iov_len = len; |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2836 | bool needs_lock) |
| 2837 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2838 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2839 | struct io_buffer *kbuf; |
| 2840 | |
| 2841 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2842 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2843 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2844 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2845 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2846 | if (!req->rw.len) |
| 2847 | return 0; |
| 2848 | else if (req->rw.len > 1) |
| 2849 | return -EINVAL; |
| 2850 | |
| 2851 | #ifdef CONFIG_COMPAT |
| 2852 | if (req->ctx->compat) |
| 2853 | return io_compat_import(req, iov, needs_lock); |
| 2854 | #endif |
| 2855 | |
| 2856 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2857 | } |
| 2858 | |
Jens Axboe | 8452fd0 | 2020-08-18 13:58:33 -0700 | [diff] [blame] | 2859 | static ssize_t __io_import_iovec(int rw, struct io_kiocb *req, |
| 2860 | struct iovec **iovec, struct iov_iter *iter, |
| 2861 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2862 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2863 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2864 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2865 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2866 | u8 opcode; |
| 2867 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2868 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2869 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2870 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2871 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2872 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2873 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2874 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2875 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2876 | return -EINVAL; |
| 2877 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2878 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2879 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2880 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 2881 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2882 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2883 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2886 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2887 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2888 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2891 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2892 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2893 | if (!ret) { |
| 2894 | ret = (*iovec)->iov_len; |
| 2895 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 2896 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2897 | *iovec = NULL; |
| 2898 | return ret; |
| 2899 | } |
| 2900 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2901 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2902 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2903 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2904 | iovec, iter); |
| 2905 | #endif |
| 2906 | |
| 2907 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2908 | } |
| 2909 | |
Jens Axboe | 8452fd0 | 2020-08-18 13:58:33 -0700 | [diff] [blame] | 2910 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2911 | struct iovec **iovec, struct iov_iter *iter, |
| 2912 | bool needs_lock) |
| 2913 | { |
| 2914 | if (!req->io) |
| 2915 | return __io_import_iovec(rw, req, iovec, iter, needs_lock); |
| 2916 | *iovec = NULL; |
| 2917 | return iov_iter_count(&req->io->rw.iter); |
| 2918 | } |
| 2919 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2920 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 2921 | { |
| 2922 | return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos; |
| 2923 | } |
| 2924 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2925 | /* |
| 2926 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2927 | * by looping over ->read() or ->write() manually. |
| 2928 | */ |
| 2929 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2930 | struct iov_iter *iter) |
| 2931 | { |
| 2932 | ssize_t ret = 0; |
| 2933 | |
| 2934 | /* |
| 2935 | * Don't support polled IO through this interface, and we can't |
| 2936 | * support non-blocking either. For the latter, this just causes |
| 2937 | * the kiocb to be handled from an async context. |
| 2938 | */ |
| 2939 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2940 | return -EOPNOTSUPP; |
| 2941 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2942 | return -EAGAIN; |
| 2943 | |
| 2944 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2945 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2946 | ssize_t nr; |
| 2947 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2948 | if (!iov_iter_is_bvec(iter)) { |
| 2949 | iovec = iov_iter_iovec(iter); |
| 2950 | } else { |
| 2951 | /* fixed buffers import bvec */ |
| 2952 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2953 | + iter->iov_offset; |
| 2954 | iovec.iov_len = min(iter->count, |
| 2955 | iter->bvec->bv_len - iter->iov_offset); |
| 2956 | } |
| 2957 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2958 | if (rw == READ) { |
| 2959 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2960 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2961 | } else { |
| 2962 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2963 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2964 | } |
| 2965 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2966 | if (iov_iter_is_bvec(iter)) |
| 2967 | kunmap(iter->bvec->bv_page); |
| 2968 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2969 | if (nr < 0) { |
| 2970 | if (!ret) |
| 2971 | ret = nr; |
| 2972 | break; |
| 2973 | } |
| 2974 | ret += nr; |
| 2975 | if (nr != iovec.iov_len) |
| 2976 | break; |
| 2977 | iov_iter_advance(iter, nr); |
| 2978 | } |
| 2979 | |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 2983 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 2984 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2985 | { |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 2986 | struct io_async_rw *rw = &req->io->rw; |
| 2987 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 2988 | memcpy(&rw->iter, iter, sizeof(*iter)); |
| 2989 | rw->free_iovec = NULL; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2990 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 2991 | /* can only be fixed buffers, no need to do anything */ |
| 2992 | if (iter->type == ITER_BVEC) |
| 2993 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 2994 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 2995 | unsigned iov_off = 0; |
| 2996 | |
| 2997 | rw->iter.iov = rw->fast_iov; |
| 2998 | if (iter->iov != fast_iov) { |
| 2999 | iov_off = iter->iov - fast_iov; |
| 3000 | rw->iter.iov += iov_off; |
| 3001 | } |
| 3002 | if (rw->fast_iov != fast_iov) |
| 3003 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3004 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3005 | } else { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3006 | rw->free_iovec = iovec; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3007 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3011 | static inline int __io_alloc_async_ctx(struct io_kiocb *req) |
| 3012 | { |
| 3013 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
| 3014 | return req->io == NULL; |
| 3015 | } |
| 3016 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3017 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3018 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3019 | if (!io_op_defs[req->opcode].async_ctx) |
| 3020 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3021 | |
| 3022 | return __io_alloc_async_ctx(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3025 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3026 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3027 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3028 | { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3029 | if (!force && !io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3030 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3031 | if (!req->io) { |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3032 | if (__io_alloc_async_ctx(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3033 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3034 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3035 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3036 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3037 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3040 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw, |
| 3041 | bool force_nonblock) |
| 3042 | { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3043 | struct io_async_rw *iorw = &req->io->rw; |
Jens Axboe | c183edf | 2020-09-04 22:36:52 -0600 | [diff] [blame] | 3044 | struct iovec *iov; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3045 | ssize_t ret; |
| 3046 | |
Jens Axboe | c183edf | 2020-09-04 22:36:52 -0600 | [diff] [blame] | 3047 | iorw->iter.iov = iov = iorw->fast_iov; |
| 3048 | ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3049 | if (unlikely(ret < 0)) |
| 3050 | return ret; |
| 3051 | |
Jens Axboe | c183edf | 2020-09-04 22:36:52 -0600 | [diff] [blame] | 3052 | iorw->iter.iov = iov; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3053 | io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3054 | return 0; |
| 3055 | } |
| 3056 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3057 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3058 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3059 | { |
| 3060 | ssize_t ret; |
| 3061 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3062 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 3063 | if (ret) |
| 3064 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3065 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3066 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3067 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3068 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3069 | /* either don't need iovec imported or already have it */ |
| 3070 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3071 | return 0; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3072 | return io_rw_prep_async(req, READ, force_nonblock); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3075 | /* |
| 3076 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3077 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3078 | * This gets called when the page is unlocked, and we generally expect that to |
| 3079 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3080 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3081 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3082 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3083 | * slow path. |
| 3084 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3085 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3086 | int sync, void *arg) |
| 3087 | { |
| 3088 | struct wait_page_queue *wpq; |
| 3089 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3090 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3091 | int ret; |
| 3092 | |
| 3093 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3094 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3095 | if (!wake_page_match(wpq, key)) |
| 3096 | return 0; |
| 3097 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3098 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3099 | list_del_init(&wait->entry); |
| 3100 | |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3101 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 3102 | percpu_ref_get(&req->ctx->refs); |
| 3103 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3104 | /* submit ref gets dropped, acquire a new one */ |
| 3105 | refcount_inc(&req->refs); |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 3106 | ret = io_req_task_work_add(req, &req->task_work, true); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3107 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3108 | struct task_struct *tsk; |
| 3109 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3110 | /* queue just for cancelation */ |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3111 | init_task_work(&req->task_work, io_req_task_cancel); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3112 | tsk = io_wq_get_task(req->ctx->io_wq); |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3113 | task_work_add(tsk, &req->task_work, 0); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3114 | wake_up_process(tsk); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3115 | } |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3116 | return 1; |
| 3117 | } |
| 3118 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3119 | /* |
| 3120 | * This controls whether a given IO request should be armed for async page |
| 3121 | * based retry. If we return false here, the request is handed to the async |
| 3122 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3123 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3124 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3125 | * will register a callback when the page is unlocked at IO completion. Through |
| 3126 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3127 | * That retry will attempt the buffered read again. The retry will generally |
| 3128 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3129 | * async worker threads for a blocking retry. |
| 3130 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3131 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3132 | { |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3133 | struct wait_page_queue *wait = &req->io->rw.wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3134 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3135 | |
| 3136 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3137 | if (req->flags & REQ_F_NOWAIT) |
| 3138 | return false; |
| 3139 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3140 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3141 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3142 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3143 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3144 | /* |
| 3145 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3146 | * support callback based unlocks |
| 3147 | */ |
| 3148 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3149 | return false; |
| 3150 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3151 | wait->wait.func = io_async_buf_func; |
| 3152 | wait->wait.private = req; |
| 3153 | wait->wait.flags = 0; |
| 3154 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3155 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3156 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3157 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3158 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3162 | { |
| 3163 | if (req->file->f_op->read_iter) |
| 3164 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3165 | else if (req->file->f_op->read) |
| 3166 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); |
| 3167 | else |
| 3168 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3169 | } |
| 3170 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3171 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
| 3172 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3173 | { |
| 3174 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3175 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3176 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3177 | ssize_t io_size, ret, ret2; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3178 | size_t iov_count; |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3179 | bool no_async; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3180 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3181 | if (req->io) |
| 3182 | iter = &req->io->rw.iter; |
| 3183 | |
| 3184 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3185 | if (ret < 0) |
| 3186 | return ret; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3187 | iov_count = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3188 | io_size = ret; |
| 3189 | req->result = io_size; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3190 | ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3191 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3192 | /* Ensure we clear previously set non-block flag */ |
| 3193 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3194 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3195 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3196 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3197 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
| 3198 | if (no_async) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3199 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3200 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3201 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3202 | if (unlikely(ret)) |
| 3203 | goto out_free; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3204 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3205 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3206 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3207 | if (!ret) { |
| 3208 | goto done; |
| 3209 | } else if (ret == -EIOCBQUEUED) { |
| 3210 | ret = 0; |
| 3211 | goto out_free; |
| 3212 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3213 | /* IOPOLL retry should happen for io-wq threads */ |
| 3214 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3215 | goto done; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3216 | /* no retry on NONBLOCK marked file */ |
| 3217 | if (req->file->f_flags & O_NONBLOCK) |
| 3218 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3219 | /* some cases will consume bytes even on error returns */ |
| 3220 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3221 | ret = 0; |
| 3222 | goto copy_iov; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3223 | } else if (ret < 0) { |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3224 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
| 3225 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /* read it all, or we did blocking attempt. no retry. */ |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3229 | if (!iov_iter_count(iter) || !force_nonblock || |
| 3230 | (req->file->f_flags & O_NONBLOCK)) |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3231 | goto done; |
| 3232 | |
| 3233 | io_size -= ret; |
| 3234 | copy_iov: |
| 3235 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
| 3236 | if (ret2) { |
| 3237 | ret = ret2; |
| 3238 | goto out_free; |
| 3239 | } |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3240 | if (no_async) |
| 3241 | return -EAGAIN; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3242 | /* it's copied and will be cleaned with ->io */ |
| 3243 | iovec = NULL; |
| 3244 | /* now use our persistent iterator, if we aren't already */ |
| 3245 | iter = &req->io->rw.iter; |
| 3246 | retry: |
| 3247 | req->io->rw.bytes_done += ret; |
| 3248 | /* if we can retry, do so with the callbacks armed */ |
| 3249 | if (!io_rw_should_retry(req)) { |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3250 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3251 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3252 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3253 | |
| 3254 | /* |
| 3255 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we |
| 3256 | * get -EIOCBQUEUED, then we'll get a notification when the desired |
| 3257 | * page gets unlocked. We can also get a partial read here, and if we |
| 3258 | * do, then just retry at the new offset. |
| 3259 | */ |
| 3260 | ret = io_iter_do_read(req, iter); |
| 3261 | if (ret == -EIOCBQUEUED) { |
| 3262 | ret = 0; |
| 3263 | goto out_free; |
| 3264 | } else if (ret > 0 && ret < io_size) { |
| 3265 | /* we got some bytes, but not all. retry. */ |
| 3266 | goto retry; |
| 3267 | } |
| 3268 | done: |
| 3269 | kiocb_done(kiocb, ret, cs); |
| 3270 | ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3271 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3272 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3273 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3274 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3275 | return ret; |
| 3276 | } |
| 3277 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3278 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3279 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3280 | { |
| 3281 | ssize_t ret; |
| 3282 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3283 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 3284 | if (ret) |
| 3285 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3286 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3287 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3288 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3289 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3290 | /* either don't need iovec imported or already have it */ |
| 3291 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3292 | return 0; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3293 | return io_rw_prep_async(req, WRITE, force_nonblock); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3296 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
| 3297 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3298 | { |
| 3299 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3300 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3301 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3302 | size_t iov_count; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3303 | ssize_t ret, ret2, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3304 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3305 | if (req->io) |
| 3306 | iter = &req->io->rw.iter; |
| 3307 | |
| 3308 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3309 | if (ret < 0) |
| 3310 | return ret; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3311 | iov_count = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3312 | io_size = ret; |
| 3313 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3314 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3315 | /* Ensure we clear previously set non-block flag */ |
| 3316 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3317 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3318 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3319 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3320 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3321 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3322 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3323 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3324 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3325 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3326 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3327 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3328 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3329 | if (unlikely(ret)) |
| 3330 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3331 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3332 | /* |
| 3333 | * Open-code file_start_write here to grab freeze protection, |
| 3334 | * which will be released by another thread in |
| 3335 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3336 | * released so that it doesn't complain about the held lock when |
| 3337 | * we return to userspace. |
| 3338 | */ |
| 3339 | if (req->flags & REQ_F_ISREG) { |
| 3340 | __sb_start_write(file_inode(req->file)->i_sb, |
| 3341 | SB_FREEZE_WRITE, true); |
| 3342 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3343 | SB_FREEZE_WRITE); |
| 3344 | } |
| 3345 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3346 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3347 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3348 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3349 | else if (req->file->f_op->write) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3350 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3351 | else |
| 3352 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3353 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3354 | /* |
| 3355 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3356 | * retry them without IOCB_NOWAIT. |
| 3357 | */ |
| 3358 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3359 | ret2 = -EAGAIN; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3360 | /* no retry on NONBLOCK marked file */ |
| 3361 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) |
| 3362 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3363 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3364 | /* IOPOLL retry should happen for io-wq threads */ |
| 3365 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3366 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3367 | done: |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3368 | kiocb_done(kiocb, ret2, cs); |
| 3369 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3370 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3371 | /* some cases will consume bytes even on error returns */ |
| 3372 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3373 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3374 | if (!ret) |
| 3375 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3376 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3377 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3378 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3379 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3380 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3381 | return ret; |
| 3382 | } |
| 3383 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3384 | static int __io_splice_prep(struct io_kiocb *req, |
| 3385 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3386 | { |
| 3387 | struct io_splice* sp = &req->splice; |
| 3388 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 3389 | int ret; |
| 3390 | |
| 3391 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3392 | return 0; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3393 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3394 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3395 | |
| 3396 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3397 | sp->len = READ_ONCE(sqe->len); |
| 3398 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3399 | |
| 3400 | if (unlikely(sp->flags & ~valid_flags)) |
| 3401 | return -EINVAL; |
| 3402 | |
| 3403 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 3404 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3405 | if (ret) |
| 3406 | return ret; |
| 3407 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3408 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3409 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3410 | /* |
| 3411 | * Splice operation will be punted aync, and here need to |
| 3412 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3413 | */ |
| 3414 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3415 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3416 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3417 | |
| 3418 | return 0; |
| 3419 | } |
| 3420 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3421 | static int io_tee_prep(struct io_kiocb *req, |
| 3422 | const struct io_uring_sqe *sqe) |
| 3423 | { |
| 3424 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3425 | return -EINVAL; |
| 3426 | return __io_splice_prep(req, sqe); |
| 3427 | } |
| 3428 | |
| 3429 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3430 | { |
| 3431 | struct io_splice *sp = &req->splice; |
| 3432 | struct file *in = sp->file_in; |
| 3433 | struct file *out = sp->file_out; |
| 3434 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3435 | long ret = 0; |
| 3436 | |
| 3437 | if (force_nonblock) |
| 3438 | return -EAGAIN; |
| 3439 | if (sp->len) |
| 3440 | ret = do_tee(in, out, sp->len, flags); |
| 3441 | |
| 3442 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3443 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3444 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3445 | if (ret != sp->len) |
| 3446 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3447 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3448 | return 0; |
| 3449 | } |
| 3450 | |
| 3451 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3452 | { |
| 3453 | struct io_splice* sp = &req->splice; |
| 3454 | |
| 3455 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3456 | sp->off_out = READ_ONCE(sqe->off); |
| 3457 | return __io_splice_prep(req, sqe); |
| 3458 | } |
| 3459 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3460 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3461 | { |
| 3462 | struct io_splice *sp = &req->splice; |
| 3463 | struct file *in = sp->file_in; |
| 3464 | struct file *out = sp->file_out; |
| 3465 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3466 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3467 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3468 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3469 | if (force_nonblock) |
| 3470 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3471 | |
| 3472 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3473 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3474 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3475 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3476 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3477 | |
| 3478 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3479 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3480 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3481 | if (ret != sp->len) |
| 3482 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3483 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3484 | return 0; |
| 3485 | } |
| 3486 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3487 | /* |
| 3488 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3489 | */ |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3490 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3491 | { |
| 3492 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3493 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3494 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3495 | return -EINVAL; |
| 3496 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3497 | __io_req_complete(req, 0, 0, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3498 | return 0; |
| 3499 | } |
| 3500 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3501 | 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] | 3502 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3503 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3504 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3505 | if (!req->file) |
| 3506 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3507 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3508 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3509 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3510 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3511 | return -EINVAL; |
| 3512 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3513 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3514 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3515 | return -EINVAL; |
| 3516 | |
| 3517 | req->sync.off = READ_ONCE(sqe->off); |
| 3518 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3519 | return 0; |
| 3520 | } |
| 3521 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3522 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3523 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3524 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3525 | int ret; |
| 3526 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3527 | /* fsync always requires a blocking context */ |
| 3528 | if (force_nonblock) |
| 3529 | return -EAGAIN; |
| 3530 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3531 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3532 | end > 0 ? end : LLONG_MAX, |
| 3533 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3534 | if (ret < 0) |
| 3535 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3536 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3537 | return 0; |
| 3538 | } |
| 3539 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3540 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3541 | const struct io_uring_sqe *sqe) |
| 3542 | { |
| 3543 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3544 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3545 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3546 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3547 | |
| 3548 | req->sync.off = READ_ONCE(sqe->off); |
| 3549 | req->sync.len = READ_ONCE(sqe->addr); |
| 3550 | req->sync.mode = READ_ONCE(sqe->len); |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3554 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3555 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3556 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3557 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3558 | /* fallocate always requiring blocking context */ |
| 3559 | if (force_nonblock) |
| 3560 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3561 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3562 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3563 | if (ret < 0) |
| 3564 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3565 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3566 | return 0; |
| 3567 | } |
| 3568 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3569 | 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] | 3570 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3571 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3572 | int ret; |
| 3573 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3574 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3575 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3576 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3577 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3578 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3579 | /* open.how should be already initialised */ |
| 3580 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3581 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3582 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3583 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3584 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3585 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3586 | if (IS_ERR(req->open.filename)) { |
| 3587 | ret = PTR_ERR(req->open.filename); |
| 3588 | req->open.filename = NULL; |
| 3589 | return ret; |
| 3590 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3591 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3592 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3593 | return 0; |
| 3594 | } |
| 3595 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3596 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3597 | { |
| 3598 | u64 flags, mode; |
| 3599 | |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3600 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3601 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3602 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3603 | return 0; |
| 3604 | mode = READ_ONCE(sqe->len); |
| 3605 | flags = READ_ONCE(sqe->open_flags); |
| 3606 | req->open.how = build_open_how(flags, mode); |
| 3607 | return __io_openat_prep(req, sqe); |
| 3608 | } |
| 3609 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3610 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3611 | { |
| 3612 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3613 | size_t len; |
| 3614 | int ret; |
| 3615 | |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3616 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3617 | return -EINVAL; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3618 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3619 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3620 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3621 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3622 | if (len < OPEN_HOW_SIZE_VER0) |
| 3623 | return -EINVAL; |
| 3624 | |
| 3625 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3626 | len); |
| 3627 | if (ret) |
| 3628 | return ret; |
| 3629 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3630 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3633 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3634 | { |
| 3635 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3636 | struct file *file; |
| 3637 | int ret; |
| 3638 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 3639 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3640 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3641 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3642 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3643 | if (ret) |
| 3644 | goto err; |
| 3645 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3646 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3647 | if (ret < 0) |
| 3648 | goto err; |
| 3649 | |
| 3650 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 3651 | if (IS_ERR(file)) { |
| 3652 | put_unused_fd(ret); |
| 3653 | ret = PTR_ERR(file); |
| 3654 | } else { |
| 3655 | fsnotify_open(file); |
| 3656 | fd_install(ret, file); |
| 3657 | } |
| 3658 | err: |
| 3659 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3660 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3661 | if (ret < 0) |
| 3662 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3663 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3664 | return 0; |
| 3665 | } |
| 3666 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3667 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3668 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3669 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3670 | } |
| 3671 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3672 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3673 | const struct io_uring_sqe *sqe) |
| 3674 | { |
| 3675 | struct io_provide_buf *p = &req->pbuf; |
| 3676 | u64 tmp; |
| 3677 | |
| 3678 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3679 | return -EINVAL; |
| 3680 | |
| 3681 | tmp = READ_ONCE(sqe->fd); |
| 3682 | if (!tmp || tmp > USHRT_MAX) |
| 3683 | return -EINVAL; |
| 3684 | |
| 3685 | memset(p, 0, sizeof(*p)); |
| 3686 | p->nbufs = tmp; |
| 3687 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
| 3691 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3692 | int bgid, unsigned nbufs) |
| 3693 | { |
| 3694 | unsigned i = 0; |
| 3695 | |
| 3696 | /* shouldn't happen */ |
| 3697 | if (!nbufs) |
| 3698 | return 0; |
| 3699 | |
| 3700 | /* the head kbuf is the list itself */ |
| 3701 | while (!list_empty(&buf->list)) { |
| 3702 | struct io_buffer *nxt; |
| 3703 | |
| 3704 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3705 | list_del(&nxt->list); |
| 3706 | kfree(nxt); |
| 3707 | if (++i == nbufs) |
| 3708 | return i; |
| 3709 | } |
| 3710 | i++; |
| 3711 | kfree(buf); |
| 3712 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3713 | |
| 3714 | return i; |
| 3715 | } |
| 3716 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3717 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3718 | struct io_comp_state *cs) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3719 | { |
| 3720 | struct io_provide_buf *p = &req->pbuf; |
| 3721 | struct io_ring_ctx *ctx = req->ctx; |
| 3722 | struct io_buffer *head; |
| 3723 | int ret = 0; |
| 3724 | |
| 3725 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3726 | |
| 3727 | lockdep_assert_held(&ctx->uring_lock); |
| 3728 | |
| 3729 | ret = -ENOENT; |
| 3730 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3731 | if (head) |
| 3732 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 3733 | |
| 3734 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3735 | if (ret < 0) |
| 3736 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3737 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3738 | return 0; |
| 3739 | } |
| 3740 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3741 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3742 | const struct io_uring_sqe *sqe) |
| 3743 | { |
| 3744 | struct io_provide_buf *p = &req->pbuf; |
| 3745 | u64 tmp; |
| 3746 | |
| 3747 | if (sqe->ioprio || sqe->rw_flags) |
| 3748 | return -EINVAL; |
| 3749 | |
| 3750 | tmp = READ_ONCE(sqe->fd); |
| 3751 | if (!tmp || tmp > USHRT_MAX) |
| 3752 | return -E2BIG; |
| 3753 | p->nbufs = tmp; |
| 3754 | p->addr = READ_ONCE(sqe->addr); |
| 3755 | p->len = READ_ONCE(sqe->len); |
| 3756 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3757 | 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] | 3758 | return -EFAULT; |
| 3759 | |
| 3760 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3761 | tmp = READ_ONCE(sqe->off); |
| 3762 | if (tmp > USHRT_MAX) |
| 3763 | return -E2BIG; |
| 3764 | p->bid = tmp; |
| 3765 | return 0; |
| 3766 | } |
| 3767 | |
| 3768 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3769 | { |
| 3770 | struct io_buffer *buf; |
| 3771 | u64 addr = pbuf->addr; |
| 3772 | int i, bid = pbuf->bid; |
| 3773 | |
| 3774 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3775 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3776 | if (!buf) |
| 3777 | break; |
| 3778 | |
| 3779 | buf->addr = addr; |
| 3780 | buf->len = pbuf->len; |
| 3781 | buf->bid = bid; |
| 3782 | addr += pbuf->len; |
| 3783 | bid++; |
| 3784 | if (!*head) { |
| 3785 | INIT_LIST_HEAD(&buf->list); |
| 3786 | *head = buf; |
| 3787 | } else { |
| 3788 | list_add_tail(&buf->list, &(*head)->list); |
| 3789 | } |
| 3790 | } |
| 3791 | |
| 3792 | return i ? i : -ENOMEM; |
| 3793 | } |
| 3794 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3795 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3796 | struct io_comp_state *cs) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3797 | { |
| 3798 | struct io_provide_buf *p = &req->pbuf; |
| 3799 | struct io_ring_ctx *ctx = req->ctx; |
| 3800 | struct io_buffer *head, *list; |
| 3801 | int ret = 0; |
| 3802 | |
| 3803 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3804 | |
| 3805 | lockdep_assert_held(&ctx->uring_lock); |
| 3806 | |
| 3807 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3808 | |
| 3809 | ret = io_add_buffers(p, &head); |
| 3810 | if (ret < 0) |
| 3811 | goto out; |
| 3812 | |
| 3813 | if (!list) { |
| 3814 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3815 | GFP_KERNEL); |
| 3816 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3817 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3818 | goto out; |
| 3819 | } |
| 3820 | } |
| 3821 | out: |
| 3822 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3823 | if (ret < 0) |
| 3824 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3825 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3826 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3829 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3830 | const struct io_uring_sqe *sqe) |
| 3831 | { |
| 3832 | #if defined(CONFIG_EPOLL) |
| 3833 | if (sqe->ioprio || sqe->buf_index) |
| 3834 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 3835 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3836 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3837 | |
| 3838 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3839 | req->epoll.op = READ_ONCE(sqe->len); |
| 3840 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3841 | |
| 3842 | if (ep_op_has_event(req->epoll.op)) { |
| 3843 | struct epoll_event __user *ev; |
| 3844 | |
| 3845 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3846 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3847 | return -EFAULT; |
| 3848 | } |
| 3849 | |
| 3850 | return 0; |
| 3851 | #else |
| 3852 | return -EOPNOTSUPP; |
| 3853 | #endif |
| 3854 | } |
| 3855 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3856 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
| 3857 | struct io_comp_state *cs) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3858 | { |
| 3859 | #if defined(CONFIG_EPOLL) |
| 3860 | struct io_epoll *ie = &req->epoll; |
| 3861 | int ret; |
| 3862 | |
| 3863 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3864 | if (force_nonblock && ret == -EAGAIN) |
| 3865 | return -EAGAIN; |
| 3866 | |
| 3867 | if (ret < 0) |
| 3868 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3869 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3870 | return 0; |
| 3871 | #else |
| 3872 | return -EOPNOTSUPP; |
| 3873 | #endif |
| 3874 | } |
| 3875 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3876 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3877 | { |
| 3878 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3879 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3880 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3881 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3882 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3883 | |
| 3884 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3885 | req->madvise.len = READ_ONCE(sqe->len); |
| 3886 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3887 | return 0; |
| 3888 | #else |
| 3889 | return -EOPNOTSUPP; |
| 3890 | #endif |
| 3891 | } |
| 3892 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3893 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3894 | { |
| 3895 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3896 | struct io_madvise *ma = &req->madvise; |
| 3897 | int ret; |
| 3898 | |
| 3899 | if (force_nonblock) |
| 3900 | return -EAGAIN; |
| 3901 | |
| 3902 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3903 | if (ret < 0) |
| 3904 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3905 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3906 | return 0; |
| 3907 | #else |
| 3908 | return -EOPNOTSUPP; |
| 3909 | #endif |
| 3910 | } |
| 3911 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3912 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3913 | { |
| 3914 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3915 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3916 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3917 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3918 | |
| 3919 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3920 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3921 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3922 | return 0; |
| 3923 | } |
| 3924 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3925 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3926 | { |
| 3927 | struct io_fadvise *fa = &req->fadvise; |
| 3928 | int ret; |
| 3929 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3930 | if (force_nonblock) { |
| 3931 | switch (fa->advice) { |
| 3932 | case POSIX_FADV_NORMAL: |
| 3933 | case POSIX_FADV_RANDOM: |
| 3934 | case POSIX_FADV_SEQUENTIAL: |
| 3935 | break; |
| 3936 | default: |
| 3937 | return -EAGAIN; |
| 3938 | } |
| 3939 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3940 | |
| 3941 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3942 | if (ret < 0) |
| 3943 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3944 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3945 | return 0; |
| 3946 | } |
| 3947 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3948 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3949 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 3950 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3951 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3952 | if (sqe->ioprio || sqe->buf_index) |
| 3953 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3954 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3955 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3956 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3957 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 3958 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3959 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3960 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3961 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3962 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3963 | return 0; |
| 3964 | } |
| 3965 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3966 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3967 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3968 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3969 | int ret; |
| 3970 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3971 | if (force_nonblock) { |
| 3972 | /* only need file table for an actual valid fd */ |
| 3973 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 3974 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3975 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3976 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3977 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3978 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 3979 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3980 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3981 | if (ret < 0) |
| 3982 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3983 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3984 | return 0; |
| 3985 | } |
| 3986 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3987 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3988 | { |
| 3989 | /* |
| 3990 | * If we queue this for async, it must not be cancellable. That would |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3991 | * leave the 'file' in an undeterminate state, and here need to modify |
| 3992 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3993 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3994 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3995 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3996 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3997 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3998 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3999 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4000 | sqe->rw_flags || sqe->buf_index) |
| 4001 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4002 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4003 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4004 | |
| 4005 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 4006 | if ((req->file && req->file->f_op == &io_uring_fops)) |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 4007 | return -EBADF; |
| 4008 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4009 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4010 | return 0; |
| 4011 | } |
| 4012 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4013 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
| 4014 | struct io_comp_state *cs) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4015 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4016 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4017 | int ret; |
| 4018 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4019 | /* might be already done during nonblock submission */ |
| 4020 | if (!close->put_file) { |
| 4021 | ret = __close_fd_get_file(close->fd, &close->put_file); |
| 4022 | if (ret < 0) |
| 4023 | return (ret == -ENOENT) ? -EBADF : ret; |
| 4024 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4025 | |
| 4026 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4027 | if (close->put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4028 | /* was never set, but play safe */ |
| 4029 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4030 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4031 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4032 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4033 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4034 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4035 | /* No ->flush() or already async, safely close from here */ |
| 4036 | ret = filp_close(close->put_file, req->work.files); |
| 4037 | if (ret < 0) |
| 4038 | req_set_fail_links(req); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4039 | fput(close->put_file); |
| 4040 | close->put_file = NULL; |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4041 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4042 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4045 | 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] | 4046 | { |
| 4047 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4048 | |
| 4049 | if (!req->file) |
| 4050 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4051 | |
| 4052 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4053 | return -EINVAL; |
| 4054 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4055 | return -EINVAL; |
| 4056 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4057 | req->sync.off = READ_ONCE(sqe->off); |
| 4058 | req->sync.len = READ_ONCE(sqe->len); |
| 4059 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4060 | return 0; |
| 4061 | } |
| 4062 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4063 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4064 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4065 | int ret; |
| 4066 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4067 | /* sync_file_range always requires a blocking context */ |
| 4068 | if (force_nonblock) |
| 4069 | return -EAGAIN; |
| 4070 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4071 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4072 | req->sync.flags); |
| 4073 | if (ret < 0) |
| 4074 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4075 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4076 | return 0; |
| 4077 | } |
| 4078 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4079 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4080 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4081 | struct io_async_msghdr *kmsg) |
| 4082 | { |
| 4083 | if (req->io) |
| 4084 | return -EAGAIN; |
| 4085 | if (io_alloc_async_ctx(req)) { |
| 4086 | if (kmsg->iov != kmsg->fast_iov) |
| 4087 | kfree(kmsg->iov); |
| 4088 | return -ENOMEM; |
| 4089 | } |
| 4090 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4091 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 4092 | return -EAGAIN; |
| 4093 | } |
| 4094 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4095 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4096 | struct io_async_msghdr *iomsg) |
| 4097 | { |
| 4098 | iomsg->iov = iomsg->fast_iov; |
| 4099 | iomsg->msg.msg_name = &iomsg->addr; |
| 4100 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
| 4101 | req->sr_msg.msg_flags, &iomsg->iov); |
| 4102 | } |
| 4103 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4104 | 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] | 4105 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4106 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4107 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4108 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4109 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4110 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4111 | return -EINVAL; |
| 4112 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4113 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4114 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4115 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4116 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4117 | #ifdef CONFIG_COMPAT |
| 4118 | if (req->ctx->compat) |
| 4119 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4120 | #endif |
| 4121 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4122 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4123 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 4124 | /* iovec is already imported */ |
| 4125 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 4126 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4127 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4128 | ret = io_sendmsg_copy_hdr(req, &io->msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4129 | if (!ret) |
| 4130 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4131 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4132 | } |
| 4133 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4134 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4135 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4136 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4137 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4138 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4139 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4140 | int ret; |
| 4141 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4142 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4143 | if (unlikely(!sock)) |
| 4144 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4145 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4146 | if (req->io) { |
| 4147 | kmsg = &req->io->msg; |
| 4148 | kmsg->msg.msg_name = &req->io->msg.addr; |
| 4149 | /* if iov is set, it's allocated already */ |
| 4150 | if (!kmsg->iov) |
| 4151 | kmsg->iov = kmsg->fast_iov; |
| 4152 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4153 | } else { |
| 4154 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4155 | if (ret) |
| 4156 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4157 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4158 | } |
| 4159 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4160 | flags = req->sr_msg.msg_flags; |
| 4161 | if (flags & MSG_DONTWAIT) |
| 4162 | req->flags |= REQ_F_NOWAIT; |
| 4163 | else if (force_nonblock) |
| 4164 | flags |= MSG_DONTWAIT; |
| 4165 | |
| 4166 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
| 4167 | if (force_nonblock && ret == -EAGAIN) |
| 4168 | return io_setup_async_msg(req, kmsg); |
| 4169 | if (ret == -ERESTARTSYS) |
| 4170 | ret = -EINTR; |
| 4171 | |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4172 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4173 | kfree(kmsg->iov); |
| 4174 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4175 | if (ret < 0) |
| 4176 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4177 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4178 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4181 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4182 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4183 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4184 | struct io_sr_msg *sr = &req->sr_msg; |
| 4185 | struct msghdr msg; |
| 4186 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4187 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4188 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4189 | int ret; |
| 4190 | |
| 4191 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4192 | if (unlikely(!sock)) |
| 4193 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4194 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4195 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4196 | if (unlikely(ret)) |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4197 | return ret;; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4198 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4199 | msg.msg_name = NULL; |
| 4200 | msg.msg_control = NULL; |
| 4201 | msg.msg_controllen = 0; |
| 4202 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4203 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4204 | flags = req->sr_msg.msg_flags; |
| 4205 | if (flags & MSG_DONTWAIT) |
| 4206 | req->flags |= REQ_F_NOWAIT; |
| 4207 | else if (force_nonblock) |
| 4208 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4209 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4210 | msg.msg_flags = flags; |
| 4211 | ret = sock_sendmsg(sock, &msg); |
| 4212 | if (force_nonblock && ret == -EAGAIN) |
| 4213 | return -EAGAIN; |
| 4214 | if (ret == -ERESTARTSYS) |
| 4215 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4216 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4217 | if (ret < 0) |
| 4218 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4219 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4220 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4221 | } |
| 4222 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4223 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4224 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4225 | { |
| 4226 | struct io_sr_msg *sr = &req->sr_msg; |
| 4227 | struct iovec __user *uiov; |
| 4228 | size_t iov_len; |
| 4229 | int ret; |
| 4230 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4231 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4232 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4233 | if (ret) |
| 4234 | return ret; |
| 4235 | |
| 4236 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4237 | if (iov_len > 1) |
| 4238 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4239 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4240 | return -EFAULT; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4241 | sr->len = iomsg->iov[0].iov_len; |
| 4242 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4243 | sr->len); |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4244 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4245 | } else { |
| 4246 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4247 | &iomsg->iov, &iomsg->msg.msg_iter); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4248 | if (ret > 0) |
| 4249 | ret = 0; |
| 4250 | } |
| 4251 | |
| 4252 | return ret; |
| 4253 | } |
| 4254 | |
| 4255 | #ifdef CONFIG_COMPAT |
| 4256 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4257 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4258 | { |
| 4259 | struct compat_msghdr __user *msg_compat; |
| 4260 | struct io_sr_msg *sr = &req->sr_msg; |
| 4261 | struct compat_iovec __user *uiov; |
| 4262 | compat_uptr_t ptr; |
| 4263 | compat_size_t len; |
| 4264 | int ret; |
| 4265 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4266 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4267 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4268 | &ptr, &len); |
| 4269 | if (ret) |
| 4270 | return ret; |
| 4271 | |
| 4272 | uiov = compat_ptr(ptr); |
| 4273 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4274 | compat_ssize_t clen; |
| 4275 | |
| 4276 | if (len > 1) |
| 4277 | return -EINVAL; |
| 4278 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4279 | return -EFAULT; |
| 4280 | if (__get_user(clen, &uiov->iov_len)) |
| 4281 | return -EFAULT; |
| 4282 | if (clen < 0) |
| 4283 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4284 | sr->len = iomsg->iov[0].iov_len; |
| 4285 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4286 | } else { |
| 4287 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4288 | &iomsg->iov, |
| 4289 | &iomsg->msg.msg_iter); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4290 | if (ret < 0) |
| 4291 | return ret; |
| 4292 | } |
| 4293 | |
| 4294 | return 0; |
| 4295 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4296 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4297 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4298 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4299 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4300 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4301 | iomsg->msg.msg_name = &iomsg->addr; |
| 4302 | iomsg->iov = iomsg->fast_iov; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4303 | |
| 4304 | #ifdef CONFIG_COMPAT |
| 4305 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4306 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4307 | #endif |
| 4308 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4309 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4312 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4313 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4314 | { |
| 4315 | struct io_sr_msg *sr = &req->sr_msg; |
| 4316 | struct io_buffer *kbuf; |
| 4317 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4318 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4319 | if (IS_ERR(kbuf)) |
| 4320 | return kbuf; |
| 4321 | |
| 4322 | sr->kbuf = kbuf; |
| 4323 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4324 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4325 | } |
| 4326 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4327 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4328 | { |
| 4329 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4330 | } |
| 4331 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4332 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4333 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4334 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4335 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4336 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4337 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4338 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4339 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4340 | return -EINVAL; |
| 4341 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4342 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4343 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4344 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4345 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4346 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4347 | #ifdef CONFIG_COMPAT |
| 4348 | if (req->ctx->compat) |
| 4349 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4350 | #endif |
| 4351 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4352 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4353 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 4354 | /* iovec is already imported */ |
| 4355 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 4356 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4357 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4358 | ret = io_recvmsg_copy_hdr(req, &io->msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4359 | if (!ret) |
| 4360 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4361 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4364 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4365 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4366 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4367 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4368 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4369 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4370 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4371 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4372 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4373 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4374 | if (unlikely(!sock)) |
| 4375 | return ret; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4376 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4377 | if (req->io) { |
| 4378 | kmsg = &req->io->msg; |
| 4379 | kmsg->msg.msg_name = &req->io->msg.addr; |
| 4380 | /* if iov is set, it's allocated already */ |
| 4381 | if (!kmsg->iov) |
| 4382 | kmsg->iov = kmsg->fast_iov; |
| 4383 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4384 | } else { |
| 4385 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4386 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4387 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4388 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4389 | } |
| 4390 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4391 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4392 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4393 | if (IS_ERR(kbuf)) |
| 4394 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4395 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4396 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4397 | 1, req->sr_msg.len); |
| 4398 | } |
| 4399 | |
| 4400 | flags = req->sr_msg.msg_flags; |
| 4401 | if (flags & MSG_DONTWAIT) |
| 4402 | req->flags |= REQ_F_NOWAIT; |
| 4403 | else if (force_nonblock) |
| 4404 | flags |= MSG_DONTWAIT; |
| 4405 | |
| 4406 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4407 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4408 | if (force_nonblock && ret == -EAGAIN) |
| 4409 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4410 | if (ret == -ERESTARTSYS) |
| 4411 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4412 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4413 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4414 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4415 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4416 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4417 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4418 | if (ret < 0) |
| 4419 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4420 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4421 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4422 | } |
| 4423 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4424 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4425 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4426 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4427 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4428 | struct io_sr_msg *sr = &req->sr_msg; |
| 4429 | struct msghdr msg; |
| 4430 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4431 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4432 | struct iovec iov; |
| 4433 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4434 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4435 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4436 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4437 | if (unlikely(!sock)) |
| 4438 | return ret; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4439 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4440 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4441 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4442 | if (IS_ERR(kbuf)) |
| 4443 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4444 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4447 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4448 | if (unlikely(ret)) |
| 4449 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4450 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4451 | msg.msg_name = NULL; |
| 4452 | msg.msg_control = NULL; |
| 4453 | msg.msg_controllen = 0; |
| 4454 | msg.msg_namelen = 0; |
| 4455 | msg.msg_iocb = NULL; |
| 4456 | msg.msg_flags = 0; |
| 4457 | |
| 4458 | flags = req->sr_msg.msg_flags; |
| 4459 | if (flags & MSG_DONTWAIT) |
| 4460 | req->flags |= REQ_F_NOWAIT; |
| 4461 | else if (force_nonblock) |
| 4462 | flags |= MSG_DONTWAIT; |
| 4463 | |
| 4464 | ret = sock_recvmsg(sock, &msg, flags); |
| 4465 | if (force_nonblock && ret == -EAGAIN) |
| 4466 | return -EAGAIN; |
| 4467 | if (ret == -ERESTARTSYS) |
| 4468 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4469 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4470 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4471 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4472 | if (ret < 0) |
| 4473 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4474 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4475 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4478 | 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] | 4479 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4480 | struct io_accept *accept = &req->accept; |
| 4481 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4482 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4483 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4484 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4485 | return -EINVAL; |
| 4486 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4487 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4488 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4489 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4490 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4491 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4492 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4493 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4494 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4495 | struct io_comp_state *cs) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4496 | { |
| 4497 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4498 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4499 | int ret; |
| 4500 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4501 | if (req->file->f_flags & O_NONBLOCK) |
| 4502 | req->flags |= REQ_F_NOWAIT; |
| 4503 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4504 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4505 | accept->addr_len, accept->flags, |
| 4506 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4507 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4508 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4509 | if (ret < 0) { |
| 4510 | if (ret == -ERESTARTSYS) |
| 4511 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4512 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4513 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4514 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4515 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4516 | } |
| 4517 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4518 | 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] | 4519 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4520 | struct io_connect *conn = &req->connect; |
| 4521 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4522 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4523 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4524 | return -EINVAL; |
| 4525 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4526 | return -EINVAL; |
| 4527 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4528 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4529 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 4530 | |
| 4531 | if (!io) |
| 4532 | return 0; |
| 4533 | |
| 4534 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4535 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4538 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4539 | struct io_comp_state *cs) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4540 | { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4541 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4542 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4543 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4544 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4545 | if (req->io) { |
| 4546 | io = req->io; |
| 4547 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4548 | ret = move_addr_to_kernel(req->connect.addr, |
| 4549 | req->connect.addr_len, |
| 4550 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4551 | if (ret) |
| 4552 | goto out; |
| 4553 | io = &__io; |
| 4554 | } |
| 4555 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4556 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4557 | |
| 4558 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 4559 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4560 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4561 | if (req->io) |
| 4562 | return -EAGAIN; |
| 4563 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4564 | ret = -ENOMEM; |
| 4565 | goto out; |
| 4566 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4567 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4568 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4569 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4570 | if (ret == -ERESTARTSYS) |
| 4571 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4572 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4573 | if (ret < 0) |
| 4574 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4575 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4576 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4577 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4578 | #else /* !CONFIG_NET */ |
| 4579 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4580 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4581 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4582 | } |
| 4583 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4584 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4585 | struct io_comp_state *cs) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4586 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4587 | return -EOPNOTSUPP; |
| 4588 | } |
| 4589 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4590 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4591 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4592 | { |
| 4593 | return -EOPNOTSUPP; |
| 4594 | } |
| 4595 | |
| 4596 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4597 | const struct io_uring_sqe *sqe) |
| 4598 | { |
| 4599 | return -EOPNOTSUPP; |
| 4600 | } |
| 4601 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4602 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4603 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4604 | { |
| 4605 | return -EOPNOTSUPP; |
| 4606 | } |
| 4607 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4608 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4609 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4610 | { |
| 4611 | return -EOPNOTSUPP; |
| 4612 | } |
| 4613 | |
| 4614 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4615 | { |
| 4616 | return -EOPNOTSUPP; |
| 4617 | } |
| 4618 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4619 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4620 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4621 | { |
| 4622 | return -EOPNOTSUPP; |
| 4623 | } |
| 4624 | |
| 4625 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4626 | { |
| 4627 | return -EOPNOTSUPP; |
| 4628 | } |
| 4629 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4630 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4631 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4632 | { |
| 4633 | return -EOPNOTSUPP; |
| 4634 | } |
| 4635 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4636 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4637 | struct io_poll_table { |
| 4638 | struct poll_table_struct pt; |
| 4639 | struct io_kiocb *req; |
| 4640 | int error; |
| 4641 | }; |
| 4642 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4643 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4644 | __poll_t mask, task_work_func_t func) |
| 4645 | { |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 4646 | bool twa_signal_ok; |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4647 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4648 | |
| 4649 | /* for instances that support it check for an event match first: */ |
| 4650 | if (mask && !(mask & poll->events)) |
| 4651 | return 0; |
| 4652 | |
| 4653 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4654 | |
| 4655 | list_del_init(&poll->wait.entry); |
| 4656 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4657 | req->result = mask; |
| 4658 | init_task_work(&req->task_work, func); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4659 | percpu_ref_get(&req->ctx->refs); |
| 4660 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4661 | /* |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 4662 | * If we using the signalfd wait_queue_head for this wakeup, then |
| 4663 | * it's not safe to use TWA_SIGNAL as we could be recursing on the |
| 4664 | * tsk->sighand->siglock on doing the wakeup. Should not be needed |
| 4665 | * either, as the normal wakeup will suffice. |
| 4666 | */ |
| 4667 | twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh); |
| 4668 | |
| 4669 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4670 | * If this fails, then the task is exiting. When a task exits, the |
| 4671 | * work gets canceled, so just cancel this request as well instead |
| 4672 | * of executing it. We can't safely execute it anyway, as we may not |
| 4673 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4674 | */ |
Jens Axboe | fd7d6de | 2020-08-23 11:00:37 -0600 | [diff] [blame] | 4675 | ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4676 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 4677 | struct task_struct *tsk; |
| 4678 | |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4679 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4680 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 4681 | task_work_add(tsk, &req->task_work, 0); |
| 4682 | wake_up_process(tsk); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4683 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4684 | return 1; |
| 4685 | } |
| 4686 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4687 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4688 | __acquires(&req->ctx->completion_lock) |
| 4689 | { |
| 4690 | struct io_ring_ctx *ctx = req->ctx; |
| 4691 | |
| 4692 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4693 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4694 | |
| 4695 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4696 | } |
| 4697 | |
| 4698 | spin_lock_irq(&ctx->completion_lock); |
| 4699 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4700 | add_wait_queue(poll->head, &poll->wait); |
| 4701 | return true; |
| 4702 | } |
| 4703 | |
| 4704 | return false; |
| 4705 | } |
| 4706 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4707 | 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] | 4708 | { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4709 | /* pure poll stashes this in ->io, poll driven retry elsewhere */ |
| 4710 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4711 | return (struct io_poll_iocb *) req->io; |
| 4712 | return req->apoll->double_poll; |
| 4713 | } |
| 4714 | |
| 4715 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4716 | { |
| 4717 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4718 | return &req->poll; |
| 4719 | return &req->apoll->poll; |
| 4720 | } |
| 4721 | |
| 4722 | static void io_poll_remove_double(struct io_kiocb *req) |
| 4723 | { |
| 4724 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4725 | |
| 4726 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4727 | |
| 4728 | if (poll && poll->head) { |
| 4729 | struct wait_queue_head *head = poll->head; |
| 4730 | |
| 4731 | spin_lock(&head->lock); |
| 4732 | list_del_init(&poll->wait.entry); |
| 4733 | if (poll->wait.private) |
| 4734 | refcount_dec(&req->refs); |
| 4735 | poll->head = NULL; |
| 4736 | spin_unlock(&head->lock); |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4741 | { |
| 4742 | struct io_ring_ctx *ctx = req->ctx; |
| 4743 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4744 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4745 | req->poll.done = true; |
| 4746 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4747 | io_commit_cqring(ctx); |
| 4748 | } |
| 4749 | |
| 4750 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4751 | { |
| 4752 | struct io_ring_ctx *ctx = req->ctx; |
| 4753 | |
| 4754 | if (io_poll_rewait(req, &req->poll)) { |
| 4755 | spin_unlock_irq(&ctx->completion_lock); |
| 4756 | return; |
| 4757 | } |
| 4758 | |
| 4759 | hash_del(&req->hash_node); |
| 4760 | io_poll_complete(req, req->result, 0); |
| 4761 | req->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 4762 | *nxt = io_put_req_find_next(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4763 | spin_unlock_irq(&ctx->completion_lock); |
| 4764 | |
| 4765 | io_cqring_ev_posted(ctx); |
| 4766 | } |
| 4767 | |
| 4768 | static void io_poll_task_func(struct callback_head *cb) |
| 4769 | { |
| 4770 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4771 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4772 | struct io_kiocb *nxt = NULL; |
| 4773 | |
| 4774 | io_poll_task_handler(req, &nxt); |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 4775 | if (nxt) |
| 4776 | __io_req_task_submit(nxt); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4777 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4781 | int sync, void *key) |
| 4782 | { |
| 4783 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4784 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4785 | __poll_t mask = key_to_poll(key); |
| 4786 | |
| 4787 | /* for instances that support it check for an event match first: */ |
| 4788 | if (mask && !(mask & poll->events)) |
| 4789 | return 0; |
| 4790 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4791 | list_del_init(&wait->entry); |
| 4792 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4793 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4794 | bool done; |
| 4795 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4796 | spin_lock(&poll->head->lock); |
| 4797 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4798 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4799 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4800 | /* make sure double remove sees this as being gone */ |
| 4801 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4802 | spin_unlock(&poll->head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4803 | if (!done) |
| 4804 | __io_async_wake(req, poll, mask, io_poll_task_func); |
| 4805 | } |
| 4806 | refcount_dec(&req->refs); |
| 4807 | return 1; |
| 4808 | } |
| 4809 | |
| 4810 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4811 | wait_queue_func_t wake_func) |
| 4812 | { |
| 4813 | poll->head = NULL; |
| 4814 | poll->done = false; |
| 4815 | poll->canceled = false; |
| 4816 | poll->events = events; |
| 4817 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4818 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4819 | } |
| 4820 | |
| 4821 | 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] | 4822 | struct wait_queue_head *head, |
| 4823 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4824 | { |
| 4825 | struct io_kiocb *req = pt->req; |
| 4826 | |
| 4827 | /* |
| 4828 | * If poll->head is already set, it's because the file being polled |
| 4829 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4830 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4831 | */ |
| 4832 | if (unlikely(poll->head)) { |
| 4833 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4834 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4835 | pt->error = -EINVAL; |
| 4836 | return; |
| 4837 | } |
| 4838 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4839 | if (!poll) { |
| 4840 | pt->error = -ENOMEM; |
| 4841 | return; |
| 4842 | } |
| 4843 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); |
| 4844 | refcount_inc(&req->refs); |
| 4845 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4846 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | pt->error = 0; |
| 4850 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4851 | |
| 4852 | if (poll->events & EPOLLEXCLUSIVE) |
| 4853 | add_wait_queue_exclusive(head, &poll->wait); |
| 4854 | else |
| 4855 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4859 | struct poll_table_struct *p) |
| 4860 | { |
| 4861 | 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] | 4862 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4863 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4864 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4865 | } |
| 4866 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4867 | static void io_async_task_func(struct callback_head *cb) |
| 4868 | { |
| 4869 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4870 | struct async_poll *apoll = req->apoll; |
| 4871 | struct io_ring_ctx *ctx = req->ctx; |
| 4872 | |
| 4873 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4874 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4875 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4876 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4877 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4878 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4879 | } |
| 4880 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4881 | /* 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] | 4882 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4883 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4884 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4885 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4886 | spin_unlock_irq(&ctx->completion_lock); |
| 4887 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 4888 | if (!READ_ONCE(apoll->poll.canceled)) |
| 4889 | __io_req_task_submit(req); |
| 4890 | else |
| 4891 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 4892 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4893 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4894 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4895 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4899 | void *key) |
| 4900 | { |
| 4901 | struct io_kiocb *req = wait->private; |
| 4902 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 4903 | |
| 4904 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 4905 | key_to_poll(key)); |
| 4906 | |
| 4907 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 4908 | } |
| 4909 | |
| 4910 | static void io_poll_req_insert(struct io_kiocb *req) |
| 4911 | { |
| 4912 | struct io_ring_ctx *ctx = req->ctx; |
| 4913 | struct hlist_head *list; |
| 4914 | |
| 4915 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 4916 | hlist_add_head(&req->hash_node, list); |
| 4917 | } |
| 4918 | |
| 4919 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 4920 | struct io_poll_iocb *poll, |
| 4921 | struct io_poll_table *ipt, __poll_t mask, |
| 4922 | wait_queue_func_t wake_func) |
| 4923 | __acquires(&ctx->completion_lock) |
| 4924 | { |
| 4925 | struct io_ring_ctx *ctx = req->ctx; |
| 4926 | bool cancel = false; |
| 4927 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4928 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 4929 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4930 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4931 | |
| 4932 | ipt->pt._key = mask; |
| 4933 | ipt->req = req; |
| 4934 | ipt->error = -EINVAL; |
| 4935 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4936 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 4937 | |
| 4938 | spin_lock_irq(&ctx->completion_lock); |
| 4939 | if (likely(poll->head)) { |
| 4940 | spin_lock(&poll->head->lock); |
| 4941 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 4942 | if (ipt->error) |
| 4943 | cancel = true; |
| 4944 | ipt->error = 0; |
| 4945 | mask = 0; |
| 4946 | } |
| 4947 | if (mask || ipt->error) |
| 4948 | list_del_init(&poll->wait.entry); |
| 4949 | else if (cancel) |
| 4950 | WRITE_ONCE(poll->canceled, true); |
| 4951 | else if (!poll->done) /* actually waiting for an event */ |
| 4952 | io_poll_req_insert(req); |
| 4953 | spin_unlock(&poll->head->lock); |
| 4954 | } |
| 4955 | |
| 4956 | return mask; |
| 4957 | } |
| 4958 | |
| 4959 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 4960 | { |
| 4961 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 4962 | struct io_ring_ctx *ctx = req->ctx; |
| 4963 | struct async_poll *apoll; |
| 4964 | struct io_poll_table ipt; |
| 4965 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 4966 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4967 | |
| 4968 | if (!req->file || !file_can_poll(req->file)) |
| 4969 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4970 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4971 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 4972 | if (def->pollin) |
| 4973 | rw = READ; |
| 4974 | else if (def->pollout) |
| 4975 | rw = WRITE; |
| 4976 | else |
| 4977 | return false; |
| 4978 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 4979 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4980 | return false; |
| 4981 | |
| 4982 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 4983 | if (unlikely(!apoll)) |
| 4984 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4985 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4986 | |
| 4987 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4988 | req->apoll = apoll; |
| 4989 | INIT_HLIST_NODE(&req->hash_node); |
| 4990 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4991 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4992 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4993 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4994 | if (def->pollout) |
| 4995 | mask |= POLLOUT | POLLWRNORM; |
| 4996 | mask |= POLLERR | POLLPRI; |
| 4997 | |
| 4998 | ipt.pt._qproc = io_async_queue_proc; |
| 4999 | |
| 5000 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5001 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5002 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5003 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5004 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5005 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5006 | kfree(apoll); |
| 5007 | return false; |
| 5008 | } |
| 5009 | spin_unlock_irq(&ctx->completion_lock); |
| 5010 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5011 | apoll->poll.events); |
| 5012 | return true; |
| 5013 | } |
| 5014 | |
| 5015 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5016 | struct io_poll_iocb *poll) |
| 5017 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5018 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5019 | |
| 5020 | spin_lock(&poll->head->lock); |
| 5021 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5022 | if (!list_empty(&poll->wait.entry)) { |
| 5023 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5024 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5025 | } |
| 5026 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5027 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5028 | return do_complete; |
| 5029 | } |
| 5030 | |
| 5031 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5032 | { |
| 5033 | bool do_complete; |
| 5034 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5035 | io_poll_remove_double(req); |
| 5036 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5037 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5038 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5039 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5040 | struct async_poll *apoll = req->apoll; |
| 5041 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5042 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5043 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5044 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5045 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5046 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5047 | kfree(apoll); |
| 5048 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5049 | } |
| 5050 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5051 | if (do_complete) { |
| 5052 | io_cqring_fill_event(req, -ECANCELED); |
| 5053 | io_commit_cqring(req->ctx); |
| 5054 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5055 | req_set_fail_links(req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5056 | io_put_req(req); |
| 5057 | } |
| 5058 | |
| 5059 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5062 | /* |
| 5063 | * Returns true if we found and killed one or more poll requests |
| 5064 | */ |
| 5065 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5066 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5067 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5068 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5069 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5070 | |
| 5071 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5072 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5073 | struct hlist_head *list; |
| 5074 | |
| 5075 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5076 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
| 5077 | if (io_task_match(req, tsk)) |
| 5078 | posted += io_poll_remove_one(req); |
| 5079 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5080 | } |
| 5081 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5082 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5083 | if (posted) |
| 5084 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5085 | |
| 5086 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5087 | } |
| 5088 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5089 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5090 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5091 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5092 | struct io_kiocb *req; |
| 5093 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5094 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5095 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5096 | if (sqe_addr != req->user_data) |
| 5097 | continue; |
| 5098 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5099 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5100 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | return -ENOENT; |
| 5104 | } |
| 5105 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5106 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5107 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5108 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5109 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5110 | return -EINVAL; |
| 5111 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5112 | sqe->poll_events) |
| 5113 | return -EINVAL; |
| 5114 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5115 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * Find a running poll command that matches one specified in sqe->addr, |
| 5121 | * and remove it if found. |
| 5122 | */ |
| 5123 | static int io_poll_remove(struct io_kiocb *req) |
| 5124 | { |
| 5125 | struct io_ring_ctx *ctx = req->ctx; |
| 5126 | u64 addr; |
| 5127 | int ret; |
| 5128 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5129 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5130 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5131 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5132 | spin_unlock_irq(&ctx->completion_lock); |
| 5133 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5134 | if (ret < 0) |
| 5135 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5136 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5137 | return 0; |
| 5138 | } |
| 5139 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5140 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5141 | void *key) |
| 5142 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5143 | struct io_kiocb *req = wait->private; |
| 5144 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5145 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5146 | 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] | 5147 | } |
| 5148 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5149 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5150 | struct poll_table_struct *p) |
| 5151 | { |
| 5152 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5153 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5154 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5155 | } |
| 5156 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5157 | 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] | 5158 | { |
| 5159 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5160 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5161 | |
| 5162 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5163 | return -EINVAL; |
| 5164 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5165 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5166 | if (!poll->file) |
| 5167 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5168 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5169 | events = READ_ONCE(sqe->poll32_events); |
| 5170 | #ifdef __BIG_ENDIAN |
| 5171 | events = swahw32(events); |
| 5172 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5173 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5174 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5175 | return 0; |
| 5176 | } |
| 5177 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5178 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5179 | { |
| 5180 | struct io_poll_iocb *poll = &req->poll; |
| 5181 | struct io_ring_ctx *ctx = req->ctx; |
| 5182 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5183 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5184 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5185 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5186 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5187 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5188 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5189 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5190 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5191 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5192 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5193 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5194 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5195 | spin_unlock_irq(&ctx->completion_lock); |
| 5196 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5197 | if (mask) { |
| 5198 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5199 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5200 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5201 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5202 | } |
| 5203 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5204 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5205 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5206 | struct io_timeout_data *data = container_of(timer, |
| 5207 | struct io_timeout_data, timer); |
| 5208 | struct io_kiocb *req = data->req; |
| 5209 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5210 | unsigned long flags; |
| 5211 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5212 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5213 | atomic_set(&req->ctx->cq_timeouts, |
| 5214 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5215 | |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 5216 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5217 | * We could be racing with timeout deletion. If the list is empty, |
| 5218 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 5219 | */ |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5220 | if (!list_empty(&req->timeout.list)) |
| 5221 | list_del_init(&req->timeout.list); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5222 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5223 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5224 | io_commit_cqring(ctx); |
| 5225 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5226 | |
| 5227 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5228 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5229 | io_put_req(req); |
| 5230 | return HRTIMER_NORESTART; |
| 5231 | } |
| 5232 | |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5233 | static int __io_timeout_cancel(struct io_kiocb *req) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5234 | { |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5235 | int ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5236 | |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5237 | list_del_init(&req->timeout.list); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5238 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5239 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5240 | if (ret == -1) |
| 5241 | return -EALREADY; |
| 5242 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5243 | req_set_fail_links(req); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame] | 5244 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5245 | io_cqring_fill_event(req, -ECANCELED); |
| 5246 | io_put_req(req); |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5250 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5251 | { |
| 5252 | struct io_kiocb *req; |
| 5253 | int ret = -ENOENT; |
| 5254 | |
| 5255 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5256 | if (user_data == req->user_data) { |
| 5257 | ret = 0; |
| 5258 | break; |
| 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | if (ret == -ENOENT) |
| 5263 | return ret; |
| 5264 | |
| 5265 | return __io_timeout_cancel(req); |
| 5266 | } |
| 5267 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5268 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5269 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5270 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5271 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5272 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5273 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5274 | return -EINVAL; |
| 5275 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5276 | return -EINVAL; |
| 5277 | |
| 5278 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 5279 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 5280 | if (req->timeout.flags) |
| 5281 | return -EINVAL; |
| 5282 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5283 | return 0; |
| 5284 | } |
| 5285 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5286 | /* |
| 5287 | * Remove or update an existing timeout command |
| 5288 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5289 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5290 | { |
| 5291 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5292 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5293 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5294 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5295 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5296 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5297 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5298 | io_commit_cqring(ctx); |
| 5299 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5300 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5301 | if (ret < 0) |
| 5302 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5303 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5304 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5305 | } |
| 5306 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5307 | 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] | 5308 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5309 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5310 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5311 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5312 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5313 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5314 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5315 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5316 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5317 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5318 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5319 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5320 | flags = READ_ONCE(sqe->timeout_flags); |
| 5321 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5322 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5323 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5324 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5325 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5326 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5327 | return -ENOMEM; |
| 5328 | |
| 5329 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5330 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5331 | |
| 5332 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5333 | return -EFAULT; |
| 5334 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5335 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5336 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5337 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5338 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5339 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5340 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5341 | return 0; |
| 5342 | } |
| 5343 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5344 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5345 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5346 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5347 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5348 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5349 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5350 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5351 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5352 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5353 | /* |
| 5354 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5355 | * timeout event to be satisfied. If it isn't set, then this is |
| 5356 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5357 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5358 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5359 | entry = ctx->timeout_list.prev; |
| 5360 | goto add; |
| 5361 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5362 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5363 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5364 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5365 | |
| 5366 | /* |
| 5367 | * Insertion sort, ensuring the first entry in the list is always |
| 5368 | * the one we need first. |
| 5369 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5370 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5371 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5372 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5373 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5374 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5375 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5376 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5377 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5378 | break; |
| 5379 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5380 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5381 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5382 | data->timer.function = io_timeout_fn; |
| 5383 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5384 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5385 | return 0; |
| 5386 | } |
| 5387 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5388 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5389 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5390 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5391 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5392 | return req->user_data == (unsigned long) data; |
| 5393 | } |
| 5394 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5395 | 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] | 5396 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5397 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5398 | int ret = 0; |
| 5399 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5400 | 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] | 5401 | switch (cancel_ret) { |
| 5402 | case IO_WQ_CANCEL_OK: |
| 5403 | ret = 0; |
| 5404 | break; |
| 5405 | case IO_WQ_CANCEL_RUNNING: |
| 5406 | ret = -EALREADY; |
| 5407 | break; |
| 5408 | case IO_WQ_CANCEL_NOTFOUND: |
| 5409 | ret = -ENOENT; |
| 5410 | break; |
| 5411 | } |
| 5412 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5413 | return ret; |
| 5414 | } |
| 5415 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5416 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5417 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5418 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5419 | { |
| 5420 | unsigned long flags; |
| 5421 | int ret; |
| 5422 | |
| 5423 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5424 | if (ret != -ENOENT) { |
| 5425 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5426 | goto done; |
| 5427 | } |
| 5428 | |
| 5429 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5430 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5431 | if (ret != -ENOENT) |
| 5432 | goto done; |
| 5433 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5434 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5435 | if (!ret) |
| 5436 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5437 | io_cqring_fill_event(req, ret); |
| 5438 | io_commit_cqring(ctx); |
| 5439 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5440 | io_cqring_ev_posted(ctx); |
| 5441 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5442 | if (ret < 0) |
| 5443 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5444 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5445 | } |
| 5446 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5447 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5448 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5449 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5450 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5451 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5452 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5453 | return -EINVAL; |
| 5454 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5455 | return -EINVAL; |
| 5456 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5457 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5461 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5462 | { |
| 5463 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5464 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5465 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5466 | return 0; |
| 5467 | } |
| 5468 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5469 | static int io_files_update_prep(struct io_kiocb *req, |
| 5470 | const struct io_uring_sqe *sqe) |
| 5471 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5472 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5473 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5474 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5475 | return -EINVAL; |
| 5476 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5477 | return -EINVAL; |
| 5478 | |
| 5479 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5480 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5481 | if (!req->files_update.nr_args) |
| 5482 | return -EINVAL; |
| 5483 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 5484 | return 0; |
| 5485 | } |
| 5486 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5487 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
| 5488 | struct io_comp_state *cs) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5489 | { |
| 5490 | struct io_ring_ctx *ctx = req->ctx; |
| 5491 | struct io_uring_files_update up; |
| 5492 | int ret; |
| 5493 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5494 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5495 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5496 | |
| 5497 | up.offset = req->files_update.offset; |
| 5498 | up.fds = req->files_update.arg; |
| 5499 | |
| 5500 | mutex_lock(&ctx->uring_lock); |
| 5501 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 5502 | mutex_unlock(&ctx->uring_lock); |
| 5503 | |
| 5504 | if (ret < 0) |
| 5505 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5506 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5507 | return 0; |
| 5508 | } |
| 5509 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5510 | static int io_req_defer_prep(struct io_kiocb *req, |
| 5511 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5512 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5513 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5514 | |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 5515 | if (!sqe) |
| 5516 | return 0; |
| 5517 | |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 5518 | if (io_alloc_async_ctx(req)) |
| 5519 | return -EAGAIN; |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 5520 | ret = io_prep_work_files(req); |
| 5521 | if (unlikely(ret)) |
| 5522 | return ret; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 5523 | |
Jens Axboe | 202700e1 | 2020-09-12 13:18:10 -0600 | [diff] [blame] | 5524 | io_prep_async_work(req); |
| 5525 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5526 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5527 | case IORING_OP_NOP: |
| 5528 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5529 | case IORING_OP_READV: |
| 5530 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5531 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5532 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5533 | break; |
| 5534 | case IORING_OP_WRITEV: |
| 5535 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5536 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5537 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5538 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5539 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5540 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5541 | break; |
| 5542 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5543 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5544 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5545 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5546 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5547 | break; |
| 5548 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5549 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5550 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5551 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5552 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5553 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5554 | break; |
| 5555 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5556 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5557 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5558 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5559 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5560 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5561 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5562 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5563 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5564 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5565 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5566 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5567 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5568 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5569 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5570 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5571 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5572 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5573 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5574 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5575 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5576 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5577 | case IORING_OP_FALLOCATE: |
| 5578 | ret = io_fallocate_prep(req, sqe); |
| 5579 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5580 | case IORING_OP_OPENAT: |
| 5581 | ret = io_openat_prep(req, sqe); |
| 5582 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5583 | case IORING_OP_CLOSE: |
| 5584 | ret = io_close_prep(req, sqe); |
| 5585 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5586 | case IORING_OP_FILES_UPDATE: |
| 5587 | ret = io_files_update_prep(req, sqe); |
| 5588 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5589 | case IORING_OP_STATX: |
| 5590 | ret = io_statx_prep(req, sqe); |
| 5591 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5592 | case IORING_OP_FADVISE: |
| 5593 | ret = io_fadvise_prep(req, sqe); |
| 5594 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5595 | case IORING_OP_MADVISE: |
| 5596 | ret = io_madvise_prep(req, sqe); |
| 5597 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5598 | case IORING_OP_OPENAT2: |
| 5599 | ret = io_openat2_prep(req, sqe); |
| 5600 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5601 | case IORING_OP_EPOLL_CTL: |
| 5602 | ret = io_epoll_ctl_prep(req, sqe); |
| 5603 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5604 | case IORING_OP_SPLICE: |
| 5605 | ret = io_splice_prep(req, sqe); |
| 5606 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5607 | case IORING_OP_PROVIDE_BUFFERS: |
| 5608 | ret = io_provide_buffers_prep(req, sqe); |
| 5609 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5610 | case IORING_OP_REMOVE_BUFFERS: |
| 5611 | ret = io_remove_buffers_prep(req, sqe); |
| 5612 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5613 | case IORING_OP_TEE: |
| 5614 | ret = io_tee_prep(req, sqe); |
| 5615 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5616 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5617 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5618 | req->opcode); |
| 5619 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5620 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5621 | } |
| 5622 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5623 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5626 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5627 | { |
| 5628 | struct io_kiocb *pos; |
| 5629 | struct io_ring_ctx *ctx = req->ctx; |
| 5630 | u32 total_submitted, nr_reqs = 1; |
| 5631 | |
| 5632 | if (req->flags & REQ_F_LINK_HEAD) |
| 5633 | list_for_each_entry(pos, &req->link_list, link_list) |
| 5634 | nr_reqs++; |
| 5635 | |
| 5636 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5637 | return total_submitted - nr_reqs; |
| 5638 | } |
| 5639 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5640 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5641 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5642 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5643 | struct io_defer_entry *de; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5644 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5645 | u32 seq; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5646 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5647 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5648 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5649 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5650 | return 0; |
| 5651 | |
| 5652 | seq = io_get_sequence(req); |
| 5653 | /* Still a chance to pass the sequence check */ |
| 5654 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5655 | return 0; |
| 5656 | |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5657 | if (!req->io) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5658 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 5659 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5660 | return ret; |
| 5661 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5662 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5663 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5664 | if (!de) |
| 5665 | return -ENOMEM; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5666 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5667 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5668 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5669 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5670 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5671 | io_queue_async_work(req); |
| 5672 | return -EIOCBQUEUED; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5673 | } |
| 5674 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 5675 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5676 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5677 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5678 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5679 | spin_unlock_irq(&ctx->completion_lock); |
| 5680 | return -EIOCBQUEUED; |
| 5681 | } |
| 5682 | |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 5683 | static void io_req_drop_files(struct io_kiocb *req) |
| 5684 | { |
| 5685 | struct io_ring_ctx *ctx = req->ctx; |
| 5686 | unsigned long flags; |
| 5687 | |
| 5688 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 5689 | list_del(&req->inflight_entry); |
| 5690 | if (waitqueue_active(&ctx->inflight_wait)) |
| 5691 | wake_up(&ctx->inflight_wait); |
| 5692 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 5693 | req->flags &= ~REQ_F_INFLIGHT; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 5694 | put_files_struct(req->work.files); |
Jens Axboe | 9b82849 | 2020-09-18 20:13:06 -0600 | [diff] [blame] | 5695 | put_nsproxy(req->work.nsproxy); |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 5696 | req->work.files = NULL; |
| 5697 | } |
| 5698 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 5699 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5700 | { |
| 5701 | struct io_async_ctx *io = req->io; |
| 5702 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5703 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5704 | switch (req->opcode) { |
| 5705 | case IORING_OP_READV: |
| 5706 | case IORING_OP_READ_FIXED: |
| 5707 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5708 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5709 | break; |
| 5710 | case IORING_OP_RECVMSG: |
| 5711 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5712 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5713 | break; |
| 5714 | } |
| 5715 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5716 | } |
| 5717 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5718 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 5719 | switch (req->opcode) { |
| 5720 | case IORING_OP_READV: |
| 5721 | case IORING_OP_READ_FIXED: |
| 5722 | case IORING_OP_READ: |
| 5723 | case IORING_OP_WRITEV: |
| 5724 | case IORING_OP_WRITE_FIXED: |
| 5725 | case IORING_OP_WRITE: |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 5726 | if (io->rw.free_iovec) |
| 5727 | kfree(io->rw.free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5728 | break; |
| 5729 | case IORING_OP_RECVMSG: |
| 5730 | case IORING_OP_SENDMSG: |
| 5731 | if (io->msg.iov != io->msg.fast_iov) |
| 5732 | kfree(io->msg.iov); |
| 5733 | break; |
| 5734 | case IORING_OP_SPLICE: |
| 5735 | case IORING_OP_TEE: |
| 5736 | io_put_file(req, req->splice.file_in, |
| 5737 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5738 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 5739 | case IORING_OP_OPENAT: |
| 5740 | case IORING_OP_OPENAT2: |
| 5741 | if (req->open.filename) |
| 5742 | putname(req->open.filename); |
| 5743 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5744 | } |
| 5745 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5746 | } |
Pavel Begunkov | bb17534 | 2020-08-20 11:33:35 +0300 | [diff] [blame] | 5747 | |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 5748 | if (req->flags & REQ_F_INFLIGHT) |
| 5749 | io_req_drop_files(req); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5750 | } |
| 5751 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5752 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5753 | bool force_nonblock, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5754 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5755 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5756 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5757 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5758 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5759 | case IORING_OP_NOP: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5760 | ret = io_nop(req, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5761 | break; |
| 5762 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5763 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5764 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5765 | if (sqe) { |
| 5766 | ret = io_read_prep(req, sqe, force_nonblock); |
| 5767 | if (ret < 0) |
| 5768 | break; |
| 5769 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5770 | ret = io_read(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5771 | break; |
| 5772 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5773 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5774 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5775 | if (sqe) { |
| 5776 | ret = io_write_prep(req, sqe, force_nonblock); |
| 5777 | if (ret < 0) |
| 5778 | break; |
| 5779 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5780 | ret = io_write(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5781 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5782 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5783 | if (sqe) { |
| 5784 | ret = io_prep_fsync(req, sqe); |
| 5785 | if (ret < 0) |
| 5786 | break; |
| 5787 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5788 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5789 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5790 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5791 | if (sqe) { |
| 5792 | ret = io_poll_add_prep(req, sqe); |
| 5793 | if (ret) |
| 5794 | break; |
| 5795 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5796 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5797 | break; |
| 5798 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5799 | if (sqe) { |
| 5800 | ret = io_poll_remove_prep(req, sqe); |
| 5801 | if (ret < 0) |
| 5802 | break; |
| 5803 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5804 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5805 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5806 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5807 | if (sqe) { |
| 5808 | ret = io_prep_sfr(req, sqe); |
| 5809 | if (ret < 0) |
| 5810 | break; |
| 5811 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5812 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5813 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5814 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5815 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5816 | if (sqe) { |
| 5817 | ret = io_sendmsg_prep(req, sqe); |
| 5818 | if (ret < 0) |
| 5819 | break; |
| 5820 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5821 | if (req->opcode == IORING_OP_SENDMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5822 | ret = io_sendmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5823 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5824 | ret = io_send(req, force_nonblock, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5825 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5826 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5827 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5828 | if (sqe) { |
| 5829 | ret = io_recvmsg_prep(req, sqe); |
| 5830 | if (ret) |
| 5831 | break; |
| 5832 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5833 | if (req->opcode == IORING_OP_RECVMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5834 | ret = io_recvmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5835 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5836 | ret = io_recv(req, force_nonblock, cs); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5837 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5838 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5839 | if (sqe) { |
| 5840 | ret = io_timeout_prep(req, sqe, false); |
| 5841 | if (ret) |
| 5842 | break; |
| 5843 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5844 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5845 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5846 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5847 | if (sqe) { |
| 5848 | ret = io_timeout_remove_prep(req, sqe); |
| 5849 | if (ret) |
| 5850 | break; |
| 5851 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5852 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5853 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5854 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5855 | if (sqe) { |
| 5856 | ret = io_accept_prep(req, sqe); |
| 5857 | if (ret) |
| 5858 | break; |
| 5859 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5860 | ret = io_accept(req, force_nonblock, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5861 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5862 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5863 | if (sqe) { |
| 5864 | ret = io_connect_prep(req, sqe); |
| 5865 | if (ret) |
| 5866 | break; |
| 5867 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5868 | ret = io_connect(req, force_nonblock, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5869 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5870 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5871 | if (sqe) { |
| 5872 | ret = io_async_cancel_prep(req, sqe); |
| 5873 | if (ret) |
| 5874 | break; |
| 5875 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5876 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5877 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5878 | case IORING_OP_FALLOCATE: |
| 5879 | if (sqe) { |
| 5880 | ret = io_fallocate_prep(req, sqe); |
| 5881 | if (ret) |
| 5882 | break; |
| 5883 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5884 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5885 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5886 | case IORING_OP_OPENAT: |
| 5887 | if (sqe) { |
| 5888 | ret = io_openat_prep(req, sqe); |
| 5889 | if (ret) |
| 5890 | break; |
| 5891 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5892 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5893 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5894 | case IORING_OP_CLOSE: |
| 5895 | if (sqe) { |
| 5896 | ret = io_close_prep(req, sqe); |
| 5897 | if (ret) |
| 5898 | break; |
| 5899 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5900 | ret = io_close(req, force_nonblock, cs); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5901 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5902 | case IORING_OP_FILES_UPDATE: |
| 5903 | if (sqe) { |
| 5904 | ret = io_files_update_prep(req, sqe); |
| 5905 | if (ret) |
| 5906 | break; |
| 5907 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5908 | ret = io_files_update(req, force_nonblock, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5909 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5910 | case IORING_OP_STATX: |
| 5911 | if (sqe) { |
| 5912 | ret = io_statx_prep(req, sqe); |
| 5913 | if (ret) |
| 5914 | break; |
| 5915 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5916 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5917 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5918 | case IORING_OP_FADVISE: |
| 5919 | if (sqe) { |
| 5920 | ret = io_fadvise_prep(req, sqe); |
| 5921 | if (ret) |
| 5922 | break; |
| 5923 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5924 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5925 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5926 | case IORING_OP_MADVISE: |
| 5927 | if (sqe) { |
| 5928 | ret = io_madvise_prep(req, sqe); |
| 5929 | if (ret) |
| 5930 | break; |
| 5931 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5932 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5933 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5934 | case IORING_OP_OPENAT2: |
| 5935 | if (sqe) { |
| 5936 | ret = io_openat2_prep(req, sqe); |
| 5937 | if (ret) |
| 5938 | break; |
| 5939 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5940 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5941 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5942 | case IORING_OP_EPOLL_CTL: |
| 5943 | if (sqe) { |
| 5944 | ret = io_epoll_ctl_prep(req, sqe); |
| 5945 | if (ret) |
| 5946 | break; |
| 5947 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5948 | ret = io_epoll_ctl(req, force_nonblock, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5949 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5950 | case IORING_OP_SPLICE: |
| 5951 | if (sqe) { |
| 5952 | ret = io_splice_prep(req, sqe); |
| 5953 | if (ret < 0) |
| 5954 | break; |
| 5955 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5956 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5957 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5958 | case IORING_OP_PROVIDE_BUFFERS: |
| 5959 | if (sqe) { |
| 5960 | ret = io_provide_buffers_prep(req, sqe); |
| 5961 | if (ret) |
| 5962 | break; |
| 5963 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5964 | ret = io_provide_buffers(req, force_nonblock, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5965 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5966 | case IORING_OP_REMOVE_BUFFERS: |
| 5967 | if (sqe) { |
| 5968 | ret = io_remove_buffers_prep(req, sqe); |
| 5969 | if (ret) |
| 5970 | break; |
| 5971 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5972 | ret = io_remove_buffers(req, force_nonblock, cs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5973 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5974 | case IORING_OP_TEE: |
| 5975 | if (sqe) { |
| 5976 | ret = io_tee_prep(req, sqe); |
| 5977 | if (ret < 0) |
| 5978 | break; |
| 5979 | } |
| 5980 | ret = io_tee(req, force_nonblock); |
| 5981 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5982 | default: |
| 5983 | ret = -EINVAL; |
| 5984 | break; |
| 5985 | } |
| 5986 | |
| 5987 | if (ret) |
| 5988 | return ret; |
| 5989 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 5990 | /* If the op doesn't have a file, we're not polling for it */ |
| 5991 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5992 | const bool in_async = io_wq_current_is_worker(); |
| 5993 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5994 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 5995 | if (in_async) |
| 5996 | mutex_lock(&ctx->uring_lock); |
| 5997 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5998 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5999 | |
| 6000 | if (in_async) |
| 6001 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6007 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6008 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6009 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6010 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6011 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6012 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6013 | timeout = io_prep_linked_timeout(req); |
| 6014 | if (timeout) |
| 6015 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6016 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6017 | /* if NO_CANCEL is set, we must still run the work */ |
| 6018 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 6019 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6020 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6021 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6022 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6023 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6024 | do { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6025 | ret = io_issue_sqe(req, NULL, false, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6026 | /* |
| 6027 | * We can get EAGAIN for polled IO even though we're |
| 6028 | * forcing a sync submission from here, since we can't |
| 6029 | * wait for request slots on the block side. |
| 6030 | */ |
| 6031 | if (ret != -EAGAIN) |
| 6032 | break; |
| 6033 | cond_resched(); |
| 6034 | } while (1); |
| 6035 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6036 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6037 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6038 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6039 | io_req_complete(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6040 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6041 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6042 | return io_steal_work(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6043 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6044 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6045 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6046 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6047 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6048 | struct fixed_file_table *table; |
| 6049 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6050 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6051 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6052 | } |
| 6053 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6054 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 6055 | int fd, struct file **out_file, bool fixed) |
| 6056 | { |
| 6057 | struct io_ring_ctx *ctx = req->ctx; |
| 6058 | struct file *file; |
| 6059 | |
| 6060 | if (fixed) { |
| 6061 | if (unlikely(!ctx->file_data || |
| 6062 | (unsigned) fd >= ctx->nr_user_files)) |
| 6063 | return -EBADF; |
| 6064 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6065 | file = io_file_from_index(ctx, fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 6066 | if (file) { |
| 6067 | req->fixed_file_refs = ctx->file_data->cur_refs; |
| 6068 | percpu_ref_get(req->fixed_file_refs); |
| 6069 | } |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6070 | } else { |
| 6071 | trace_io_uring_file_get(ctx, fd); |
| 6072 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6073 | } |
| 6074 | |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 6075 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
| 6076 | *out_file = file; |
| 6077 | return 0; |
| 6078 | } |
| 6079 | return -EBADF; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6080 | } |
| 6081 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6082 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6083 | int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6084 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6085 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6086 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6087 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6088 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6089 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6090 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6091 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6092 | } |
| 6093 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6094 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6095 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6096 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6097 | |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 6098 | io_req_init_async(req); |
| 6099 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 6100 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 6101 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6102 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6103 | req->work.files = get_files_struct(current); |
Jens Axboe | 9b82849 | 2020-09-18 20:13:06 -0600 | [diff] [blame] | 6104 | get_nsproxy(current->nsproxy); |
| 6105 | req->work.nsproxy = current->nsproxy; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6106 | req->flags |= REQ_F_INFLIGHT; |
| 6107 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6108 | spin_lock_irq(&ctx->inflight_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6109 | list_add(&req->inflight_entry, &ctx->inflight_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6110 | spin_unlock_irq(&ctx->inflight_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6111 | return 0; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6112 | } |
| 6113 | |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 6114 | static inline int io_prep_work_files(struct io_kiocb *req) |
| 6115 | { |
| 6116 | if (!io_op_defs[req->opcode].file_table) |
| 6117 | return 0; |
| 6118 | return io_grab_files(req); |
| 6119 | } |
| 6120 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6121 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6122 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6123 | struct io_timeout_data *data = container_of(timer, |
| 6124 | struct io_timeout_data, timer); |
| 6125 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6126 | struct io_ring_ctx *ctx = req->ctx; |
| 6127 | struct io_kiocb *prev = NULL; |
| 6128 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6129 | |
| 6130 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 6131 | |
| 6132 | /* |
| 6133 | * We don't expect the list to be empty, that will only happen if we |
| 6134 | * race with the completion of the linked work. |
| 6135 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 6136 | if (!list_empty(&req->link_list)) { |
| 6137 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 6138 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 6139 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 6140 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 6141 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 6142 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6143 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6147 | |
| 6148 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6149 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6150 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6151 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6152 | } else { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6153 | io_req_complete(req, -ETIME); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6154 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6155 | return HRTIMER_NORESTART; |
| 6156 | } |
| 6157 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6158 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6159 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6160 | /* |
| 6161 | * If the list is now empty, then our linked request finished before |
| 6162 | * we got a chance to setup the timer |
| 6163 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 6164 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6165 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6166 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6167 | data->timer.function = io_link_timeout_fn; |
| 6168 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6169 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6170 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6174 | { |
| 6175 | struct io_ring_ctx *ctx = req->ctx; |
| 6176 | |
| 6177 | spin_lock_irq(&ctx->completion_lock); |
| 6178 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6179 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6180 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6181 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6182 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6183 | } |
| 6184 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6185 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6186 | { |
| 6187 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6188 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 6189 | if (!(req->flags & REQ_F_LINK_HEAD)) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6190 | return NULL; |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6191 | if (req->flags & REQ_F_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6192 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6193 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 6194 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 6195 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6196 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6197 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6198 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6199 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6200 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6203 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6204 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6205 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6206 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6207 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6208 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6209 | int ret; |
| 6210 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6211 | again: |
| 6212 | linked_timeout = io_prep_linked_timeout(req); |
| 6213 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6214 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
| 6215 | req->work.creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6216 | if (old_creds) |
| 6217 | revert_creds(old_creds); |
| 6218 | if (old_creds == req->work.creds) |
| 6219 | old_creds = NULL; /* restored original creds */ |
| 6220 | else |
| 6221 | old_creds = override_creds(req->work.creds); |
| 6222 | } |
| 6223 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6224 | ret = io_issue_sqe(req, sqe, true, cs); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6225 | |
| 6226 | /* |
| 6227 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6228 | * doesn't support non-blocking read/write attempts |
| 6229 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6230 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6231 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 6232 | punt: |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6233 | ret = io_prep_work_files(req); |
| 6234 | if (unlikely(ret)) |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6235 | goto err; |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6236 | /* |
| 6237 | * Queued up for async execution, worker will release |
| 6238 | * submit reference when the iocb is actually submitted. |
| 6239 | */ |
| 6240 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6241 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6242 | |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6243 | if (linked_timeout) |
| 6244 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6245 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6246 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6247 | |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6248 | if (unlikely(ret)) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6249 | err: |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6250 | /* un-prep timeout, so it'll be killed as any other linked */ |
| 6251 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6252 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6253 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6254 | io_req_complete(req, ret); |
| 6255 | goto exit; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6256 | } |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6257 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6258 | /* drop submission reference */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 6259 | nxt = io_put_req_find_next(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6260 | if (linked_timeout) |
| 6261 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6262 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6263 | if (nxt) { |
| 6264 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 6265 | |
| 6266 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 6267 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6268 | goto again; |
| 6269 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6270 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6271 | if (old_creds) |
| 6272 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6273 | } |
| 6274 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6275 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6276 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6277 | { |
| 6278 | int ret; |
| 6279 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6280 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6281 | if (ret) { |
| 6282 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6283 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6284 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6285 | io_put_req(req); |
| 6286 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6287 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6288 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6289 | if (!req->io) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6290 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6291 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6292 | goto fail_req; |
| 6293 | } |
| 6294 | |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6295 | /* |
| 6296 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 6297 | * to async execution. |
| 6298 | */ |
Pavel Begunkov | 3e863ea | 2020-07-23 20:17:20 +0300 | [diff] [blame] | 6299 | io_req_init_async(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6300 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 6301 | io_queue_async_work(req); |
| 6302 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6303 | __io_queue_sqe(req, sqe, cs); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6304 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6305 | } |
| 6306 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6307 | static inline void io_queue_link_head(struct io_kiocb *req, |
| 6308 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6309 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6310 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6311 | io_put_req(req); |
| 6312 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6313 | } else |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6314 | io_queue_sqe(req, NULL, cs); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6315 | } |
| 6316 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6317 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6318 | struct io_kiocb **link, struct io_comp_state *cs) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6319 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6320 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6321 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6322 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6323 | /* |
| 6324 | * If we already have a head request, queue this one for async |
| 6325 | * submittal once the head completes. If we don't have a head but |
| 6326 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6327 | * submitted sync once the chain is complete. If none of those |
| 6328 | * conditions are true (normal request), then just queue it. |
| 6329 | */ |
| 6330 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6331 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6332 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6333 | /* |
| 6334 | * Taking sequential execution of a link, draining both sides |
| 6335 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6336 | * requests in the link. So, it drains the head and the |
| 6337 | * next after the link request. The last one is done via |
| 6338 | * drain_next flag to persist the effect across calls. |
| 6339 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6340 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6341 | head->flags |= REQ_F_IO_DRAIN; |
| 6342 | ctx->drain_next = 1; |
| 6343 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6344 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6345 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6346 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6347 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6348 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6349 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6350 | trace_io_uring_link(ctx, req, head); |
| 6351 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6352 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6353 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6354 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6355 | io_queue_link_head(head, cs); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6356 | *link = NULL; |
| 6357 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6358 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6359 | if (unlikely(ctx->drain_next)) { |
| 6360 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6361 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6362 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6363 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 6364 | req->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6365 | INIT_LIST_HEAD(&req->link_list); |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 6366 | |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6367 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6368 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6369 | req->flags |= REQ_F_FAIL_LINK; |
| 6370 | *link = req; |
| 6371 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6372 | io_queue_sqe(req, sqe, cs); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6373 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6374 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6375 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6376 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6377 | } |
| 6378 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6379 | /* |
| 6380 | * Batched submission is done, ensure local IO is flushed out. |
| 6381 | */ |
| 6382 | static void io_submit_state_end(struct io_submit_state *state) |
| 6383 | { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6384 | if (!list_empty(&state->comp.list)) |
| 6385 | io_submit_flush_completions(&state->comp); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6386 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6387 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6388 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6389 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | /* |
| 6393 | * Start submission side cache. |
| 6394 | */ |
| 6395 | static void io_submit_state_start(struct io_submit_state *state, |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6396 | struct io_ring_ctx *ctx, unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6397 | { |
| 6398 | blk_start_plug(&state->plug); |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6399 | state->comp.nr = 0; |
| 6400 | INIT_LIST_HEAD(&state->comp.list); |
| 6401 | state->comp.ctx = ctx; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6402 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6403 | state->file = NULL; |
| 6404 | state->ios_left = max_ios; |
| 6405 | } |
| 6406 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6407 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6408 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6409 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6410 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6411 | /* |
| 6412 | * Ensure any loads from the SQEs are done at this point, |
| 6413 | * since once we write the new head, the application could |
| 6414 | * write new data to them. |
| 6415 | */ |
| 6416 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6420 | * 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] | 6421 | * that is mapped by userspace. This means that care needs to be taken to |
| 6422 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6423 | * being a good citizen. If members of the sqe are validated and then later |
| 6424 | * used, it's important that those reads are done through READ_ONCE() to |
| 6425 | * prevent a re-load down the line. |
| 6426 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6427 | 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] | 6428 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6429 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6430 | unsigned head; |
| 6431 | |
| 6432 | /* |
| 6433 | * The cached sq head (or cq tail) serves two purposes: |
| 6434 | * |
| 6435 | * 1) allows us to batch the cost of updating the user visible |
| 6436 | * head updates. |
| 6437 | * 2) allows the kernel side to track the head on its own, even |
| 6438 | * though the application is the one updating it. |
| 6439 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6440 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6441 | if (likely(head < ctx->sq_entries)) |
| 6442 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6443 | |
| 6444 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6445 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6446 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6447 | return NULL; |
| 6448 | } |
| 6449 | |
| 6450 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6451 | { |
| 6452 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6453 | } |
| 6454 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6455 | /* |
| 6456 | * Check SQE restrictions (opcode and flags). |
| 6457 | * |
| 6458 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6459 | */ |
| 6460 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6461 | struct io_kiocb *req, |
| 6462 | unsigned int sqe_flags) |
| 6463 | { |
| 6464 | if (!ctx->restricted) |
| 6465 | return true; |
| 6466 | |
| 6467 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6468 | return false; |
| 6469 | |
| 6470 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6471 | ctx->restrictions.sqe_flags_required) |
| 6472 | return false; |
| 6473 | |
| 6474 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6475 | ctx->restrictions.sqe_flags_required)) |
| 6476 | return false; |
| 6477 | |
| 6478 | return true; |
| 6479 | } |
| 6480 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6481 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6482 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6483 | IOSQE_BUFFER_SELECT) |
| 6484 | |
| 6485 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6486 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6487 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6488 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6489 | unsigned int sqe_flags; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6490 | int id; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6491 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6492 | req->opcode = READ_ONCE(sqe->opcode); |
| 6493 | req->user_data = READ_ONCE(sqe->user_data); |
| 6494 | req->io = NULL; |
| 6495 | req->file = NULL; |
| 6496 | req->ctx = ctx; |
| 6497 | req->flags = 0; |
| 6498 | /* one is dropped after submission, the other at completion */ |
| 6499 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6500 | req->task = current; |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 6501 | get_task_struct(req->task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6502 | atomic_long_inc(&req->task->io_uring->req_issue); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6503 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6504 | |
| 6505 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6506 | return -EINVAL; |
| 6507 | |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6508 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
| 6509 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6510 | |
| 6511 | sqe_flags = READ_ONCE(sqe->flags); |
| 6512 | /* enforce forwards compatibility on users */ |
| 6513 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6514 | return -EINVAL; |
| 6515 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6516 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6517 | return -EACCES; |
| 6518 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6519 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6520 | !io_op_defs[req->opcode].buffer_select) |
| 6521 | return -EOPNOTSUPP; |
| 6522 | |
| 6523 | id = READ_ONCE(sqe->personality); |
| 6524 | if (id) { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6525 | io_req_init_async(req); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6526 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 6527 | if (unlikely(!req->work.creds)) |
| 6528 | return -EINVAL; |
| 6529 | get_cred(req->work.creds); |
| 6530 | } |
| 6531 | |
| 6532 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6533 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6534 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6535 | if (!io_op_defs[req->opcode].needs_file) |
| 6536 | return 0; |
| 6537 | |
| 6538 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6539 | } |
| 6540 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6541 | 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] | 6542 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6543 | struct io_submit_state state; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6544 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6545 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6546 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6547 | /* 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] | 6548 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 6549 | if (!list_empty(&ctx->cq_overflow_list) && |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 6550 | !io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6551 | return -EBUSY; |
| 6552 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6553 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6554 | /* make sure SQ entry isn't read before tail */ |
| 6555 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6556 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6557 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6558 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6559 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6560 | io_submit_state_start(&state, ctx, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6561 | |
| 6562 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6563 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6564 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6565 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6566 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6567 | sqe = io_get_sqe(ctx); |
| 6568 | if (unlikely(!sqe)) { |
| 6569 | io_consume_sqe(ctx); |
| 6570 | break; |
| 6571 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6572 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6573 | if (unlikely(!req)) { |
| 6574 | if (!submitted) |
| 6575 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6576 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6577 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6578 | |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6579 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6580 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6581 | /* will complete beyond this point, count as submitted */ |
| 6582 | submitted++; |
| 6583 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6584 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6585 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6586 | io_put_req(req); |
| 6587 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6588 | break; |
| 6589 | } |
| 6590 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6591 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6592 | true, io_async_submit(ctx)); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6593 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6594 | if (err) |
| 6595 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6596 | } |
| 6597 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6598 | if (unlikely(submitted != nr)) { |
| 6599 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 6600 | |
| 6601 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 6602 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6603 | if (link) |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6604 | io_queue_link_head(link, &state.comp); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6605 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6606 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6607 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6608 | io_commit_sqring(ctx); |
| 6609 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6610 | return submitted; |
| 6611 | } |
| 6612 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6613 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6614 | { |
| 6615 | /* Tell userspace we may need a wakeup call */ |
| 6616 | spin_lock_irq(&ctx->completion_lock); |
| 6617 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6618 | spin_unlock_irq(&ctx->completion_lock); |
| 6619 | } |
| 6620 | |
| 6621 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6622 | { |
| 6623 | spin_lock_irq(&ctx->completion_lock); |
| 6624 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6625 | spin_unlock_irq(&ctx->completion_lock); |
| 6626 | } |
| 6627 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6628 | static int io_sq_thread(void *data) |
| 6629 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6630 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6631 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6632 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6633 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6634 | |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6635 | init_wait(&ctx->sqo_wait_entry); |
| 6636 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6637 | complete(&ctx->sq_thread_comp); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 6638 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6639 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6640 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6641 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6642 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6643 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6644 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6645 | if (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6646 | unsigned nr_events = 0; |
| 6647 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6648 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6649 | if (!list_empty(&ctx->iopoll_list) && !need_resched()) |
Pavel Begunkov | 9dedd56 | 2020-07-07 16:36:20 +0300 | [diff] [blame] | 6650 | io_do_iopoll(ctx, &nr_events, 0); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6651 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6652 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6653 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6654 | } |
| 6655 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6656 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6657 | |
| 6658 | /* |
| 6659 | * If submit got -EBUSY, flag us as needing the application |
| 6660 | * to enter the kernel to reap and flush events. |
| 6661 | */ |
Xuan Zhuo | b772f07 | 2020-06-23 19:34:06 +0800 | [diff] [blame] | 6662 | if (!to_submit || ret == -EBUSY || need_resched()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6663 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6664 | * Drop cur_mm before scheduling, we can't hold it for |
| 6665 | * long periods (or over schedule()). Do this before |
| 6666 | * adding ourselves to the waitqueue, as the unuse/drop |
| 6667 | * may sleep. |
| 6668 | */ |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 6669 | io_sq_thread_drop_mm(); |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6670 | |
| 6671 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6672 | * We're polling. If we're within the defined idle |
| 6673 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6674 | * to sleep. The exception is if we got EBUSY doing |
| 6675 | * more IO, we should wait for the application to |
| 6676 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6677 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6678 | if (!list_empty(&ctx->iopoll_list) || need_resched() || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6679 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 6680 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6681 | io_run_task_work(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 6682 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6683 | continue; |
| 6684 | } |
| 6685 | |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6686 | prepare_to_wait(ctx->sqo_wait, &ctx->sqo_wait_entry, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6687 | TASK_INTERRUPTIBLE); |
| 6688 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6689 | /* |
| 6690 | * While doing polled IO, before going to sleep, we need |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6691 | * to check if there are new reqs added to iopoll_list, |
| 6692 | * it is because reqs may have been punted to io worker |
| 6693 | * and will be added to iopoll_list later, hence check |
| 6694 | * the iopoll_list again. |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6695 | */ |
| 6696 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6697 | !list_empty_careful(&ctx->iopoll_list)) { |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6698 | finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6699 | continue; |
| 6700 | } |
| 6701 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6702 | io_ring_set_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6703 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6704 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6705 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6706 | if (kthread_should_park()) { |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6707 | finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6708 | break; |
| 6709 | } |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6710 | if (io_run_task_work()) { |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6711 | finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6712 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6713 | continue; |
| 6714 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6715 | schedule(); |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6716 | finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6717 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6718 | io_ring_clear_wakeup_flag(ctx); |
Xiaoguang Wang | d4ae271 | 2020-05-20 21:24:35 +0800 | [diff] [blame] | 6719 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6720 | continue; |
| 6721 | } |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 6722 | finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6723 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6724 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6725 | } |
| 6726 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6727 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 6b668c9 | 2020-05-20 15:35:03 +0800 | [diff] [blame] | 6728 | if (likely(!percpu_ref_is_dying(&ctx->refs))) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6729 | ret = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6730 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6731 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6732 | } |
| 6733 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6734 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6735 | |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 6736 | io_sq_thread_drop_mm(); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6737 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6738 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6739 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6740 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6741 | return 0; |
| 6742 | } |
| 6743 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6744 | struct io_wait_queue { |
| 6745 | struct wait_queue_entry wq; |
| 6746 | struct io_ring_ctx *ctx; |
| 6747 | unsigned to_wait; |
| 6748 | unsigned nr_timeouts; |
| 6749 | }; |
| 6750 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6751 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6752 | { |
| 6753 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6754 | |
| 6755 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6756 | * 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] | 6757 | * started waiting. For timeouts, we always want to return to userspace, |
| 6758 | * regardless of event count. |
| 6759 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6760 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6761 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6762 | } |
| 6763 | |
| 6764 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6765 | int wake_flags, void *key) |
| 6766 | { |
| 6767 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6768 | wq); |
| 6769 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6770 | /* use noflush == true, as we can't safely rely on locking context */ |
| 6771 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6772 | return -1; |
| 6773 | |
| 6774 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6775 | } |
| 6776 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6777 | /* |
| 6778 | * Wait until events become available, if we don't already have some. The |
| 6779 | * application must reap them itself, as they reside on the shared cq ring. |
| 6780 | */ |
| 6781 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 6782 | const sigset_t __user *sig, size_t sigsz) |
| 6783 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6784 | struct io_wait_queue iowq = { |
| 6785 | .wq = { |
| 6786 | .private = current, |
| 6787 | .func = io_wake_function, |
| 6788 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6789 | }, |
| 6790 | .ctx = ctx, |
| 6791 | .to_wait = min_events, |
| 6792 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6793 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6794 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6795 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6796 | do { |
| 6797 | if (io_cqring_events(ctx, false) >= min_events) |
| 6798 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6799 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6800 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6801 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6802 | |
| 6803 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6804 | #ifdef CONFIG_COMPAT |
| 6805 | if (in_compat_syscall()) |
| 6806 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6807 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6808 | else |
| 6809 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6810 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6811 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6812 | if (ret) |
| 6813 | return ret; |
| 6814 | } |
| 6815 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6816 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6817 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6818 | do { |
| 6819 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6820 | TASK_INTERRUPTIBLE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6821 | /* make sure we run task_work before checking for signals */ |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6822 | if (io_run_task_work()) |
| 6823 | continue; |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6824 | if (signal_pending(current)) { |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6825 | if (current->jobctl & JOBCTL_TASK_WORK) { |
| 6826 | spin_lock_irq(¤t->sighand->siglock); |
| 6827 | current->jobctl &= ~JOBCTL_TASK_WORK; |
| 6828 | recalc_sigpending(); |
| 6829 | spin_unlock_irq(¤t->sighand->siglock); |
| 6830 | continue; |
| 6831 | } |
| 6832 | ret = -EINTR; |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6833 | break; |
| 6834 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6835 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6836 | break; |
| 6837 | schedule(); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6838 | } while (1); |
| 6839 | finish_wait(&ctx->wait, &iowq.wq); |
| 6840 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6841 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6842 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6843 | 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] | 6844 | } |
| 6845 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6846 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6847 | { |
| 6848 | #if defined(CONFIG_UNIX) |
| 6849 | if (ctx->ring_sock) { |
| 6850 | struct sock *sock = ctx->ring_sock->sk; |
| 6851 | struct sk_buff *skb; |
| 6852 | |
| 6853 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6854 | kfree_skb(skb); |
| 6855 | } |
| 6856 | #else |
| 6857 | int i; |
| 6858 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6859 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6860 | struct file *file; |
| 6861 | |
| 6862 | file = io_file_from_index(ctx, i); |
| 6863 | if (file) |
| 6864 | fput(file); |
| 6865 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6866 | #endif |
| 6867 | } |
| 6868 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6869 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 6870 | { |
| 6871 | struct fixed_file_data *data; |
| 6872 | |
| 6873 | data = container_of(ref, struct fixed_file_data, refs); |
| 6874 | complete(&data->done); |
| 6875 | } |
| 6876 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6877 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6878 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6879 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6880 | struct fixed_file_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6881 | unsigned nr_tables, i; |
| 6882 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6883 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6884 | return -ENXIO; |
| 6885 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6886 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6887 | if (!list_empty(&data->ref_list)) |
| 6888 | ref_node = list_first_entry(&data->ref_list, |
| 6889 | struct fixed_file_ref_node, node); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6890 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6891 | if (ref_node) |
| 6892 | percpu_ref_kill(&ref_node->refs); |
| 6893 | |
| 6894 | percpu_ref_kill(&data->refs); |
| 6895 | |
| 6896 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6897 | flush_delayed_work(&ctx->file_put_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6898 | wait_for_completion(&data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6899 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6900 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6901 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 6902 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6903 | kfree(data->table[i].files); |
| 6904 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6905 | percpu_ref_exit(&data->refs); |
| 6906 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6907 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6908 | ctx->nr_user_files = 0; |
| 6909 | return 0; |
| 6910 | } |
| 6911 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6912 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 6913 | { |
| 6914 | if (ctx->sqo_thread) { |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 6915 | /* |
| 6916 | * We may arrive here from the error branch in |
| 6917 | * io_sq_offload_create() where the kthread is created |
| 6918 | * without being waked up, thus wake it up now to make |
| 6919 | * sure the wait will complete. |
| 6920 | */ |
| 6921 | wake_up_process(ctx->sqo_thread); |
| 6922 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6923 | wait_for_completion(&ctx->sq_thread_comp); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6924 | /* |
| 6925 | * The park is a bit of a work-around, without it we get |
| 6926 | * warning spews on shutdown with SQPOLL set and affinity |
| 6927 | * set to a single CPU. |
| 6928 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6929 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6930 | kthread_stop(ctx->sqo_thread); |
| 6931 | ctx->sqo_thread = NULL; |
| 6932 | } |
| 6933 | } |
| 6934 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6935 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 6936 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6937 | io_sq_thread_stop(ctx); |
| 6938 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6939 | if (ctx->io_wq) { |
| 6940 | io_wq_destroy(ctx->io_wq); |
| 6941 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6942 | } |
| 6943 | } |
| 6944 | |
| 6945 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6946 | /* |
| 6947 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 6948 | * the io_uring can be safely unregistered on process exit, even if we have |
| 6949 | * loops in the file referencing. |
| 6950 | */ |
| 6951 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 6952 | { |
| 6953 | struct sock *sk = ctx->ring_sock->sk; |
| 6954 | struct scm_fp_list *fpl; |
| 6955 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6956 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6957 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6958 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 6959 | if (!fpl) |
| 6960 | return -ENOMEM; |
| 6961 | |
| 6962 | skb = alloc_skb(0, GFP_KERNEL); |
| 6963 | if (!skb) { |
| 6964 | kfree(fpl); |
| 6965 | return -ENOMEM; |
| 6966 | } |
| 6967 | |
| 6968 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6969 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6970 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6971 | fpl->user = get_uid(ctx->user); |
| 6972 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6973 | struct file *file = io_file_from_index(ctx, i + offset); |
| 6974 | |
| 6975 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6976 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6977 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6978 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 6979 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6980 | } |
| 6981 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6982 | if (nr_files) { |
| 6983 | fpl->max = SCM_MAX_FD; |
| 6984 | fpl->count = nr_files; |
| 6985 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6986 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6987 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 6988 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6989 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6990 | for (i = 0; i < nr_files; i++) |
| 6991 | fput(fpl->fp[i]); |
| 6992 | } else { |
| 6993 | kfree_skb(skb); |
| 6994 | kfree(fpl); |
| 6995 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6996 | |
| 6997 | return 0; |
| 6998 | } |
| 6999 | |
| 7000 | /* |
| 7001 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7002 | * causes regular reference counting to break down. We rely on the UNIX |
| 7003 | * garbage collection to take care of this problem for us. |
| 7004 | */ |
| 7005 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7006 | { |
| 7007 | unsigned left, total; |
| 7008 | int ret = 0; |
| 7009 | |
| 7010 | total = 0; |
| 7011 | left = ctx->nr_user_files; |
| 7012 | while (left) { |
| 7013 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7014 | |
| 7015 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7016 | if (ret) |
| 7017 | break; |
| 7018 | left -= this_files; |
| 7019 | total += this_files; |
| 7020 | } |
| 7021 | |
| 7022 | if (!ret) |
| 7023 | return 0; |
| 7024 | |
| 7025 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7026 | struct file *file = io_file_from_index(ctx, total); |
| 7027 | |
| 7028 | if (file) |
| 7029 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7030 | total++; |
| 7031 | } |
| 7032 | |
| 7033 | return ret; |
| 7034 | } |
| 7035 | #else |
| 7036 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7037 | { |
| 7038 | return 0; |
| 7039 | } |
| 7040 | #endif |
| 7041 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7042 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 7043 | unsigned nr_files) |
| 7044 | { |
| 7045 | int i; |
| 7046 | |
| 7047 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7048 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7049 | unsigned this_files; |
| 7050 | |
| 7051 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7052 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7053 | GFP_KERNEL); |
| 7054 | if (!table->files) |
| 7055 | break; |
| 7056 | nr_files -= this_files; |
| 7057 | } |
| 7058 | |
| 7059 | if (i == nr_tables) |
| 7060 | return 0; |
| 7061 | |
| 7062 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7063 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7064 | kfree(table->files); |
| 7065 | } |
| 7066 | return 1; |
| 7067 | } |
| 7068 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7069 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7070 | { |
| 7071 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7072 | struct sock *sock = ctx->ring_sock->sk; |
| 7073 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7074 | struct sk_buff *skb; |
| 7075 | int i; |
| 7076 | |
| 7077 | __skb_queue_head_init(&list); |
| 7078 | |
| 7079 | /* |
| 7080 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7081 | * remove this entry and rearrange the file array. |
| 7082 | */ |
| 7083 | skb = skb_dequeue(head); |
| 7084 | while (skb) { |
| 7085 | struct scm_fp_list *fp; |
| 7086 | |
| 7087 | fp = UNIXCB(skb).fp; |
| 7088 | for (i = 0; i < fp->count; i++) { |
| 7089 | int left; |
| 7090 | |
| 7091 | if (fp->fp[i] != file) |
| 7092 | continue; |
| 7093 | |
| 7094 | unix_notinflight(fp->user, fp->fp[i]); |
| 7095 | left = fp->count - 1 - i; |
| 7096 | if (left) { |
| 7097 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7098 | left * sizeof(struct file *)); |
| 7099 | } |
| 7100 | fp->count--; |
| 7101 | if (!fp->count) { |
| 7102 | kfree_skb(skb); |
| 7103 | skb = NULL; |
| 7104 | } else { |
| 7105 | __skb_queue_tail(&list, skb); |
| 7106 | } |
| 7107 | fput(file); |
| 7108 | file = NULL; |
| 7109 | break; |
| 7110 | } |
| 7111 | |
| 7112 | if (!file) |
| 7113 | break; |
| 7114 | |
| 7115 | __skb_queue_tail(&list, skb); |
| 7116 | |
| 7117 | skb = skb_dequeue(head); |
| 7118 | } |
| 7119 | |
| 7120 | if (skb_peek(&list)) { |
| 7121 | spin_lock_irq(&head->lock); |
| 7122 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7123 | __skb_queue_tail(head, skb); |
| 7124 | spin_unlock_irq(&head->lock); |
| 7125 | } |
| 7126 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7127 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7128 | #endif |
| 7129 | } |
| 7130 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7131 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7132 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7133 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7134 | }; |
| 7135 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7136 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7137 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7138 | struct fixed_file_data *file_data = ref_node->file_data; |
| 7139 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7140 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7141 | |
| 7142 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7143 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7144 | io_ring_file_put(ctx, pfile->file); |
| 7145 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7146 | } |
| 7147 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7148 | spin_lock(&file_data->lock); |
| 7149 | list_del(&ref_node->node); |
| 7150 | spin_unlock(&file_data->lock); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 7151 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7152 | percpu_ref_exit(&ref_node->refs); |
| 7153 | kfree(ref_node); |
| 7154 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7155 | } |
| 7156 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7157 | static void io_file_put_work(struct work_struct *work) |
| 7158 | { |
| 7159 | struct io_ring_ctx *ctx; |
| 7160 | struct llist_node *node; |
| 7161 | |
| 7162 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 7163 | node = llist_del_all(&ctx->file_put_llist); |
| 7164 | |
| 7165 | while (node) { |
| 7166 | struct fixed_file_ref_node *ref_node; |
| 7167 | struct llist_node *next = node->next; |
| 7168 | |
| 7169 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 7170 | __io_file_put_work(ref_node); |
| 7171 | node = next; |
| 7172 | } |
| 7173 | } |
| 7174 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7175 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 7176 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7177 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7178 | struct io_ring_ctx *ctx; |
| 7179 | bool first_add; |
| 7180 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7181 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7182 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7183 | ctx = ref_node->file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7184 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7185 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
| 7186 | delay = 0; |
| 7187 | |
| 7188 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 7189 | if (!delay) |
| 7190 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 7191 | else if (first_add) |
| 7192 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7193 | } |
| 7194 | |
| 7195 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 7196 | struct io_ring_ctx *ctx) |
| 7197 | { |
| 7198 | struct fixed_file_ref_node *ref_node; |
| 7199 | |
| 7200 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7201 | if (!ref_node) |
| 7202 | return ERR_PTR(-ENOMEM); |
| 7203 | |
| 7204 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 7205 | 0, GFP_KERNEL)) { |
| 7206 | kfree(ref_node); |
| 7207 | return ERR_PTR(-ENOMEM); |
| 7208 | } |
| 7209 | INIT_LIST_HEAD(&ref_node->node); |
| 7210 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7211 | ref_node->file_data = ctx->file_data; |
| 7212 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 7216 | { |
| 7217 | percpu_ref_exit(&ref_node->refs); |
| 7218 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7219 | } |
| 7220 | |
| 7221 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7222 | unsigned nr_args) |
| 7223 | { |
| 7224 | __s32 __user *fds = (__s32 __user *) arg; |
| 7225 | unsigned nr_tables; |
| 7226 | struct file *file; |
| 7227 | int fd, ret = 0; |
| 7228 | unsigned i; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7229 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7230 | |
| 7231 | if (ctx->file_data) |
| 7232 | return -EBUSY; |
| 7233 | if (!nr_args) |
| 7234 | return -EINVAL; |
| 7235 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7236 | return -EMFILE; |
| 7237 | |
| 7238 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 7239 | if (!ctx->file_data) |
| 7240 | return -ENOMEM; |
| 7241 | ctx->file_data->ctx = ctx; |
| 7242 | init_completion(&ctx->file_data->done); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7243 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
Xiaoguang Wang | f7fe934 | 2020-04-07 20:02:31 +0800 | [diff] [blame] | 7244 | spin_lock_init(&ctx->file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7245 | |
| 7246 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 7247 | ctx->file_data->table = kcalloc(nr_tables, |
| 7248 | sizeof(struct fixed_file_table), |
| 7249 | GFP_KERNEL); |
| 7250 | if (!ctx->file_data->table) { |
| 7251 | kfree(ctx->file_data); |
| 7252 | ctx->file_data = NULL; |
| 7253 | return -ENOMEM; |
| 7254 | } |
| 7255 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7256 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7257 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7258 | kfree(ctx->file_data->table); |
| 7259 | kfree(ctx->file_data); |
| 7260 | ctx->file_data = NULL; |
| 7261 | return -ENOMEM; |
| 7262 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7263 | |
| 7264 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 7265 | percpu_ref_exit(&ctx->file_data->refs); |
| 7266 | kfree(ctx->file_data->table); |
| 7267 | kfree(ctx->file_data); |
| 7268 | ctx->file_data = NULL; |
| 7269 | return -ENOMEM; |
| 7270 | } |
| 7271 | |
| 7272 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 7273 | struct fixed_file_table *table; |
| 7274 | unsigned index; |
| 7275 | |
| 7276 | ret = -EFAULT; |
| 7277 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 7278 | break; |
| 7279 | /* allow sparse sets */ |
| 7280 | if (fd == -1) { |
| 7281 | ret = 0; |
| 7282 | continue; |
| 7283 | } |
| 7284 | |
| 7285 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7286 | index = i & IORING_FILE_TABLE_MASK; |
| 7287 | file = fget(fd); |
| 7288 | |
| 7289 | ret = -EBADF; |
| 7290 | if (!file) |
| 7291 | break; |
| 7292 | |
| 7293 | /* |
| 7294 | * Don't allow io_uring instances to be registered. If UNIX |
| 7295 | * isn't enabled, then this causes a reference cycle and this |
| 7296 | * instance can never get freed. If UNIX is enabled we'll |
| 7297 | * handle it just fine, but there's still no point in allowing |
| 7298 | * a ring fd as it doesn't support regular read/write anyway. |
| 7299 | */ |
| 7300 | if (file->f_op == &io_uring_fops) { |
| 7301 | fput(file); |
| 7302 | break; |
| 7303 | } |
| 7304 | ret = 0; |
| 7305 | table->files[index] = file; |
| 7306 | } |
| 7307 | |
| 7308 | if (ret) { |
| 7309 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7310 | file = io_file_from_index(ctx, i); |
| 7311 | if (file) |
| 7312 | fput(file); |
| 7313 | } |
| 7314 | for (i = 0; i < nr_tables; i++) |
| 7315 | kfree(ctx->file_data->table[i].files); |
| 7316 | |
Yang Yingliang | 667e57d | 2020-07-10 14:14:20 +0000 | [diff] [blame] | 7317 | percpu_ref_exit(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7318 | kfree(ctx->file_data->table); |
| 7319 | kfree(ctx->file_data); |
| 7320 | ctx->file_data = NULL; |
| 7321 | ctx->nr_user_files = 0; |
| 7322 | return ret; |
| 7323 | } |
| 7324 | |
| 7325 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7326 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7327 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7328 | return ret; |
| 7329 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7330 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7331 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7332 | if (IS_ERR(ref_node)) { |
| 7333 | io_sqe_files_unregister(ctx); |
| 7334 | return PTR_ERR(ref_node); |
| 7335 | } |
| 7336 | |
| 7337 | ctx->file_data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7338 | spin_lock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7339 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7340 | spin_unlock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7341 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7342 | return ret; |
| 7343 | } |
| 7344 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7345 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7346 | int index) |
| 7347 | { |
| 7348 | #if defined(CONFIG_UNIX) |
| 7349 | struct sock *sock = ctx->ring_sock->sk; |
| 7350 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7351 | struct sk_buff *skb; |
| 7352 | |
| 7353 | /* |
| 7354 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7355 | * file set. If there's no room, fall back to allocating a new skb |
| 7356 | * and filling it in. |
| 7357 | */ |
| 7358 | spin_lock_irq(&head->lock); |
| 7359 | skb = skb_peek(head); |
| 7360 | if (skb) { |
| 7361 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7362 | |
| 7363 | if (fpl->count < SCM_MAX_FD) { |
| 7364 | __skb_unlink(skb, head); |
| 7365 | spin_unlock_irq(&head->lock); |
| 7366 | fpl->fp[fpl->count] = get_file(file); |
| 7367 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7368 | fpl->count++; |
| 7369 | spin_lock_irq(&head->lock); |
| 7370 | __skb_queue_head(head, skb); |
| 7371 | } else { |
| 7372 | skb = NULL; |
| 7373 | } |
| 7374 | } |
| 7375 | spin_unlock_irq(&head->lock); |
| 7376 | |
| 7377 | if (skb) { |
| 7378 | fput(file); |
| 7379 | return 0; |
| 7380 | } |
| 7381 | |
| 7382 | return __io_sqe_files_scm(ctx, 1, index); |
| 7383 | #else |
| 7384 | return 0; |
| 7385 | #endif |
| 7386 | } |
| 7387 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7388 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7389 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7390 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7391 | struct io_file_put *pfile; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7392 | struct percpu_ref *refs = data->cur_refs; |
| 7393 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7394 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7395 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7396 | if (!pfile) |
| 7397 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7398 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7399 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7400 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7401 | list_add(&pfile->list, &ref_node->file_list); |
| 7402 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7403 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 7407 | struct io_uring_files_update *up, |
| 7408 | unsigned nr_args) |
| 7409 | { |
| 7410 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7411 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7412 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7413 | __s32 __user *fds; |
| 7414 | int fd, i, err; |
| 7415 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7416 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7417 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7418 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7419 | return -EOVERFLOW; |
| 7420 | if (done > ctx->nr_user_files) |
| 7421 | return -EINVAL; |
| 7422 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7423 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7424 | if (IS_ERR(ref_node)) |
| 7425 | return PTR_ERR(ref_node); |
| 7426 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7427 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7428 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7429 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7430 | struct fixed_file_table *table; |
| 7431 | unsigned index; |
| 7432 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7433 | err = 0; |
| 7434 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7435 | err = -EFAULT; |
| 7436 | break; |
| 7437 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7438 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7439 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7440 | index = i & IORING_FILE_TABLE_MASK; |
| 7441 | if (table->files[index]) { |
Jiufei Xue | 98dfd50 | 2020-09-01 13:35:02 +0800 | [diff] [blame] | 7442 | file = table->files[index]; |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7443 | err = io_queue_file_removal(data, file); |
| 7444 | if (err) |
| 7445 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7446 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7447 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7448 | } |
| 7449 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7450 | file = fget(fd); |
| 7451 | if (!file) { |
| 7452 | err = -EBADF; |
| 7453 | break; |
| 7454 | } |
| 7455 | /* |
| 7456 | * Don't allow io_uring instances to be registered. If |
| 7457 | * UNIX isn't enabled, then this causes a reference |
| 7458 | * cycle and this instance can never get freed. If UNIX |
| 7459 | * is enabled we'll handle it just fine, but there's |
| 7460 | * still no point in allowing a ring fd as it doesn't |
| 7461 | * support regular read/write anyway. |
| 7462 | */ |
| 7463 | if (file->f_op == &io_uring_fops) { |
| 7464 | fput(file); |
| 7465 | err = -EBADF; |
| 7466 | break; |
| 7467 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7468 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7469 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7470 | if (err) { |
Jiufei Xue | 95d1c8e | 2020-09-02 17:59:39 +0800 | [diff] [blame] | 7471 | table->files[index] = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7472 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7473 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7474 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7475 | } |
| 7476 | nr_args--; |
| 7477 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7478 | up->offset++; |
| 7479 | } |
| 7480 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7481 | if (needs_switch) { |
| 7482 | percpu_ref_kill(data->cur_refs); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7483 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7484 | list_add(&ref_node->node, &data->ref_list); |
| 7485 | data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7486 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7487 | percpu_ref_get(&ctx->file_data->refs); |
| 7488 | } else |
| 7489 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7490 | |
| 7491 | return done ? done : err; |
| 7492 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7493 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7494 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7495 | unsigned nr_args) |
| 7496 | { |
| 7497 | struct io_uring_files_update up; |
| 7498 | |
| 7499 | if (!ctx->file_data) |
| 7500 | return -ENXIO; |
| 7501 | if (!nr_args) |
| 7502 | return -EINVAL; |
| 7503 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7504 | return -EFAULT; |
| 7505 | if (up.resv) |
| 7506 | return -EINVAL; |
| 7507 | |
| 7508 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7509 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7510 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7511 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7512 | { |
| 7513 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7514 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7515 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7516 | io_put_req(req); |
| 7517 | } |
| 7518 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7519 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7520 | struct io_uring_params *p) |
| 7521 | { |
| 7522 | struct io_wq_data data; |
| 7523 | struct fd f; |
| 7524 | struct io_ring_ctx *ctx_attach; |
| 7525 | unsigned int concurrency; |
| 7526 | int ret = 0; |
| 7527 | |
| 7528 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7529 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7530 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7531 | |
| 7532 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 7533 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7534 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 7535 | |
| 7536 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 7537 | if (IS_ERR(ctx->io_wq)) { |
| 7538 | ret = PTR_ERR(ctx->io_wq); |
| 7539 | ctx->io_wq = NULL; |
| 7540 | } |
| 7541 | return ret; |
| 7542 | } |
| 7543 | |
| 7544 | f = fdget(p->wq_fd); |
| 7545 | if (!f.file) |
| 7546 | return -EBADF; |
| 7547 | |
| 7548 | if (f.file->f_op != &io_uring_fops) { |
| 7549 | ret = -EINVAL; |
| 7550 | goto out_fput; |
| 7551 | } |
| 7552 | |
| 7553 | ctx_attach = f.file->private_data; |
| 7554 | /* @io_wq is protected by holding the fd */ |
| 7555 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 7556 | ret = -EINVAL; |
| 7557 | goto out_fput; |
| 7558 | } |
| 7559 | |
| 7560 | ctx->io_wq = ctx_attach->io_wq; |
| 7561 | out_fput: |
| 7562 | fdput(f); |
| 7563 | return ret; |
| 7564 | } |
| 7565 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7566 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 7567 | { |
| 7568 | struct io_uring_task *tctx; |
| 7569 | |
| 7570 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7571 | if (unlikely(!tctx)) |
| 7572 | return -ENOMEM; |
| 7573 | |
| 7574 | xa_init(&tctx->xa); |
| 7575 | init_waitqueue_head(&tctx->wait); |
| 7576 | tctx->last = NULL; |
| 7577 | tctx->in_idle = 0; |
| 7578 | atomic_long_set(&tctx->req_issue, 0); |
| 7579 | atomic_long_set(&tctx->req_complete, 0); |
| 7580 | task->io_uring = tctx; |
| 7581 | return 0; |
| 7582 | } |
| 7583 | |
| 7584 | void __io_uring_free(struct task_struct *tsk) |
| 7585 | { |
| 7586 | struct io_uring_task *tctx = tsk->io_uring; |
| 7587 | |
| 7588 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
| 7589 | xa_destroy(&tctx->xa); |
| 7590 | kfree(tctx); |
| 7591 | tsk->io_uring = NULL; |
| 7592 | } |
| 7593 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7594 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7595 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7596 | { |
| 7597 | int ret; |
| 7598 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7599 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7600 | ret = -EPERM; |
| 7601 | if (!capable(CAP_SYS_ADMIN)) |
| 7602 | goto err; |
| 7603 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7604 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7605 | if (!ctx->sq_thread_idle) |
| 7606 | ctx->sq_thread_idle = HZ; |
| 7607 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7608 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7609 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7610 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7611 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7612 | if (cpu >= nr_cpu_ids) |
| 7613 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7614 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7615 | goto err; |
| 7616 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7617 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 7618 | ctx, cpu, |
| 7619 | "io_uring-sq"); |
| 7620 | } else { |
| 7621 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 7622 | "io_uring-sq"); |
| 7623 | } |
| 7624 | if (IS_ERR(ctx->sqo_thread)) { |
| 7625 | ret = PTR_ERR(ctx->sqo_thread); |
| 7626 | ctx->sqo_thread = NULL; |
| 7627 | goto err; |
| 7628 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7629 | ret = io_uring_alloc_task_context(ctx->sqo_thread); |
| 7630 | if (ret) |
| 7631 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7632 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7633 | /* Can't have SQ_AFF without SQPOLL */ |
| 7634 | ret = -EINVAL; |
| 7635 | goto err; |
| 7636 | } |
| 7637 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7638 | ret = io_init_wq_offload(ctx, p); |
| 7639 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7640 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7641 | |
| 7642 | return 0; |
| 7643 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 7644 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7645 | return ret; |
| 7646 | } |
| 7647 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7648 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 7649 | { |
| 7650 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sqo_thread) |
| 7651 | wake_up_process(ctx->sqo_thread); |
| 7652 | } |
| 7653 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7654 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7655 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7656 | { |
| 7657 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7658 | } |
| 7659 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7660 | static inline int __io_account_mem(struct user_struct *user, |
| 7661 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7662 | { |
| 7663 | unsigned long page_limit, cur_pages, new_pages; |
| 7664 | |
| 7665 | /* Don't allow more pages than we can safely lock */ |
| 7666 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7667 | |
| 7668 | do { |
| 7669 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7670 | new_pages = cur_pages + nr_pages; |
| 7671 | if (new_pages > page_limit) |
| 7672 | return -ENOMEM; |
| 7673 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7674 | new_pages) != cur_pages); |
| 7675 | |
| 7676 | return 0; |
| 7677 | } |
| 7678 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7679 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7680 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7681 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 7682 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7683 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7684 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7685 | if (ctx->mm_account) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7686 | if (acct == ACCT_LOCKED) |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7687 | ctx->mm_account->locked_vm -= nr_pages; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7688 | else if (acct == ACCT_PINNED) |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7689 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7690 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7691 | } |
| 7692 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7693 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7694 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7695 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7696 | int ret; |
| 7697 | |
| 7698 | if (ctx->limit_mem) { |
| 7699 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7700 | if (ret) |
| 7701 | return ret; |
| 7702 | } |
| 7703 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7704 | if (ctx->mm_account) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7705 | if (acct == ACCT_LOCKED) |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7706 | ctx->mm_account->locked_vm += nr_pages; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7707 | else if (acct == ACCT_PINNED) |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 7708 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7709 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7710 | |
| 7711 | return 0; |
| 7712 | } |
| 7713 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7714 | static void io_mem_free(void *ptr) |
| 7715 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7716 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7717 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7718 | if (!ptr) |
| 7719 | return; |
| 7720 | |
| 7721 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7722 | if (put_page_testzero(page)) |
| 7723 | free_compound_page(page); |
| 7724 | } |
| 7725 | |
| 7726 | static void *io_mem_alloc(size_t size) |
| 7727 | { |
| 7728 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 7729 | __GFP_NORETRY; |
| 7730 | |
| 7731 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7732 | } |
| 7733 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7734 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7735 | size_t *sq_offset) |
| 7736 | { |
| 7737 | struct io_rings *rings; |
| 7738 | size_t off, sq_array_size; |
| 7739 | |
| 7740 | off = struct_size(rings, cqes, cq_entries); |
| 7741 | if (off == SIZE_MAX) |
| 7742 | return SIZE_MAX; |
| 7743 | |
| 7744 | #ifdef CONFIG_SMP |
| 7745 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 7746 | if (off == 0) |
| 7747 | return SIZE_MAX; |
| 7748 | #endif |
| 7749 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 7750 | if (sq_offset) |
| 7751 | *sq_offset = off; |
| 7752 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7753 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 7754 | if (sq_array_size == SIZE_MAX) |
| 7755 | return SIZE_MAX; |
| 7756 | |
| 7757 | if (check_add_overflow(off, sq_array_size, &off)) |
| 7758 | return SIZE_MAX; |
| 7759 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7760 | return off; |
| 7761 | } |
| 7762 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7763 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 7764 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7765 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7766 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7767 | pages = (size_t)1 << get_order( |
| 7768 | rings_size(sq_entries, cq_entries, NULL)); |
| 7769 | pages += (size_t)1 << get_order( |
| 7770 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7771 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7772 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7773 | } |
| 7774 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7775 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 7776 | { |
| 7777 | int i, j; |
| 7778 | |
| 7779 | if (!ctx->user_bufs) |
| 7780 | return -ENXIO; |
| 7781 | |
| 7782 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7783 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7784 | |
| 7785 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7786 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7787 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7788 | io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7789 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7790 | imu->nr_bvecs = 0; |
| 7791 | } |
| 7792 | |
| 7793 | kfree(ctx->user_bufs); |
| 7794 | ctx->user_bufs = NULL; |
| 7795 | ctx->nr_user_bufs = 0; |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
| 7799 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 7800 | void __user *arg, unsigned index) |
| 7801 | { |
| 7802 | struct iovec __user *src; |
| 7803 | |
| 7804 | #ifdef CONFIG_COMPAT |
| 7805 | if (ctx->compat) { |
| 7806 | struct compat_iovec __user *ciovs; |
| 7807 | struct compat_iovec ciov; |
| 7808 | |
| 7809 | ciovs = (struct compat_iovec __user *) arg; |
| 7810 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 7811 | return -EFAULT; |
| 7812 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 7813 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7814 | dst->iov_len = ciov.iov_len; |
| 7815 | return 0; |
| 7816 | } |
| 7817 | #endif |
| 7818 | src = (struct iovec __user *) arg; |
| 7819 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 7820 | return -EFAULT; |
| 7821 | return 0; |
| 7822 | } |
| 7823 | |
| 7824 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7825 | unsigned nr_args) |
| 7826 | { |
| 7827 | struct vm_area_struct **vmas = NULL; |
| 7828 | struct page **pages = NULL; |
| 7829 | int i, j, got_pages = 0; |
| 7830 | int ret = -EINVAL; |
| 7831 | |
| 7832 | if (ctx->user_bufs) |
| 7833 | return -EBUSY; |
| 7834 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 7835 | return -EINVAL; |
| 7836 | |
| 7837 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 7838 | GFP_KERNEL); |
| 7839 | if (!ctx->user_bufs) |
| 7840 | return -ENOMEM; |
| 7841 | |
| 7842 | for (i = 0; i < nr_args; i++) { |
| 7843 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7844 | unsigned long off, start, end, ubuf; |
| 7845 | int pret, nr_pages; |
| 7846 | struct iovec iov; |
| 7847 | size_t size; |
| 7848 | |
| 7849 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 7850 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 7851 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7852 | |
| 7853 | /* |
| 7854 | * Don't impose further limits on the size and buffer |
| 7855 | * constraints here, we'll -EINVAL later when IO is |
| 7856 | * submitted if they are wrong. |
| 7857 | */ |
| 7858 | ret = -EFAULT; |
| 7859 | if (!iov.iov_base || !iov.iov_len) |
| 7860 | goto err; |
| 7861 | |
| 7862 | /* arbitrary limit, but we need something */ |
| 7863 | if (iov.iov_len > SZ_1G) |
| 7864 | goto err; |
| 7865 | |
| 7866 | ubuf = (unsigned long) iov.iov_base; |
| 7867 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 7868 | start = ubuf >> PAGE_SHIFT; |
| 7869 | nr_pages = end - start; |
| 7870 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7871 | ret = io_account_mem(ctx, nr_pages, ACCT_PINNED); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7872 | if (ret) |
| 7873 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7874 | |
| 7875 | ret = 0; |
| 7876 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 7877 | kvfree(vmas); |
| 7878 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7879 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7880 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7881 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7882 | sizeof(struct vm_area_struct *), |
| 7883 | GFP_KERNEL); |
| 7884 | if (!pages || !vmas) { |
| 7885 | ret = -ENOMEM; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7886 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7887 | goto err; |
| 7888 | } |
| 7889 | got_pages = nr_pages; |
| 7890 | } |
| 7891 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7892 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7893 | GFP_KERNEL); |
| 7894 | ret = -ENOMEM; |
| 7895 | if (!imu->bvec) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7896 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7897 | goto err; |
| 7898 | } |
| 7899 | |
| 7900 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7901 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 7902 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 7903 | FOLL_WRITE | FOLL_LONGTERM, |
| 7904 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7905 | if (pret == nr_pages) { |
| 7906 | /* don't support file backed memory */ |
| 7907 | for (j = 0; j < nr_pages; j++) { |
| 7908 | struct vm_area_struct *vma = vmas[j]; |
| 7909 | |
| 7910 | if (vma->vm_file && |
| 7911 | !is_file_hugepages(vma->vm_file)) { |
| 7912 | ret = -EOPNOTSUPP; |
| 7913 | break; |
| 7914 | } |
| 7915 | } |
| 7916 | } else { |
| 7917 | ret = pret < 0 ? pret : -EFAULT; |
| 7918 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7919 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7920 | if (ret) { |
| 7921 | /* |
| 7922 | * if we did partial map, or found file backed vmas, |
| 7923 | * release any pages we did get |
| 7924 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 7925 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7926 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7927 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7928 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7929 | goto err; |
| 7930 | } |
| 7931 | |
| 7932 | off = ubuf & ~PAGE_MASK; |
| 7933 | size = iov.iov_len; |
| 7934 | for (j = 0; j < nr_pages; j++) { |
| 7935 | size_t vec_len; |
| 7936 | |
| 7937 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 7938 | imu->bvec[j].bv_page = pages[j]; |
| 7939 | imu->bvec[j].bv_len = vec_len; |
| 7940 | imu->bvec[j].bv_offset = off; |
| 7941 | off = 0; |
| 7942 | size -= vec_len; |
| 7943 | } |
| 7944 | /* store original address for later verification */ |
| 7945 | imu->ubuf = ubuf; |
| 7946 | imu->len = iov.iov_len; |
| 7947 | imu->nr_bvecs = nr_pages; |
| 7948 | |
| 7949 | ctx->nr_user_bufs++; |
| 7950 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7951 | kvfree(pages); |
| 7952 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7953 | return 0; |
| 7954 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7955 | kvfree(pages); |
| 7956 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7957 | io_sqe_buffer_unregister(ctx); |
| 7958 | return ret; |
| 7959 | } |
| 7960 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7961 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 7962 | { |
| 7963 | __s32 __user *fds = arg; |
| 7964 | int fd; |
| 7965 | |
| 7966 | if (ctx->cq_ev_fd) |
| 7967 | return -EBUSY; |
| 7968 | |
| 7969 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 7970 | return -EFAULT; |
| 7971 | |
| 7972 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 7973 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 7974 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 7975 | ctx->cq_ev_fd = NULL; |
| 7976 | return ret; |
| 7977 | } |
| 7978 | |
| 7979 | return 0; |
| 7980 | } |
| 7981 | |
| 7982 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 7983 | { |
| 7984 | if (ctx->cq_ev_fd) { |
| 7985 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 7986 | ctx->cq_ev_fd = NULL; |
| 7987 | return 0; |
| 7988 | } |
| 7989 | |
| 7990 | return -ENXIO; |
| 7991 | } |
| 7992 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7993 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 7994 | { |
| 7995 | struct io_ring_ctx *ctx = data; |
| 7996 | struct io_buffer *buf = p; |
| 7997 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 7998 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7999 | return 0; |
| 8000 | } |
| 8001 | |
| 8002 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8003 | { |
| 8004 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8005 | idr_destroy(&ctx->io_buffer_idr); |
| 8006 | } |
| 8007 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8008 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8009 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8010 | io_finish_async(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8011 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8012 | |
| 8013 | if (ctx->sqo_task) { |
| 8014 | put_task_struct(ctx->sqo_task); |
| 8015 | ctx->sqo_task = NULL; |
| 8016 | mmdrop(ctx->mm_account); |
| 8017 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8018 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8019 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8020 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8021 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8022 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8023 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8024 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8025 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8026 | if (ctx->ring_sock) { |
| 8027 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8028 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8029 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8030 | #endif |
| 8031 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8032 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8033 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8034 | |
| 8035 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8036 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8037 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8038 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 8039 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8040 | kfree(ctx); |
| 8041 | } |
| 8042 | |
| 8043 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8044 | { |
| 8045 | struct io_ring_ctx *ctx = file->private_data; |
| 8046 | __poll_t mask = 0; |
| 8047 | |
| 8048 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8049 | /* |
| 8050 | * synchronizes with barrier from wq_has_sleeper call in |
| 8051 | * io_commit_cqring |
| 8052 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8053 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8054 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 8055 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8056 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 8057 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8058 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8059 | |
| 8060 | return mask; |
| 8061 | } |
| 8062 | |
| 8063 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8064 | { |
| 8065 | struct io_ring_ctx *ctx = file->private_data; |
| 8066 | |
| 8067 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8068 | } |
| 8069 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8070 | static int io_remove_personalities(int id, void *p, void *data) |
| 8071 | { |
| 8072 | struct io_ring_ctx *ctx = data; |
| 8073 | const struct cred *cred; |
| 8074 | |
| 8075 | cred = idr_remove(&ctx->personality_idr, id); |
| 8076 | if (cred) |
| 8077 | put_cred(cred); |
| 8078 | return 0; |
| 8079 | } |
| 8080 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8081 | static void io_ring_exit_work(struct work_struct *work) |
| 8082 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8083 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8084 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8085 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8086 | /* |
| 8087 | * If we're doing polled IO and end up having requests being |
| 8088 | * submitted async (out-of-line), then completions can come in while |
| 8089 | * we're waiting for refs to drop. We need to reap these manually, |
| 8090 | * as nobody else will be looking for them. |
| 8091 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8092 | do { |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8093 | if (ctx->rings) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 8094 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8095 | io_iopoll_try_reap_events(ctx); |
| 8096 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8097 | io_ring_ctx_free(ctx); |
| 8098 | } |
| 8099 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8100 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8101 | { |
| 8102 | mutex_lock(&ctx->uring_lock); |
| 8103 | percpu_ref_kill(&ctx->refs); |
| 8104 | mutex_unlock(&ctx->uring_lock); |
| 8105 | |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 8106 | io_kill_timeouts(ctx, NULL); |
| 8107 | io_poll_remove_all(ctx, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8108 | |
| 8109 | if (ctx->io_wq) |
| 8110 | io_wq_cancel_all(ctx->io_wq); |
| 8111 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8112 | /* if we failed setting up the ctx, we might not have any rings */ |
| 8113 | if (ctx->rings) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 8114 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8115 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8116 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8117 | |
| 8118 | /* |
| 8119 | * Do this upfront, so we won't have a grace period where the ring |
| 8120 | * is closed but resources aren't reaped yet. This can cause |
| 8121 | * spurious failure in setting up a new ring. |
| 8122 | */ |
Jens Axboe | 760618f | 2020-07-24 12:53:31 -0600 | [diff] [blame] | 8123 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 8124 | ACCT_LOCKED); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8125 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8126 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8127 | /* |
| 8128 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8129 | * if we're exiting a ton of rings at the same time. It just adds |
| 8130 | * noise and overhead, there's no discernable change in runtime |
| 8131 | * over using system_wq. |
| 8132 | */ |
| 8133 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8134 | } |
| 8135 | |
| 8136 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8137 | { |
| 8138 | struct io_ring_ctx *ctx = file->private_data; |
| 8139 | |
| 8140 | file->private_data = NULL; |
| 8141 | io_ring_ctx_wait_and_kill(ctx); |
| 8142 | return 0; |
| 8143 | } |
| 8144 | |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8145 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
| 8146 | { |
| 8147 | struct files_struct *files = data; |
| 8148 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8149 | return !files || work->files == files; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8150 | } |
| 8151 | |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 8152 | /* |
| 8153 | * Returns true if 'preq' is the link parent of 'req' |
| 8154 | */ |
| 8155 | static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req) |
| 8156 | { |
| 8157 | struct io_kiocb *link; |
| 8158 | |
| 8159 | if (!(preq->flags & REQ_F_LINK_HEAD)) |
| 8160 | return false; |
| 8161 | |
| 8162 | list_for_each_entry(link, &preq->link_list, link_list) { |
| 8163 | if (link == req) |
| 8164 | return true; |
| 8165 | } |
| 8166 | |
| 8167 | return false; |
| 8168 | } |
| 8169 | |
Pavel Begunkov | c127a2a | 2020-09-06 00:45:15 +0300 | [diff] [blame] | 8170 | static bool io_match_link_files(struct io_kiocb *req, |
| 8171 | struct files_struct *files) |
| 8172 | { |
| 8173 | struct io_kiocb *link; |
| 8174 | |
| 8175 | if (io_match_files(req, files)) |
| 8176 | return true; |
| 8177 | if (req->flags & REQ_F_LINK_HEAD) { |
| 8178 | list_for_each_entry(link, &req->link_list, link_list) { |
| 8179 | if (io_match_files(link, files)) |
| 8180 | return true; |
| 8181 | } |
| 8182 | } |
| 8183 | return false; |
| 8184 | } |
| 8185 | |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 8186 | /* |
| 8187 | * We're looking to cancel 'req' because it's holding on to our files, but |
| 8188 | * 'req' could be a link to another request. See if it is, and cancel that |
| 8189 | * parent request if so. |
| 8190 | */ |
| 8191 | static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req) |
| 8192 | { |
| 8193 | struct hlist_node *tmp; |
| 8194 | struct io_kiocb *preq; |
| 8195 | bool found = false; |
| 8196 | int i; |
| 8197 | |
| 8198 | spin_lock_irq(&ctx->completion_lock); |
| 8199 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 8200 | struct hlist_head *list; |
| 8201 | |
| 8202 | list = &ctx->cancel_hash[i]; |
| 8203 | hlist_for_each_entry_safe(preq, tmp, list, hash_node) { |
| 8204 | found = io_match_link(preq, req); |
| 8205 | if (found) { |
| 8206 | io_poll_remove_one(preq); |
| 8207 | break; |
| 8208 | } |
| 8209 | } |
| 8210 | } |
| 8211 | spin_unlock_irq(&ctx->completion_lock); |
| 8212 | return found; |
| 8213 | } |
| 8214 | |
| 8215 | static bool io_timeout_remove_link(struct io_ring_ctx *ctx, |
| 8216 | struct io_kiocb *req) |
| 8217 | { |
| 8218 | struct io_kiocb *preq; |
| 8219 | bool found = false; |
| 8220 | |
| 8221 | spin_lock_irq(&ctx->completion_lock); |
| 8222 | list_for_each_entry(preq, &ctx->timeout_list, timeout.list) { |
| 8223 | found = io_match_link(preq, req); |
| 8224 | if (found) { |
| 8225 | __io_timeout_cancel(preq); |
| 8226 | break; |
| 8227 | } |
| 8228 | } |
| 8229 | spin_unlock_irq(&ctx->completion_lock); |
| 8230 | return found; |
| 8231 | } |
| 8232 | |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8233 | static bool io_cancel_link_cb(struct io_wq_work *work, void *data) |
| 8234 | { |
| 8235 | return io_match_link(container_of(work, struct io_kiocb, work), data); |
| 8236 | } |
| 8237 | |
| 8238 | static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req) |
| 8239 | { |
| 8240 | enum io_wq_cancel cret; |
| 8241 | |
| 8242 | /* cancel this particular work, if it's running */ |
| 8243 | cret = io_wq_cancel_work(ctx->io_wq, &req->work); |
| 8244 | if (cret != IO_WQ_CANCEL_NOTFOUND) |
| 8245 | return; |
| 8246 | |
| 8247 | /* find links that hold this pending, cancel those */ |
| 8248 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true); |
| 8249 | if (cret != IO_WQ_CANCEL_NOTFOUND) |
| 8250 | return; |
| 8251 | |
| 8252 | /* if we have a poll link holding this pending, cancel that */ |
| 8253 | if (io_poll_remove_link(ctx, req)) |
| 8254 | return; |
| 8255 | |
| 8256 | /* final option, timeout link is holding this req pending */ |
| 8257 | io_timeout_remove_link(ctx, req); |
| 8258 | } |
| 8259 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8260 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
| 8261 | struct files_struct *files) |
| 8262 | { |
| 8263 | struct io_defer_entry *de = NULL; |
| 8264 | LIST_HEAD(list); |
| 8265 | |
| 8266 | spin_lock_irq(&ctx->completion_lock); |
| 8267 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | c127a2a | 2020-09-06 00:45:15 +0300 | [diff] [blame] | 8268 | if (io_match_link_files(de->req, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8269 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8270 | break; |
| 8271 | } |
| 8272 | } |
| 8273 | spin_unlock_irq(&ctx->completion_lock); |
| 8274 | |
| 8275 | while (!list_empty(&list)) { |
| 8276 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8277 | list_del_init(&de->list); |
| 8278 | req_set_fail_links(de->req); |
| 8279 | io_put_req(de->req); |
| 8280 | io_req_complete(de->req, -ECANCELED); |
| 8281 | kfree(de); |
| 8282 | } |
| 8283 | } |
| 8284 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 8285 | /* |
| 8286 | * Returns true if we found and killed one or more files pinning requests |
| 8287 | */ |
| 8288 | static bool io_uring_cancel_files(struct io_ring_ctx *ctx, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8289 | struct files_struct *files) |
| 8290 | { |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8291 | if (list_empty_careful(&ctx->inflight_list)) |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 8292 | return false; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8293 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8294 | io_cancel_defer_files(ctx, files); |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8295 | /* cancel all at once, should be faster than doing it one by one*/ |
| 8296 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); |
| 8297 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8298 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8299 | struct io_kiocb *cancel_req = NULL, *req; |
| 8300 | DEFINE_WAIT(wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8301 | |
| 8302 | spin_lock_irq(&ctx->inflight_lock); |
| 8303 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8304 | if (files && req->work.files != files) |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8305 | continue; |
| 8306 | /* req is being completed, ignore */ |
| 8307 | if (!refcount_inc_not_zero(&req->refs)) |
| 8308 | continue; |
| 8309 | cancel_req = req; |
| 8310 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8311 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8312 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8313 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8314 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8315 | spin_unlock_irq(&ctx->inflight_lock); |
| 8316 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8317 | /* We need to keep going until we don't find a matching req */ |
| 8318 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8319 | break; |
Pavel Begunkov | bb17534 | 2020-08-20 11:33:35 +0300 | [diff] [blame] | 8320 | /* cancel this request, or head link requests */ |
| 8321 | io_attempt_cancel(ctx, cancel_req); |
| 8322 | io_put_req(cancel_req); |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 8323 | /* cancellations _may_ trigger task work */ |
| 8324 | io_run_task_work(); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8325 | schedule(); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8326 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8327 | } |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 8328 | |
| 8329 | return true; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8330 | } |
| 8331 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 8332 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8333 | { |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 8334 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8335 | struct task_struct *task = data; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8336 | |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 8337 | return io_task_match(req, task); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8338 | } |
| 8339 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8340 | static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8341 | struct task_struct *task, |
| 8342 | struct files_struct *files) |
| 8343 | { |
| 8344 | bool ret; |
| 8345 | |
| 8346 | ret = io_uring_cancel_files(ctx, files); |
| 8347 | if (!files) { |
| 8348 | enum io_wq_cancel cret; |
| 8349 | |
| 8350 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true); |
| 8351 | if (cret != IO_WQ_CANCEL_NOTFOUND) |
| 8352 | ret = true; |
| 8353 | |
| 8354 | /* SQPOLL thread does its own polling */ |
| 8355 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { |
| 8356 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8357 | io_iopoll_try_reap_events(ctx); |
| 8358 | ret = true; |
| 8359 | } |
| 8360 | } |
| 8361 | |
| 8362 | ret |= io_poll_remove_all(ctx, task); |
| 8363 | ret |= io_kill_timeouts(ctx, task); |
| 8364 | } |
| 8365 | |
| 8366 | return ret; |
| 8367 | } |
| 8368 | |
| 8369 | /* |
| 8370 | * We need to iteratively cancel requests, in case a request has dependent |
| 8371 | * hard links. These persist even for failure of cancelations, hence keep |
| 8372 | * looping until none are found. |
| 8373 | */ |
| 8374 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8375 | struct files_struct *files) |
| 8376 | { |
| 8377 | struct task_struct *task = current; |
| 8378 | |
| 8379 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 8380 | task = ctx->sqo_thread; |
| 8381 | |
| 8382 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8383 | |
| 8384 | while (__io_uring_cancel_task_requests(ctx, task, files)) { |
| 8385 | io_run_task_work(); |
| 8386 | cond_resched(); |
| 8387 | } |
| 8388 | } |
| 8389 | |
| 8390 | /* |
| 8391 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8392 | */ |
| 8393 | static int io_uring_add_task_file(struct file *file) |
| 8394 | { |
| 8395 | if (unlikely(!current->io_uring)) { |
| 8396 | int ret; |
| 8397 | |
| 8398 | ret = io_uring_alloc_task_context(current); |
| 8399 | if (unlikely(ret)) |
| 8400 | return ret; |
| 8401 | } |
| 8402 | if (current->io_uring->last != file) { |
| 8403 | XA_STATE(xas, ¤t->io_uring->xa, (unsigned long) file); |
| 8404 | void *old; |
| 8405 | |
| 8406 | rcu_read_lock(); |
| 8407 | old = xas_load(&xas); |
| 8408 | if (old != file) { |
| 8409 | get_file(file); |
| 8410 | xas_lock(&xas); |
| 8411 | xas_store(&xas, file); |
| 8412 | xas_unlock(&xas); |
| 8413 | } |
| 8414 | rcu_read_unlock(); |
| 8415 | current->io_uring->last = file; |
| 8416 | } |
| 8417 | |
| 8418 | return 0; |
| 8419 | } |
| 8420 | |
| 8421 | /* |
| 8422 | * Remove this io_uring_file -> task mapping. |
| 8423 | */ |
| 8424 | static void io_uring_del_task_file(struct file *file) |
| 8425 | { |
| 8426 | struct io_uring_task *tctx = current->io_uring; |
| 8427 | XA_STATE(xas, &tctx->xa, (unsigned long) file); |
| 8428 | |
| 8429 | if (tctx->last == file) |
| 8430 | tctx->last = NULL; |
| 8431 | |
| 8432 | xas_lock(&xas); |
| 8433 | file = xas_store(&xas, NULL); |
| 8434 | xas_unlock(&xas); |
| 8435 | |
| 8436 | if (file) |
| 8437 | fput(file); |
| 8438 | } |
| 8439 | |
| 8440 | static void __io_uring_attempt_task_drop(struct file *file) |
| 8441 | { |
| 8442 | XA_STATE(xas, ¤t->io_uring->xa, (unsigned long) file); |
| 8443 | struct file *old; |
| 8444 | |
| 8445 | rcu_read_lock(); |
| 8446 | old = xas_load(&xas); |
| 8447 | rcu_read_unlock(); |
| 8448 | |
| 8449 | if (old == file) |
| 8450 | io_uring_del_task_file(file); |
| 8451 | } |
| 8452 | |
| 8453 | /* |
| 8454 | * Drop task note for this file if we're the only ones that hold it after |
| 8455 | * pending fput() |
| 8456 | */ |
| 8457 | static void io_uring_attempt_task_drop(struct file *file, bool exiting) |
| 8458 | { |
| 8459 | if (!current->io_uring) |
| 8460 | return; |
| 8461 | /* |
| 8462 | * fput() is pending, will be 2 if the only other ref is our potential |
| 8463 | * task file note. If the task is exiting, drop regardless of count. |
| 8464 | */ |
| 8465 | if (!exiting && atomic_long_read(&file->f_count) != 2) |
| 8466 | return; |
| 8467 | |
| 8468 | __io_uring_attempt_task_drop(file); |
| 8469 | } |
| 8470 | |
| 8471 | void __io_uring_files_cancel(struct files_struct *files) |
| 8472 | { |
| 8473 | struct io_uring_task *tctx = current->io_uring; |
| 8474 | XA_STATE(xas, &tctx->xa, 0); |
| 8475 | |
| 8476 | /* make sure overflow events are dropped */ |
| 8477 | tctx->in_idle = true; |
| 8478 | |
| 8479 | do { |
| 8480 | struct io_ring_ctx *ctx; |
| 8481 | struct file *file; |
| 8482 | |
| 8483 | xas_lock(&xas); |
| 8484 | file = xas_next_entry(&xas, ULONG_MAX); |
| 8485 | xas_unlock(&xas); |
| 8486 | |
| 8487 | if (!file) |
| 8488 | break; |
| 8489 | |
| 8490 | ctx = file->private_data; |
| 8491 | |
| 8492 | io_uring_cancel_task_requests(ctx, files); |
| 8493 | if (files) |
| 8494 | io_uring_del_task_file(file); |
| 8495 | } while (1); |
| 8496 | } |
| 8497 | |
| 8498 | static inline bool io_uring_task_idle(struct io_uring_task *tctx) |
| 8499 | { |
| 8500 | return atomic_long_read(&tctx->req_issue) == |
| 8501 | atomic_long_read(&tctx->req_complete); |
| 8502 | } |
| 8503 | |
| 8504 | /* |
| 8505 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8506 | * requests. |
| 8507 | */ |
| 8508 | void __io_uring_task_cancel(void) |
| 8509 | { |
| 8510 | struct io_uring_task *tctx = current->io_uring; |
| 8511 | DEFINE_WAIT(wait); |
| 8512 | long completions; |
| 8513 | |
| 8514 | /* make sure overflow events are dropped */ |
| 8515 | tctx->in_idle = true; |
| 8516 | |
| 8517 | while (!io_uring_task_idle(tctx)) { |
| 8518 | /* read completions before cancelations */ |
| 8519 | completions = atomic_long_read(&tctx->req_complete); |
| 8520 | __io_uring_files_cancel(NULL); |
| 8521 | |
| 8522 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8523 | |
| 8524 | /* |
| 8525 | * If we've seen completions, retry. This avoids a race where |
| 8526 | * a completion comes in before we did prepare_to_wait(). |
| 8527 | */ |
| 8528 | if (completions != atomic_long_read(&tctx->req_complete)) |
| 8529 | continue; |
| 8530 | if (io_uring_task_idle(tctx)) |
| 8531 | break; |
| 8532 | schedule(); |
| 8533 | } |
| 8534 | |
| 8535 | finish_wait(&tctx->wait, &wait); |
| 8536 | tctx->in_idle = false; |
| 8537 | } |
| 8538 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8539 | static int io_uring_flush(struct file *file, void *data) |
| 8540 | { |
| 8541 | struct io_ring_ctx *ctx = file->private_data; |
| 8542 | |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 8543 | /* |
| 8544 | * If the task is going away, cancel work it may have pending |
| 8545 | */ |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 8546 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8547 | data = NULL; |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 8548 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8549 | io_uring_cancel_task_requests(ctx, data); |
| 8550 | io_uring_attempt_task_drop(file, !data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8551 | return 0; |
| 8552 | } |
| 8553 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8554 | static void *io_uring_validate_mmap_request(struct file *file, |
| 8555 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8556 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8557 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8558 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8559 | struct page *page; |
| 8560 | void *ptr; |
| 8561 | |
| 8562 | switch (offset) { |
| 8563 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8564 | case IORING_OFF_CQ_RING: |
| 8565 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8566 | break; |
| 8567 | case IORING_OFF_SQES: |
| 8568 | ptr = ctx->sq_sqes; |
| 8569 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8570 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8571 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8572 | } |
| 8573 | |
| 8574 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 8575 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8576 | return ERR_PTR(-EINVAL); |
| 8577 | |
| 8578 | return ptr; |
| 8579 | } |
| 8580 | |
| 8581 | #ifdef CONFIG_MMU |
| 8582 | |
| 8583 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8584 | { |
| 8585 | size_t sz = vma->vm_end - vma->vm_start; |
| 8586 | unsigned long pfn; |
| 8587 | void *ptr; |
| 8588 | |
| 8589 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 8590 | if (IS_ERR(ptr)) |
| 8591 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8592 | |
| 8593 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 8594 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 8595 | } |
| 8596 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8597 | #else /* !CONFIG_MMU */ |
| 8598 | |
| 8599 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8600 | { |
| 8601 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 8602 | } |
| 8603 | |
| 8604 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 8605 | { |
| 8606 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 8607 | } |
| 8608 | |
| 8609 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 8610 | unsigned long addr, unsigned long len, |
| 8611 | unsigned long pgoff, unsigned long flags) |
| 8612 | { |
| 8613 | void *ptr; |
| 8614 | |
| 8615 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 8616 | if (IS_ERR(ptr)) |
| 8617 | return PTR_ERR(ptr); |
| 8618 | |
| 8619 | return (unsigned long) ptr; |
| 8620 | } |
| 8621 | |
| 8622 | #endif /* !CONFIG_MMU */ |
| 8623 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8624 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 8625 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 8626 | size_t, sigsz) |
| 8627 | { |
| 8628 | struct io_ring_ctx *ctx; |
| 8629 | long ret = -EBADF; |
| 8630 | int submitted = 0; |
| 8631 | struct fd f; |
| 8632 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 8633 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 8634 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8635 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8636 | return -EINVAL; |
| 8637 | |
| 8638 | f = fdget(fd); |
| 8639 | if (!f.file) |
| 8640 | return -EBADF; |
| 8641 | |
| 8642 | ret = -EOPNOTSUPP; |
| 8643 | if (f.file->f_op != &io_uring_fops) |
| 8644 | goto out_fput; |
| 8645 | |
| 8646 | ret = -ENXIO; |
| 8647 | ctx = f.file->private_data; |
| 8648 | if (!percpu_ref_tryget(&ctx->refs)) |
| 8649 | goto out_fput; |
| 8650 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8651 | ret = -EBADFD; |
| 8652 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 8653 | goto out; |
| 8654 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8655 | /* |
| 8656 | * For SQ polling, the thread will do all submissions and completions. |
| 8657 | * Just return the requested submit count, and wake the thread if |
| 8658 | * we were asked to. |
| 8659 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 8660 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8661 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 8662 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 8663 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8664 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 6a77938 | 2020-09-02 12:21:41 -0600 | [diff] [blame^] | 8665 | wake_up(ctx->sqo_wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8666 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 8667 | } else if (to_submit) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8668 | ret = io_uring_add_task_file(f.file); |
| 8669 | if (unlikely(ret)) |
| 8670 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8671 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8672 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8673 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 8674 | |
| 8675 | if (submitted != to_submit) |
| 8676 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8677 | } |
| 8678 | if (flags & IORING_ENTER_GETEVENTS) { |
| 8679 | min_complete = min(min_complete, ctx->cq_entries); |
| 8680 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 8681 | /* |
| 8682 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 8683 | * space applications don't need to do io completion events |
| 8684 | * polling again, they can rely on io_sq_thread to do polling |
| 8685 | * work, which can reduce cpu usage and uring_lock contention. |
| 8686 | */ |
| 8687 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 8688 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 8689 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8690 | } else { |
| 8691 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 8692 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8693 | } |
| 8694 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 8695 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 8696 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8697 | out_fput: |
| 8698 | fdput(f); |
| 8699 | return submitted ? submitted : ret; |
| 8700 | } |
| 8701 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8702 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8703 | static int io_uring_show_cred(int id, void *p, void *data) |
| 8704 | { |
| 8705 | const struct cred *cred = p; |
| 8706 | struct seq_file *m = data; |
| 8707 | struct user_namespace *uns = seq_user_ns(m); |
| 8708 | struct group_info *gi; |
| 8709 | kernel_cap_t cap; |
| 8710 | unsigned __capi; |
| 8711 | int g; |
| 8712 | |
| 8713 | seq_printf(m, "%5d\n", id); |
| 8714 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 8715 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 8716 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 8717 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 8718 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 8719 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 8720 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 8721 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 8722 | seq_puts(m, "\n\tGroups:\t"); |
| 8723 | gi = cred->group_info; |
| 8724 | for (g = 0; g < gi->ngroups; g++) { |
| 8725 | seq_put_decimal_ull(m, g ? " " : "", |
| 8726 | from_kgid_munged(uns, gi->gid[g])); |
| 8727 | } |
| 8728 | seq_puts(m, "\n\tCapEff:\t"); |
| 8729 | cap = cred->cap_effective; |
| 8730 | CAP_FOR_EACH_U32(__capi) |
| 8731 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 8732 | seq_putc(m, '\n'); |
| 8733 | return 0; |
| 8734 | } |
| 8735 | |
| 8736 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 8737 | { |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8738 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8739 | int i; |
| 8740 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8741 | /* |
| 8742 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 8743 | * since fdinfo case grabs it in the opposite direction of normal use |
| 8744 | * cases. If we fail to get the lock, we just don't iterate any |
| 8745 | * structures that could be going away outside the io_uring mutex. |
| 8746 | */ |
| 8747 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 8748 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8749 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8750 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8751 | struct fixed_file_table *table; |
| 8752 | struct file *f; |
| 8753 | |
| 8754 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 8755 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 8756 | if (f) |
| 8757 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 8758 | else |
| 8759 | seq_printf(m, "%5u: <none>\n", i); |
| 8760 | } |
| 8761 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8762 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8763 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 8764 | |
| 8765 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 8766 | (unsigned int) buf->len); |
| 8767 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8768 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8769 | seq_printf(m, "Personalities:\n"); |
| 8770 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 8771 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 8772 | seq_printf(m, "PollList:\n"); |
| 8773 | spin_lock_irq(&ctx->completion_lock); |
| 8774 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 8775 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 8776 | struct io_kiocb *req; |
| 8777 | |
| 8778 | hlist_for_each_entry(req, list, hash_node) |
| 8779 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 8780 | req->task->task_works != NULL); |
| 8781 | } |
| 8782 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 8783 | if (has_lock) |
| 8784 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8785 | } |
| 8786 | |
| 8787 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 8788 | { |
| 8789 | struct io_ring_ctx *ctx = f->private_data; |
| 8790 | |
| 8791 | if (percpu_ref_tryget(&ctx->refs)) { |
| 8792 | __io_uring_show_fdinfo(ctx, m); |
| 8793 | percpu_ref_put(&ctx->refs); |
| 8794 | } |
| 8795 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8796 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8797 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8798 | static const struct file_operations io_uring_fops = { |
| 8799 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8800 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8801 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8802 | #ifndef CONFIG_MMU |
| 8803 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 8804 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 8805 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8806 | .poll = io_uring_poll, |
| 8807 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8808 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8809 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8810 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8811 | }; |
| 8812 | |
| 8813 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 8814 | struct io_uring_params *p) |
| 8815 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8816 | struct io_rings *rings; |
| 8817 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8818 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 8819 | /* make sure these are sane, as we already accounted them */ |
| 8820 | ctx->sq_entries = p->sq_entries; |
| 8821 | ctx->cq_entries = p->cq_entries; |
| 8822 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8823 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 8824 | if (size == SIZE_MAX) |
| 8825 | return -EOVERFLOW; |
| 8826 | |
| 8827 | rings = io_mem_alloc(size); |
| 8828 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8829 | return -ENOMEM; |
| 8830 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8831 | ctx->rings = rings; |
| 8832 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 8833 | rings->sq_ring_mask = p->sq_entries - 1; |
| 8834 | rings->cq_ring_mask = p->cq_entries - 1; |
| 8835 | rings->sq_ring_entries = p->sq_entries; |
| 8836 | rings->cq_ring_entries = p->cq_entries; |
| 8837 | ctx->sq_mask = rings->sq_ring_mask; |
| 8838 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8839 | |
| 8840 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8841 | if (size == SIZE_MAX) { |
| 8842 | io_mem_free(ctx->rings); |
| 8843 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8844 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8845 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8846 | |
| 8847 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8848 | if (!ctx->sq_sqes) { |
| 8849 | io_mem_free(ctx->rings); |
| 8850 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8851 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8852 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8853 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8854 | return 0; |
| 8855 | } |
| 8856 | |
| 8857 | /* |
| 8858 | * Allocate an anonymous fd, this is what constitutes the application |
| 8859 | * visible backing of an io_uring instance. The application mmaps this |
| 8860 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 8861 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 8862 | */ |
| 8863 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 8864 | { |
| 8865 | struct file *file; |
| 8866 | int ret; |
| 8867 | |
| 8868 | #if defined(CONFIG_UNIX) |
| 8869 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 8870 | &ctx->ring_sock); |
| 8871 | if (ret) |
| 8872 | return ret; |
| 8873 | #endif |
| 8874 | |
| 8875 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 8876 | if (ret < 0) |
| 8877 | goto err; |
| 8878 | |
| 8879 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 8880 | O_RDWR | O_CLOEXEC); |
| 8881 | if (IS_ERR(file)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8882 | err_fd: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8883 | put_unused_fd(ret); |
| 8884 | ret = PTR_ERR(file); |
| 8885 | goto err; |
| 8886 | } |
| 8887 | |
| 8888 | #if defined(CONFIG_UNIX) |
| 8889 | ctx->ring_sock->file = file; |
| 8890 | #endif |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8891 | if (unlikely(io_uring_add_task_file(file))) { |
| 8892 | file = ERR_PTR(-ENOMEM); |
| 8893 | goto err_fd; |
| 8894 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8895 | fd_install(ret, file); |
| 8896 | return ret; |
| 8897 | err: |
| 8898 | #if defined(CONFIG_UNIX) |
| 8899 | sock_release(ctx->ring_sock); |
| 8900 | ctx->ring_sock = NULL; |
| 8901 | #endif |
| 8902 | return ret; |
| 8903 | } |
| 8904 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8905 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 8906 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8907 | { |
| 8908 | struct user_struct *user = NULL; |
| 8909 | struct io_ring_ctx *ctx; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8910 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8911 | int ret; |
| 8912 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8913 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8914 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8915 | if (entries > IORING_MAX_ENTRIES) { |
| 8916 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8917 | return -EINVAL; |
| 8918 | entries = IORING_MAX_ENTRIES; |
| 8919 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8920 | |
| 8921 | /* |
| 8922 | * Use twice as many entries for the CQ ring. It's possible for the |
| 8923 | * application to drive a higher depth than the size of the SQ ring, |
| 8924 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8925 | * some flexibility in overcommitting a bit. If the application has |
| 8926 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 8927 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8928 | */ |
| 8929 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8930 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 8931 | /* |
| 8932 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 8933 | * to a power-of-two, if it isn't already. We do NOT impose |
| 8934 | * any cq vs sq ring sizing. |
| 8935 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8936 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8937 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8938 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 8939 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8940 | return -EINVAL; |
| 8941 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 8942 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8943 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 8944 | } else { |
| 8945 | p->cq_entries = 2 * p->sq_entries; |
| 8946 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8947 | |
| 8948 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8949 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8950 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8951 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8952 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8953 | ring_pages(p->sq_entries, p->cq_entries)); |
| 8954 | if (ret) { |
| 8955 | free_uid(user); |
| 8956 | return ret; |
| 8957 | } |
| 8958 | } |
| 8959 | |
| 8960 | ctx = io_ring_ctx_alloc(p); |
| 8961 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8962 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8963 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8964 | p->cq_entries)); |
| 8965 | free_uid(user); |
| 8966 | return -ENOMEM; |
| 8967 | } |
| 8968 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8969 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 8970 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8971 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8972 | ctx->sqo_task = get_task_struct(current); |
| 8973 | |
| 8974 | /* |
| 8975 | * This is just grabbed for accounting purposes. When a process exits, |
| 8976 | * the mm is exited and dropped before the files, hence we need to hang |
| 8977 | * on to this mm purely for the purposes of being able to unaccount |
| 8978 | * memory (locked/pinned vm). It's not used for anything else. |
| 8979 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 8980 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8981 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 8982 | |
Jens Axboe | f74441e | 2020-08-05 13:00:44 -0600 | [diff] [blame] | 8983 | /* |
| 8984 | * Account memory _before_ installing the file descriptor. Once |
| 8985 | * the descriptor is installed, it can get closed at any time. Also |
| 8986 | * do this before hitting the general error path, as ring freeing |
| 8987 | * will un-account as well. |
| 8988 | */ |
| 8989 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 8990 | ACCT_LOCKED); |
| 8991 | ctx->limit_mem = limit_mem; |
| 8992 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8993 | ret = io_allocate_scq_urings(ctx, p); |
| 8994 | if (ret) |
| 8995 | goto err; |
| 8996 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8997 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8998 | if (ret) |
| 8999 | goto err; |
| 9000 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9001 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9002 | io_sq_offload_start(ctx); |
| 9003 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9004 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9005 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9006 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9007 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9008 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9009 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9010 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9011 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9012 | |
| 9013 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9014 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9015 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9016 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9017 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9018 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9019 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9020 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9021 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9022 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9023 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9024 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
| 9025 | IORING_FEAT_POLL_32BITS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9026 | |
| 9027 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9028 | ret = -EFAULT; |
| 9029 | goto err; |
| 9030 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9031 | |
| 9032 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9033 | * Install ring fd as the very last thing, so we don't risk someone |
| 9034 | * having closed it before we finish setup |
| 9035 | */ |
| 9036 | ret = io_uring_get_fd(ctx); |
| 9037 | if (ret < 0) |
| 9038 | goto err; |
| 9039 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9040 | 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] | 9041 | return ret; |
| 9042 | err: |
| 9043 | io_ring_ctx_wait_and_kill(ctx); |
| 9044 | return ret; |
| 9045 | } |
| 9046 | |
| 9047 | /* |
| 9048 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9049 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9050 | * params structure passed in. |
| 9051 | */ |
| 9052 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9053 | { |
| 9054 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9055 | int i; |
| 9056 | |
| 9057 | if (copy_from_user(&p, params, sizeof(p))) |
| 9058 | return -EFAULT; |
| 9059 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9060 | if (p.resv[i]) |
| 9061 | return -EINVAL; |
| 9062 | } |
| 9063 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9064 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9065 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9066 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9067 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9068 | return -EINVAL; |
| 9069 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9070 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9071 | } |
| 9072 | |
| 9073 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9074 | struct io_uring_params __user *, params) |
| 9075 | { |
| 9076 | return io_uring_setup(entries, params); |
| 9077 | } |
| 9078 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9079 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9080 | { |
| 9081 | struct io_uring_probe *p; |
| 9082 | size_t size; |
| 9083 | int i, ret; |
| 9084 | |
| 9085 | size = struct_size(p, ops, nr_args); |
| 9086 | if (size == SIZE_MAX) |
| 9087 | return -EOVERFLOW; |
| 9088 | p = kzalloc(size, GFP_KERNEL); |
| 9089 | if (!p) |
| 9090 | return -ENOMEM; |
| 9091 | |
| 9092 | ret = -EFAULT; |
| 9093 | if (copy_from_user(p, arg, size)) |
| 9094 | goto out; |
| 9095 | ret = -EINVAL; |
| 9096 | if (memchr_inv(p, 0, size)) |
| 9097 | goto out; |
| 9098 | |
| 9099 | p->last_op = IORING_OP_LAST - 1; |
| 9100 | if (nr_args > IORING_OP_LAST) |
| 9101 | nr_args = IORING_OP_LAST; |
| 9102 | |
| 9103 | for (i = 0; i < nr_args; i++) { |
| 9104 | p->ops[i].op = i; |
| 9105 | if (!io_op_defs[i].not_supported) |
| 9106 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9107 | } |
| 9108 | p->ops_len = i; |
| 9109 | |
| 9110 | ret = 0; |
| 9111 | if (copy_to_user(arg, p, size)) |
| 9112 | ret = -EFAULT; |
| 9113 | out: |
| 9114 | kfree(p); |
| 9115 | return ret; |
| 9116 | } |
| 9117 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9118 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9119 | { |
| 9120 | const struct cred *creds = get_current_cred(); |
| 9121 | int id; |
| 9122 | |
| 9123 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 9124 | USHRT_MAX, GFP_KERNEL); |
| 9125 | if (id < 0) |
| 9126 | put_cred(creds); |
| 9127 | return id; |
| 9128 | } |
| 9129 | |
| 9130 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 9131 | { |
| 9132 | const struct cred *old_creds; |
| 9133 | |
| 9134 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 9135 | if (old_creds) { |
| 9136 | put_cred(old_creds); |
| 9137 | return 0; |
| 9138 | } |
| 9139 | |
| 9140 | return -EINVAL; |
| 9141 | } |
| 9142 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9143 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9144 | unsigned int nr_args) |
| 9145 | { |
| 9146 | struct io_uring_restriction *res; |
| 9147 | size_t size; |
| 9148 | int i, ret; |
| 9149 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9150 | /* Restrictions allowed only if rings started disabled */ |
| 9151 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9152 | return -EBADFD; |
| 9153 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9154 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9155 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9156 | return -EBUSY; |
| 9157 | |
| 9158 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9159 | return -EINVAL; |
| 9160 | |
| 9161 | size = array_size(nr_args, sizeof(*res)); |
| 9162 | if (size == SIZE_MAX) |
| 9163 | return -EOVERFLOW; |
| 9164 | |
| 9165 | res = memdup_user(arg, size); |
| 9166 | if (IS_ERR(res)) |
| 9167 | return PTR_ERR(res); |
| 9168 | |
| 9169 | ret = 0; |
| 9170 | |
| 9171 | for (i = 0; i < nr_args; i++) { |
| 9172 | switch (res[i].opcode) { |
| 9173 | case IORING_RESTRICTION_REGISTER_OP: |
| 9174 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9175 | ret = -EINVAL; |
| 9176 | goto out; |
| 9177 | } |
| 9178 | |
| 9179 | __set_bit(res[i].register_op, |
| 9180 | ctx->restrictions.register_op); |
| 9181 | break; |
| 9182 | case IORING_RESTRICTION_SQE_OP: |
| 9183 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9184 | ret = -EINVAL; |
| 9185 | goto out; |
| 9186 | } |
| 9187 | |
| 9188 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9189 | break; |
| 9190 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9191 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9192 | break; |
| 9193 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9194 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9195 | break; |
| 9196 | default: |
| 9197 | ret = -EINVAL; |
| 9198 | goto out; |
| 9199 | } |
| 9200 | } |
| 9201 | |
| 9202 | out: |
| 9203 | /* Reset all restrictions if an error happened */ |
| 9204 | if (ret != 0) |
| 9205 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9206 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9207 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9208 | |
| 9209 | kfree(res); |
| 9210 | return ret; |
| 9211 | } |
| 9212 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9213 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9214 | { |
| 9215 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9216 | return -EBADFD; |
| 9217 | |
| 9218 | if (ctx->restrictions.registered) |
| 9219 | ctx->restricted = 1; |
| 9220 | |
| 9221 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9222 | |
| 9223 | io_sq_offload_start(ctx); |
| 9224 | |
| 9225 | return 0; |
| 9226 | } |
| 9227 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9228 | static bool io_register_op_must_quiesce(int op) |
| 9229 | { |
| 9230 | switch (op) { |
| 9231 | case IORING_UNREGISTER_FILES: |
| 9232 | case IORING_REGISTER_FILES_UPDATE: |
| 9233 | case IORING_REGISTER_PROBE: |
| 9234 | case IORING_REGISTER_PERSONALITY: |
| 9235 | case IORING_UNREGISTER_PERSONALITY: |
| 9236 | return false; |
| 9237 | default: |
| 9238 | return true; |
| 9239 | } |
| 9240 | } |
| 9241 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9242 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9243 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9244 | __releases(ctx->uring_lock) |
| 9245 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9246 | { |
| 9247 | int ret; |
| 9248 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9249 | /* |
| 9250 | * We're inside the ring mutex, if the ref is already dying, then |
| 9251 | * someone else killed the ctx or is already going through |
| 9252 | * io_uring_register(). |
| 9253 | */ |
| 9254 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9255 | return -ENXIO; |
| 9256 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9257 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9258 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9259 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9260 | /* |
| 9261 | * Drop uring mutex before waiting for references to exit. If |
| 9262 | * another thread is currently inside io_uring_enter() it might |
| 9263 | * need to grab the uring_lock to make progress. If we hold it |
| 9264 | * here across the drain wait, then we can deadlock. It's safe |
| 9265 | * to drop the mutex here, since no new references will come in |
| 9266 | * after we've killed the percpu ref. |
| 9267 | */ |
| 9268 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9269 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9270 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9271 | if (ret) { |
| 9272 | percpu_ref_resurrect(&ctx->refs); |
| 9273 | ret = -EINTR; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9274 | goto out_quiesce; |
| 9275 | } |
| 9276 | } |
| 9277 | |
| 9278 | if (ctx->restricted) { |
| 9279 | if (opcode >= IORING_REGISTER_LAST) { |
| 9280 | ret = -EINVAL; |
| 9281 | goto out; |
| 9282 | } |
| 9283 | |
| 9284 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 9285 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9286 | goto out; |
| 9287 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9288 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9289 | |
| 9290 | switch (opcode) { |
| 9291 | case IORING_REGISTER_BUFFERS: |
| 9292 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 9293 | break; |
| 9294 | case IORING_UNREGISTER_BUFFERS: |
| 9295 | ret = -EINVAL; |
| 9296 | if (arg || nr_args) |
| 9297 | break; |
| 9298 | ret = io_sqe_buffer_unregister(ctx); |
| 9299 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9300 | case IORING_REGISTER_FILES: |
| 9301 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9302 | break; |
| 9303 | case IORING_UNREGISTER_FILES: |
| 9304 | ret = -EINVAL; |
| 9305 | if (arg || nr_args) |
| 9306 | break; |
| 9307 | ret = io_sqe_files_unregister(ctx); |
| 9308 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9309 | case IORING_REGISTER_FILES_UPDATE: |
| 9310 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9311 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9312 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9313 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9314 | ret = -EINVAL; |
| 9315 | if (nr_args != 1) |
| 9316 | break; |
| 9317 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9318 | if (ret) |
| 9319 | break; |
| 9320 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9321 | ctx->eventfd_async = 1; |
| 9322 | else |
| 9323 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9324 | break; |
| 9325 | case IORING_UNREGISTER_EVENTFD: |
| 9326 | ret = -EINVAL; |
| 9327 | if (arg || nr_args) |
| 9328 | break; |
| 9329 | ret = io_eventfd_unregister(ctx); |
| 9330 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9331 | case IORING_REGISTER_PROBE: |
| 9332 | ret = -EINVAL; |
| 9333 | if (!arg || nr_args > 256) |
| 9334 | break; |
| 9335 | ret = io_probe(ctx, arg, nr_args); |
| 9336 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9337 | case IORING_REGISTER_PERSONALITY: |
| 9338 | ret = -EINVAL; |
| 9339 | if (arg || nr_args) |
| 9340 | break; |
| 9341 | ret = io_register_personality(ctx); |
| 9342 | break; |
| 9343 | case IORING_UNREGISTER_PERSONALITY: |
| 9344 | ret = -EINVAL; |
| 9345 | if (arg) |
| 9346 | break; |
| 9347 | ret = io_unregister_personality(ctx, nr_args); |
| 9348 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9349 | case IORING_REGISTER_ENABLE_RINGS: |
| 9350 | ret = -EINVAL; |
| 9351 | if (arg || nr_args) |
| 9352 | break; |
| 9353 | ret = io_register_enable_rings(ctx); |
| 9354 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9355 | case IORING_REGISTER_RESTRICTIONS: |
| 9356 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9357 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9358 | default: |
| 9359 | ret = -EINVAL; |
| 9360 | break; |
| 9361 | } |
| 9362 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9363 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9364 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9365 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9366 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9367 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9368 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9369 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9370 | return ret; |
| 9371 | } |
| 9372 | |
| 9373 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9374 | void __user *, arg, unsigned int, nr_args) |
| 9375 | { |
| 9376 | struct io_ring_ctx *ctx; |
| 9377 | long ret = -EBADF; |
| 9378 | struct fd f; |
| 9379 | |
| 9380 | f = fdget(fd); |
| 9381 | if (!f.file) |
| 9382 | return -EBADF; |
| 9383 | |
| 9384 | ret = -EOPNOTSUPP; |
| 9385 | if (f.file->f_op != &io_uring_fops) |
| 9386 | goto out_fput; |
| 9387 | |
| 9388 | ctx = f.file->private_data; |
| 9389 | |
| 9390 | mutex_lock(&ctx->uring_lock); |
| 9391 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9392 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9393 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9394 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9395 | out_fput: |
| 9396 | fdput(f); |
| 9397 | return ret; |
| 9398 | } |
| 9399 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9400 | static int __init io_uring_init(void) |
| 9401 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9402 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9403 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9404 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9405 | } while (0) |
| 9406 | |
| 9407 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9408 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9409 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9410 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9411 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9412 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9413 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9414 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9415 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9416 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9417 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9418 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9419 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9420 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9421 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9422 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9423 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9424 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9425 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9426 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9427 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9428 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9429 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9430 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9431 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9432 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9433 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9434 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9435 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9436 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9437 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9438 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9439 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9440 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9441 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 9442 | return 0; |
| 9443 | }; |
| 9444 | __initcall(io_uring_init); |