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 | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 82 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 83 | #define CREATE_TRACE_POINTS |
| 84 | #include <trace/events/io_uring.h> |
| 85 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 86 | #include <uapi/linux/io_uring.h> |
| 87 | |
| 88 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 89 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 90 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 91 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 96 | */ |
| 97 | #define IORING_FILE_TABLE_SHIFT 9 |
| 98 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 99 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 100 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 101 | |
| 102 | struct io_uring { |
| 103 | u32 head ____cacheline_aligned_in_smp; |
| 104 | u32 tail ____cacheline_aligned_in_smp; |
| 105 | }; |
| 106 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 107 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 108 | * This data is shared with the application through the mmap at offsets |
| 109 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 110 | * |
| 111 | * The offsets to the member fields are published through struct |
| 112 | * io_sqring_offsets when calling io_uring_setup. |
| 113 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 114 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
| 116 | * Head and tail offsets into the ring; the offsets need to be |
| 117 | * masked to get valid indices. |
| 118 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 120 | * and the application controls tail of the sq ring and the head of the |
| 121 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 125 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 126 | * ring_entries - 1) |
| 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | u32 sq_ring_mask, cq_ring_mask; |
| 129 | /* Ring sizes (constant, power of 2) */ |
| 130 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Number of invalid entries dropped by the kernel due to |
| 133 | * invalid index stored in array |
| 134 | * |
| 135 | * Written by the kernel, shouldn't be modified by the |
| 136 | * application (i.e. get number of "new events" by comparing to |
| 137 | * cached value). |
| 138 | * |
| 139 | * After a new SQ head value was read by the application this |
| 140 | * counter includes all submissions that were dropped reaching |
| 141 | * the new SQ head (and possibly more). |
| 142 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 144 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 145 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 146 | * |
| 147 | * Written by the kernel, shouldn't be modified by the |
| 148 | * application. |
| 149 | * |
| 150 | * The application needs a full memory barrier before checking |
| 151 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 152 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 153 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 155 | * Runtime CQ flags |
| 156 | * |
| 157 | * Written by the application, shouldn't be modified by the |
| 158 | * kernel. |
| 159 | */ |
| 160 | u32 cq_flags; |
| 161 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | * Number of completion events lost because the queue was full; |
| 163 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 164 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 165 | * the completion queue. |
| 166 | * |
| 167 | * Written by the kernel, shouldn't be modified by the |
| 168 | * application (i.e. get number of "new events" by comparing to |
| 169 | * cached value). |
| 170 | * |
| 171 | * As completion events come in out of order this counter is not |
| 172 | * ordered with any other data. |
| 173 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 174 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Ring buffer of completion events. |
| 177 | * |
| 178 | * The kernel writes completion events fresh every time they are |
| 179 | * produced, so the application is allowed to modify pending |
| 180 | * entries. |
| 181 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 182 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 185 | struct io_mapped_ubuf { |
| 186 | u64 ubuf; |
| 187 | size_t len; |
| 188 | struct bio_vec *bvec; |
| 189 | unsigned int nr_bvecs; |
| 190 | }; |
| 191 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 192 | struct fixed_file_table { |
| 193 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 196 | struct fixed_file_ref_node { |
| 197 | struct percpu_ref refs; |
| 198 | struct list_head node; |
| 199 | struct list_head file_list; |
| 200 | struct fixed_file_data *file_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 201 | struct llist_node llist; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 202 | }; |
| 203 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 204 | struct fixed_file_data { |
| 205 | struct fixed_file_table *table; |
| 206 | struct io_ring_ctx *ctx; |
| 207 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 208 | struct percpu_ref *cur_refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 209 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 210 | struct completion done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 211 | struct list_head ref_list; |
| 212 | spinlock_t lock; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 215 | struct io_buffer { |
| 216 | struct list_head list; |
| 217 | __u64 addr; |
| 218 | __s32 len; |
| 219 | __u16 bid; |
| 220 | }; |
| 221 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 222 | struct io_ring_ctx { |
| 223 | struct { |
| 224 | struct percpu_ref refs; |
| 225 | } ____cacheline_aligned_in_smp; |
| 226 | |
| 227 | struct { |
| 228 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 229 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 230 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 231 | unsigned int cq_overflow_flushed: 1; |
| 232 | unsigned int drain_next: 1; |
| 233 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 235 | /* |
| 236 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 237 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 238 | * |
| 239 | * This indirection could e.g. be used to assign fixed |
| 240 | * io_uring_sqe entries to operations and only submit them to |
| 241 | * the queue when needed. |
| 242 | * |
| 243 | * The kernel modifies neither the indices array nor the entries |
| 244 | * array. |
| 245 | */ |
| 246 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 247 | unsigned cached_sq_head; |
| 248 | unsigned sq_entries; |
| 249 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 250 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 251 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 252 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 253 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 254 | |
| 255 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 256 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 257 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 258 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 259 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 260 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | } ____cacheline_aligned_in_smp; |
| 262 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 263 | struct io_rings *rings; |
| 264 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 265 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 266 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 267 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 268 | struct mm_struct *sqo_mm; |
| 269 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 270 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 271 | /* |
| 272 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 273 | * readers must ensure that ->refs is alive as long as the file* is |
| 274 | * used. Only updated through io_uring_register(2). |
| 275 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 276 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 277 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 278 | int ring_fd; |
| 279 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 280 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 281 | /* if used, fixed mapped user buffers */ |
| 282 | unsigned nr_user_bufs; |
| 283 | struct io_mapped_ubuf *user_bufs; |
| 284 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 285 | struct user_struct *user; |
| 286 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 287 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 288 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 289 | struct completion ref_comp; |
| 290 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 291 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 292 | /* if all else fails... */ |
| 293 | struct io_kiocb *fallback_req; |
| 294 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 295 | #if defined(CONFIG_UNIX) |
| 296 | struct socket *ring_sock; |
| 297 | #endif |
| 298 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 299 | struct idr io_buffer_idr; |
| 300 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 301 | struct idr personality_idr; |
| 302 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 303 | struct { |
| 304 | unsigned cached_cq_tail; |
| 305 | unsigned cq_entries; |
| 306 | unsigned cq_mask; |
| 307 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 308 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 309 | struct wait_queue_head cq_wait; |
| 310 | struct fasync_struct *cq_fasync; |
| 311 | struct eventfd_ctx *cq_ev_fd; |
| 312 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 313 | |
| 314 | struct { |
| 315 | struct mutex uring_lock; |
| 316 | wait_queue_head_t wait; |
| 317 | } ____cacheline_aligned_in_smp; |
| 318 | |
| 319 | struct { |
| 320 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 321 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 322 | /* |
| 323 | * ->poll_list is protected by the ctx->uring_lock for |
| 324 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 325 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 326 | * manipulate the list, hence no extra locking is needed there. |
| 327 | */ |
| 328 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 329 | struct hlist_head *cancel_hash; |
| 330 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 331 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 332 | |
| 333 | spinlock_t inflight_lock; |
| 334 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 335 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 336 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 337 | struct delayed_work file_put_work; |
| 338 | struct llist_head file_put_llist; |
| 339 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 340 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 343 | /* |
| 344 | * First field must be the file pointer in all the |
| 345 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 346 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 347 | struct io_poll_iocb { |
| 348 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 349 | union { |
| 350 | struct wait_queue_head *head; |
| 351 | u64 addr; |
| 352 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 353 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 354 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 355 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 356 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 359 | struct io_close { |
| 360 | struct file *file; |
| 361 | struct file *put_file; |
| 362 | int fd; |
| 363 | }; |
| 364 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 365 | struct io_timeout_data { |
| 366 | struct io_kiocb *req; |
| 367 | struct hrtimer timer; |
| 368 | struct timespec64 ts; |
| 369 | enum hrtimer_mode mode; |
| 370 | }; |
| 371 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 372 | struct io_accept { |
| 373 | struct file *file; |
| 374 | struct sockaddr __user *addr; |
| 375 | int __user *addr_len; |
| 376 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 377 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | struct io_sync { |
| 381 | struct file *file; |
| 382 | loff_t len; |
| 383 | loff_t off; |
| 384 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 385 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 388 | struct io_cancel { |
| 389 | struct file *file; |
| 390 | u64 addr; |
| 391 | }; |
| 392 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 393 | struct io_timeout { |
| 394 | struct file *file; |
| 395 | u64 addr; |
| 396 | int flags; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 397 | u32 off; |
| 398 | u32 target_seq; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 401 | struct io_rw { |
| 402 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 403 | struct kiocb kiocb; |
| 404 | u64 addr; |
| 405 | u64 len; |
| 406 | }; |
| 407 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 408 | struct io_connect { |
| 409 | struct file *file; |
| 410 | struct sockaddr __user *addr; |
| 411 | int addr_len; |
| 412 | }; |
| 413 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 414 | struct io_sr_msg { |
| 415 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 416 | union { |
| 417 | struct user_msghdr __user *msg; |
| 418 | void __user *buf; |
| 419 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 420 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 421 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 422 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 423 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 426 | struct io_open { |
| 427 | struct file *file; |
| 428 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 429 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 430 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 431 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 432 | }; |
| 433 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 434 | struct io_files_update { |
| 435 | struct file *file; |
| 436 | u64 arg; |
| 437 | u32 nr_args; |
| 438 | u32 offset; |
| 439 | }; |
| 440 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 441 | struct io_fadvise { |
| 442 | struct file *file; |
| 443 | u64 offset; |
| 444 | u32 len; |
| 445 | u32 advice; |
| 446 | }; |
| 447 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 448 | struct io_madvise { |
| 449 | struct file *file; |
| 450 | u64 addr; |
| 451 | u32 len; |
| 452 | u32 advice; |
| 453 | }; |
| 454 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 455 | struct io_epoll { |
| 456 | struct file *file; |
| 457 | int epfd; |
| 458 | int op; |
| 459 | int fd; |
| 460 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 463 | struct io_splice { |
| 464 | struct file *file_out; |
| 465 | struct file *file_in; |
| 466 | loff_t off_out; |
| 467 | loff_t off_in; |
| 468 | u64 len; |
| 469 | unsigned int flags; |
| 470 | }; |
| 471 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 472 | struct io_provide_buf { |
| 473 | struct file *file; |
| 474 | __u64 addr; |
| 475 | __s32 len; |
| 476 | __u32 bgid; |
| 477 | __u16 nbufs; |
| 478 | __u16 bid; |
| 479 | }; |
| 480 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 481 | struct io_statx { |
| 482 | struct file *file; |
| 483 | int dfd; |
| 484 | unsigned int mask; |
| 485 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 486 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 487 | struct statx __user *buffer; |
| 488 | }; |
| 489 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 490 | struct io_async_connect { |
| 491 | struct sockaddr_storage address; |
| 492 | }; |
| 493 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 494 | struct io_async_msghdr { |
| 495 | struct iovec fast_iov[UIO_FASTIOV]; |
| 496 | struct iovec *iov; |
| 497 | struct sockaddr __user *uaddr; |
| 498 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 499 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 502 | struct io_async_rw { |
| 503 | struct iovec fast_iov[UIO_FASTIOV]; |
| 504 | struct iovec *iov; |
| 505 | ssize_t nr_segs; |
| 506 | ssize_t size; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 507 | struct wait_page_queue wpq; |
| 508 | struct callback_head task_work; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 511 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 512 | union { |
| 513 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 514 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 515 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 516 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 517 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 520 | enum { |
| 521 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 522 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 523 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 524 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 525 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 526 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 527 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 528 | REQ_F_LINK_HEAD_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 529 | REQ_F_LINK_NEXT_BIT, |
| 530 | REQ_F_FAIL_LINK_BIT, |
| 531 | REQ_F_INFLIGHT_BIT, |
| 532 | REQ_F_CUR_POS_BIT, |
| 533 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 534 | REQ_F_LINK_TIMEOUT_BIT, |
| 535 | REQ_F_TIMEOUT_BIT, |
| 536 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 537 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 538 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 539 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 540 | REQ_F_OVERFLOW_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 541 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 542 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 543 | REQ_F_NO_FILE_TABLE_BIT, |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 544 | REQ_F_QUEUE_TIMEOUT_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 545 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 546 | REQ_F_TASK_PINNED_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 547 | |
| 548 | /* not a real bit, just to check we're not overflowing the space */ |
| 549 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | enum { |
| 553 | /* ctx owns file */ |
| 554 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 555 | /* drain existing IO first */ |
| 556 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 557 | /* linked sqes */ |
| 558 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 559 | /* doesn't sever on completion < 0 */ |
| 560 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 561 | /* IOSQE_ASYNC */ |
| 562 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 563 | /* IOSQE_BUFFER_SELECT */ |
| 564 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 565 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 566 | /* head of a link */ |
| 567 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 568 | /* already grabbed next link */ |
| 569 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 570 | /* fail rest of links */ |
| 571 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 572 | /* on inflight list */ |
| 573 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 574 | /* read/write uses file position */ |
| 575 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 576 | /* must not punt to workers */ |
| 577 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 578 | /* has linked timeout */ |
| 579 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 580 | /* timeout request */ |
| 581 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 582 | /* regular file */ |
| 583 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 584 | /* no timeout sequence */ |
| 585 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 586 | /* completion under lock */ |
| 587 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 588 | /* needs cleanup */ |
| 589 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 590 | /* in overflow list */ |
| 591 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 592 | /* already went through poll handler */ |
| 593 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 594 | /* buffer already selected */ |
| 595 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 596 | /* doesn't need file table for this request */ |
| 597 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 598 | /* needs to queue linked timeout */ |
| 599 | REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 600 | /* io_wq_work is initialized */ |
| 601 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 602 | /* req->task is refcounted */ |
| 603 | REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | struct async_poll { |
| 607 | struct io_poll_iocb poll; |
| 608 | struct io_wq_work work; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 609 | }; |
| 610 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 611 | /* |
| 612 | * NOTE! Each of the iocb union members has the file pointer |
| 613 | * as the first entry in their struct definition. So you can |
| 614 | * access the file pointer through any of the sub-structs, |
| 615 | * or directly as just 'ki_filp' in this struct. |
| 616 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 617 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 618 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 619 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 620 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 621 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 622 | struct io_accept accept; |
| 623 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 624 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 625 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 626 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 627 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 628 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 629 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 630 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 631 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 632 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 633 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 634 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 635 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 636 | struct io_statx statx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 637 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 638 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 639 | struct io_async_ctx *io; |
Pavel Begunkov | c398ecb | 2020-04-09 08:17:59 +0300 | [diff] [blame] | 640 | int cflags; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 641 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 642 | /* polled IO has completed */ |
| 643 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 644 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 645 | u16 buf_index; |
| 646 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 647 | struct io_ring_ctx *ctx; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 648 | struct list_head list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 649 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 650 | refcount_t refs; |
Jens Axboe | 3537b6a | 2020-04-03 11:19:06 -0600 | [diff] [blame] | 651 | struct task_struct *task; |
| 652 | unsigned long fsize; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 653 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 654 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 655 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 656 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 657 | struct list_head link_list; |
| 658 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 659 | struct list_head inflight_entry; |
| 660 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 661 | struct percpu_ref *fixed_file_refs; |
| 662 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 663 | union { |
| 664 | /* |
| 665 | * Only commands that never go async can use the below fields, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 666 | * obviously. Right now only IORING_OP_POLL_ADD uses them, and |
| 667 | * async armed poll handlers for regular commands. The latter |
| 668 | * restore the work, if needed. |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 669 | */ |
| 670 | struct { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 671 | struct callback_head task_work; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 672 | struct hlist_node hash_node; |
| 673 | struct async_poll *apoll; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 674 | }; |
| 675 | struct io_wq_work work; |
| 676 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 677 | }; |
| 678 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 679 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 680 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 681 | struct io_submit_state { |
| 682 | struct blk_plug plug; |
| 683 | |
| 684 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 685 | * io_kiocb alloc cache |
| 686 | */ |
| 687 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 688 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 689 | |
| 690 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 691 | * File reference cache |
| 692 | */ |
| 693 | struct file *file; |
| 694 | unsigned int fd; |
| 695 | unsigned int has_refs; |
| 696 | unsigned int used_refs; |
| 697 | unsigned int ios_left; |
| 698 | }; |
| 699 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 700 | struct io_op_def { |
| 701 | /* needs req->io allocated for deferral/async */ |
| 702 | unsigned async_ctx : 1; |
| 703 | /* needs current->mm setup, does mm access */ |
| 704 | unsigned needs_mm : 1; |
| 705 | /* needs req->file assigned */ |
| 706 | unsigned needs_file : 1; |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 707 | /* don't fail if file grab fails */ |
| 708 | unsigned needs_file_no_error : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 709 | /* hash wq insertion if file is a regular file */ |
| 710 | unsigned hash_reg_file : 1; |
| 711 | /* unbound wq insertion if file is a non-regular file */ |
| 712 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 713 | /* opcode is not supported by this kernel */ |
| 714 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 715 | /* needs file table */ |
| 716 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 717 | /* needs ->fs */ |
| 718 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 719 | /* set if opcode supports polled "wait" */ |
| 720 | unsigned pollin : 1; |
| 721 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 722 | /* op supports buffer selection */ |
| 723 | unsigned buffer_select : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 724 | }; |
| 725 | |
| 726 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 727 | [IORING_OP_NOP] = {}, |
| 728 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 729 | .async_ctx = 1, |
| 730 | .needs_mm = 1, |
| 731 | .needs_file = 1, |
| 732 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 733 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 734 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 735 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 736 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 737 | .async_ctx = 1, |
| 738 | .needs_mm = 1, |
| 739 | .needs_file = 1, |
| 740 | .hash_reg_file = 1, |
| 741 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 742 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 743 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 744 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 745 | .needs_file = 1, |
| 746 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 747 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 748 | .needs_file = 1, |
| 749 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 750 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 751 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 752 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 753 | .needs_file = 1, |
| 754 | .hash_reg_file = 1, |
| 755 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 756 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 757 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 758 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 759 | .needs_file = 1, |
| 760 | .unbound_nonreg_file = 1, |
| 761 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 762 | [IORING_OP_POLL_REMOVE] = {}, |
| 763 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 764 | .needs_file = 1, |
| 765 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 766 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 767 | .async_ctx = 1, |
| 768 | .needs_mm = 1, |
| 769 | .needs_file = 1, |
| 770 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 771 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 772 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 773 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 774 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 775 | .async_ctx = 1, |
| 776 | .needs_mm = 1, |
| 777 | .needs_file = 1, |
| 778 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 779 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 780 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 781 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 782 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 783 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 784 | .async_ctx = 1, |
| 785 | .needs_mm = 1, |
| 786 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 787 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 788 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 789 | .needs_mm = 1, |
| 790 | .needs_file = 1, |
| 791 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 792 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 793 | .pollin = 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_ASYNC_CANCEL] = {}, |
| 796 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 797 | .async_ctx = 1, |
| 798 | .needs_mm = 1, |
| 799 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 800 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 801 | .async_ctx = 1, |
| 802 | .needs_mm = 1, |
| 803 | .needs_file = 1, |
| 804 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 805 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 806 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 807 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 808 | .needs_file = 1, |
| 809 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 810 | [IORING_OP_OPENAT] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 811 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 812 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 813 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 814 | [IORING_OP_CLOSE] = { |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 815 | .needs_file = 1, |
| 816 | .needs_file_no_error = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 817 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 818 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 819 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 820 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 821 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 823 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 824 | .needs_mm = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 825 | .needs_fs = 1, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 826 | .file_table = 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_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 829 | .needs_mm = 1, |
| 830 | .needs_file = 1, |
| 831 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 832 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 833 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 834 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 835 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 836 | .needs_mm = 1, |
| 837 | .needs_file = 1, |
| 838 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 839 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 840 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 841 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 842 | .needs_file = 1, |
| 843 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 844 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 845 | .needs_mm = 1, |
| 846 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 847 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 848 | .needs_mm = 1, |
| 849 | .needs_file = 1, |
| 850 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 851 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 852 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 853 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 854 | .needs_mm = 1, |
| 855 | .needs_file = 1, |
| 856 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 857 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 858 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 859 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 860 | [IORING_OP_OPENAT2] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 861 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 862 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 863 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 864 | [IORING_OP_EPOLL_CTL] = { |
| 865 | .unbound_nonreg_file = 1, |
| 866 | .file_table = 1, |
| 867 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 868 | [IORING_OP_SPLICE] = { |
| 869 | .needs_file = 1, |
| 870 | .hash_reg_file = 1, |
| 871 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 872 | }, |
| 873 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 874 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 875 | [IORING_OP_TEE] = { |
| 876 | .needs_file = 1, |
| 877 | .hash_reg_file = 1, |
| 878 | .unbound_nonreg_file = 1, |
| 879 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 880 | }; |
| 881 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 882 | enum io_mem_account { |
| 883 | ACCT_LOCKED, |
| 884 | ACCT_PINNED, |
| 885 | }; |
| 886 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 887 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 888 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 889 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 890 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 891 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 892 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 893 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 894 | struct io_uring_files_update *ip, |
| 895 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 896 | static int io_grab_files(struct io_kiocb *req); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 897 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 898 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 899 | int fd, struct file **out_file, bool fixed); |
| 900 | static void __io_queue_sqe(struct io_kiocb *req, |
| 901 | const struct io_uring_sqe *sqe); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 902 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 903 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 904 | struct iovec **iovec, struct iov_iter *iter, |
| 905 | bool needs_lock); |
| 906 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 907 | struct iovec *iovec, struct iovec *fast_iov, |
| 908 | struct iov_iter *iter); |
| 909 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 910 | static struct kmem_cache *req_cachep; |
| 911 | |
| 912 | static const struct file_operations io_uring_fops; |
| 913 | |
| 914 | struct sock *io_uring_get_socket(struct file *file) |
| 915 | { |
| 916 | #if defined(CONFIG_UNIX) |
| 917 | if (file->f_op == &io_uring_fops) { |
| 918 | struct io_ring_ctx *ctx = file->private_data; |
| 919 | |
| 920 | return ctx->ring_sock->sk; |
| 921 | } |
| 922 | #endif |
| 923 | return NULL; |
| 924 | } |
| 925 | EXPORT_SYMBOL(io_uring_get_socket); |
| 926 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 927 | static void io_get_req_task(struct io_kiocb *req) |
| 928 | { |
| 929 | if (req->flags & REQ_F_TASK_PINNED) |
| 930 | return; |
| 931 | get_task_struct(req->task); |
| 932 | req->flags |= REQ_F_TASK_PINNED; |
| 933 | } |
| 934 | |
| 935 | /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */ |
| 936 | static void __io_put_req_task(struct io_kiocb *req) |
| 937 | { |
| 938 | if (req->flags & REQ_F_TASK_PINNED) |
| 939 | put_task_struct(req->task); |
| 940 | } |
| 941 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 942 | static void io_file_put_work(struct work_struct *work); |
| 943 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 944 | /* |
| 945 | * Note: must call io_req_init_async() for the first time you |
| 946 | * touch any members of io_wq_work. |
| 947 | */ |
| 948 | static inline void io_req_init_async(struct io_kiocb *req) |
| 949 | { |
| 950 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 951 | return; |
| 952 | |
| 953 | memset(&req->work, 0, sizeof(req->work)); |
| 954 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 955 | } |
| 956 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 957 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 958 | { |
| 959 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 960 | } |
| 961 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 962 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 963 | { |
| 964 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 965 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 966 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 970 | { |
| 971 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 972 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 973 | |
| 974 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 975 | if (!ctx) |
| 976 | return NULL; |
| 977 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 978 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 979 | if (!ctx->fallback_req) |
| 980 | goto err; |
| 981 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 982 | /* |
| 983 | * Use 5 bits less than the max cq entries, that should give us around |
| 984 | * 32 entries per hash list if totally full and uniformly spread. |
| 985 | */ |
| 986 | hash_bits = ilog2(p->cq_entries); |
| 987 | hash_bits -= 5; |
| 988 | if (hash_bits <= 0) |
| 989 | hash_bits = 1; |
| 990 | ctx->cancel_hash_bits = hash_bits; |
| 991 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 992 | GFP_KERNEL); |
| 993 | if (!ctx->cancel_hash) |
| 994 | goto err; |
| 995 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 996 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 997 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 998 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 999 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1000 | |
| 1001 | ctx->flags = p->flags; |
Jens Axboe | 583863e | 2020-05-17 09:20:00 -0600 | [diff] [blame] | 1002 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1003 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1004 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1005 | init_completion(&ctx->ref_comp); |
| 1006 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1007 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1008 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1009 | mutex_init(&ctx->uring_lock); |
| 1010 | init_waitqueue_head(&ctx->wait); |
| 1011 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1012 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1013 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1014 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1015 | init_waitqueue_head(&ctx->inflight_wait); |
| 1016 | spin_lock_init(&ctx->inflight_lock); |
| 1017 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1018 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1019 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1020 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1021 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1022 | if (ctx->fallback_req) |
| 1023 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1024 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1025 | kfree(ctx); |
| 1026 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1029 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1030 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1031 | struct io_ring_ctx *ctx = req->ctx; |
| 1032 | |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 1033 | return req->sequence != ctx->cached_cq_tail |
| 1034 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1035 | } |
| 1036 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1037 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1038 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1039 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1040 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1041 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1042 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1043 | } |
| 1044 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1045 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1046 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1047 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1048 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1049 | /* order cqe stores with ring update */ |
| 1050 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1051 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1052 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 1053 | wake_up_interruptible(&ctx->cq_wait); |
| 1054 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1058 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 1059 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1060 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1061 | if (!req->work.mm && def->needs_mm) { |
| 1062 | mmgrab(current->mm); |
| 1063 | req->work.mm = current->mm; |
| 1064 | } |
| 1065 | if (!req->work.creds) |
| 1066 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1067 | if (!req->work.fs && def->needs_fs) { |
| 1068 | spin_lock(¤t->fs->lock); |
| 1069 | if (!current->fs->in_exec) { |
| 1070 | req->work.fs = current->fs; |
| 1071 | req->work.fs->users++; |
| 1072 | } else { |
| 1073 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1074 | } |
| 1075 | spin_unlock(¤t->fs->lock); |
| 1076 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 1080 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1081 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1082 | return; |
| 1083 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1084 | if (req->work.mm) { |
| 1085 | mmdrop(req->work.mm); |
| 1086 | req->work.mm = NULL; |
| 1087 | } |
| 1088 | if (req->work.creds) { |
| 1089 | put_cred(req->work.creds); |
| 1090 | req->work.creds = NULL; |
| 1091 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1092 | if (req->work.fs) { |
| 1093 | struct fs_struct *fs = req->work.fs; |
| 1094 | |
| 1095 | spin_lock(&req->work.fs->lock); |
| 1096 | if (--fs->users) |
| 1097 | fs = NULL; |
| 1098 | spin_unlock(&req->work.fs->lock); |
| 1099 | if (fs) |
| 1100 | free_fs_struct(fs); |
| 1101 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1102 | } |
| 1103 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1104 | static inline void io_prep_async_work(struct io_kiocb *req, |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1105 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1106 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1107 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1108 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1109 | if (req->flags & REQ_F_ISREG) { |
| 1110 | if (def->hash_reg_file) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1111 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1112 | } else { |
| 1113 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1114 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1115 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1116 | |
Pavel Begunkov | 59960b9 | 2020-06-15 16:36:30 +0300 | [diff] [blame] | 1117 | io_req_init_async(req); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1118 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1119 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1120 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1123 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1124 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1125 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1126 | struct io_kiocb *link; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1127 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1128 | io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1129 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1130 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1131 | &req->work, req->flags); |
| 1132 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1133 | |
| 1134 | if (link) |
| 1135 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1136 | } |
| 1137 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1138 | static void io_kill_timeout(struct io_kiocb *req) |
| 1139 | { |
| 1140 | int ret; |
| 1141 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1142 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1143 | if (ret != -1) { |
| 1144 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1145 | list_del_init(&req->list); |
Pavel Begunkov | f0e20b8 | 2020-03-07 01:15:22 +0300 | [diff] [blame] | 1146 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1147 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1148 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1153 | { |
| 1154 | struct io_kiocb *req, *tmp; |
| 1155 | |
| 1156 | spin_lock_irq(&ctx->completion_lock); |
| 1157 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1158 | io_kill_timeout(req); |
| 1159 | spin_unlock_irq(&ctx->completion_lock); |
| 1160 | } |
| 1161 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1162 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1163 | { |
| 1164 | do { |
| 1165 | struct io_kiocb *req = list_first_entry(&ctx->defer_list, |
| 1166 | struct io_kiocb, list); |
| 1167 | |
| 1168 | if (req_need_defer(req)) |
| 1169 | break; |
| 1170 | list_del_init(&req->list); |
| 1171 | io_queue_async_work(req); |
| 1172 | } while (!list_empty(&ctx->defer_list)); |
| 1173 | } |
| 1174 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1175 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1176 | { |
| 1177 | while (!list_empty(&ctx->timeout_list)) { |
| 1178 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
| 1179 | struct io_kiocb, list); |
| 1180 | |
| 1181 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 1182 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1183 | if (req->timeout.target_seq != ctx->cached_cq_tail |
| 1184 | - atomic_read(&ctx->cq_timeouts)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1185 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1186 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1187 | list_del_init(&req->list); |
| 1188 | io_kill_timeout(req); |
| 1189 | } |
| 1190 | } |
| 1191 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1192 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1193 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1194 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1195 | __io_commit_cqring(ctx); |
| 1196 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1197 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1198 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1199 | } |
| 1200 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1201 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1202 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1203 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1204 | unsigned tail; |
| 1205 | |
| 1206 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1207 | /* |
| 1208 | * writes to the cq entry need to come after reading head; the |
| 1209 | * control dependency is enough as we're using WRITE_ONCE to |
| 1210 | * fill the cq entry |
| 1211 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1212 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1213 | return NULL; |
| 1214 | |
| 1215 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1216 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1219 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1220 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1221 | if (!ctx->cq_ev_fd) |
| 1222 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1223 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1224 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1225 | if (!ctx->eventfd_async) |
| 1226 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1227 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1230 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1231 | { |
| 1232 | if (waitqueue_active(&ctx->wait)) |
| 1233 | wake_up(&ctx->wait); |
| 1234 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1235 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1236 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1237 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1238 | } |
| 1239 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1240 | /* Returns true if there are no backlogged entries after the flush */ |
| 1241 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1242 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1243 | struct io_rings *rings = ctx->rings; |
| 1244 | struct io_uring_cqe *cqe; |
| 1245 | struct io_kiocb *req; |
| 1246 | unsigned long flags; |
| 1247 | LIST_HEAD(list); |
| 1248 | |
| 1249 | if (!force) { |
| 1250 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1251 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1252 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1253 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1254 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1258 | |
| 1259 | /* if force is set, the ring is going away. always drop after that */ |
| 1260 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1261 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1262 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1263 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1264 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1265 | cqe = io_get_cqring(ctx); |
| 1266 | if (!cqe && !force) |
| 1267 | break; |
| 1268 | |
| 1269 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1270 | list); |
| 1271 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1272 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1273 | if (cqe) { |
| 1274 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1275 | WRITE_ONCE(cqe->res, req->result); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1276 | WRITE_ONCE(cqe->flags, req->cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1277 | } else { |
| 1278 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1279 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1284 | if (cqe) { |
| 1285 | clear_bit(0, &ctx->sq_check_overflow); |
| 1286 | clear_bit(0, &ctx->cq_check_overflow); |
| 1287 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1288 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1289 | io_cqring_ev_posted(ctx); |
| 1290 | |
| 1291 | while (!list_empty(&list)) { |
| 1292 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1293 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1294 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1295 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1296 | |
| 1297 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1300 | 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] | 1301 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1302 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1303 | struct io_uring_cqe *cqe; |
| 1304 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1305 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1306 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1307 | /* |
| 1308 | * If we can't get a cq entry, userspace overflowed the |
| 1309 | * submission (by quite a lot). Increment the overflow count in |
| 1310 | * the ring. |
| 1311 | */ |
| 1312 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1313 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1314 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1315 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1316 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1317 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1318 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1319 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1320 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1321 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1322 | set_bit(0, &ctx->sq_check_overflow); |
| 1323 | set_bit(0, &ctx->cq_check_overflow); |
| 1324 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1325 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1326 | refcount_inc(&req->refs); |
| 1327 | req->result = res; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1328 | req->cflags = cflags; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1329 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1333 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1334 | { |
| 1335 | __io_cqring_fill_event(req, res, 0); |
| 1336 | } |
| 1337 | |
| 1338 | 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] | 1339 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1340 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1341 | unsigned long flags; |
| 1342 | |
| 1343 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1344 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1345 | io_commit_cqring(ctx); |
| 1346 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1347 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1348 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1351 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
| 1352 | { |
| 1353 | __io_cqring_add_event(req, res, 0); |
| 1354 | } |
| 1355 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1356 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1357 | { |
| 1358 | return req == (struct io_kiocb *) |
| 1359 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1360 | } |
| 1361 | |
| 1362 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1363 | { |
| 1364 | struct io_kiocb *req; |
| 1365 | |
| 1366 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1367 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1368 | return req; |
| 1369 | |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1373 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1374 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1375 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1376 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1377 | struct io_kiocb *req; |
| 1378 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame^] | 1379 | if (!state->free_reqs) { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1380 | size_t sz; |
| 1381 | int ret; |
| 1382 | |
| 1383 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1384 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1385 | |
| 1386 | /* |
| 1387 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1388 | * retry single alloc to be on the safe side. |
| 1389 | */ |
| 1390 | if (unlikely(ret <= 0)) { |
| 1391 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1392 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1393 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1394 | ret = 1; |
| 1395 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1396 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1397 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1398 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1399 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1400 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1403 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1404 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1405 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1408 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1409 | bool fixed) |
| 1410 | { |
| 1411 | if (fixed) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 1412 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1413 | else |
| 1414 | fput(file); |
| 1415 | } |
| 1416 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1417 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1418 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1419 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1420 | io_cleanup_req(req); |
| 1421 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1422 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1423 | if (req->file) |
| 1424 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 1425 | __io_put_req_task(req); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1426 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | static void __io_free_req(struct io_kiocb *req) |
| 1430 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1431 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1432 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1433 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1434 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1435 | unsigned long flags; |
| 1436 | |
| 1437 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1438 | list_del(&req->inflight_entry); |
| 1439 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1440 | wake_up(&ctx->inflight_wait); |
| 1441 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1442 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1443 | |
| 1444 | percpu_ref_put(&req->ctx->refs); |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 1445 | if (likely(!io_is_fallback_req(req))) |
| 1446 | kmem_cache_free(req_cachep, req); |
| 1447 | else |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1448 | clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1449 | } |
| 1450 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1451 | struct req_batch { |
| 1452 | void *reqs[IO_IOPOLL_BATCH]; |
| 1453 | int to_free; |
| 1454 | int need_iter; |
| 1455 | }; |
| 1456 | |
| 1457 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1458 | { |
| 1459 | if (!rb->to_free) |
| 1460 | return; |
| 1461 | if (rb->need_iter) { |
| 1462 | int i, inflight = 0; |
| 1463 | unsigned long flags; |
| 1464 | |
| 1465 | for (i = 0; i < rb->to_free; i++) { |
| 1466 | struct io_kiocb *req = rb->reqs[i]; |
| 1467 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1468 | if (req->flags & REQ_F_INFLIGHT) |
| 1469 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1470 | __io_req_aux_free(req); |
| 1471 | } |
| 1472 | if (!inflight) |
| 1473 | goto do_free; |
| 1474 | |
| 1475 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1476 | for (i = 0; i < rb->to_free; i++) { |
| 1477 | struct io_kiocb *req = rb->reqs[i]; |
| 1478 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1479 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1480 | list_del(&req->inflight_entry); |
| 1481 | if (!--inflight) |
| 1482 | break; |
| 1483 | } |
| 1484 | } |
| 1485 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1486 | |
| 1487 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1488 | wake_up(&ctx->inflight_wait); |
| 1489 | } |
| 1490 | do_free: |
| 1491 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1492 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1493 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1494 | } |
| 1495 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1496 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1497 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1498 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1499 | int ret; |
| 1500 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1501 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1502 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1503 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1504 | io_commit_cqring(ctx); |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1505 | req->flags &= ~REQ_F_LINK_HEAD; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1506 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1507 | return true; |
| 1508 | } |
| 1509 | |
| 1510 | return false; |
| 1511 | } |
| 1512 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1513 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1514 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1515 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1516 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1517 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1518 | /* Already got next link */ |
| 1519 | if (req->flags & REQ_F_LINK_NEXT) |
| 1520 | return; |
| 1521 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1522 | /* |
| 1523 | * The list should never be empty when we are called here. But could |
| 1524 | * potentially happen if the chain is messed up, check to be on the |
| 1525 | * safe side. |
| 1526 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1527 | while (!list_empty(&req->link_list)) { |
| 1528 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1529 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1530 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1531 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1532 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1533 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1534 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1535 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1536 | continue; |
| 1537 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1538 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1539 | list_del_init(&req->link_list); |
| 1540 | if (!list_empty(&nxt->link_list)) |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1541 | nxt->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1542 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1543 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1544 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1545 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1546 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1547 | if (wake_ev) |
| 1548 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | /* |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1552 | * 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] | 1553 | */ |
| 1554 | static void io_fail_links(struct io_kiocb *req) |
| 1555 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1556 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1557 | unsigned long flags; |
| 1558 | |
| 1559 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1560 | |
| 1561 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1562 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1563 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1564 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1565 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1566 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1567 | |
| 1568 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1569 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1570 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1571 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1572 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1573 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1574 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1575 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1576 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1577 | |
| 1578 | io_commit_cqring(ctx); |
| 1579 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1580 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1581 | } |
| 1582 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1583 | static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1584 | { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1585 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1586 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1587 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1588 | /* |
| 1589 | * If LINK is set, we have dependent requests in this chain. If we |
| 1590 | * didn't fail this request, queue the first one up, moving any other |
| 1591 | * dependencies to the next request. In case of failure, fail the rest |
| 1592 | * of the chain. |
| 1593 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1594 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1595 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1596 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1597 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1598 | struct io_ring_ctx *ctx = req->ctx; |
| 1599 | unsigned long flags; |
| 1600 | |
| 1601 | /* |
| 1602 | * If this is a timeout link, we could be racing with the |
| 1603 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1604 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1605 | */ |
| 1606 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1607 | io_req_link_next(req, nxt); |
| 1608 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1609 | } else { |
| 1610 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1611 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1612 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1613 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1614 | static void io_free_req(struct io_kiocb *req) |
| 1615 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1616 | struct io_kiocb *nxt = NULL; |
| 1617 | |
| 1618 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1619 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1620 | |
| 1621 | if (nxt) |
| 1622 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1625 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 1626 | { |
| 1627 | struct io_kiocb *link; |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1628 | const struct io_op_def *def = &io_op_defs[nxt->opcode]; |
| 1629 | |
| 1630 | if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file) |
| 1631 | io_wq_hash_work(&nxt->work, file_inode(nxt->file)); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1632 | |
| 1633 | *workptr = &nxt->work; |
| 1634 | link = io_prep_linked_timeout(nxt); |
Pavel Begunkov | 18a542f | 2020-03-23 00:23:29 +0300 | [diff] [blame] | 1635 | if (link) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 1636 | nxt->flags |= REQ_F_QUEUE_TIMEOUT; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1637 | } |
| 1638 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1639 | /* |
| 1640 | * Drop reference to request, return next in chain (if there is one) if this |
| 1641 | * was the last reference to this request. |
| 1642 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1643 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1644 | static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1645 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1646 | if (refcount_dec_and_test(&req->refs)) { |
| 1647 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1648 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1649 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1652 | static void io_put_req(struct io_kiocb *req) |
| 1653 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1654 | if (refcount_dec_and_test(&req->refs)) |
| 1655 | io_free_req(req); |
| 1656 | } |
| 1657 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1658 | static void io_steal_work(struct io_kiocb *req, |
| 1659 | struct io_wq_work **workptr) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1660 | { |
| 1661 | /* |
| 1662 | * It's in an io-wq worker, so there always should be at least |
| 1663 | * one reference, which will be dropped in io_put_work() just |
| 1664 | * after the current handler returns. |
| 1665 | * |
| 1666 | * It also means, that if the counter dropped to 1, then there is |
| 1667 | * no asynchronous users left, so it's safe to steal the next work. |
| 1668 | */ |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1669 | if (refcount_read(&req->refs) == 1) { |
| 1670 | struct io_kiocb *nxt = NULL; |
| 1671 | |
| 1672 | io_req_find_next(req, &nxt); |
| 1673 | if (nxt) |
| 1674 | io_wq_assign_next(workptr, nxt); |
| 1675 | } |
| 1676 | } |
| 1677 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1678 | /* |
| 1679 | * Must only be used if we don't need to care about links, usually from |
| 1680 | * within the completion handling itself. |
| 1681 | */ |
| 1682 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1683 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1684 | /* drop both submit and complete references */ |
| 1685 | if (refcount_sub_and_test(2, &req->refs)) |
| 1686 | __io_free_req(req); |
| 1687 | } |
| 1688 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1689 | static void io_double_put_req(struct io_kiocb *req) |
| 1690 | { |
| 1691 | /* drop both submit and complete references */ |
| 1692 | if (refcount_sub_and_test(2, &req->refs)) |
| 1693 | io_free_req(req); |
| 1694 | } |
| 1695 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1696 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1697 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1698 | struct io_rings *rings = ctx->rings; |
| 1699 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1700 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1701 | /* |
| 1702 | * noflush == true is from the waitqueue handler, just ensure |
| 1703 | * we wake up the task, and the next invocation will flush the |
| 1704 | * entries. We cannot safely to it from here. |
| 1705 | */ |
| 1706 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1707 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1708 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1709 | io_cqring_overflow_flush(ctx, false); |
| 1710 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1711 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1712 | /* See comment at the top of this file */ |
| 1713 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1714 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1715 | } |
| 1716 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1717 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1718 | { |
| 1719 | struct io_rings *rings = ctx->rings; |
| 1720 | |
| 1721 | /* make sure SQ entry isn't read before tail */ |
| 1722 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1723 | } |
| 1724 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1725 | static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1726 | { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1727 | if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req)) |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1728 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1729 | |
Jens Axboe | 9d9e88a | 2020-05-13 12:53:19 -0600 | [diff] [blame] | 1730 | if (req->file || req->io) |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1731 | rb->need_iter++; |
| 1732 | |
| 1733 | rb->reqs[rb->to_free++] = req; |
| 1734 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1735 | io_free_req_many(req->ctx, rb); |
| 1736 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1739 | static int io_put_kbuf(struct io_kiocb *req) |
| 1740 | { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1741 | struct io_buffer *kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1742 | int cflags; |
| 1743 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1744 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1745 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1746 | cflags |= IORING_CQE_F_BUFFER; |
| 1747 | req->rw.addr = 0; |
| 1748 | kfree(kbuf); |
| 1749 | return cflags; |
| 1750 | } |
| 1751 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1752 | static void io_iopoll_queue(struct list_head *again) |
| 1753 | { |
| 1754 | struct io_kiocb *req; |
| 1755 | |
| 1756 | do { |
| 1757 | req = list_first_entry(again, struct io_kiocb, list); |
| 1758 | list_del(&req->list); |
| 1759 | refcount_inc(&req->refs); |
| 1760 | io_queue_async_work(req); |
| 1761 | } while (!list_empty(again)); |
| 1762 | } |
| 1763 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1764 | /* |
| 1765 | * Find and free completed poll iocbs |
| 1766 | */ |
| 1767 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1768 | struct list_head *done) |
| 1769 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1770 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1771 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1772 | LIST_HEAD(again); |
| 1773 | |
| 1774 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 1775 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1776 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1777 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1778 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1779 | int cflags = 0; |
| 1780 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1781 | req = list_first_entry(done, struct io_kiocb, list); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1782 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 1783 | req->iopoll_completed = 0; |
| 1784 | list_move_tail(&req->list, &again); |
| 1785 | continue; |
| 1786 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1787 | list_del(&req->list); |
| 1788 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1789 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1790 | cflags = io_put_kbuf(req); |
| 1791 | |
| 1792 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1793 | (*nr_events)++; |
| 1794 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1795 | if (refcount_dec_and_test(&req->refs) && |
| 1796 | !io_req_multi_free(&rb, req)) |
| 1797 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1798 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1799 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1800 | io_commit_cqring(ctx); |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 1801 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 1802 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1803 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1804 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1805 | if (!list_empty(&again)) |
| 1806 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1809 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1810 | long min) |
| 1811 | { |
| 1812 | struct io_kiocb *req, *tmp; |
| 1813 | LIST_HEAD(done); |
| 1814 | bool spin; |
| 1815 | int ret; |
| 1816 | |
| 1817 | /* |
| 1818 | * Only spin for completions if we don't have multiple devices hanging |
| 1819 | * off our complete list, and we're under the requested amount. |
| 1820 | */ |
| 1821 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1822 | |
| 1823 | ret = 0; |
| 1824 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1825 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1826 | |
| 1827 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 1828 | * Move completed and retryable entries to our local lists. |
| 1829 | * If we find a request that requires polling, break out |
| 1830 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1831 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 1832 | if (READ_ONCE(req->iopoll_completed)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1833 | list_move_tail(&req->list, &done); |
| 1834 | continue; |
| 1835 | } |
| 1836 | if (!list_empty(&done)) |
| 1837 | break; |
| 1838 | |
| 1839 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1840 | if (ret < 0) |
| 1841 | break; |
| 1842 | |
| 1843 | if (ret && spin) |
| 1844 | spin = false; |
| 1845 | ret = 0; |
| 1846 | } |
| 1847 | |
| 1848 | if (!list_empty(&done)) |
| 1849 | io_iopoll_complete(ctx, nr_events, &done); |
| 1850 | |
| 1851 | return ret; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1855 | * 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] | 1856 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1857 | * as a non-spinning completion check. |
| 1858 | */ |
| 1859 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1860 | long min) |
| 1861 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1862 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1863 | int ret; |
| 1864 | |
| 1865 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1866 | if (ret < 0) |
| 1867 | return ret; |
| 1868 | if (!min || *nr_events >= min) |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * We can't just wait for polled events to come to us, we have to actively |
| 1877 | * find and complete them. |
| 1878 | */ |
| 1879 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1880 | { |
| 1881 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1882 | return; |
| 1883 | |
| 1884 | mutex_lock(&ctx->uring_lock); |
| 1885 | while (!list_empty(&ctx->poll_list)) { |
| 1886 | unsigned int nr_events = 0; |
| 1887 | |
| 1888 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1889 | |
| 1890 | /* |
| 1891 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1892 | * in this case we need to ensure that we reap all events. |
| 1893 | */ |
| 1894 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1895 | } |
| 1896 | mutex_unlock(&ctx->uring_lock); |
| 1897 | } |
| 1898 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1899 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1900 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1901 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1902 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1903 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1904 | /* |
| 1905 | * We disallow the app entering submit/complete with polling, but we |
| 1906 | * still need to lock the ring to prevent racing with polled issue |
| 1907 | * that got punted to a workqueue. |
| 1908 | */ |
| 1909 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1910 | do { |
| 1911 | int tmin = 0; |
| 1912 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1913 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1914 | * Don't enter poll loop if we already have events pending. |
| 1915 | * If we do, we can potentially be spinning for commands that |
| 1916 | * already triggered a CQE (eg in error). |
| 1917 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1918 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1919 | break; |
| 1920 | |
| 1921 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1922 | * If a submit got punted to a workqueue, we can have the |
| 1923 | * application entering polling for a command before it gets |
| 1924 | * issued. That app will hold the uring_lock for the duration |
| 1925 | * of the poll right here, so we need to take a breather every |
| 1926 | * now and then to ensure that the issue has a chance to add |
| 1927 | * the poll to the issued list. Otherwise we can spin here |
| 1928 | * forever, while the workqueue is stuck trying to acquire the |
| 1929 | * very same mutex. |
| 1930 | */ |
| 1931 | if (!(++iters & 7)) { |
| 1932 | mutex_unlock(&ctx->uring_lock); |
| 1933 | mutex_lock(&ctx->uring_lock); |
| 1934 | } |
| 1935 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1936 | if (*nr_events < min) |
| 1937 | tmin = min - *nr_events; |
| 1938 | |
| 1939 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1940 | if (ret <= 0) |
| 1941 | break; |
| 1942 | ret = 0; |
| 1943 | } while (min && !*nr_events && !need_resched()); |
| 1944 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1945 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1946 | return ret; |
| 1947 | } |
| 1948 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1949 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1950 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1951 | /* |
| 1952 | * Tell lockdep we inherited freeze protection from submission |
| 1953 | * thread. |
| 1954 | */ |
| 1955 | if (req->flags & REQ_F_ISREG) { |
| 1956 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1957 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1958 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1959 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1960 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1963 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1964 | { |
| 1965 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1966 | req->flags |= REQ_F_FAIL_LINK; |
| 1967 | } |
| 1968 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1969 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1970 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1971 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1972 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1973 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1974 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1975 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1976 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1977 | if (res != req->result) |
| 1978 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1979 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1980 | cflags = io_put_kbuf(req); |
| 1981 | __io_cqring_add_event(req, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1982 | } |
| 1983 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 1984 | static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx) |
| 1985 | { |
| 1986 | struct mm_struct *mm = current->mm; |
| 1987 | |
| 1988 | if (mm) { |
| 1989 | kthread_unuse_mm(mm); |
| 1990 | mmput(mm); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, |
| 1995 | struct io_kiocb *req) |
| 1996 | { |
| 1997 | if (io_op_defs[req->opcode].needs_mm && !current->mm) { |
| 1998 | if (unlikely(!mmget_not_zero(ctx->sqo_mm))) |
| 1999 | return -EFAULT; |
| 2000 | kthread_use_mm(ctx->sqo_mm); |
| 2001 | } |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
| 2006 | #ifdef CONFIG_BLOCK |
| 2007 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2008 | { |
| 2009 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2010 | ssize_t ret = -ECANCELED; |
| 2011 | struct iov_iter iter; |
| 2012 | int rw; |
| 2013 | |
| 2014 | if (error) { |
| 2015 | ret = error; |
| 2016 | goto end_req; |
| 2017 | } |
| 2018 | |
| 2019 | switch (req->opcode) { |
| 2020 | case IORING_OP_READV: |
| 2021 | case IORING_OP_READ_FIXED: |
| 2022 | case IORING_OP_READ: |
| 2023 | rw = READ; |
| 2024 | break; |
| 2025 | case IORING_OP_WRITEV: |
| 2026 | case IORING_OP_WRITE_FIXED: |
| 2027 | case IORING_OP_WRITE: |
| 2028 | rw = WRITE; |
| 2029 | break; |
| 2030 | default: |
| 2031 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2032 | req->opcode); |
| 2033 | goto end_req; |
| 2034 | } |
| 2035 | |
| 2036 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2037 | if (ret < 0) |
| 2038 | goto end_req; |
| 2039 | ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter); |
| 2040 | if (!ret) |
| 2041 | return true; |
| 2042 | kfree(iovec); |
| 2043 | end_req: |
| 2044 | io_cqring_add_event(req, ret); |
| 2045 | req_set_fail_links(req); |
| 2046 | io_put_req(req); |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
| 2050 | static void io_rw_resubmit(struct callback_head *cb) |
| 2051 | { |
| 2052 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2053 | struct io_ring_ctx *ctx = req->ctx; |
| 2054 | int err; |
| 2055 | |
| 2056 | __set_current_state(TASK_RUNNING); |
| 2057 | |
| 2058 | err = io_sq_thread_acquire_mm(ctx, req); |
| 2059 | |
| 2060 | if (io_resubmit_prep(req, err)) { |
| 2061 | refcount_inc(&req->refs); |
| 2062 | io_queue_async_work(req); |
| 2063 | } |
| 2064 | } |
| 2065 | #endif |
| 2066 | |
| 2067 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2068 | { |
| 2069 | #ifdef CONFIG_BLOCK |
| 2070 | struct task_struct *tsk; |
| 2071 | int ret; |
| 2072 | |
| 2073 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2074 | return false; |
| 2075 | |
| 2076 | tsk = req->task; |
| 2077 | init_task_work(&req->task_work, io_rw_resubmit); |
| 2078 | ret = task_work_add(tsk, &req->task_work, true); |
| 2079 | if (!ret) |
| 2080 | return true; |
| 2081 | #endif |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2085 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2086 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2087 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2088 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2089 | if (!io_rw_reissue(req, res)) { |
| 2090 | io_complete_rw_common(kiocb, res); |
| 2091 | io_put_req(req); |
| 2092 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2095 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2096 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2097 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2098 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2099 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2100 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2101 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2102 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2103 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2104 | |
| 2105 | WRITE_ONCE(req->result, res); |
| 2106 | /* order with io_poll_complete() checking ->result */ |
| 2107 | if (res != -EAGAIN) { |
| 2108 | smp_wmb(); |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2109 | WRITE_ONCE(req->iopoll_completed, 1); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2110 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2115 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2116 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2117 | * accessing the kiocb cookie. |
| 2118 | */ |
| 2119 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 2120 | { |
| 2121 | struct io_ring_ctx *ctx = req->ctx; |
| 2122 | |
| 2123 | /* |
| 2124 | * Track whether we have multiple files in our lists. This will impact |
| 2125 | * how we do polling eventually, not spinning if we're on potentially |
| 2126 | * different devices. |
| 2127 | */ |
| 2128 | if (list_empty(&ctx->poll_list)) { |
| 2129 | ctx->poll_multi_file = false; |
| 2130 | } else if (!ctx->poll_multi_file) { |
| 2131 | struct io_kiocb *list_req; |
| 2132 | |
| 2133 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 2134 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2135 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2136 | ctx->poll_multi_file = true; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * For fast devices, IO may have already completed. If it has, add |
| 2141 | * it to the front so we find it first. |
| 2142 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2143 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2144 | list_add(&req->list, &ctx->poll_list); |
| 2145 | else |
| 2146 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2147 | |
| 2148 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2149 | wq_has_sleeper(&ctx->sqo_wait)) |
| 2150 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2153 | static void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2154 | { |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2155 | int diff = state->has_refs - state->used_refs; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2156 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2157 | if (diff) |
| 2158 | fput_many(state->file, diff); |
| 2159 | state->file = NULL; |
| 2160 | } |
| 2161 | |
| 2162 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2163 | { |
| 2164 | if (state->file) |
| 2165 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /* |
| 2169 | * Get as many references to a file as we have IOs left in this submission, |
| 2170 | * assuming most submissions are for one file, or at least that each file |
| 2171 | * has more than one submission. |
| 2172 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2173 | 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] | 2174 | { |
| 2175 | if (!state) |
| 2176 | return fget(fd); |
| 2177 | |
| 2178 | if (state->file) { |
| 2179 | if (state->fd == fd) { |
| 2180 | state->used_refs++; |
| 2181 | state->ios_left--; |
| 2182 | return state->file; |
| 2183 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2184 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2185 | } |
| 2186 | state->file = fget_many(fd, state->ios_left); |
| 2187 | if (!state->file) |
| 2188 | return NULL; |
| 2189 | |
| 2190 | state->fd = fd; |
| 2191 | state->has_refs = state->ios_left; |
| 2192 | state->used_refs = 1; |
| 2193 | state->ios_left--; |
| 2194 | return state->file; |
| 2195 | } |
| 2196 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2197 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2198 | { |
| 2199 | #ifdef CONFIG_BLOCK |
| 2200 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); |
| 2201 | #else |
| 2202 | return true; |
| 2203 | #endif |
| 2204 | } |
| 2205 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2206 | /* |
| 2207 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2208 | * any file. For now, just ensure that anything potentially problematic is done |
| 2209 | * inline. |
| 2210 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2211 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2212 | { |
| 2213 | umode_t mode = file_inode(file)->i_mode; |
| 2214 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2215 | if (S_ISBLK(mode)) { |
| 2216 | if (io_bdev_nowait(file->f_inode->i_bdev)) |
| 2217 | return true; |
| 2218 | return false; |
| 2219 | } |
| 2220 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2221 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2222 | if (S_ISREG(mode)) { |
| 2223 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
| 2224 | file->f_op != &io_uring_fops) |
| 2225 | return true; |
| 2226 | return false; |
| 2227 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2228 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2229 | /* any ->read/write should understand O_NONBLOCK */ |
| 2230 | if (file->f_flags & O_NONBLOCK) |
| 2231 | return true; |
| 2232 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2233 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2234 | return false; |
| 2235 | |
| 2236 | if (rw == READ) |
| 2237 | return file->f_op->read_iter != NULL; |
| 2238 | |
| 2239 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2242 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2243 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2244 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2245 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2246 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2247 | unsigned ioprio; |
| 2248 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2249 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2250 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2251 | req->flags |= REQ_F_ISREG; |
| 2252 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2253 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2254 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2255 | req->flags |= REQ_F_CUR_POS; |
| 2256 | kiocb->ki_pos = req->file->f_pos; |
| 2257 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2258 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2259 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2260 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2261 | if (unlikely(ret)) |
| 2262 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2263 | |
| 2264 | ioprio = READ_ONCE(sqe->ioprio); |
| 2265 | if (ioprio) { |
| 2266 | ret = ioprio_check_cap(ioprio); |
| 2267 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2268 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2269 | |
| 2270 | kiocb->ki_ioprio = ioprio; |
| 2271 | } else |
| 2272 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2273 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2274 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2275 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2276 | req->flags |= REQ_F_NOWAIT; |
| 2277 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2278 | if (kiocb->ki_flags & IOCB_DIRECT) |
| 2279 | io_get_req_task(req); |
| 2280 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2281 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2282 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2283 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2284 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2285 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2286 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2287 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2288 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2289 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2290 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 2291 | req->result = 0; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2292 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2293 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2294 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2295 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2296 | kiocb->ki_complete = io_complete_rw; |
| 2297 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2298 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2299 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2300 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2301 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2302 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2306 | { |
| 2307 | switch (ret) { |
| 2308 | case -EIOCBQUEUED: |
| 2309 | break; |
| 2310 | case -ERESTARTSYS: |
| 2311 | case -ERESTARTNOINTR: |
| 2312 | case -ERESTARTNOHAND: |
| 2313 | case -ERESTART_RESTARTBLOCK: |
| 2314 | /* |
| 2315 | * We can't just restart the syscall, since previously |
| 2316 | * submitted sqes may already be in progress. Just fail this |
| 2317 | * IO with EINTR. |
| 2318 | */ |
| 2319 | ret = -EINTR; |
| 2320 | /* fall through */ |
| 2321 | default: |
| 2322 | kiocb->ki_complete(kiocb, ret, 0); |
| 2323 | } |
| 2324 | } |
| 2325 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2326 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2327 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2328 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2329 | |
| 2330 | if (req->flags & REQ_F_CUR_POS) |
| 2331 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2332 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2333 | io_complete_rw(kiocb, ret, 0); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2334 | else |
| 2335 | io_rw_done(kiocb, ret); |
| 2336 | } |
| 2337 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2338 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2339 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2340 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2341 | struct io_ring_ctx *ctx = req->ctx; |
| 2342 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2343 | struct io_mapped_ubuf *imu; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2344 | u16 index, buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2345 | size_t offset; |
| 2346 | u64 buf_addr; |
| 2347 | |
| 2348 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2349 | if (unlikely(!ctx->user_bufs)) |
| 2350 | return -EFAULT; |
| 2351 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2352 | buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2353 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2354 | return -EFAULT; |
| 2355 | |
| 2356 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2357 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2358 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2359 | |
| 2360 | /* overflow */ |
| 2361 | if (buf_addr + len < buf_addr) |
| 2362 | return -EFAULT; |
| 2363 | /* not inside the mapped region */ |
| 2364 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2365 | return -EFAULT; |
| 2366 | |
| 2367 | /* |
| 2368 | * May not be a start of buffer, set size appropriately |
| 2369 | * and advance us to the beginning. |
| 2370 | */ |
| 2371 | offset = buf_addr - imu->ubuf; |
| 2372 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2373 | |
| 2374 | if (offset) { |
| 2375 | /* |
| 2376 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2377 | * using the latter parts of a big fixed buffer - it iterates |
| 2378 | * over each segment manually. We can cheat a bit here, because |
| 2379 | * we know that: |
| 2380 | * |
| 2381 | * 1) it's a BVEC iter, we set it up |
| 2382 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2383 | * first and last bvec |
| 2384 | * |
| 2385 | * So just find our index, and adjust the iterator afterwards. |
| 2386 | * If the offset is within the first bvec (or the whole first |
| 2387 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2388 | * since we can just skip the first segment, which may not |
| 2389 | * be PAGE_SIZE aligned. |
| 2390 | */ |
| 2391 | const struct bio_vec *bvec = imu->bvec; |
| 2392 | |
| 2393 | if (offset <= bvec->bv_len) { |
| 2394 | iov_iter_advance(iter, offset); |
| 2395 | } else { |
| 2396 | unsigned long seg_skip; |
| 2397 | |
| 2398 | /* skip first vec */ |
| 2399 | offset -= bvec->bv_len; |
| 2400 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2401 | |
| 2402 | iter->bvec = bvec + seg_skip; |
| 2403 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2404 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2405 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2409 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2410 | } |
| 2411 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2412 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2413 | { |
| 2414 | if (needs_lock) |
| 2415 | mutex_unlock(&ctx->uring_lock); |
| 2416 | } |
| 2417 | |
| 2418 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2419 | { |
| 2420 | /* |
| 2421 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2422 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2423 | * The only exception is when we've detached the request and issue it |
| 2424 | * from an async worker thread, grab the lock for that case. |
| 2425 | */ |
| 2426 | if (needs_lock) |
| 2427 | mutex_lock(&ctx->uring_lock); |
| 2428 | } |
| 2429 | |
| 2430 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2431 | int bgid, struct io_buffer *kbuf, |
| 2432 | bool needs_lock) |
| 2433 | { |
| 2434 | struct io_buffer *head; |
| 2435 | |
| 2436 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2437 | return kbuf; |
| 2438 | |
| 2439 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2440 | |
| 2441 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2442 | |
| 2443 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2444 | if (head) { |
| 2445 | if (!list_empty(&head->list)) { |
| 2446 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2447 | list); |
| 2448 | list_del(&kbuf->list); |
| 2449 | } else { |
| 2450 | kbuf = head; |
| 2451 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2452 | } |
| 2453 | if (*len > kbuf->len) |
| 2454 | *len = kbuf->len; |
| 2455 | } else { |
| 2456 | kbuf = ERR_PTR(-ENOBUFS); |
| 2457 | } |
| 2458 | |
| 2459 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2460 | |
| 2461 | return kbuf; |
| 2462 | } |
| 2463 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2464 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2465 | bool needs_lock) |
| 2466 | { |
| 2467 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2468 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2469 | |
| 2470 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2471 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2472 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2473 | if (IS_ERR(kbuf)) |
| 2474 | return kbuf; |
| 2475 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2476 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2477 | return u64_to_user_ptr(kbuf->addr); |
| 2478 | } |
| 2479 | |
| 2480 | #ifdef CONFIG_COMPAT |
| 2481 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2482 | bool needs_lock) |
| 2483 | { |
| 2484 | struct compat_iovec __user *uiov; |
| 2485 | compat_ssize_t clen; |
| 2486 | void __user *buf; |
| 2487 | ssize_t len; |
| 2488 | |
| 2489 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2490 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2491 | return -EFAULT; |
| 2492 | if (__get_user(clen, &uiov->iov_len)) |
| 2493 | return -EFAULT; |
| 2494 | if (clen < 0) |
| 2495 | return -EINVAL; |
| 2496 | |
| 2497 | len = clen; |
| 2498 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2499 | if (IS_ERR(buf)) |
| 2500 | return PTR_ERR(buf); |
| 2501 | iov[0].iov_base = buf; |
| 2502 | iov[0].iov_len = (compat_size_t) len; |
| 2503 | return 0; |
| 2504 | } |
| 2505 | #endif |
| 2506 | |
| 2507 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2508 | bool needs_lock) |
| 2509 | { |
| 2510 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2511 | void __user *buf; |
| 2512 | ssize_t len; |
| 2513 | |
| 2514 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2515 | return -EFAULT; |
| 2516 | |
| 2517 | len = iov[0].iov_len; |
| 2518 | if (len < 0) |
| 2519 | return -EINVAL; |
| 2520 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2521 | if (IS_ERR(buf)) |
| 2522 | return PTR_ERR(buf); |
| 2523 | iov[0].iov_base = buf; |
| 2524 | iov[0].iov_len = len; |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
| 2528 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2529 | bool needs_lock) |
| 2530 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2531 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2532 | struct io_buffer *kbuf; |
| 2533 | |
| 2534 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2535 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2536 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2537 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2538 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2539 | if (!req->rw.len) |
| 2540 | return 0; |
| 2541 | else if (req->rw.len > 1) |
| 2542 | return -EINVAL; |
| 2543 | |
| 2544 | #ifdef CONFIG_COMPAT |
| 2545 | if (req->ctx->compat) |
| 2546 | return io_compat_import(req, iov, needs_lock); |
| 2547 | #endif |
| 2548 | |
| 2549 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2550 | } |
| 2551 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2552 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2553 | struct iovec **iovec, struct iov_iter *iter, |
| 2554 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2555 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2556 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2557 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2558 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2559 | u8 opcode; |
| 2560 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2561 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2562 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2563 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2564 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2565 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2566 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2567 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2568 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2569 | return -EINVAL; |
| 2570 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2571 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2572 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2573 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
| 2574 | if (IS_ERR(buf)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2575 | *iovec = NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2576 | return PTR_ERR(buf); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2577 | } |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2578 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2579 | } |
| 2580 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2581 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2582 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2583 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2586 | if (req->io) { |
| 2587 | struct io_async_rw *iorw = &req->io->rw; |
| 2588 | |
| 2589 | *iovec = iorw->iov; |
| 2590 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2591 | if (iorw->iov == iorw->fast_iov) |
| 2592 | *iovec = NULL; |
| 2593 | return iorw->size; |
| 2594 | } |
| 2595 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2596 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2597 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2598 | if (!ret) { |
| 2599 | ret = (*iovec)->iov_len; |
| 2600 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 2601 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2602 | *iovec = NULL; |
| 2603 | return ret; |
| 2604 | } |
| 2605 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2606 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2607 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2608 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2609 | iovec, iter); |
| 2610 | #endif |
| 2611 | |
| 2612 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2613 | } |
| 2614 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2615 | /* |
| 2616 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2617 | * by looping over ->read() or ->write() manually. |
| 2618 | */ |
| 2619 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2620 | struct iov_iter *iter) |
| 2621 | { |
| 2622 | ssize_t ret = 0; |
| 2623 | |
| 2624 | /* |
| 2625 | * Don't support polled IO through this interface, and we can't |
| 2626 | * support non-blocking either. For the latter, this just causes |
| 2627 | * the kiocb to be handled from an async context. |
| 2628 | */ |
| 2629 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2630 | return -EOPNOTSUPP; |
| 2631 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2632 | return -EAGAIN; |
| 2633 | |
| 2634 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2635 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2636 | ssize_t nr; |
| 2637 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2638 | if (!iov_iter_is_bvec(iter)) { |
| 2639 | iovec = iov_iter_iovec(iter); |
| 2640 | } else { |
| 2641 | /* fixed buffers import bvec */ |
| 2642 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2643 | + iter->iov_offset; |
| 2644 | iovec.iov_len = min(iter->count, |
| 2645 | iter->bvec->bv_len - iter->iov_offset); |
| 2646 | } |
| 2647 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2648 | if (rw == READ) { |
| 2649 | nr = file->f_op->read(file, iovec.iov_base, |
| 2650 | iovec.iov_len, &kiocb->ki_pos); |
| 2651 | } else { |
| 2652 | nr = file->f_op->write(file, iovec.iov_base, |
| 2653 | iovec.iov_len, &kiocb->ki_pos); |
| 2654 | } |
| 2655 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2656 | if (iov_iter_is_bvec(iter)) |
| 2657 | kunmap(iter->bvec->bv_page); |
| 2658 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2659 | if (nr < 0) { |
| 2660 | if (!ret) |
| 2661 | ret = nr; |
| 2662 | break; |
| 2663 | } |
| 2664 | ret += nr; |
| 2665 | if (nr != iovec.iov_len) |
| 2666 | break; |
| 2667 | iov_iter_advance(iter, nr); |
| 2668 | } |
| 2669 | |
| 2670 | return ret; |
| 2671 | } |
| 2672 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2673 | static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2674 | struct iovec *iovec, struct iovec *fast_iov, |
| 2675 | struct iov_iter *iter) |
| 2676 | { |
| 2677 | req->io->rw.nr_segs = iter->nr_segs; |
| 2678 | req->io->rw.size = io_size; |
| 2679 | req->io->rw.iov = iovec; |
| 2680 | if (!req->io->rw.iov) { |
| 2681 | req->io->rw.iov = req->io->rw.fast_iov; |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 2682 | if (req->io->rw.iov != fast_iov) |
| 2683 | memcpy(req->io->rw.iov, fast_iov, |
| 2684 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2685 | } else { |
| 2686 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2690 | static inline int __io_alloc_async_ctx(struct io_kiocb *req) |
| 2691 | { |
| 2692 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
| 2693 | return req->io == NULL; |
| 2694 | } |
| 2695 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2696 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2697 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2698 | if (!io_op_defs[req->opcode].async_ctx) |
| 2699 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2700 | |
| 2701 | return __io_alloc_async_ctx(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2704 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2705 | struct iovec *iovec, struct iovec *fast_iov, |
| 2706 | struct iov_iter *iter) |
| 2707 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2708 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2709 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2710 | if (!req->io) { |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2711 | if (__io_alloc_async_ctx(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2712 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2713 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2714 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2715 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2716 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2719 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2720 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2721 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2722 | struct io_async_ctx *io; |
| 2723 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2724 | ssize_t ret; |
| 2725 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2726 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2727 | if (ret) |
| 2728 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2729 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2730 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2731 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2732 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2733 | /* either don't need iovec imported or already have it */ |
| 2734 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2735 | return 0; |
| 2736 | |
| 2737 | io = req->io; |
| 2738 | io->rw.iov = io->rw.fast_iov; |
| 2739 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2740 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2741 | req->io = io; |
| 2742 | if (ret < 0) |
| 2743 | return ret; |
| 2744 | |
| 2745 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2746 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2749 | static void __io_async_buf_error(struct io_kiocb *req, int error) |
| 2750 | { |
| 2751 | struct io_ring_ctx *ctx = req->ctx; |
| 2752 | |
| 2753 | spin_lock_irq(&ctx->completion_lock); |
| 2754 | io_cqring_fill_event(req, error); |
| 2755 | io_commit_cqring(ctx); |
| 2756 | spin_unlock_irq(&ctx->completion_lock); |
| 2757 | |
| 2758 | io_cqring_ev_posted(ctx); |
| 2759 | req_set_fail_links(req); |
| 2760 | io_double_put_req(req); |
| 2761 | } |
| 2762 | |
| 2763 | static void io_async_buf_cancel(struct callback_head *cb) |
| 2764 | { |
| 2765 | struct io_async_rw *rw; |
| 2766 | struct io_kiocb *req; |
| 2767 | |
| 2768 | rw = container_of(cb, struct io_async_rw, task_work); |
| 2769 | req = rw->wpq.wait.private; |
| 2770 | __io_async_buf_error(req, -ECANCELED); |
| 2771 | } |
| 2772 | |
| 2773 | static void io_async_buf_retry(struct callback_head *cb) |
| 2774 | { |
| 2775 | struct io_async_rw *rw; |
| 2776 | struct io_ring_ctx *ctx; |
| 2777 | struct io_kiocb *req; |
| 2778 | |
| 2779 | rw = container_of(cb, struct io_async_rw, task_work); |
| 2780 | req = rw->wpq.wait.private; |
| 2781 | ctx = req->ctx; |
| 2782 | |
| 2783 | __set_current_state(TASK_RUNNING); |
| 2784 | if (!io_sq_thread_acquire_mm(ctx, req)) { |
| 2785 | mutex_lock(&ctx->uring_lock); |
| 2786 | __io_queue_sqe(req, NULL); |
| 2787 | mutex_unlock(&ctx->uring_lock); |
| 2788 | } else { |
| 2789 | __io_async_buf_error(req, -EFAULT); |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 2794 | int sync, void *arg) |
| 2795 | { |
| 2796 | struct wait_page_queue *wpq; |
| 2797 | struct io_kiocb *req = wait->private; |
| 2798 | struct io_async_rw *rw = &req->io->rw; |
| 2799 | struct wait_page_key *key = arg; |
| 2800 | struct task_struct *tsk; |
| 2801 | int ret; |
| 2802 | |
| 2803 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 2804 | |
| 2805 | ret = wake_page_match(wpq, key); |
| 2806 | if (ret != 1) |
| 2807 | return ret; |
| 2808 | |
| 2809 | list_del_init(&wait->entry); |
| 2810 | |
| 2811 | init_task_work(&rw->task_work, io_async_buf_retry); |
| 2812 | /* submit ref gets dropped, acquire a new one */ |
| 2813 | refcount_inc(&req->refs); |
| 2814 | tsk = req->task; |
| 2815 | ret = task_work_add(tsk, &rw->task_work, true); |
| 2816 | if (unlikely(ret)) { |
| 2817 | /* queue just for cancelation */ |
| 2818 | init_task_work(&rw->task_work, io_async_buf_cancel); |
| 2819 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 2820 | task_work_add(tsk, &rw->task_work, true); |
| 2821 | } |
| 2822 | wake_up_process(tsk); |
| 2823 | return 1; |
| 2824 | } |
| 2825 | |
| 2826 | static bool io_rw_should_retry(struct io_kiocb *req) |
| 2827 | { |
| 2828 | struct kiocb *kiocb = &req->rw.kiocb; |
| 2829 | int ret; |
| 2830 | |
| 2831 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 2832 | if (req->flags & REQ_F_NOWAIT) |
| 2833 | return false; |
| 2834 | |
| 2835 | /* already tried, or we're doing O_DIRECT */ |
| 2836 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ)) |
| 2837 | return false; |
| 2838 | /* |
| 2839 | * just use poll if we can, and don't attempt if the fs doesn't |
| 2840 | * support callback based unlocks |
| 2841 | */ |
| 2842 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 2843 | return false; |
| 2844 | |
| 2845 | /* |
| 2846 | * If request type doesn't require req->io to defer in general, |
| 2847 | * we need to allocate it here |
| 2848 | */ |
| 2849 | if (!req->io && __io_alloc_async_ctx(req)) |
| 2850 | return false; |
| 2851 | |
| 2852 | ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq, |
| 2853 | io_async_buf_func, req); |
| 2854 | if (!ret) { |
| 2855 | io_get_req_task(req); |
| 2856 | return true; |
| 2857 | } |
| 2858 | |
| 2859 | return false; |
| 2860 | } |
| 2861 | |
| 2862 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 2863 | { |
| 2864 | if (req->file->f_op->read_iter) |
| 2865 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
| 2866 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); |
| 2867 | } |
| 2868 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2869 | static int io_read(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2870 | { |
| 2871 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2872 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2873 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2874 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2875 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2876 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2877 | ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2878 | if (ret < 0) |
| 2879 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2880 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2881 | /* Ensure we clear previously set non-block flag */ |
| 2882 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2883 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2884 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2885 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2886 | io_size = ret; |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 2887 | if (req->flags & REQ_F_LINK_HEAD) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2888 | req->result = io_size; |
| 2889 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 2890 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2891 | if (force_nonblock && !io_file_supports_async(req->file, READ)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2892 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2893 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2894 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2895 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2896 | if (!ret) { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2897 | unsigned long nr_segs = iter.nr_segs; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2898 | ssize_t ret2 = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2899 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2900 | ret2 = io_iter_do_read(req, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2901 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2902 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2903 | if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2904 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2905 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2906 | iter.count = iov_count; |
| 2907 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2908 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2909 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2910 | inline_vecs, &iter); |
| 2911 | if (ret) |
| 2912 | goto out_free; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2913 | /* if we can retry, do so with the callbacks armed */ |
| 2914 | if (io_rw_should_retry(req)) { |
| 2915 | ret2 = io_iter_do_read(req, &iter); |
| 2916 | if (ret2 == -EIOCBQUEUED) { |
| 2917 | goto out_free; |
| 2918 | } else if (ret2 != -EAGAIN) { |
| 2919 | kiocb_done(kiocb, ret2); |
| 2920 | goto out_free; |
| 2921 | } |
| 2922 | } |
| 2923 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2924 | return -EAGAIN; |
| 2925 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2926 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2927 | out_free: |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 2928 | if (!(req->flags & REQ_F_NEED_CLEANUP)) |
| 2929 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2930 | return ret; |
| 2931 | } |
| 2932 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2933 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2934 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2935 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2936 | struct io_async_ctx *io; |
| 2937 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2938 | ssize_t ret; |
| 2939 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2940 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2941 | if (ret) |
| 2942 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2943 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2944 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2945 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2946 | |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 2947 | req->fsize = rlimit(RLIMIT_FSIZE); |
| 2948 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2949 | /* either don't need iovec imported or already have it */ |
| 2950 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2951 | return 0; |
| 2952 | |
| 2953 | io = req->io; |
| 2954 | io->rw.iov = io->rw.fast_iov; |
| 2955 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2956 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2957 | req->io = io; |
| 2958 | if (ret < 0) |
| 2959 | return ret; |
| 2960 | |
| 2961 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2962 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2965 | static int io_write(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2966 | { |
| 2967 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2968 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2969 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2970 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2971 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2972 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2973 | ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2974 | if (ret < 0) |
| 2975 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2976 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2977 | /* Ensure we clear previously set non-block flag */ |
| 2978 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2979 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2980 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2981 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2982 | io_size = ret; |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 2983 | if (req->flags & REQ_F_LINK_HEAD) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2984 | req->result = io_size; |
| 2985 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 2986 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2987 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2988 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2989 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2990 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2991 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2992 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2993 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2994 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2995 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2996 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2997 | if (!ret) { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2998 | unsigned long nr_segs = iter.nr_segs; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2999 | ssize_t ret2; |
| 3000 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3001 | /* |
| 3002 | * Open-code file_start_write here to grab freeze protection, |
| 3003 | * which will be released by another thread in |
| 3004 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3005 | * released so that it doesn't complain about the held lock when |
| 3006 | * we return to userspace. |
| 3007 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 3008 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3009 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3010 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3011 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3012 | SB_FREEZE_WRITE); |
| 3013 | } |
| 3014 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3015 | |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3016 | if (!force_nonblock) |
| 3017 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize; |
| 3018 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3019 | if (req->file->f_op->write_iter) |
| 3020 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3021 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3022 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3023 | |
| 3024 | if (!force_nonblock) |
| 3025 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 3026 | |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 3027 | /* |
Chucheng Luo | bff6035 | 2020-03-25 11:31:38 +0800 | [diff] [blame] | 3028 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 3029 | * retry them without IOCB_NOWAIT. |
| 3030 | */ |
| 3031 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3032 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3033 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3034 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3035 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 3036 | iter.count = iov_count; |
| 3037 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3038 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3039 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3040 | inline_vecs, &iter); |
| 3041 | if (ret) |
| 3042 | goto out_free; |
| 3043 | return -EAGAIN; |
| 3044 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3045 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3046 | out_free: |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3047 | if (!(req->flags & REQ_F_NEED_CLEANUP)) |
| 3048 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3049 | return ret; |
| 3050 | } |
| 3051 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3052 | static int __io_splice_prep(struct io_kiocb *req, |
| 3053 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3054 | { |
| 3055 | struct io_splice* sp = &req->splice; |
| 3056 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 3057 | int ret; |
| 3058 | |
| 3059 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3060 | return 0; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3061 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3062 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3063 | |
| 3064 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3065 | sp->len = READ_ONCE(sqe->len); |
| 3066 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3067 | |
| 3068 | if (unlikely(sp->flags & ~valid_flags)) |
| 3069 | return -EINVAL; |
| 3070 | |
| 3071 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 3072 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3073 | if (ret) |
| 3074 | return ret; |
| 3075 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3076 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3077 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3078 | /* |
| 3079 | * Splice operation will be punted aync, and here need to |
| 3080 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3081 | */ |
| 3082 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3083 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3084 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3085 | |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3089 | static int io_tee_prep(struct io_kiocb *req, |
| 3090 | const struct io_uring_sqe *sqe) |
| 3091 | { |
| 3092 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3093 | return -EINVAL; |
| 3094 | return __io_splice_prep(req, sqe); |
| 3095 | } |
| 3096 | |
| 3097 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3098 | { |
| 3099 | struct io_splice *sp = &req->splice; |
| 3100 | struct file *in = sp->file_in; |
| 3101 | struct file *out = sp->file_out; |
| 3102 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3103 | long ret = 0; |
| 3104 | |
| 3105 | if (force_nonblock) |
| 3106 | return -EAGAIN; |
| 3107 | if (sp->len) |
| 3108 | ret = do_tee(in, out, sp->len, flags); |
| 3109 | |
| 3110 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3111 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3112 | |
| 3113 | io_cqring_add_event(req, ret); |
| 3114 | if (ret != sp->len) |
| 3115 | req_set_fail_links(req); |
| 3116 | io_put_req(req); |
| 3117 | return 0; |
| 3118 | } |
| 3119 | |
| 3120 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3121 | { |
| 3122 | struct io_splice* sp = &req->splice; |
| 3123 | |
| 3124 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3125 | sp->off_out = READ_ONCE(sqe->off); |
| 3126 | return __io_splice_prep(req, sqe); |
| 3127 | } |
| 3128 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3129 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3130 | { |
| 3131 | struct io_splice *sp = &req->splice; |
| 3132 | struct file *in = sp->file_in; |
| 3133 | struct file *out = sp->file_out; |
| 3134 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3135 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3136 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3137 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3138 | if (force_nonblock) |
| 3139 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3140 | |
| 3141 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3142 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3143 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3144 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3145 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3146 | |
| 3147 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3148 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3149 | |
| 3150 | io_cqring_add_event(req, ret); |
| 3151 | if (ret != sp->len) |
| 3152 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3153 | io_put_req(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3154 | return 0; |
| 3155 | } |
| 3156 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3157 | /* |
| 3158 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3159 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3160 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3161 | { |
| 3162 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3163 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3164 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3165 | return -EINVAL; |
| 3166 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3167 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3168 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3169 | return 0; |
| 3170 | } |
| 3171 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3172 | 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] | 3173 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3174 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3175 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3176 | if (!req->file) |
| 3177 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3178 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3179 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3180 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3181 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3182 | return -EINVAL; |
| 3183 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3184 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3185 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3186 | return -EINVAL; |
| 3187 | |
| 3188 | req->sync.off = READ_ONCE(sqe->off); |
| 3189 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3193 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3194 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3195 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3196 | int ret; |
| 3197 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3198 | /* fsync always requires a blocking context */ |
| 3199 | if (force_nonblock) |
| 3200 | return -EAGAIN; |
| 3201 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3202 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3203 | end > 0 ? end : LLONG_MAX, |
| 3204 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3205 | if (ret < 0) |
| 3206 | req_set_fail_links(req); |
| 3207 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3208 | io_put_req(req); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3209 | return 0; |
| 3210 | } |
| 3211 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3212 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3213 | const struct io_uring_sqe *sqe) |
| 3214 | { |
| 3215 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3216 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3217 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3218 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3219 | |
| 3220 | req->sync.off = READ_ONCE(sqe->off); |
| 3221 | req->sync.len = READ_ONCE(sqe->addr); |
| 3222 | req->sync.mode = READ_ONCE(sqe->len); |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3223 | req->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3224 | return 0; |
| 3225 | } |
| 3226 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3227 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3228 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3229 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3230 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3231 | /* fallocate always requiring blocking context */ |
| 3232 | if (force_nonblock) |
| 3233 | return -EAGAIN; |
| 3234 | |
| 3235 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize; |
| 3236 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3237 | req->sync.len); |
| 3238 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 3239 | if (ret < 0) |
| 3240 | req_set_fail_links(req); |
| 3241 | io_cqring_add_event(req, ret); |
| 3242 | io_put_req(req); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3243 | return 0; |
| 3244 | } |
| 3245 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3246 | 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] | 3247 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3248 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3249 | int ret; |
| 3250 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3251 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3252 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3253 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3254 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3255 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3256 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3257 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3258 | /* open.how should be already initialised */ |
| 3259 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3260 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3261 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3262 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3263 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3264 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3265 | if (IS_ERR(req->open.filename)) { |
| 3266 | ret = PTR_ERR(req->open.filename); |
| 3267 | req->open.filename = NULL; |
| 3268 | return ret; |
| 3269 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3270 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3271 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3272 | return 0; |
| 3273 | } |
| 3274 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3275 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3276 | { |
| 3277 | u64 flags, mode; |
| 3278 | |
| 3279 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3280 | return 0; |
| 3281 | mode = READ_ONCE(sqe->len); |
| 3282 | flags = READ_ONCE(sqe->open_flags); |
| 3283 | req->open.how = build_open_how(flags, mode); |
| 3284 | return __io_openat_prep(req, sqe); |
| 3285 | } |
| 3286 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3287 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3288 | { |
| 3289 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3290 | size_t len; |
| 3291 | int ret; |
| 3292 | |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3293 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3294 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3295 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3296 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3297 | if (len < OPEN_HOW_SIZE_VER0) |
| 3298 | return -EINVAL; |
| 3299 | |
| 3300 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3301 | len); |
| 3302 | if (ret) |
| 3303 | return ret; |
| 3304 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3305 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3306 | } |
| 3307 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3308 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3309 | { |
| 3310 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3311 | struct file *file; |
| 3312 | int ret; |
| 3313 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 3314 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3315 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3316 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3317 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3318 | if (ret) |
| 3319 | goto err; |
| 3320 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3321 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3322 | if (ret < 0) |
| 3323 | goto err; |
| 3324 | |
| 3325 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 3326 | if (IS_ERR(file)) { |
| 3327 | put_unused_fd(ret); |
| 3328 | ret = PTR_ERR(file); |
| 3329 | } else { |
| 3330 | fsnotify_open(file); |
| 3331 | fd_install(ret, file); |
| 3332 | } |
| 3333 | err: |
| 3334 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3335 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3336 | if (ret < 0) |
| 3337 | req_set_fail_links(req); |
| 3338 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3339 | io_put_req(req); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3340 | return 0; |
| 3341 | } |
| 3342 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3343 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3344 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3345 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3348 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3349 | const struct io_uring_sqe *sqe) |
| 3350 | { |
| 3351 | struct io_provide_buf *p = &req->pbuf; |
| 3352 | u64 tmp; |
| 3353 | |
| 3354 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3355 | return -EINVAL; |
| 3356 | |
| 3357 | tmp = READ_ONCE(sqe->fd); |
| 3358 | if (!tmp || tmp > USHRT_MAX) |
| 3359 | return -EINVAL; |
| 3360 | |
| 3361 | memset(p, 0, sizeof(*p)); |
| 3362 | p->nbufs = tmp; |
| 3363 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3368 | int bgid, unsigned nbufs) |
| 3369 | { |
| 3370 | unsigned i = 0; |
| 3371 | |
| 3372 | /* shouldn't happen */ |
| 3373 | if (!nbufs) |
| 3374 | return 0; |
| 3375 | |
| 3376 | /* the head kbuf is the list itself */ |
| 3377 | while (!list_empty(&buf->list)) { |
| 3378 | struct io_buffer *nxt; |
| 3379 | |
| 3380 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3381 | list_del(&nxt->list); |
| 3382 | kfree(nxt); |
| 3383 | if (++i == nbufs) |
| 3384 | return i; |
| 3385 | } |
| 3386 | i++; |
| 3387 | kfree(buf); |
| 3388 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3389 | |
| 3390 | return i; |
| 3391 | } |
| 3392 | |
| 3393 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock) |
| 3394 | { |
| 3395 | struct io_provide_buf *p = &req->pbuf; |
| 3396 | struct io_ring_ctx *ctx = req->ctx; |
| 3397 | struct io_buffer *head; |
| 3398 | int ret = 0; |
| 3399 | |
| 3400 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3401 | |
| 3402 | lockdep_assert_held(&ctx->uring_lock); |
| 3403 | |
| 3404 | ret = -ENOENT; |
| 3405 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3406 | if (head) |
| 3407 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 3408 | |
| 3409 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3410 | if (ret < 0) |
| 3411 | req_set_fail_links(req); |
| 3412 | io_cqring_add_event(req, ret); |
| 3413 | io_put_req(req); |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3417 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3418 | const struct io_uring_sqe *sqe) |
| 3419 | { |
| 3420 | struct io_provide_buf *p = &req->pbuf; |
| 3421 | u64 tmp; |
| 3422 | |
| 3423 | if (sqe->ioprio || sqe->rw_flags) |
| 3424 | return -EINVAL; |
| 3425 | |
| 3426 | tmp = READ_ONCE(sqe->fd); |
| 3427 | if (!tmp || tmp > USHRT_MAX) |
| 3428 | return -E2BIG; |
| 3429 | p->nbufs = tmp; |
| 3430 | p->addr = READ_ONCE(sqe->addr); |
| 3431 | p->len = READ_ONCE(sqe->len); |
| 3432 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3433 | 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] | 3434 | return -EFAULT; |
| 3435 | |
| 3436 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3437 | tmp = READ_ONCE(sqe->off); |
| 3438 | if (tmp > USHRT_MAX) |
| 3439 | return -E2BIG; |
| 3440 | p->bid = tmp; |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
| 3444 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3445 | { |
| 3446 | struct io_buffer *buf; |
| 3447 | u64 addr = pbuf->addr; |
| 3448 | int i, bid = pbuf->bid; |
| 3449 | |
| 3450 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3451 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3452 | if (!buf) |
| 3453 | break; |
| 3454 | |
| 3455 | buf->addr = addr; |
| 3456 | buf->len = pbuf->len; |
| 3457 | buf->bid = bid; |
| 3458 | addr += pbuf->len; |
| 3459 | bid++; |
| 3460 | if (!*head) { |
| 3461 | INIT_LIST_HEAD(&buf->list); |
| 3462 | *head = buf; |
| 3463 | } else { |
| 3464 | list_add_tail(&buf->list, &(*head)->list); |
| 3465 | } |
| 3466 | } |
| 3467 | |
| 3468 | return i ? i : -ENOMEM; |
| 3469 | } |
| 3470 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3471 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock) |
| 3472 | { |
| 3473 | struct io_provide_buf *p = &req->pbuf; |
| 3474 | struct io_ring_ctx *ctx = req->ctx; |
| 3475 | struct io_buffer *head, *list; |
| 3476 | int ret = 0; |
| 3477 | |
| 3478 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3479 | |
| 3480 | lockdep_assert_held(&ctx->uring_lock); |
| 3481 | |
| 3482 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3483 | |
| 3484 | ret = io_add_buffers(p, &head); |
| 3485 | if (ret < 0) |
| 3486 | goto out; |
| 3487 | |
| 3488 | if (!list) { |
| 3489 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3490 | GFP_KERNEL); |
| 3491 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3492 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3493 | goto out; |
| 3494 | } |
| 3495 | } |
| 3496 | out: |
| 3497 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3498 | if (ret < 0) |
| 3499 | req_set_fail_links(req); |
| 3500 | io_cqring_add_event(req, ret); |
| 3501 | io_put_req(req); |
| 3502 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3505 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3506 | const struct io_uring_sqe *sqe) |
| 3507 | { |
| 3508 | #if defined(CONFIG_EPOLL) |
| 3509 | if (sqe->ioprio || sqe->buf_index) |
| 3510 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3511 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3512 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3513 | |
| 3514 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3515 | req->epoll.op = READ_ONCE(sqe->len); |
| 3516 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3517 | |
| 3518 | if (ep_op_has_event(req->epoll.op)) { |
| 3519 | struct epoll_event __user *ev; |
| 3520 | |
| 3521 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3522 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3523 | return -EFAULT; |
| 3524 | } |
| 3525 | |
| 3526 | return 0; |
| 3527 | #else |
| 3528 | return -EOPNOTSUPP; |
| 3529 | #endif |
| 3530 | } |
| 3531 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3532 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3533 | { |
| 3534 | #if defined(CONFIG_EPOLL) |
| 3535 | struct io_epoll *ie = &req->epoll; |
| 3536 | int ret; |
| 3537 | |
| 3538 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3539 | if (force_nonblock && ret == -EAGAIN) |
| 3540 | return -EAGAIN; |
| 3541 | |
| 3542 | if (ret < 0) |
| 3543 | req_set_fail_links(req); |
| 3544 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3545 | io_put_req(req); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3546 | return 0; |
| 3547 | #else |
| 3548 | return -EOPNOTSUPP; |
| 3549 | #endif |
| 3550 | } |
| 3551 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3552 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3553 | { |
| 3554 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3555 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3556 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3557 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3558 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3559 | |
| 3560 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3561 | req->madvise.len = READ_ONCE(sqe->len); |
| 3562 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3563 | return 0; |
| 3564 | #else |
| 3565 | return -EOPNOTSUPP; |
| 3566 | #endif |
| 3567 | } |
| 3568 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3569 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3570 | { |
| 3571 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3572 | struct io_madvise *ma = &req->madvise; |
| 3573 | int ret; |
| 3574 | |
| 3575 | if (force_nonblock) |
| 3576 | return -EAGAIN; |
| 3577 | |
| 3578 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3579 | if (ret < 0) |
| 3580 | req_set_fail_links(req); |
| 3581 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3582 | io_put_req(req); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3583 | return 0; |
| 3584 | #else |
| 3585 | return -EOPNOTSUPP; |
| 3586 | #endif |
| 3587 | } |
| 3588 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3589 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3590 | { |
| 3591 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3592 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3593 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3594 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3595 | |
| 3596 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3597 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3598 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3599 | return 0; |
| 3600 | } |
| 3601 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3602 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3603 | { |
| 3604 | struct io_fadvise *fa = &req->fadvise; |
| 3605 | int ret; |
| 3606 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3607 | if (force_nonblock) { |
| 3608 | switch (fa->advice) { |
| 3609 | case POSIX_FADV_NORMAL: |
| 3610 | case POSIX_FADV_RANDOM: |
| 3611 | case POSIX_FADV_SEQUENTIAL: |
| 3612 | break; |
| 3613 | default: |
| 3614 | return -EAGAIN; |
| 3615 | } |
| 3616 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3617 | |
| 3618 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3619 | if (ret < 0) |
| 3620 | req_set_fail_links(req); |
| 3621 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3622 | io_put_req(req); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3623 | return 0; |
| 3624 | } |
| 3625 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3626 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3627 | { |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3628 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3629 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3630 | if (sqe->ioprio || sqe->buf_index) |
| 3631 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3632 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3633 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3634 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3635 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 3636 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3637 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3638 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3639 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3640 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3641 | return 0; |
| 3642 | } |
| 3643 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3644 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3645 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3646 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3647 | int ret; |
| 3648 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3649 | if (force_nonblock) { |
| 3650 | /* only need file table for an actual valid fd */ |
| 3651 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 3652 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3653 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3654 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3655 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3656 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 3657 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3658 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3659 | if (ret < 0) |
| 3660 | req_set_fail_links(req); |
| 3661 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3662 | io_put_req(req); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3663 | return 0; |
| 3664 | } |
| 3665 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3666 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3667 | { |
| 3668 | /* |
| 3669 | * 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] | 3670 | * leave the 'file' in an undeterminate state, and here need to modify |
| 3671 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3672 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3673 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3674 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3675 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3676 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3677 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3678 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 3679 | sqe->rw_flags || sqe->buf_index) |
| 3680 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3681 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3682 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3683 | |
| 3684 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 3685 | if ((req->file && req->file->f_op == &io_uring_fops) || |
| 3686 | req->close.fd == req->ctx->ring_fd) |
| 3687 | return -EBADF; |
| 3688 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3689 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3690 | return 0; |
| 3691 | } |
| 3692 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3693 | static int io_close(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3694 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3695 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3696 | int ret; |
| 3697 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3698 | /* might be already done during nonblock submission */ |
| 3699 | if (!close->put_file) { |
| 3700 | ret = __close_fd_get_file(close->fd, &close->put_file); |
| 3701 | if (ret < 0) |
| 3702 | return (ret == -ENOENT) ? -EBADF : ret; |
| 3703 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3704 | |
| 3705 | /* 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] | 3706 | if (close->put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3707 | /* was never set, but play safe */ |
| 3708 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3709 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3710 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3711 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3712 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3713 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3714 | /* No ->flush() or already async, safely close from here */ |
| 3715 | ret = filp_close(close->put_file, req->work.files); |
| 3716 | if (ret < 0) |
| 3717 | req_set_fail_links(req); |
| 3718 | io_cqring_add_event(req, ret); |
| 3719 | fput(close->put_file); |
| 3720 | close->put_file = NULL; |
| 3721 | io_put_req(req); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3722 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3725 | 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] | 3726 | { |
| 3727 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3728 | |
| 3729 | if (!req->file) |
| 3730 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3731 | |
| 3732 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3733 | return -EINVAL; |
| 3734 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3735 | return -EINVAL; |
| 3736 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3737 | req->sync.off = READ_ONCE(sqe->off); |
| 3738 | req->sync.len = READ_ONCE(sqe->len); |
| 3739 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3740 | return 0; |
| 3741 | } |
| 3742 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3743 | 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] | 3744 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3745 | int ret; |
| 3746 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3747 | /* sync_file_range always requires a blocking context */ |
| 3748 | if (force_nonblock) |
| 3749 | return -EAGAIN; |
| 3750 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3751 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3752 | req->sync.flags); |
| 3753 | if (ret < 0) |
| 3754 | req_set_fail_links(req); |
| 3755 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3756 | io_put_req(req); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3757 | return 0; |
| 3758 | } |
| 3759 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 3760 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3761 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3762 | struct io_async_msghdr *kmsg) |
| 3763 | { |
| 3764 | if (req->io) |
| 3765 | return -EAGAIN; |
| 3766 | if (io_alloc_async_ctx(req)) { |
| 3767 | if (kmsg->iov != kmsg->fast_iov) |
| 3768 | kfree(kmsg->iov); |
| 3769 | return -ENOMEM; |
| 3770 | } |
| 3771 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3772 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3773 | return -EAGAIN; |
| 3774 | } |
| 3775 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3776 | 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] | 3777 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3778 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3779 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3780 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3781 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 3782 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3783 | return -EINVAL; |
| 3784 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3785 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3786 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3787 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3788 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3789 | #ifdef CONFIG_COMPAT |
| 3790 | if (req->ctx->compat) |
| 3791 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3792 | #endif |
| 3793 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3794 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3795 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3796 | /* iovec is already imported */ |
| 3797 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3798 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3799 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3800 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3801 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3802 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3803 | if (!ret) |
| 3804 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3805 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3808 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3809 | { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3810 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3811 | struct socket *sock; |
| 3812 | int ret; |
| 3813 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3814 | sock = sock_from_file(req->file, &ret); |
| 3815 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3816 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3817 | unsigned flags; |
| 3818 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3819 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3820 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3821 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3822 | /* if iov is set, it's allocated already */ |
| 3823 | if (!kmsg->iov) |
| 3824 | kmsg->iov = kmsg->fast_iov; |
| 3825 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3826 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3827 | struct io_sr_msg *sr = &req->sr_msg; |
| 3828 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3829 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3830 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3831 | |
| 3832 | io.msg.iov = io.msg.fast_iov; |
| 3833 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3834 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3835 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3836 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3837 | } |
| 3838 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3839 | flags = req->sr_msg.msg_flags; |
| 3840 | if (flags & MSG_DONTWAIT) |
| 3841 | req->flags |= REQ_F_NOWAIT; |
| 3842 | else if (force_nonblock) |
| 3843 | flags |= MSG_DONTWAIT; |
| 3844 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3845 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3846 | if (force_nonblock && ret == -EAGAIN) |
| 3847 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3848 | if (ret == -ERESTARTSYS) |
| 3849 | ret = -EINTR; |
| 3850 | } |
| 3851 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3852 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3853 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3854 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3855 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3856 | if (ret < 0) |
| 3857 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3858 | io_put_req(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3859 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3860 | } |
| 3861 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3862 | static int io_send(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3863 | { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3864 | struct socket *sock; |
| 3865 | int ret; |
| 3866 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3867 | sock = sock_from_file(req->file, &ret); |
| 3868 | if (sock) { |
| 3869 | struct io_sr_msg *sr = &req->sr_msg; |
| 3870 | struct msghdr msg; |
| 3871 | struct iovec iov; |
| 3872 | unsigned flags; |
| 3873 | |
| 3874 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3875 | &msg.msg_iter); |
| 3876 | if (ret) |
| 3877 | return ret; |
| 3878 | |
| 3879 | msg.msg_name = NULL; |
| 3880 | msg.msg_control = NULL; |
| 3881 | msg.msg_controllen = 0; |
| 3882 | msg.msg_namelen = 0; |
| 3883 | |
| 3884 | flags = req->sr_msg.msg_flags; |
| 3885 | if (flags & MSG_DONTWAIT) |
| 3886 | req->flags |= REQ_F_NOWAIT; |
| 3887 | else if (force_nonblock) |
| 3888 | flags |= MSG_DONTWAIT; |
| 3889 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3890 | msg.msg_flags = flags; |
| 3891 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3892 | if (force_nonblock && ret == -EAGAIN) |
| 3893 | return -EAGAIN; |
| 3894 | if (ret == -ERESTARTSYS) |
| 3895 | ret = -EINTR; |
| 3896 | } |
| 3897 | |
| 3898 | io_cqring_add_event(req, ret); |
| 3899 | if (ret < 0) |
| 3900 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3901 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3902 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3905 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 3906 | { |
| 3907 | struct io_sr_msg *sr = &req->sr_msg; |
| 3908 | struct iovec __user *uiov; |
| 3909 | size_t iov_len; |
| 3910 | int ret; |
| 3911 | |
| 3912 | ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr, |
| 3913 | &uiov, &iov_len); |
| 3914 | if (ret) |
| 3915 | return ret; |
| 3916 | |
| 3917 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3918 | if (iov_len > 1) |
| 3919 | return -EINVAL; |
| 3920 | if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov))) |
| 3921 | return -EFAULT; |
| 3922 | sr->len = io->msg.iov[0].iov_len; |
| 3923 | iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1, |
| 3924 | sr->len); |
| 3925 | io->msg.iov = NULL; |
| 3926 | } else { |
| 3927 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
| 3928 | &io->msg.iov, &io->msg.msg.msg_iter); |
| 3929 | if (ret > 0) |
| 3930 | ret = 0; |
| 3931 | } |
| 3932 | |
| 3933 | return ret; |
| 3934 | } |
| 3935 | |
| 3936 | #ifdef CONFIG_COMPAT |
| 3937 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
| 3938 | struct io_async_ctx *io) |
| 3939 | { |
| 3940 | struct compat_msghdr __user *msg_compat; |
| 3941 | struct io_sr_msg *sr = &req->sr_msg; |
| 3942 | struct compat_iovec __user *uiov; |
| 3943 | compat_uptr_t ptr; |
| 3944 | compat_size_t len; |
| 3945 | int ret; |
| 3946 | |
| 3947 | msg_compat = (struct compat_msghdr __user *) sr->msg; |
| 3948 | ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr, |
| 3949 | &ptr, &len); |
| 3950 | if (ret) |
| 3951 | return ret; |
| 3952 | |
| 3953 | uiov = compat_ptr(ptr); |
| 3954 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3955 | compat_ssize_t clen; |
| 3956 | |
| 3957 | if (len > 1) |
| 3958 | return -EINVAL; |
| 3959 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3960 | return -EFAULT; |
| 3961 | if (__get_user(clen, &uiov->iov_len)) |
| 3962 | return -EFAULT; |
| 3963 | if (clen < 0) |
| 3964 | return -EINVAL; |
| 3965 | sr->len = io->msg.iov[0].iov_len; |
| 3966 | io->msg.iov = NULL; |
| 3967 | } else { |
| 3968 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, |
| 3969 | &io->msg.iov, |
| 3970 | &io->msg.msg.msg_iter); |
| 3971 | if (ret < 0) |
| 3972 | return ret; |
| 3973 | } |
| 3974 | |
| 3975 | return 0; |
| 3976 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3977 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3978 | |
| 3979 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 3980 | { |
| 3981 | io->msg.iov = io->msg.fast_iov; |
| 3982 | |
| 3983 | #ifdef CONFIG_COMPAT |
| 3984 | if (req->ctx->compat) |
| 3985 | return __io_compat_recvmsg_copy_hdr(req, io); |
| 3986 | #endif |
| 3987 | |
| 3988 | return __io_recvmsg_copy_hdr(req, io); |
| 3989 | } |
| 3990 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3991 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
| 3992 | int *cflags, bool needs_lock) |
| 3993 | { |
| 3994 | struct io_sr_msg *sr = &req->sr_msg; |
| 3995 | struct io_buffer *kbuf; |
| 3996 | |
| 3997 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 3998 | return NULL; |
| 3999 | |
| 4000 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4001 | if (IS_ERR(kbuf)) |
| 4002 | return kbuf; |
| 4003 | |
| 4004 | sr->kbuf = kbuf; |
| 4005 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 4006 | |
| 4007 | *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 4008 | *cflags |= IORING_CQE_F_BUFFER; |
| 4009 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4012 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4013 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4014 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4015 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4016 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4017 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4018 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4019 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4020 | return -EINVAL; |
| 4021 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4022 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 4023 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4024 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4025 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4026 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4027 | #ifdef CONFIG_COMPAT |
| 4028 | if (req->ctx->compat) |
| 4029 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4030 | #endif |
| 4031 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4032 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4033 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 4034 | /* iovec is already imported */ |
| 4035 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 4036 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4037 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4038 | ret = io_recvmsg_copy_hdr(req, io); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4039 | if (!ret) |
| 4040 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4041 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4044 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4045 | { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4046 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4047 | struct socket *sock; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4048 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4049 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4050 | sock = sock_from_file(req->file, &ret); |
| 4051 | if (sock) { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4052 | struct io_buffer *kbuf; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4053 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4054 | unsigned flags; |
| 4055 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4056 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4057 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 4058 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4059 | /* if iov is set, it's allocated already */ |
| 4060 | if (!kmsg->iov) |
| 4061 | kmsg->iov = kmsg->fast_iov; |
| 4062 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4063 | } else { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4064 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 4065 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4066 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4067 | ret = io_recvmsg_copy_hdr(req, &io); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4068 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4069 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4070 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4071 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4072 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 4073 | if (IS_ERR(kbuf)) { |
| 4074 | return PTR_ERR(kbuf); |
| 4075 | } else if (kbuf) { |
| 4076 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4077 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4078 | 1, req->sr_msg.len); |
| 4079 | } |
| 4080 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4081 | flags = req->sr_msg.msg_flags; |
| 4082 | if (flags & MSG_DONTWAIT) |
| 4083 | req->flags |= REQ_F_NOWAIT; |
| 4084 | else if (force_nonblock) |
| 4085 | flags |= MSG_DONTWAIT; |
| 4086 | |
| 4087 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 4088 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4089 | if (force_nonblock && ret == -EAGAIN) |
| 4090 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 4091 | if (ret == -ERESTARTSYS) |
| 4092 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4093 | } |
| 4094 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 4095 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4096 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4097 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4098 | __io_cqring_add_event(req, ret, cflags); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4099 | if (ret < 0) |
| 4100 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4101 | io_put_req(req); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4102 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4103 | } |
| 4104 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4105 | static int io_recv(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4106 | { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4107 | struct io_buffer *kbuf = NULL; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4108 | struct socket *sock; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4109 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4110 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4111 | sock = sock_from_file(req->file, &ret); |
| 4112 | if (sock) { |
| 4113 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4114 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4115 | struct msghdr msg; |
| 4116 | struct iovec iov; |
| 4117 | unsigned flags; |
| 4118 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4119 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 4120 | if (IS_ERR(kbuf)) |
| 4121 | return PTR_ERR(kbuf); |
| 4122 | else if (kbuf) |
| 4123 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4124 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4125 | ret = import_single_range(READ, buf, sr->len, &iov, |
| 4126 | &msg.msg_iter); |
| 4127 | if (ret) { |
| 4128 | kfree(kbuf); |
| 4129 | return ret; |
| 4130 | } |
| 4131 | |
| 4132 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4133 | msg.msg_name = NULL; |
| 4134 | msg.msg_control = NULL; |
| 4135 | msg.msg_controllen = 0; |
| 4136 | msg.msg_namelen = 0; |
| 4137 | msg.msg_iocb = NULL; |
| 4138 | msg.msg_flags = 0; |
| 4139 | |
| 4140 | flags = req->sr_msg.msg_flags; |
| 4141 | if (flags & MSG_DONTWAIT) |
| 4142 | req->flags |= REQ_F_NOWAIT; |
| 4143 | else if (force_nonblock) |
| 4144 | flags |= MSG_DONTWAIT; |
| 4145 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4146 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4147 | if (force_nonblock && ret == -EAGAIN) |
| 4148 | return -EAGAIN; |
| 4149 | if (ret == -ERESTARTSYS) |
| 4150 | ret = -EINTR; |
| 4151 | } |
| 4152 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4153 | kfree(kbuf); |
| 4154 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4155 | __io_cqring_add_event(req, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4156 | if (ret < 0) |
| 4157 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4158 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4159 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4160 | } |
| 4161 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4162 | 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] | 4163 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4164 | struct io_accept *accept = &req->accept; |
| 4165 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4166 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4167 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4168 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4169 | return -EINVAL; |
| 4170 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4171 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4172 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4173 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4174 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4175 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4176 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4177 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4178 | static int io_accept(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4179 | { |
| 4180 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4181 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4182 | int ret; |
| 4183 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4184 | if (req->file->f_flags & O_NONBLOCK) |
| 4185 | req->flags |= REQ_F_NOWAIT; |
| 4186 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4187 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4188 | accept->addr_len, accept->flags, |
| 4189 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4190 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4191 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4192 | if (ret < 0) { |
| 4193 | if (ret == -ERESTARTSYS) |
| 4194 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4195 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4196 | } |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4197 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4198 | io_put_req(req); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4199 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4200 | } |
| 4201 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4202 | 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] | 4203 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4204 | struct io_connect *conn = &req->connect; |
| 4205 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4206 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4207 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4208 | return -EINVAL; |
| 4209 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4210 | return -EINVAL; |
| 4211 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4212 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4213 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 4214 | |
| 4215 | if (!io) |
| 4216 | return 0; |
| 4217 | |
| 4218 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4219 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4220 | } |
| 4221 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4222 | static int io_connect(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4223 | { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4224 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4225 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4226 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4227 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4228 | if (req->io) { |
| 4229 | io = req->io; |
| 4230 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4231 | ret = move_addr_to_kernel(req->connect.addr, |
| 4232 | req->connect.addr_len, |
| 4233 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4234 | if (ret) |
| 4235 | goto out; |
| 4236 | io = &__io; |
| 4237 | } |
| 4238 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4239 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4240 | |
| 4241 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 4242 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4243 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4244 | if (req->io) |
| 4245 | return -EAGAIN; |
| 4246 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4247 | ret = -ENOMEM; |
| 4248 | goto out; |
| 4249 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4250 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4251 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4252 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4253 | if (ret == -ERESTARTSYS) |
| 4254 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4255 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4256 | if (ret < 0) |
| 4257 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4258 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4259 | io_put_req(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4260 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4261 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4262 | #else /* !CONFIG_NET */ |
| 4263 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4264 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4265 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4268 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4269 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4270 | return -EOPNOTSUPP; |
| 4271 | } |
| 4272 | |
| 4273 | static int io_send(struct io_kiocb *req, bool force_nonblock) |
| 4274 | { |
| 4275 | return -EOPNOTSUPP; |
| 4276 | } |
| 4277 | |
| 4278 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4279 | const struct io_uring_sqe *sqe) |
| 4280 | { |
| 4281 | return -EOPNOTSUPP; |
| 4282 | } |
| 4283 | |
| 4284 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock) |
| 4285 | { |
| 4286 | return -EOPNOTSUPP; |
| 4287 | } |
| 4288 | |
| 4289 | static int io_recv(struct io_kiocb *req, bool force_nonblock) |
| 4290 | { |
| 4291 | return -EOPNOTSUPP; |
| 4292 | } |
| 4293 | |
| 4294 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4295 | { |
| 4296 | return -EOPNOTSUPP; |
| 4297 | } |
| 4298 | |
| 4299 | static int io_accept(struct io_kiocb *req, bool force_nonblock) |
| 4300 | { |
| 4301 | return -EOPNOTSUPP; |
| 4302 | } |
| 4303 | |
| 4304 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4305 | { |
| 4306 | return -EOPNOTSUPP; |
| 4307 | } |
| 4308 | |
| 4309 | static int io_connect(struct io_kiocb *req, bool force_nonblock) |
| 4310 | { |
| 4311 | return -EOPNOTSUPP; |
| 4312 | } |
| 4313 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4314 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4315 | struct io_poll_table { |
| 4316 | struct poll_table_struct pt; |
| 4317 | struct io_kiocb *req; |
| 4318 | int error; |
| 4319 | }; |
| 4320 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4321 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4322 | __poll_t mask, task_work_func_t func) |
| 4323 | { |
| 4324 | struct task_struct *tsk; |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4325 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4326 | |
| 4327 | /* for instances that support it check for an event match first: */ |
| 4328 | if (mask && !(mask & poll->events)) |
| 4329 | return 0; |
| 4330 | |
| 4331 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4332 | |
| 4333 | list_del_init(&poll->wait.entry); |
| 4334 | |
| 4335 | tsk = req->task; |
| 4336 | req->result = mask; |
| 4337 | init_task_work(&req->task_work, func); |
| 4338 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4339 | * If this fails, then the task is exiting. When a task exits, the |
| 4340 | * work gets canceled, so just cancel this request as well instead |
| 4341 | * of executing it. We can't safely execute it anyway, as we may not |
| 4342 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4343 | */ |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4344 | ret = task_work_add(tsk, &req->task_work, true); |
| 4345 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4346 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4347 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 4348 | task_work_add(tsk, &req->task_work, true); |
| 4349 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4350 | wake_up_process(tsk); |
| 4351 | return 1; |
| 4352 | } |
| 4353 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4354 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4355 | __acquires(&req->ctx->completion_lock) |
| 4356 | { |
| 4357 | struct io_ring_ctx *ctx = req->ctx; |
| 4358 | |
| 4359 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4360 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4361 | |
| 4362 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4363 | } |
| 4364 | |
| 4365 | spin_lock_irq(&ctx->completion_lock); |
| 4366 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4367 | add_wait_queue(poll->head, &poll->wait); |
| 4368 | return true; |
| 4369 | } |
| 4370 | |
| 4371 | return false; |
| 4372 | } |
| 4373 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4374 | static void io_poll_remove_double(struct io_kiocb *req) |
| 4375 | { |
| 4376 | struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io; |
| 4377 | |
| 4378 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4379 | |
| 4380 | if (poll && poll->head) { |
| 4381 | struct wait_queue_head *head = poll->head; |
| 4382 | |
| 4383 | spin_lock(&head->lock); |
| 4384 | list_del_init(&poll->wait.entry); |
| 4385 | if (poll->wait.private) |
| 4386 | refcount_dec(&req->refs); |
| 4387 | poll->head = NULL; |
| 4388 | spin_unlock(&head->lock); |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4393 | { |
| 4394 | struct io_ring_ctx *ctx = req->ctx; |
| 4395 | |
| 4396 | io_poll_remove_double(req); |
| 4397 | req->poll.done = true; |
| 4398 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4399 | io_commit_cqring(ctx); |
| 4400 | } |
| 4401 | |
| 4402 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4403 | { |
| 4404 | struct io_ring_ctx *ctx = req->ctx; |
| 4405 | |
| 4406 | if (io_poll_rewait(req, &req->poll)) { |
| 4407 | spin_unlock_irq(&ctx->completion_lock); |
| 4408 | return; |
| 4409 | } |
| 4410 | |
| 4411 | hash_del(&req->hash_node); |
| 4412 | io_poll_complete(req, req->result, 0); |
| 4413 | req->flags |= REQ_F_COMP_LOCKED; |
| 4414 | io_put_req_find_next(req, nxt); |
| 4415 | spin_unlock_irq(&ctx->completion_lock); |
| 4416 | |
| 4417 | io_cqring_ev_posted(ctx); |
| 4418 | } |
| 4419 | |
| 4420 | static void io_poll_task_func(struct callback_head *cb) |
| 4421 | { |
| 4422 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4423 | struct io_kiocb *nxt = NULL; |
| 4424 | |
| 4425 | io_poll_task_handler(req, &nxt); |
| 4426 | if (nxt) { |
| 4427 | struct io_ring_ctx *ctx = nxt->ctx; |
| 4428 | |
| 4429 | mutex_lock(&ctx->uring_lock); |
| 4430 | __io_queue_sqe(nxt, NULL); |
| 4431 | mutex_unlock(&ctx->uring_lock); |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4436 | int sync, void *key) |
| 4437 | { |
| 4438 | struct io_kiocb *req = wait->private; |
| 4439 | struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io; |
| 4440 | __poll_t mask = key_to_poll(key); |
| 4441 | |
| 4442 | /* for instances that support it check for an event match first: */ |
| 4443 | if (mask && !(mask & poll->events)) |
| 4444 | return 0; |
| 4445 | |
| 4446 | if (req->poll.head) { |
| 4447 | bool done; |
| 4448 | |
| 4449 | spin_lock(&req->poll.head->lock); |
| 4450 | done = list_empty(&req->poll.wait.entry); |
| 4451 | if (!done) |
| 4452 | list_del_init(&req->poll.wait.entry); |
| 4453 | spin_unlock(&req->poll.head->lock); |
| 4454 | if (!done) |
| 4455 | __io_async_wake(req, poll, mask, io_poll_task_func); |
| 4456 | } |
| 4457 | refcount_dec(&req->refs); |
| 4458 | return 1; |
| 4459 | } |
| 4460 | |
| 4461 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4462 | wait_queue_func_t wake_func) |
| 4463 | { |
| 4464 | poll->head = NULL; |
| 4465 | poll->done = false; |
| 4466 | poll->canceled = false; |
| 4467 | poll->events = events; |
| 4468 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4469 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4470 | } |
| 4471 | |
| 4472 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
| 4473 | struct wait_queue_head *head) |
| 4474 | { |
| 4475 | struct io_kiocb *req = pt->req; |
| 4476 | |
| 4477 | /* |
| 4478 | * If poll->head is already set, it's because the file being polled |
| 4479 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4480 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4481 | */ |
| 4482 | if (unlikely(poll->head)) { |
| 4483 | /* already have a 2nd entry, fail a third attempt */ |
| 4484 | if (req->io) { |
| 4485 | pt->error = -EINVAL; |
| 4486 | return; |
| 4487 | } |
| 4488 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4489 | if (!poll) { |
| 4490 | pt->error = -ENOMEM; |
| 4491 | return; |
| 4492 | } |
| 4493 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); |
| 4494 | refcount_inc(&req->refs); |
| 4495 | poll->wait.private = req; |
| 4496 | req->io = (void *) poll; |
| 4497 | } |
| 4498 | |
| 4499 | pt->error = 0; |
| 4500 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4501 | |
| 4502 | if (poll->events & EPOLLEXCLUSIVE) |
| 4503 | add_wait_queue_exclusive(head, &poll->wait); |
| 4504 | else |
| 4505 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4509 | struct poll_table_struct *p) |
| 4510 | { |
| 4511 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4512 | |
| 4513 | __io_queue_proc(&pt->req->apoll->poll, pt, head); |
| 4514 | } |
| 4515 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4516 | static void io_async_task_func(struct callback_head *cb) |
| 4517 | { |
| 4518 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4519 | struct async_poll *apoll = req->apoll; |
| 4520 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4521 | bool canceled = false; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4522 | |
| 4523 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4524 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4525 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4526 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4527 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4528 | } |
| 4529 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4530 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
| 4531 | if (hash_hashed(&req->hash_node)) { |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4532 | hash_del(&req->hash_node); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4533 | } else { |
| 4534 | canceled = READ_ONCE(apoll->poll.canceled); |
| 4535 | if (canceled) { |
| 4536 | io_cqring_fill_event(req, -ECANCELED); |
| 4537 | io_commit_cqring(ctx); |
| 4538 | } |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4539 | } |
| 4540 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4541 | spin_unlock_irq(&ctx->completion_lock); |
| 4542 | |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4543 | /* restore ->work in case we need to retry again */ |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4544 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4545 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4546 | kfree(apoll); |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4547 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4548 | if (!canceled) { |
| 4549 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 4550 | if (io_sq_thread_acquire_mm(ctx, req)) { |
| 4551 | io_cqring_add_event(req, -EFAULT); |
| 4552 | goto end_req; |
| 4553 | } |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4554 | mutex_lock(&ctx->uring_lock); |
| 4555 | __io_queue_sqe(req, NULL); |
| 4556 | mutex_unlock(&ctx->uring_lock); |
| 4557 | } else { |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4558 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 4559 | end_req: |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4560 | req_set_fail_links(req); |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4561 | io_double_put_req(req); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4562 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4566 | void *key) |
| 4567 | { |
| 4568 | struct io_kiocb *req = wait->private; |
| 4569 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 4570 | |
| 4571 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 4572 | key_to_poll(key)); |
| 4573 | |
| 4574 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 4575 | } |
| 4576 | |
| 4577 | static void io_poll_req_insert(struct io_kiocb *req) |
| 4578 | { |
| 4579 | struct io_ring_ctx *ctx = req->ctx; |
| 4580 | struct hlist_head *list; |
| 4581 | |
| 4582 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 4583 | hlist_add_head(&req->hash_node, list); |
| 4584 | } |
| 4585 | |
| 4586 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 4587 | struct io_poll_iocb *poll, |
| 4588 | struct io_poll_table *ipt, __poll_t mask, |
| 4589 | wait_queue_func_t wake_func) |
| 4590 | __acquires(&ctx->completion_lock) |
| 4591 | { |
| 4592 | struct io_ring_ctx *ctx = req->ctx; |
| 4593 | bool cancel = false; |
| 4594 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4595 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 4596 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4597 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4598 | |
| 4599 | ipt->pt._key = mask; |
| 4600 | ipt->req = req; |
| 4601 | ipt->error = -EINVAL; |
| 4602 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4603 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 4604 | |
| 4605 | spin_lock_irq(&ctx->completion_lock); |
| 4606 | if (likely(poll->head)) { |
| 4607 | spin_lock(&poll->head->lock); |
| 4608 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 4609 | if (ipt->error) |
| 4610 | cancel = true; |
| 4611 | ipt->error = 0; |
| 4612 | mask = 0; |
| 4613 | } |
| 4614 | if (mask || ipt->error) |
| 4615 | list_del_init(&poll->wait.entry); |
| 4616 | else if (cancel) |
| 4617 | WRITE_ONCE(poll->canceled, true); |
| 4618 | else if (!poll->done) /* actually waiting for an event */ |
| 4619 | io_poll_req_insert(req); |
| 4620 | spin_unlock(&poll->head->lock); |
| 4621 | } |
| 4622 | |
| 4623 | return mask; |
| 4624 | } |
| 4625 | |
| 4626 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 4627 | { |
| 4628 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 4629 | struct io_ring_ctx *ctx = req->ctx; |
| 4630 | struct async_poll *apoll; |
| 4631 | struct io_poll_table ipt; |
| 4632 | __poll_t mask, ret; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4633 | bool had_io; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4634 | |
| 4635 | if (!req->file || !file_can_poll(req->file)) |
| 4636 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4637 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4638 | return false; |
| 4639 | if (!def->pollin && !def->pollout) |
| 4640 | return false; |
| 4641 | |
| 4642 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 4643 | if (unlikely(!apoll)) |
| 4644 | return false; |
| 4645 | |
| 4646 | req->flags |= REQ_F_POLLED; |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4647 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4648 | memcpy(&apoll->work, &req->work, sizeof(req->work)); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4649 | had_io = req->io != NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4650 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4651 | io_get_req_task(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4652 | req->apoll = apoll; |
| 4653 | INIT_HLIST_NODE(&req->hash_node); |
| 4654 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4655 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4656 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4657 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4658 | if (def->pollout) |
| 4659 | mask |= POLLOUT | POLLWRNORM; |
| 4660 | mask |= POLLERR | POLLPRI; |
| 4661 | |
| 4662 | ipt.pt._qproc = io_async_queue_proc; |
| 4663 | |
| 4664 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 4665 | io_async_wake); |
| 4666 | if (ret) { |
| 4667 | ipt.error = 0; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4668 | /* only remove double add if we did it here */ |
| 4669 | if (!had_io) |
| 4670 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4671 | spin_unlock_irq(&ctx->completion_lock); |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4672 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4673 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4674 | kfree(apoll); |
| 4675 | return false; |
| 4676 | } |
| 4677 | spin_unlock_irq(&ctx->completion_lock); |
| 4678 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 4679 | apoll->poll.events); |
| 4680 | return true; |
| 4681 | } |
| 4682 | |
| 4683 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 4684 | struct io_poll_iocb *poll) |
| 4685 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4686 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4687 | |
| 4688 | spin_lock(&poll->head->lock); |
| 4689 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 4690 | if (!list_empty(&poll->wait.entry)) { |
| 4691 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4692 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4693 | } |
| 4694 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4695 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4696 | return do_complete; |
| 4697 | } |
| 4698 | |
| 4699 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 4700 | { |
| 4701 | bool do_complete; |
| 4702 | |
| 4703 | if (req->opcode == IORING_OP_POLL_ADD) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4704 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4705 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 4706 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4707 | struct async_poll *apoll = req->apoll; |
| 4708 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4709 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4710 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 4711 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4712 | io_put_req(req); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4713 | /* |
| 4714 | * restore ->work because we will call |
| 4715 | * io_req_work_drop_env below when dropping the |
| 4716 | * final reference. |
| 4717 | */ |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4718 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4719 | memcpy(&req->work, &apoll->work, |
| 4720 | sizeof(req->work)); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4721 | kfree(apoll); |
| 4722 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 4723 | } |
| 4724 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4725 | if (do_complete) { |
| 4726 | io_cqring_fill_event(req, -ECANCELED); |
| 4727 | io_commit_cqring(req->ctx); |
| 4728 | req->flags |= REQ_F_COMP_LOCKED; |
| 4729 | io_put_req(req); |
| 4730 | } |
| 4731 | |
| 4732 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 4736 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4737 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4738 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4739 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4740 | |
| 4741 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4742 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 4743 | struct hlist_head *list; |
| 4744 | |
| 4745 | list = &ctx->cancel_hash[i]; |
| 4746 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4747 | posted += io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4748 | } |
| 4749 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4750 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4751 | if (posted) |
| 4752 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4755 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 4756 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4757 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4758 | struct io_kiocb *req; |
| 4759 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4760 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 4761 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4762 | if (sqe_addr != req->user_data) |
| 4763 | continue; |
| 4764 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4765 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4766 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | return -ENOENT; |
| 4770 | } |
| 4771 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4772 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 4773 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4774 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4776 | return -EINVAL; |
| 4777 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 4778 | sqe->poll_events) |
| 4779 | return -EINVAL; |
| 4780 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4781 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4782 | return 0; |
| 4783 | } |
| 4784 | |
| 4785 | /* |
| 4786 | * Find a running poll command that matches one specified in sqe->addr, |
| 4787 | * and remove it if found. |
| 4788 | */ |
| 4789 | static int io_poll_remove(struct io_kiocb *req) |
| 4790 | { |
| 4791 | struct io_ring_ctx *ctx = req->ctx; |
| 4792 | u64 addr; |
| 4793 | int ret; |
| 4794 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4795 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4796 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4797 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4798 | spin_unlock_irq(&ctx->completion_lock); |
| 4799 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4800 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4801 | if (ret < 0) |
| 4802 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4803 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4804 | return 0; |
| 4805 | } |
| 4806 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4807 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4808 | void *key) |
| 4809 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 4810 | struct io_kiocb *req = wait->private; |
| 4811 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4812 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4813 | 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] | 4814 | } |
| 4815 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4816 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4817 | struct poll_table_struct *p) |
| 4818 | { |
| 4819 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4820 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4821 | __io_queue_proc(&pt->req->poll, pt, head); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4822 | } |
| 4823 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4824 | 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] | 4825 | { |
| 4826 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4827 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4828 | |
| 4829 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4830 | return -EINVAL; |
| 4831 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 4832 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4833 | if (!poll->file) |
| 4834 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4835 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4836 | events = READ_ONCE(sqe->poll32_events); |
| 4837 | #ifdef __BIG_ENDIAN |
| 4838 | events = swahw32(events); |
| 4839 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4840 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 4841 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4842 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4843 | io_get_req_task(req); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4844 | return 0; |
| 4845 | } |
| 4846 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4847 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4848 | { |
| 4849 | struct io_poll_iocb *poll = &req->poll; |
| 4850 | struct io_ring_ctx *ctx = req->ctx; |
| 4851 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4852 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4853 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4854 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4855 | INIT_LIST_HEAD(&req->list); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4856 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4857 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4858 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 4859 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4860 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4861 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4862 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4863 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4864 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4865 | spin_unlock_irq(&ctx->completion_lock); |
| 4866 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4867 | if (mask) { |
| 4868 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4869 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4870 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4871 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4872 | } |
| 4873 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4874 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 4875 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4876 | struct io_timeout_data *data = container_of(timer, |
| 4877 | struct io_timeout_data, timer); |
| 4878 | struct io_kiocb *req = data->req; |
| 4879 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4880 | unsigned long flags; |
| 4881 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4882 | atomic_inc(&ctx->cq_timeouts); |
| 4883 | |
| 4884 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4885 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4886 | * We could be racing with timeout deletion. If the list is empty, |
| 4887 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4888 | */ |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 4889 | if (!list_empty(&req->list)) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4890 | list_del_init(&req->list); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4891 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4892 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4893 | io_commit_cqring(ctx); |
| 4894 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4895 | |
| 4896 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4897 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4898 | io_put_req(req); |
| 4899 | return HRTIMER_NORESTART; |
| 4900 | } |
| 4901 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4902 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 4903 | { |
| 4904 | struct io_kiocb *req; |
| 4905 | int ret = -ENOENT; |
| 4906 | |
| 4907 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 4908 | if (user_data == req->user_data) { |
| 4909 | list_del_init(&req->list); |
| 4910 | ret = 0; |
| 4911 | break; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | if (ret == -ENOENT) |
| 4916 | return ret; |
| 4917 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4918 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4919 | if (ret == -1) |
| 4920 | return -EALREADY; |
| 4921 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4922 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4923 | io_cqring_fill_event(req, -ECANCELED); |
| 4924 | io_put_req(req); |
| 4925 | return 0; |
| 4926 | } |
| 4927 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4928 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 4929 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4930 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4931 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4932 | return -EINVAL; |
| 4933 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 4934 | return -EINVAL; |
| 4935 | |
| 4936 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 4937 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 4938 | if (req->timeout.flags) |
| 4939 | return -EINVAL; |
| 4940 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4941 | return 0; |
| 4942 | } |
| 4943 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4944 | /* |
| 4945 | * Remove or update an existing timeout command |
| 4946 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4947 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4948 | { |
| 4949 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4950 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4951 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4952 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4953 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4954 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4955 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4956 | io_commit_cqring(ctx); |
| 4957 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4958 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4959 | if (ret < 0) |
| 4960 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4961 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4962 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4963 | } |
| 4964 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4965 | 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] | 4966 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4967 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4968 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4969 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 4970 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4971 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4972 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4973 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4974 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4975 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 4976 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4977 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4978 | flags = READ_ONCE(sqe->timeout_flags); |
| 4979 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4980 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 4981 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 4982 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4983 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4984 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4985 | return -ENOMEM; |
| 4986 | |
| 4987 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4988 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4989 | req->flags |= REQ_F_TIMEOUT; |
| 4990 | |
| 4991 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4992 | return -EFAULT; |
| 4993 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4994 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4995 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4996 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4997 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4998 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4999 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5003 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5004 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5005 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5006 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5007 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5008 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5009 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5010 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5011 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5012 | /* |
| 5013 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5014 | * timeout event to be satisfied. If it isn't set, then this is |
| 5015 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5016 | */ |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5017 | if (!off) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5018 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5019 | entry = ctx->timeout_list.prev; |
| 5020 | goto add; |
| 5021 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5022 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5023 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5024 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5025 | |
| 5026 | /* |
| 5027 | * Insertion sort, ensuring the first entry in the list is always |
| 5028 | * the one we need first. |
| 5029 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5030 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 5031 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5032 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5033 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 5034 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5035 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5036 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5037 | break; |
| 5038 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5039 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5040 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5041 | data->timer.function = io_timeout_fn; |
| 5042 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5043 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5044 | return 0; |
| 5045 | } |
| 5046 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5047 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5048 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5049 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5050 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5051 | return req->user_data == (unsigned long) data; |
| 5052 | } |
| 5053 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5054 | 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] | 5055 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5056 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5057 | int ret = 0; |
| 5058 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5059 | 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] | 5060 | switch (cancel_ret) { |
| 5061 | case IO_WQ_CANCEL_OK: |
| 5062 | ret = 0; |
| 5063 | break; |
| 5064 | case IO_WQ_CANCEL_RUNNING: |
| 5065 | ret = -EALREADY; |
| 5066 | break; |
| 5067 | case IO_WQ_CANCEL_NOTFOUND: |
| 5068 | ret = -ENOENT; |
| 5069 | break; |
| 5070 | } |
| 5071 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5072 | return ret; |
| 5073 | } |
| 5074 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5075 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5076 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5077 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5078 | { |
| 5079 | unsigned long flags; |
| 5080 | int ret; |
| 5081 | |
| 5082 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5083 | if (ret != -ENOENT) { |
| 5084 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5085 | goto done; |
| 5086 | } |
| 5087 | |
| 5088 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5089 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5090 | if (ret != -ENOENT) |
| 5091 | goto done; |
| 5092 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5093 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5094 | if (!ret) |
| 5095 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5096 | io_cqring_fill_event(req, ret); |
| 5097 | io_commit_cqring(ctx); |
| 5098 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5099 | io_cqring_ev_posted(ctx); |
| 5100 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5101 | if (ret < 0) |
| 5102 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5103 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5106 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5107 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5108 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5109 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5110 | return -EINVAL; |
| 5111 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 5112 | sqe->cancel_flags) |
| 5113 | return -EINVAL; |
| 5114 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5115 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5119 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5120 | { |
| 5121 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5122 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5123 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5124 | return 0; |
| 5125 | } |
| 5126 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5127 | static int io_files_update_prep(struct io_kiocb *req, |
| 5128 | const struct io_uring_sqe *sqe) |
| 5129 | { |
| 5130 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 5131 | return -EINVAL; |
| 5132 | |
| 5133 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5134 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5135 | if (!req->files_update.nr_args) |
| 5136 | return -EINVAL; |
| 5137 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 5138 | return 0; |
| 5139 | } |
| 5140 | |
| 5141 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 5142 | { |
| 5143 | struct io_ring_ctx *ctx = req->ctx; |
| 5144 | struct io_uring_files_update up; |
| 5145 | int ret; |
| 5146 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5147 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5148 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5149 | |
| 5150 | up.offset = req->files_update.offset; |
| 5151 | up.fds = req->files_update.arg; |
| 5152 | |
| 5153 | mutex_lock(&ctx->uring_lock); |
| 5154 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 5155 | mutex_unlock(&ctx->uring_lock); |
| 5156 | |
| 5157 | if (ret < 0) |
| 5158 | req_set_fail_links(req); |
| 5159 | io_cqring_add_event(req, ret); |
| 5160 | io_put_req(req); |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5164 | static int io_req_defer_prep(struct io_kiocb *req, |
| 5165 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5166 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5167 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5168 | |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 5169 | if (!sqe) |
| 5170 | return 0; |
| 5171 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5172 | io_req_init_async(req); |
| 5173 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5174 | if (io_op_defs[req->opcode].file_table) { |
| 5175 | ret = io_grab_files(req); |
| 5176 | if (unlikely(ret)) |
| 5177 | return ret; |
| 5178 | } |
| 5179 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 5180 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 5181 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5182 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5183 | case IORING_OP_NOP: |
| 5184 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5185 | case IORING_OP_READV: |
| 5186 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5187 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5188 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5189 | break; |
| 5190 | case IORING_OP_WRITEV: |
| 5191 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5192 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5193 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5194 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5195 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5196 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5197 | break; |
| 5198 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5199 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5200 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5201 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5202 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5203 | break; |
| 5204 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5205 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5206 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5207 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5208 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5209 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5210 | break; |
| 5211 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5212 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5213 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5214 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5215 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5216 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5217 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5218 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5219 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5220 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5221 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5222 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5223 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5224 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5225 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5226 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5227 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5228 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5229 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5230 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5231 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5232 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5233 | case IORING_OP_FALLOCATE: |
| 5234 | ret = io_fallocate_prep(req, sqe); |
| 5235 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5236 | case IORING_OP_OPENAT: |
| 5237 | ret = io_openat_prep(req, sqe); |
| 5238 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5239 | case IORING_OP_CLOSE: |
| 5240 | ret = io_close_prep(req, sqe); |
| 5241 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5242 | case IORING_OP_FILES_UPDATE: |
| 5243 | ret = io_files_update_prep(req, sqe); |
| 5244 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5245 | case IORING_OP_STATX: |
| 5246 | ret = io_statx_prep(req, sqe); |
| 5247 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5248 | case IORING_OP_FADVISE: |
| 5249 | ret = io_fadvise_prep(req, sqe); |
| 5250 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5251 | case IORING_OP_MADVISE: |
| 5252 | ret = io_madvise_prep(req, sqe); |
| 5253 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5254 | case IORING_OP_OPENAT2: |
| 5255 | ret = io_openat2_prep(req, sqe); |
| 5256 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5257 | case IORING_OP_EPOLL_CTL: |
| 5258 | ret = io_epoll_ctl_prep(req, sqe); |
| 5259 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5260 | case IORING_OP_SPLICE: |
| 5261 | ret = io_splice_prep(req, sqe); |
| 5262 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5263 | case IORING_OP_PROVIDE_BUFFERS: |
| 5264 | ret = io_provide_buffers_prep(req, sqe); |
| 5265 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5266 | case IORING_OP_REMOVE_BUFFERS: |
| 5267 | ret = io_remove_buffers_prep(req, sqe); |
| 5268 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5269 | case IORING_OP_TEE: |
| 5270 | ret = io_tee_prep(req, sqe); |
| 5271 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5272 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5273 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5274 | req->opcode); |
| 5275 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5276 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5277 | } |
| 5278 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5279 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5280 | } |
| 5281 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5282 | 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] | 5283 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5284 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5285 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5286 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5287 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 4ee3631 | 2020-05-01 17:09:37 +0300 | [diff] [blame] | 5288 | if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5289 | return 0; |
| 5290 | |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5291 | if (!req->io) { |
| 5292 | if (io_alloc_async_ctx(req)) |
| 5293 | return -EAGAIN; |
| 5294 | ret = io_req_defer_prep(req, sqe); |
| 5295 | if (ret < 0) |
| 5296 | return ret; |
| 5297 | } |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5298 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5299 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5300 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5301 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5302 | return 0; |
| 5303 | } |
| 5304 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 5305 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5306 | list_add_tail(&req->list, &ctx->defer_list); |
| 5307 | spin_unlock_irq(&ctx->completion_lock); |
| 5308 | return -EIOCBQUEUED; |
| 5309 | } |
| 5310 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5311 | static void io_cleanup_req(struct io_kiocb *req) |
| 5312 | { |
| 5313 | struct io_async_ctx *io = req->io; |
| 5314 | |
| 5315 | switch (req->opcode) { |
| 5316 | case IORING_OP_READV: |
| 5317 | case IORING_OP_READ_FIXED: |
| 5318 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5319 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5320 | kfree((void *)(unsigned long)req->rw.addr); |
| 5321 | /* fallthrough */ |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5322 | case IORING_OP_WRITEV: |
| 5323 | case IORING_OP_WRITE_FIXED: |
| 5324 | case IORING_OP_WRITE: |
| 5325 | if (io->rw.iov != io->rw.fast_iov) |
| 5326 | kfree(io->rw.iov); |
| 5327 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5328 | case IORING_OP_RECVMSG: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5329 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5330 | kfree(req->sr_msg.kbuf); |
| 5331 | /* fallthrough */ |
| 5332 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5333 | if (io->msg.iov != io->msg.fast_iov) |
| 5334 | kfree(io->msg.iov); |
| 5335 | break; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5336 | case IORING_OP_RECV: |
| 5337 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5338 | kfree(req->sr_msg.kbuf); |
| 5339 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 5340 | case IORING_OP_OPENAT: |
| 5341 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 5342 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5343 | case IORING_OP_SPLICE: |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5344 | case IORING_OP_TEE: |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5345 | io_put_file(req, req->splice.file_in, |
| 5346 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5347 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5351 | } |
| 5352 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5353 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5354 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5355 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5356 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5357 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5358 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5359 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5360 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5361 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5362 | break; |
| 5363 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5364 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5365 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5366 | if (sqe) { |
| 5367 | ret = io_read_prep(req, sqe, force_nonblock); |
| 5368 | if (ret < 0) |
| 5369 | break; |
| 5370 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5371 | ret = io_read(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5372 | break; |
| 5373 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5374 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5375 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5376 | if (sqe) { |
| 5377 | ret = io_write_prep(req, sqe, force_nonblock); |
| 5378 | if (ret < 0) |
| 5379 | break; |
| 5380 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5381 | ret = io_write(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5382 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5383 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5384 | if (sqe) { |
| 5385 | ret = io_prep_fsync(req, sqe); |
| 5386 | if (ret < 0) |
| 5387 | break; |
| 5388 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5389 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5390 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5391 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5392 | if (sqe) { |
| 5393 | ret = io_poll_add_prep(req, sqe); |
| 5394 | if (ret) |
| 5395 | break; |
| 5396 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5397 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5398 | break; |
| 5399 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5400 | if (sqe) { |
| 5401 | ret = io_poll_remove_prep(req, sqe); |
| 5402 | if (ret < 0) |
| 5403 | break; |
| 5404 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5405 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5406 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5407 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5408 | if (sqe) { |
| 5409 | ret = io_prep_sfr(req, sqe); |
| 5410 | if (ret < 0) |
| 5411 | break; |
| 5412 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5413 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5414 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5415 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5416 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5417 | if (sqe) { |
| 5418 | ret = io_sendmsg_prep(req, sqe); |
| 5419 | if (ret < 0) |
| 5420 | break; |
| 5421 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5422 | if (req->opcode == IORING_OP_SENDMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5423 | ret = io_sendmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5424 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5425 | ret = io_send(req, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5426 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5427 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5428 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5429 | if (sqe) { |
| 5430 | ret = io_recvmsg_prep(req, sqe); |
| 5431 | if (ret) |
| 5432 | break; |
| 5433 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5434 | if (req->opcode == IORING_OP_RECVMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5435 | ret = io_recvmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5436 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5437 | ret = io_recv(req, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5438 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5439 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5440 | if (sqe) { |
| 5441 | ret = io_timeout_prep(req, sqe, false); |
| 5442 | if (ret) |
| 5443 | break; |
| 5444 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5445 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5446 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5447 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5448 | if (sqe) { |
| 5449 | ret = io_timeout_remove_prep(req, sqe); |
| 5450 | if (ret) |
| 5451 | break; |
| 5452 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5453 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5454 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5455 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5456 | if (sqe) { |
| 5457 | ret = io_accept_prep(req, sqe); |
| 5458 | if (ret) |
| 5459 | break; |
| 5460 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5461 | ret = io_accept(req, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5462 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5463 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5464 | if (sqe) { |
| 5465 | ret = io_connect_prep(req, sqe); |
| 5466 | if (ret) |
| 5467 | break; |
| 5468 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5469 | ret = io_connect(req, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5470 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5471 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5472 | if (sqe) { |
| 5473 | ret = io_async_cancel_prep(req, sqe); |
| 5474 | if (ret) |
| 5475 | break; |
| 5476 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5477 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5478 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5479 | case IORING_OP_FALLOCATE: |
| 5480 | if (sqe) { |
| 5481 | ret = io_fallocate_prep(req, sqe); |
| 5482 | if (ret) |
| 5483 | break; |
| 5484 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5485 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5486 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5487 | case IORING_OP_OPENAT: |
| 5488 | if (sqe) { |
| 5489 | ret = io_openat_prep(req, sqe); |
| 5490 | if (ret) |
| 5491 | break; |
| 5492 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5493 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5494 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5495 | case IORING_OP_CLOSE: |
| 5496 | if (sqe) { |
| 5497 | ret = io_close_prep(req, sqe); |
| 5498 | if (ret) |
| 5499 | break; |
| 5500 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5501 | ret = io_close(req, force_nonblock); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5502 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5503 | case IORING_OP_FILES_UPDATE: |
| 5504 | if (sqe) { |
| 5505 | ret = io_files_update_prep(req, sqe); |
| 5506 | if (ret) |
| 5507 | break; |
| 5508 | } |
| 5509 | ret = io_files_update(req, force_nonblock); |
| 5510 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5511 | case IORING_OP_STATX: |
| 5512 | if (sqe) { |
| 5513 | ret = io_statx_prep(req, sqe); |
| 5514 | if (ret) |
| 5515 | break; |
| 5516 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5517 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5518 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5519 | case IORING_OP_FADVISE: |
| 5520 | if (sqe) { |
| 5521 | ret = io_fadvise_prep(req, sqe); |
| 5522 | if (ret) |
| 5523 | break; |
| 5524 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5525 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5526 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5527 | case IORING_OP_MADVISE: |
| 5528 | if (sqe) { |
| 5529 | ret = io_madvise_prep(req, sqe); |
| 5530 | if (ret) |
| 5531 | break; |
| 5532 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5533 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5534 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5535 | case IORING_OP_OPENAT2: |
| 5536 | if (sqe) { |
| 5537 | ret = io_openat2_prep(req, sqe); |
| 5538 | if (ret) |
| 5539 | break; |
| 5540 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5541 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5542 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5543 | case IORING_OP_EPOLL_CTL: |
| 5544 | if (sqe) { |
| 5545 | ret = io_epoll_ctl_prep(req, sqe); |
| 5546 | if (ret) |
| 5547 | break; |
| 5548 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5549 | ret = io_epoll_ctl(req, force_nonblock); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5550 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5551 | case IORING_OP_SPLICE: |
| 5552 | if (sqe) { |
| 5553 | ret = io_splice_prep(req, sqe); |
| 5554 | if (ret < 0) |
| 5555 | break; |
| 5556 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5557 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5558 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5559 | case IORING_OP_PROVIDE_BUFFERS: |
| 5560 | if (sqe) { |
| 5561 | ret = io_provide_buffers_prep(req, sqe); |
| 5562 | if (ret) |
| 5563 | break; |
| 5564 | } |
| 5565 | ret = io_provide_buffers(req, force_nonblock); |
| 5566 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5567 | case IORING_OP_REMOVE_BUFFERS: |
| 5568 | if (sqe) { |
| 5569 | ret = io_remove_buffers_prep(req, sqe); |
| 5570 | if (ret) |
| 5571 | break; |
| 5572 | } |
| 5573 | ret = io_remove_buffers(req, force_nonblock); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5574 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5575 | case IORING_OP_TEE: |
| 5576 | if (sqe) { |
| 5577 | ret = io_tee_prep(req, sqe); |
| 5578 | if (ret < 0) |
| 5579 | break; |
| 5580 | } |
| 5581 | ret = io_tee(req, force_nonblock); |
| 5582 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5583 | default: |
| 5584 | ret = -EINVAL; |
| 5585 | break; |
| 5586 | } |
| 5587 | |
| 5588 | if (ret) |
| 5589 | return ret; |
| 5590 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 5591 | /* If the op doesn't have a file, we're not polling for it */ |
| 5592 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5593 | const bool in_async = io_wq_current_is_worker(); |
| 5594 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5595 | if (req->result == -EAGAIN) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5596 | return -EAGAIN; |
| 5597 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5598 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 5599 | if (in_async) |
| 5600 | mutex_lock(&ctx->uring_lock); |
| 5601 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5602 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5603 | |
| 5604 | if (in_async) |
| 5605 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | return 0; |
| 5609 | } |
| 5610 | |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5611 | static void io_arm_async_linked_timeout(struct io_kiocb *req) |
| 5612 | { |
| 5613 | struct io_kiocb *link; |
| 5614 | |
| 5615 | /* link head's timeout is queued in io_queue_async_work() */ |
| 5616 | if (!(req->flags & REQ_F_QUEUE_TIMEOUT)) |
| 5617 | return; |
| 5618 | |
| 5619 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 5620 | io_queue_linked_timeout(link); |
| 5621 | } |
| 5622 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5623 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5624 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5625 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5626 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5627 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5628 | |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5629 | io_arm_async_linked_timeout(req); |
| 5630 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5631 | /* if NO_CANCEL is set, we must still run the work */ |
| 5632 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 5633 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5634 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5635 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5636 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5637 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5638 | do { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5639 | ret = io_issue_sqe(req, NULL, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5640 | /* |
| 5641 | * We can get EAGAIN for polled IO even though we're |
| 5642 | * forcing a sync submission from here, since we can't |
| 5643 | * wait for request slots on the block side. |
| 5644 | */ |
| 5645 | if (ret != -EAGAIN) |
| 5646 | break; |
| 5647 | cond_resched(); |
| 5648 | } while (1); |
| 5649 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5650 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5651 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5652 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5653 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 5654 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5655 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5656 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 5657 | io_steal_work(req, workptr); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5658 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5659 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5660 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 5661 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5662 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5663 | struct fixed_file_table *table; |
| 5664 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5665 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 5666 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5667 | } |
| 5668 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5669 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 5670 | int fd, struct file **out_file, bool fixed) |
| 5671 | { |
| 5672 | struct io_ring_ctx *ctx = req->ctx; |
| 5673 | struct file *file; |
| 5674 | |
| 5675 | if (fixed) { |
| 5676 | if (unlikely(!ctx->file_data || |
| 5677 | (unsigned) fd >= ctx->nr_user_files)) |
| 5678 | return -EBADF; |
| 5679 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 5680 | file = io_file_from_index(ctx, fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5681 | if (file) { |
| 5682 | req->fixed_file_refs = ctx->file_data->cur_refs; |
| 5683 | percpu_ref_get(req->fixed_file_refs); |
| 5684 | } |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5685 | } else { |
| 5686 | trace_io_uring_file_get(ctx, fd); |
| 5687 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5688 | } |
| 5689 | |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5690 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
| 5691 | *out_file = file; |
| 5692 | return 0; |
| 5693 | } |
| 5694 | return -EBADF; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5695 | } |
| 5696 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5697 | 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] | 5698 | int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5699 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5700 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5701 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 5702 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 5703 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5704 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5705 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5706 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5707 | } |
| 5708 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5709 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5710 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5711 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5712 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5713 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 5714 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5715 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5716 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5717 | return -EBADF; |
| 5718 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5719 | rcu_read_lock(); |
| 5720 | spin_lock_irq(&ctx->inflight_lock); |
| 5721 | /* |
| 5722 | * We use the f_ops->flush() handler to ensure that we can flush |
| 5723 | * out work accessing these files if the fd is closed. Check if |
| 5724 | * the fd has changed since we started down this path, and disallow |
| 5725 | * this operation if it has. |
| 5726 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5727 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5728 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 5729 | req->flags |= REQ_F_INFLIGHT; |
| 5730 | req->work.files = current->files; |
| 5731 | ret = 0; |
| 5732 | } |
| 5733 | spin_unlock_irq(&ctx->inflight_lock); |
| 5734 | rcu_read_unlock(); |
| 5735 | |
| 5736 | return ret; |
| 5737 | } |
| 5738 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5739 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 5740 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5741 | struct io_timeout_data *data = container_of(timer, |
| 5742 | struct io_timeout_data, timer); |
| 5743 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5744 | struct io_ring_ctx *ctx = req->ctx; |
| 5745 | struct io_kiocb *prev = NULL; |
| 5746 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5747 | |
| 5748 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5749 | |
| 5750 | /* |
| 5751 | * We don't expect the list to be empty, that will only happen if we |
| 5752 | * race with the completion of the linked work. |
| 5753 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5754 | if (!list_empty(&req->link_list)) { |
| 5755 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 5756 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5757 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5758 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5759 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 5760 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5761 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5765 | |
| 5766 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5767 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5768 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5769 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5770 | } else { |
| 5771 | io_cqring_add_event(req, -ETIME); |
| 5772 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5773 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5774 | return HRTIMER_NORESTART; |
| 5775 | } |
| 5776 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5777 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5778 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5779 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5780 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5781 | /* |
| 5782 | * If the list is now empty, then our linked request finished before |
| 5783 | * we got a chance to setup the timer |
| 5784 | */ |
| 5785 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5786 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5787 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5788 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5789 | data->timer.function = io_link_timeout_fn; |
| 5790 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 5791 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5792 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5793 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5794 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5795 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5796 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5797 | } |
| 5798 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5799 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5800 | { |
| 5801 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5802 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 5803 | if (!(req->flags & REQ_F_LINK_HEAD)) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5804 | return NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5805 | /* for polled retry, if flag is set, we already went through here */ |
| 5806 | if (req->flags & REQ_F_POLLED) |
| 5807 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5808 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5809 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 5810 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5811 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5812 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5813 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5814 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5815 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5816 | } |
| 5817 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5818 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5819 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5820 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5821 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5822 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5823 | int ret; |
| 5824 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5825 | again: |
| 5826 | linked_timeout = io_prep_linked_timeout(req); |
| 5827 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5828 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
| 5829 | req->work.creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5830 | if (old_creds) |
| 5831 | revert_creds(old_creds); |
| 5832 | if (old_creds == req->work.creds) |
| 5833 | old_creds = NULL; /* restored original creds */ |
| 5834 | else |
| 5835 | old_creds = override_creds(req->work.creds); |
| 5836 | } |
| 5837 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5838 | ret = io_issue_sqe(req, sqe, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 5839 | |
| 5840 | /* |
| 5841 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 5842 | * doesn't support non-blocking read/write attempts |
| 5843 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5844 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5845 | if (io_arm_poll_handler(req)) { |
| 5846 | if (linked_timeout) |
| 5847 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5848 | goto exit; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5849 | } |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5850 | punt: |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5851 | io_req_init_async(req); |
| 5852 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5853 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5854 | ret = io_grab_files(req); |
| 5855 | if (ret) |
| 5856 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5857 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5858 | |
| 5859 | /* |
| 5860 | * Queued up for async execution, worker will release |
| 5861 | * submit reference when the iocb is actually submitted. |
| 5862 | */ |
| 5863 | io_queue_async_work(req); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5864 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5865 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5866 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5867 | err: |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5868 | nxt = NULL; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5869 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 5870 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5871 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5872 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5873 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5874 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5875 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5876 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5877 | } |
| 5878 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5879 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5880 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5881 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5882 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5883 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5884 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5885 | if (nxt) { |
| 5886 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5887 | |
| 5888 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 5889 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5890 | goto again; |
| 5891 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5892 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5893 | if (old_creds) |
| 5894 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5895 | } |
| 5896 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5897 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5898 | { |
| 5899 | int ret; |
| 5900 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5901 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5902 | if (ret) { |
| 5903 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5904 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5905 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5906 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5907 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5908 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 5909 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 5910 | if (!req->io) { |
| 5911 | ret = -EAGAIN; |
| 5912 | if (io_alloc_async_ctx(req)) |
| 5913 | goto fail_req; |
| 5914 | ret = io_req_defer_prep(req, sqe); |
| 5915 | if (unlikely(ret < 0)) |
| 5916 | goto fail_req; |
| 5917 | } |
| 5918 | |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5919 | /* |
| 5920 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 5921 | * to async execution. |
| 5922 | */ |
| 5923 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 5924 | io_queue_async_work(req); |
| 5925 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5926 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5927 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5930 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5931 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5932 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5933 | io_cqring_add_event(req, -ECANCELED); |
| 5934 | io_double_put_req(req); |
| 5935 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5936 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5937 | } |
| 5938 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 5939 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Xiaoguang Wang | 7d01bd7 | 2020-05-08 21:19:30 +0800 | [diff] [blame] | 5940 | struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5941 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5942 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 5943 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5944 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5945 | /* |
| 5946 | * If we already have a head request, queue this one for async |
| 5947 | * submittal once the head completes. If we don't have a head but |
| 5948 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 5949 | * submitted sync once the chain is complete. If none of those |
| 5950 | * conditions are true (normal request), then just queue it. |
| 5951 | */ |
| 5952 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5953 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5954 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 5955 | /* |
| 5956 | * Taking sequential execution of a link, draining both sides |
| 5957 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 5958 | * requests in the link. So, it drains the head and the |
| 5959 | * next after the link request. The last one is done via |
| 5960 | * drain_next flag to persist the effect across calls. |
| 5961 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 5962 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5963 | head->flags |= REQ_F_IO_DRAIN; |
| 5964 | ctx->drain_next = 1; |
| 5965 | } |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 5966 | if (io_alloc_async_ctx(req)) |
| 5967 | return -EAGAIN; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5968 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5969 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5970 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5971 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5972 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 5973 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5974 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5975 | trace_io_uring_link(ctx, req, head); |
| 5976 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5977 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5978 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 5979 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5980 | io_queue_link_head(head); |
| 5981 | *link = NULL; |
| 5982 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5983 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5984 | if (unlikely(ctx->drain_next)) { |
| 5985 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 5986 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5987 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 5988 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 5989 | req->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5990 | INIT_LIST_HEAD(&req->link_list); |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 5991 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 5992 | if (io_alloc_async_ctx(req)) |
| 5993 | return -EAGAIN; |
| 5994 | |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5995 | ret = io_req_defer_prep(req, sqe); |
| 5996 | if (ret) |
| 5997 | req->flags |= REQ_F_FAIL_LINK; |
| 5998 | *link = req; |
| 5999 | } else { |
| 6000 | io_queue_sqe(req, sqe); |
| 6001 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6002 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6003 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6004 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6005 | } |
| 6006 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6007 | /* |
| 6008 | * Batched submission is done, ensure local IO is flushed out. |
| 6009 | */ |
| 6010 | static void io_submit_state_end(struct io_submit_state *state) |
| 6011 | { |
| 6012 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6013 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6014 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6015 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | /* |
| 6019 | * Start submission side cache. |
| 6020 | */ |
| 6021 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 6022 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6023 | { |
| 6024 | blk_start_plug(&state->plug); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 6025 | #ifdef CONFIG_BLOCK |
| 6026 | state->plug.nowait = true; |
| 6027 | #endif |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6028 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6029 | state->file = NULL; |
| 6030 | state->ios_left = max_ios; |
| 6031 | } |
| 6032 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6033 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6034 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6035 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6036 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6037 | /* |
| 6038 | * Ensure any loads from the SQEs are done at this point, |
| 6039 | * since once we write the new head, the application could |
| 6040 | * write new data to them. |
| 6041 | */ |
| 6042 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6046 | * 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] | 6047 | * that is mapped by userspace. This means that care needs to be taken to |
| 6048 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6049 | * being a good citizen. If members of the sqe are validated and then later |
| 6050 | * used, it's important that those reads are done through READ_ONCE() to |
| 6051 | * prevent a re-load down the line. |
| 6052 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6053 | 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] | 6054 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6055 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6056 | unsigned head; |
| 6057 | |
| 6058 | /* |
| 6059 | * The cached sq head (or cq tail) serves two purposes: |
| 6060 | * |
| 6061 | * 1) allows us to batch the cost of updating the user visible |
| 6062 | * head updates. |
| 6063 | * 2) allows the kernel side to track the head on its own, even |
| 6064 | * though the application is the one updating it. |
| 6065 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6066 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6067 | if (likely(head < ctx->sq_entries)) |
| 6068 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6069 | |
| 6070 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6071 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6072 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6073 | return NULL; |
| 6074 | } |
| 6075 | |
| 6076 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6077 | { |
| 6078 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6079 | } |
| 6080 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6081 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6082 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6083 | IOSQE_BUFFER_SELECT) |
| 6084 | |
| 6085 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6086 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6087 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6088 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6089 | unsigned int sqe_flags; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6090 | int id; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6091 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6092 | /* |
| 6093 | * All io need record the previous position, if LINK vs DARIN, |
| 6094 | * it can be used to mark the position of the first IO in the |
| 6095 | * link list. |
| 6096 | */ |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 6097 | req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6098 | req->opcode = READ_ONCE(sqe->opcode); |
| 6099 | req->user_data = READ_ONCE(sqe->user_data); |
| 6100 | req->io = NULL; |
| 6101 | req->file = NULL; |
| 6102 | req->ctx = ctx; |
| 6103 | req->flags = 0; |
| 6104 | /* one is dropped after submission, the other at completion */ |
| 6105 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6106 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6107 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6108 | |
| 6109 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6110 | return -EINVAL; |
| 6111 | |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6112 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
| 6113 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6114 | |
| 6115 | sqe_flags = READ_ONCE(sqe->flags); |
| 6116 | /* enforce forwards compatibility on users */ |
| 6117 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6118 | return -EINVAL; |
| 6119 | |
| 6120 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6121 | !io_op_defs[req->opcode].buffer_select) |
| 6122 | return -EOPNOTSUPP; |
| 6123 | |
| 6124 | id = READ_ONCE(sqe->personality); |
| 6125 | if (id) { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6126 | io_req_init_async(req); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6127 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 6128 | if (unlikely(!req->work.creds)) |
| 6129 | return -EINVAL; |
| 6130 | get_cred(req->work.creds); |
| 6131 | } |
| 6132 | |
| 6133 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6134 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6135 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6136 | if (!io_op_defs[req->opcode].needs_file) |
| 6137 | return 0; |
| 6138 | |
| 6139 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6140 | } |
| 6141 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6142 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6143 | struct file *ring_file, int ring_fd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6144 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6145 | struct io_submit_state state; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6146 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6147 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6148 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6149 | /* 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] | 6150 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 6151 | if (!list_empty(&ctx->cq_overflow_list) && |
| 6152 | !io_cqring_overflow_flush(ctx, false)) |
| 6153 | return -EBUSY; |
| 6154 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6155 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6156 | /* make sure SQ entry isn't read before tail */ |
| 6157 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6158 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6159 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6160 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6161 | |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6162 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6163 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6164 | ctx->ring_fd = ring_fd; |
| 6165 | ctx->ring_file = ring_file; |
| 6166 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6167 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6168 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6169 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6170 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6171 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6172 | sqe = io_get_sqe(ctx); |
| 6173 | if (unlikely(!sqe)) { |
| 6174 | io_consume_sqe(ctx); |
| 6175 | break; |
| 6176 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6177 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6178 | if (unlikely(!req)) { |
| 6179 | if (!submitted) |
| 6180 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6181 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6182 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6183 | |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6184 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6185 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6186 | /* will complete beyond this point, count as submitted */ |
| 6187 | submitted++; |
| 6188 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6189 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6190 | fail_req: |
| 6191 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6192 | io_double_put_req(req); |
| 6193 | break; |
| 6194 | } |
| 6195 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6196 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6197 | true, io_async_submit(ctx)); |
Xiaoguang Wang | 7d01bd7 | 2020-05-08 21:19:30 +0800 | [diff] [blame] | 6198 | err = io_submit_sqe(req, sqe, &link); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6199 | if (err) |
| 6200 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6203 | if (unlikely(submitted != nr)) { |
| 6204 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 6205 | |
| 6206 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 6207 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6208 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6209 | io_queue_link_head(link); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6210 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6211 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6212 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6213 | io_commit_sqring(ctx); |
| 6214 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6215 | return submitted; |
| 6216 | } |
| 6217 | |
| 6218 | static int io_sq_thread(void *data) |
| 6219 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6220 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6221 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6222 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6223 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6224 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6225 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6226 | complete(&ctx->sq_thread_comp); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 6227 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6228 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6229 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6230 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6231 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6232 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6233 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6234 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6235 | unsigned nr_events = 0; |
| 6236 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6237 | mutex_lock(&ctx->uring_lock); |
| 6238 | if (!list_empty(&ctx->poll_list)) |
| 6239 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 6240 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6241 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6242 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6245 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6246 | |
| 6247 | /* |
| 6248 | * If submit got -EBUSY, flag us as needing the application |
| 6249 | * to enter the kernel to reap and flush events. |
| 6250 | */ |
| 6251 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6252 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6253 | * Drop cur_mm before scheduling, we can't hold it for |
| 6254 | * long periods (or over schedule()). Do this before |
| 6255 | * adding ourselves to the waitqueue, as the unuse/drop |
| 6256 | * may sleep. |
| 6257 | */ |
Pavel Begunkov | bf9c2f1 | 2020-04-12 02:05:02 +0300 | [diff] [blame] | 6258 | io_sq_thread_drop_mm(ctx); |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6259 | |
| 6260 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6261 | * We're polling. If we're within the defined idle |
| 6262 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6263 | * to sleep. The exception is if we got EBUSY doing |
| 6264 | * more IO, we should wait for the application to |
| 6265 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6266 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6267 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6268 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 6269 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6270 | if (current->task_works) |
| 6271 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 6272 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6273 | continue; |
| 6274 | } |
| 6275 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6276 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 6277 | TASK_INTERRUPTIBLE); |
| 6278 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6279 | /* |
| 6280 | * While doing polled IO, before going to sleep, we need |
| 6281 | * to check if there are new reqs added to poll_list, it |
| 6282 | * is because reqs may have been punted to io worker and |
| 6283 | * will be added to poll_list later, hence check the |
| 6284 | * poll_list again. |
| 6285 | */ |
| 6286 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6287 | !list_empty_careful(&ctx->poll_list)) { |
| 6288 | finish_wait(&ctx->sqo_wait, &wait); |
| 6289 | continue; |
| 6290 | } |
| 6291 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6292 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6293 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 6294 | /* make sure to read SQ tail after writing flags */ |
| 6295 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6296 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6297 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6298 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6299 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6300 | finish_wait(&ctx->sqo_wait, &wait); |
| 6301 | break; |
| 6302 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6303 | if (current->task_works) { |
| 6304 | task_work_run(); |
Hillf Danton | 10bea96 | 2020-04-01 17:19:33 +0800 | [diff] [blame] | 6305 | finish_wait(&ctx->sqo_wait, &wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6306 | continue; |
| 6307 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6308 | if (signal_pending(current)) |
| 6309 | flush_signals(current); |
| 6310 | schedule(); |
| 6311 | finish_wait(&ctx->sqo_wait, &wait); |
| 6312 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6313 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Xiaoguang Wang | d4ae271 | 2020-05-20 21:24:35 +0800 | [diff] [blame] | 6314 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6315 | continue; |
| 6316 | } |
| 6317 | finish_wait(&ctx->sqo_wait, &wait); |
| 6318 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6319 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6322 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 6b668c9 | 2020-05-20 15:35:03 +0800 | [diff] [blame] | 6323 | if (likely(!percpu_ref_is_dying(&ctx->refs))) |
| 6324 | ret = io_submit_sqes(ctx, to_submit, NULL, -1); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6325 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6326 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6327 | } |
| 6328 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6329 | if (current->task_works) |
| 6330 | task_work_run(); |
| 6331 | |
Pavel Begunkov | bf9c2f1 | 2020-04-12 02:05:02 +0300 | [diff] [blame] | 6332 | io_sq_thread_drop_mm(ctx); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6333 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6334 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6335 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6336 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
| 6339 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6340 | struct io_wait_queue { |
| 6341 | struct wait_queue_entry wq; |
| 6342 | struct io_ring_ctx *ctx; |
| 6343 | unsigned to_wait; |
| 6344 | unsigned nr_timeouts; |
| 6345 | }; |
| 6346 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6347 | 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] | 6348 | { |
| 6349 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6350 | |
| 6351 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6352 | * 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] | 6353 | * started waiting. For timeouts, we always want to return to userspace, |
| 6354 | * regardless of event count. |
| 6355 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6356 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6357 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6358 | } |
| 6359 | |
| 6360 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6361 | int wake_flags, void *key) |
| 6362 | { |
| 6363 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6364 | wq); |
| 6365 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6366 | /* use noflush == true, as we can't safely rely on locking context */ |
| 6367 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6368 | return -1; |
| 6369 | |
| 6370 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6371 | } |
| 6372 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6373 | /* |
| 6374 | * Wait until events become available, if we don't already have some. The |
| 6375 | * application must reap them itself, as they reside on the shared cq ring. |
| 6376 | */ |
| 6377 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 6378 | const sigset_t __user *sig, size_t sigsz) |
| 6379 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6380 | struct io_wait_queue iowq = { |
| 6381 | .wq = { |
| 6382 | .private = current, |
| 6383 | .func = io_wake_function, |
| 6384 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6385 | }, |
| 6386 | .ctx = ctx, |
| 6387 | .to_wait = min_events, |
| 6388 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6389 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6390 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6391 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6392 | do { |
| 6393 | if (io_cqring_events(ctx, false) >= min_events) |
| 6394 | return 0; |
| 6395 | if (!current->task_works) |
| 6396 | break; |
| 6397 | task_work_run(); |
| 6398 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6399 | |
| 6400 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6401 | #ifdef CONFIG_COMPAT |
| 6402 | if (in_compat_syscall()) |
| 6403 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6404 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6405 | else |
| 6406 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6407 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6408 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6409 | if (ret) |
| 6410 | return ret; |
| 6411 | } |
| 6412 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6413 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6414 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6415 | do { |
| 6416 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6417 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6418 | if (current->task_works) |
| 6419 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6420 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6421 | break; |
| 6422 | schedule(); |
| 6423 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6424 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6425 | break; |
| 6426 | } |
| 6427 | } while (1); |
| 6428 | finish_wait(&ctx->wait, &iowq.wq); |
| 6429 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6430 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6431 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6432 | 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] | 6433 | } |
| 6434 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6435 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6436 | { |
| 6437 | #if defined(CONFIG_UNIX) |
| 6438 | if (ctx->ring_sock) { |
| 6439 | struct sock *sock = ctx->ring_sock->sk; |
| 6440 | struct sk_buff *skb; |
| 6441 | |
| 6442 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6443 | kfree_skb(skb); |
| 6444 | } |
| 6445 | #else |
| 6446 | int i; |
| 6447 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6448 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6449 | struct file *file; |
| 6450 | |
| 6451 | file = io_file_from_index(ctx, i); |
| 6452 | if (file) |
| 6453 | fput(file); |
| 6454 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6455 | #endif |
| 6456 | } |
| 6457 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6458 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 6459 | { |
| 6460 | struct fixed_file_data *data; |
| 6461 | |
| 6462 | data = container_of(ref, struct fixed_file_data, refs); |
| 6463 | complete(&data->done); |
| 6464 | } |
| 6465 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6466 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6467 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6468 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6469 | struct fixed_file_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6470 | unsigned nr_tables, i; |
| 6471 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6472 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6473 | return -ENXIO; |
| 6474 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6475 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6476 | if (!list_empty(&data->ref_list)) |
| 6477 | ref_node = list_first_entry(&data->ref_list, |
| 6478 | struct fixed_file_ref_node, node); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6479 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6480 | if (ref_node) |
| 6481 | percpu_ref_kill(&ref_node->refs); |
| 6482 | |
| 6483 | percpu_ref_kill(&data->refs); |
| 6484 | |
| 6485 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6486 | flush_delayed_work(&ctx->file_put_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6487 | wait_for_completion(&data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6488 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6489 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6490 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 6491 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6492 | kfree(data->table[i].files); |
| 6493 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6494 | percpu_ref_exit(&data->refs); |
| 6495 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6496 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6497 | ctx->nr_user_files = 0; |
| 6498 | return 0; |
| 6499 | } |
| 6500 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6501 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 6502 | { |
| 6503 | if (ctx->sqo_thread) { |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6504 | wait_for_completion(&ctx->sq_thread_comp); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6505 | /* |
| 6506 | * The park is a bit of a work-around, without it we get |
| 6507 | * warning spews on shutdown with SQPOLL set and affinity |
| 6508 | * set to a single CPU. |
| 6509 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6510 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6511 | kthread_stop(ctx->sqo_thread); |
| 6512 | ctx->sqo_thread = NULL; |
| 6513 | } |
| 6514 | } |
| 6515 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6516 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 6517 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6518 | io_sq_thread_stop(ctx); |
| 6519 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6520 | if (ctx->io_wq) { |
| 6521 | io_wq_destroy(ctx->io_wq); |
| 6522 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6523 | } |
| 6524 | } |
| 6525 | |
| 6526 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6527 | /* |
| 6528 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 6529 | * the io_uring can be safely unregistered on process exit, even if we have |
| 6530 | * loops in the file referencing. |
| 6531 | */ |
| 6532 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 6533 | { |
| 6534 | struct sock *sk = ctx->ring_sock->sk; |
| 6535 | struct scm_fp_list *fpl; |
| 6536 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6537 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6538 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6539 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 6540 | if (!fpl) |
| 6541 | return -ENOMEM; |
| 6542 | |
| 6543 | skb = alloc_skb(0, GFP_KERNEL); |
| 6544 | if (!skb) { |
| 6545 | kfree(fpl); |
| 6546 | return -ENOMEM; |
| 6547 | } |
| 6548 | |
| 6549 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6550 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6551 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6552 | fpl->user = get_uid(ctx->user); |
| 6553 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6554 | struct file *file = io_file_from_index(ctx, i + offset); |
| 6555 | |
| 6556 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6557 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6558 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6559 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 6560 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6561 | } |
| 6562 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6563 | if (nr_files) { |
| 6564 | fpl->max = SCM_MAX_FD; |
| 6565 | fpl->count = nr_files; |
| 6566 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6567 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6568 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 6569 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6570 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6571 | for (i = 0; i < nr_files; i++) |
| 6572 | fput(fpl->fp[i]); |
| 6573 | } else { |
| 6574 | kfree_skb(skb); |
| 6575 | kfree(fpl); |
| 6576 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6577 | |
| 6578 | return 0; |
| 6579 | } |
| 6580 | |
| 6581 | /* |
| 6582 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 6583 | * causes regular reference counting to break down. We rely on the UNIX |
| 6584 | * garbage collection to take care of this problem for us. |
| 6585 | */ |
| 6586 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6587 | { |
| 6588 | unsigned left, total; |
| 6589 | int ret = 0; |
| 6590 | |
| 6591 | total = 0; |
| 6592 | left = ctx->nr_user_files; |
| 6593 | while (left) { |
| 6594 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6595 | |
| 6596 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 6597 | if (ret) |
| 6598 | break; |
| 6599 | left -= this_files; |
| 6600 | total += this_files; |
| 6601 | } |
| 6602 | |
| 6603 | if (!ret) |
| 6604 | return 0; |
| 6605 | |
| 6606 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6607 | struct file *file = io_file_from_index(ctx, total); |
| 6608 | |
| 6609 | if (file) |
| 6610 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6611 | total++; |
| 6612 | } |
| 6613 | |
| 6614 | return ret; |
| 6615 | } |
| 6616 | #else |
| 6617 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6618 | { |
| 6619 | return 0; |
| 6620 | } |
| 6621 | #endif |
| 6622 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6623 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 6624 | unsigned nr_files) |
| 6625 | { |
| 6626 | int i; |
| 6627 | |
| 6628 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6629 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6630 | unsigned this_files; |
| 6631 | |
| 6632 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 6633 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 6634 | GFP_KERNEL); |
| 6635 | if (!table->files) |
| 6636 | break; |
| 6637 | nr_files -= this_files; |
| 6638 | } |
| 6639 | |
| 6640 | if (i == nr_tables) |
| 6641 | return 0; |
| 6642 | |
| 6643 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6644 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6645 | kfree(table->files); |
| 6646 | } |
| 6647 | return 1; |
| 6648 | } |
| 6649 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6650 | 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] | 6651 | { |
| 6652 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6653 | struct sock *sock = ctx->ring_sock->sk; |
| 6654 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 6655 | struct sk_buff *skb; |
| 6656 | int i; |
| 6657 | |
| 6658 | __skb_queue_head_init(&list); |
| 6659 | |
| 6660 | /* |
| 6661 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 6662 | * remove this entry and rearrange the file array. |
| 6663 | */ |
| 6664 | skb = skb_dequeue(head); |
| 6665 | while (skb) { |
| 6666 | struct scm_fp_list *fp; |
| 6667 | |
| 6668 | fp = UNIXCB(skb).fp; |
| 6669 | for (i = 0; i < fp->count; i++) { |
| 6670 | int left; |
| 6671 | |
| 6672 | if (fp->fp[i] != file) |
| 6673 | continue; |
| 6674 | |
| 6675 | unix_notinflight(fp->user, fp->fp[i]); |
| 6676 | left = fp->count - 1 - i; |
| 6677 | if (left) { |
| 6678 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 6679 | left * sizeof(struct file *)); |
| 6680 | } |
| 6681 | fp->count--; |
| 6682 | if (!fp->count) { |
| 6683 | kfree_skb(skb); |
| 6684 | skb = NULL; |
| 6685 | } else { |
| 6686 | __skb_queue_tail(&list, skb); |
| 6687 | } |
| 6688 | fput(file); |
| 6689 | file = NULL; |
| 6690 | break; |
| 6691 | } |
| 6692 | |
| 6693 | if (!file) |
| 6694 | break; |
| 6695 | |
| 6696 | __skb_queue_tail(&list, skb); |
| 6697 | |
| 6698 | skb = skb_dequeue(head); |
| 6699 | } |
| 6700 | |
| 6701 | if (skb_peek(&list)) { |
| 6702 | spin_lock_irq(&head->lock); |
| 6703 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 6704 | __skb_queue_tail(head, skb); |
| 6705 | spin_unlock_irq(&head->lock); |
| 6706 | } |
| 6707 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6708 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6709 | #endif |
| 6710 | } |
| 6711 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6712 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6713 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6714 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6715 | }; |
| 6716 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6717 | 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] | 6718 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6719 | struct fixed_file_data *file_data = ref_node->file_data; |
| 6720 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6721 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6722 | |
| 6723 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6724 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6725 | io_ring_file_put(ctx, pfile->file); |
| 6726 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6727 | } |
| 6728 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6729 | spin_lock(&file_data->lock); |
| 6730 | list_del(&ref_node->node); |
| 6731 | spin_unlock(&file_data->lock); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6732 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6733 | percpu_ref_exit(&ref_node->refs); |
| 6734 | kfree(ref_node); |
| 6735 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6736 | } |
| 6737 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6738 | static void io_file_put_work(struct work_struct *work) |
| 6739 | { |
| 6740 | struct io_ring_ctx *ctx; |
| 6741 | struct llist_node *node; |
| 6742 | |
| 6743 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 6744 | node = llist_del_all(&ctx->file_put_llist); |
| 6745 | |
| 6746 | while (node) { |
| 6747 | struct fixed_file_ref_node *ref_node; |
| 6748 | struct llist_node *next = node->next; |
| 6749 | |
| 6750 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 6751 | __io_file_put_work(ref_node); |
| 6752 | node = next; |
| 6753 | } |
| 6754 | } |
| 6755 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6756 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 6757 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6758 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6759 | struct io_ring_ctx *ctx; |
| 6760 | bool first_add; |
| 6761 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6762 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6763 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6764 | ctx = ref_node->file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6765 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6766 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
| 6767 | delay = 0; |
| 6768 | |
| 6769 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 6770 | if (!delay) |
| 6771 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 6772 | else if (first_add) |
| 6773 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 6777 | struct io_ring_ctx *ctx) |
| 6778 | { |
| 6779 | struct fixed_file_ref_node *ref_node; |
| 6780 | |
| 6781 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 6782 | if (!ref_node) |
| 6783 | return ERR_PTR(-ENOMEM); |
| 6784 | |
| 6785 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 6786 | 0, GFP_KERNEL)) { |
| 6787 | kfree(ref_node); |
| 6788 | return ERR_PTR(-ENOMEM); |
| 6789 | } |
| 6790 | INIT_LIST_HEAD(&ref_node->node); |
| 6791 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6792 | ref_node->file_data = ctx->file_data; |
| 6793 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6794 | } |
| 6795 | |
| 6796 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 6797 | { |
| 6798 | percpu_ref_exit(&ref_node->refs); |
| 6799 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6803 | unsigned nr_args) |
| 6804 | { |
| 6805 | __s32 __user *fds = (__s32 __user *) arg; |
| 6806 | unsigned nr_tables; |
| 6807 | struct file *file; |
| 6808 | int fd, ret = 0; |
| 6809 | unsigned i; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6810 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6811 | |
| 6812 | if (ctx->file_data) |
| 6813 | return -EBUSY; |
| 6814 | if (!nr_args) |
| 6815 | return -EINVAL; |
| 6816 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 6817 | return -EMFILE; |
| 6818 | |
| 6819 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 6820 | if (!ctx->file_data) |
| 6821 | return -ENOMEM; |
| 6822 | ctx->file_data->ctx = ctx; |
| 6823 | init_completion(&ctx->file_data->done); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6824 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
Xiaoguang Wang | f7fe934 | 2020-04-07 20:02:31 +0800 | [diff] [blame] | 6825 | spin_lock_init(&ctx->file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6826 | |
| 6827 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 6828 | ctx->file_data->table = kcalloc(nr_tables, |
| 6829 | sizeof(struct fixed_file_table), |
| 6830 | GFP_KERNEL); |
| 6831 | if (!ctx->file_data->table) { |
| 6832 | kfree(ctx->file_data); |
| 6833 | ctx->file_data = NULL; |
| 6834 | return -ENOMEM; |
| 6835 | } |
| 6836 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6837 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6838 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 6839 | kfree(ctx->file_data->table); |
| 6840 | kfree(ctx->file_data); |
| 6841 | ctx->file_data = NULL; |
| 6842 | return -ENOMEM; |
| 6843 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6844 | |
| 6845 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 6846 | percpu_ref_exit(&ctx->file_data->refs); |
| 6847 | kfree(ctx->file_data->table); |
| 6848 | kfree(ctx->file_data); |
| 6849 | ctx->file_data = NULL; |
| 6850 | return -ENOMEM; |
| 6851 | } |
| 6852 | |
| 6853 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 6854 | struct fixed_file_table *table; |
| 6855 | unsigned index; |
| 6856 | |
| 6857 | ret = -EFAULT; |
| 6858 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 6859 | break; |
| 6860 | /* allow sparse sets */ |
| 6861 | if (fd == -1) { |
| 6862 | ret = 0; |
| 6863 | continue; |
| 6864 | } |
| 6865 | |
| 6866 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6867 | index = i & IORING_FILE_TABLE_MASK; |
| 6868 | file = fget(fd); |
| 6869 | |
| 6870 | ret = -EBADF; |
| 6871 | if (!file) |
| 6872 | break; |
| 6873 | |
| 6874 | /* |
| 6875 | * Don't allow io_uring instances to be registered. If UNIX |
| 6876 | * isn't enabled, then this causes a reference cycle and this |
| 6877 | * instance can never get freed. If UNIX is enabled we'll |
| 6878 | * handle it just fine, but there's still no point in allowing |
| 6879 | * a ring fd as it doesn't support regular read/write anyway. |
| 6880 | */ |
| 6881 | if (file->f_op == &io_uring_fops) { |
| 6882 | fput(file); |
| 6883 | break; |
| 6884 | } |
| 6885 | ret = 0; |
| 6886 | table->files[index] = file; |
| 6887 | } |
| 6888 | |
| 6889 | if (ret) { |
| 6890 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6891 | file = io_file_from_index(ctx, i); |
| 6892 | if (file) |
| 6893 | fput(file); |
| 6894 | } |
| 6895 | for (i = 0; i < nr_tables; i++) |
| 6896 | kfree(ctx->file_data->table[i].files); |
| 6897 | |
| 6898 | kfree(ctx->file_data->table); |
| 6899 | kfree(ctx->file_data); |
| 6900 | ctx->file_data = NULL; |
| 6901 | ctx->nr_user_files = 0; |
| 6902 | return ret; |
| 6903 | } |
| 6904 | |
| 6905 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6906 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6907 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6908 | return ret; |
| 6909 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6910 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6911 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 6912 | if (IS_ERR(ref_node)) { |
| 6913 | io_sqe_files_unregister(ctx); |
| 6914 | return PTR_ERR(ref_node); |
| 6915 | } |
| 6916 | |
| 6917 | ctx->file_data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6918 | spin_lock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6919 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6920 | spin_unlock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6921 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6922 | return ret; |
| 6923 | } |
| 6924 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6925 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 6926 | int index) |
| 6927 | { |
| 6928 | #if defined(CONFIG_UNIX) |
| 6929 | struct sock *sock = ctx->ring_sock->sk; |
| 6930 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 6931 | struct sk_buff *skb; |
| 6932 | |
| 6933 | /* |
| 6934 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 6935 | * file set. If there's no room, fall back to allocating a new skb |
| 6936 | * and filling it in. |
| 6937 | */ |
| 6938 | spin_lock_irq(&head->lock); |
| 6939 | skb = skb_peek(head); |
| 6940 | if (skb) { |
| 6941 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 6942 | |
| 6943 | if (fpl->count < SCM_MAX_FD) { |
| 6944 | __skb_unlink(skb, head); |
| 6945 | spin_unlock_irq(&head->lock); |
| 6946 | fpl->fp[fpl->count] = get_file(file); |
| 6947 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 6948 | fpl->count++; |
| 6949 | spin_lock_irq(&head->lock); |
| 6950 | __skb_queue_head(head, skb); |
| 6951 | } else { |
| 6952 | skb = NULL; |
| 6953 | } |
| 6954 | } |
| 6955 | spin_unlock_irq(&head->lock); |
| 6956 | |
| 6957 | if (skb) { |
| 6958 | fput(file); |
| 6959 | return 0; |
| 6960 | } |
| 6961 | |
| 6962 | return __io_sqe_files_scm(ctx, 1, index); |
| 6963 | #else |
| 6964 | return 0; |
| 6965 | #endif |
| 6966 | } |
| 6967 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 6968 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6969 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6970 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 6971 | struct io_file_put *pfile; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6972 | struct percpu_ref *refs = data->cur_refs; |
| 6973 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6974 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6975 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 6976 | if (!pfile) |
| 6977 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6978 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6979 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6980 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6981 | list_add(&pfile->list, &ref_node->file_list); |
| 6982 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 6983 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 6987 | struct io_uring_files_update *up, |
| 6988 | unsigned nr_args) |
| 6989 | { |
| 6990 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6991 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6992 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6993 | __s32 __user *fds; |
| 6994 | int fd, i, err; |
| 6995 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6996 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6997 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6998 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6999 | return -EOVERFLOW; |
| 7000 | if (done > ctx->nr_user_files) |
| 7001 | return -EINVAL; |
| 7002 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7003 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7004 | if (IS_ERR(ref_node)) |
| 7005 | return PTR_ERR(ref_node); |
| 7006 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7007 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7008 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7009 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7010 | struct fixed_file_table *table; |
| 7011 | unsigned index; |
| 7012 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7013 | err = 0; |
| 7014 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7015 | err = -EFAULT; |
| 7016 | break; |
| 7017 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7018 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7019 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7020 | index = i & IORING_FILE_TABLE_MASK; |
| 7021 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7022 | file = io_file_from_index(ctx, index); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7023 | err = io_queue_file_removal(data, file); |
| 7024 | if (err) |
| 7025 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7026 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7027 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7028 | } |
| 7029 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7030 | file = fget(fd); |
| 7031 | if (!file) { |
| 7032 | err = -EBADF; |
| 7033 | break; |
| 7034 | } |
| 7035 | /* |
| 7036 | * Don't allow io_uring instances to be registered. If |
| 7037 | * UNIX isn't enabled, then this causes a reference |
| 7038 | * cycle and this instance can never get freed. If UNIX |
| 7039 | * is enabled we'll handle it just fine, but there's |
| 7040 | * still no point in allowing a ring fd as it doesn't |
| 7041 | * support regular read/write anyway. |
| 7042 | */ |
| 7043 | if (file->f_op == &io_uring_fops) { |
| 7044 | fput(file); |
| 7045 | err = -EBADF; |
| 7046 | break; |
| 7047 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7048 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7049 | err = io_sqe_file_register(ctx, file, i); |
| 7050 | if (err) |
| 7051 | break; |
| 7052 | } |
| 7053 | nr_args--; |
| 7054 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7055 | up->offset++; |
| 7056 | } |
| 7057 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7058 | if (needs_switch) { |
| 7059 | percpu_ref_kill(data->cur_refs); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7060 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7061 | list_add(&ref_node->node, &data->ref_list); |
| 7062 | data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7063 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7064 | percpu_ref_get(&ctx->file_data->refs); |
| 7065 | } else |
| 7066 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7067 | |
| 7068 | return done ? done : err; |
| 7069 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7070 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7071 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7072 | unsigned nr_args) |
| 7073 | { |
| 7074 | struct io_uring_files_update up; |
| 7075 | |
| 7076 | if (!ctx->file_data) |
| 7077 | return -ENXIO; |
| 7078 | if (!nr_args) |
| 7079 | return -EINVAL; |
| 7080 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7081 | return -EFAULT; |
| 7082 | if (up.resv) |
| 7083 | return -EINVAL; |
| 7084 | |
| 7085 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7086 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7087 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7088 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7089 | { |
| 7090 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7091 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7092 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7093 | io_put_req(req); |
| 7094 | } |
| 7095 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7096 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7097 | struct io_uring_params *p) |
| 7098 | { |
| 7099 | struct io_wq_data data; |
| 7100 | struct fd f; |
| 7101 | struct io_ring_ctx *ctx_attach; |
| 7102 | unsigned int concurrency; |
| 7103 | int ret = 0; |
| 7104 | |
| 7105 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7106 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7107 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7108 | |
| 7109 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 7110 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7111 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 7112 | |
| 7113 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 7114 | if (IS_ERR(ctx->io_wq)) { |
| 7115 | ret = PTR_ERR(ctx->io_wq); |
| 7116 | ctx->io_wq = NULL; |
| 7117 | } |
| 7118 | return ret; |
| 7119 | } |
| 7120 | |
| 7121 | f = fdget(p->wq_fd); |
| 7122 | if (!f.file) |
| 7123 | return -EBADF; |
| 7124 | |
| 7125 | if (f.file->f_op != &io_uring_fops) { |
| 7126 | ret = -EINVAL; |
| 7127 | goto out_fput; |
| 7128 | } |
| 7129 | |
| 7130 | ctx_attach = f.file->private_data; |
| 7131 | /* @io_wq is protected by holding the fd */ |
| 7132 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 7133 | ret = -EINVAL; |
| 7134 | goto out_fput; |
| 7135 | } |
| 7136 | |
| 7137 | ctx->io_wq = ctx_attach->io_wq; |
| 7138 | out_fput: |
| 7139 | fdput(f); |
| 7140 | return ret; |
| 7141 | } |
| 7142 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7143 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 7144 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7145 | { |
| 7146 | int ret; |
| 7147 | |
| 7148 | mmgrab(current->mm); |
| 7149 | ctx->sqo_mm = current->mm; |
| 7150 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7151 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7152 | ret = -EPERM; |
| 7153 | if (!capable(CAP_SYS_ADMIN)) |
| 7154 | goto err; |
| 7155 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7156 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7157 | if (!ctx->sq_thread_idle) |
| 7158 | ctx->sq_thread_idle = HZ; |
| 7159 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7160 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7161 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7162 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7163 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7164 | if (cpu >= nr_cpu_ids) |
| 7165 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7166 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7167 | goto err; |
| 7168 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7169 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 7170 | ctx, cpu, |
| 7171 | "io_uring-sq"); |
| 7172 | } else { |
| 7173 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 7174 | "io_uring-sq"); |
| 7175 | } |
| 7176 | if (IS_ERR(ctx->sqo_thread)) { |
| 7177 | ret = PTR_ERR(ctx->sqo_thread); |
| 7178 | ctx->sqo_thread = NULL; |
| 7179 | goto err; |
| 7180 | } |
| 7181 | wake_up_process(ctx->sqo_thread); |
| 7182 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7183 | /* Can't have SQ_AFF without SQPOLL */ |
| 7184 | ret = -EINVAL; |
| 7185 | goto err; |
| 7186 | } |
| 7187 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7188 | ret = io_init_wq_offload(ctx, p); |
| 7189 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7190 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7191 | |
| 7192 | return 0; |
| 7193 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 7194 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7195 | mmdrop(ctx->sqo_mm); |
| 7196 | ctx->sqo_mm = NULL; |
| 7197 | return ret; |
| 7198 | } |
| 7199 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7200 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7201 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7202 | { |
| 7203 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7204 | } |
| 7205 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7206 | static inline int __io_account_mem(struct user_struct *user, |
| 7207 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7208 | { |
| 7209 | unsigned long page_limit, cur_pages, new_pages; |
| 7210 | |
| 7211 | /* Don't allow more pages than we can safely lock */ |
| 7212 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7213 | |
| 7214 | do { |
| 7215 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7216 | new_pages = cur_pages + nr_pages; |
| 7217 | if (new_pages > page_limit) |
| 7218 | return -ENOMEM; |
| 7219 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7220 | new_pages) != cur_pages); |
| 7221 | |
| 7222 | return 0; |
| 7223 | } |
| 7224 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7225 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7226 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7227 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 7228 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7229 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7230 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7231 | if (ctx->sqo_mm) { |
| 7232 | if (acct == ACCT_LOCKED) |
| 7233 | ctx->sqo_mm->locked_vm -= nr_pages; |
| 7234 | else if (acct == ACCT_PINNED) |
| 7235 | atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7236 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7237 | } |
| 7238 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7239 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7240 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7241 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7242 | int ret; |
| 7243 | |
| 7244 | if (ctx->limit_mem) { |
| 7245 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7246 | if (ret) |
| 7247 | return ret; |
| 7248 | } |
| 7249 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7250 | if (ctx->sqo_mm) { |
| 7251 | if (acct == ACCT_LOCKED) |
| 7252 | ctx->sqo_mm->locked_vm += nr_pages; |
| 7253 | else if (acct == ACCT_PINNED) |
| 7254 | atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7255 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7256 | |
| 7257 | return 0; |
| 7258 | } |
| 7259 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7260 | static void io_mem_free(void *ptr) |
| 7261 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7262 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7263 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7264 | if (!ptr) |
| 7265 | return; |
| 7266 | |
| 7267 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7268 | if (put_page_testzero(page)) |
| 7269 | free_compound_page(page); |
| 7270 | } |
| 7271 | |
| 7272 | static void *io_mem_alloc(size_t size) |
| 7273 | { |
| 7274 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 7275 | __GFP_NORETRY; |
| 7276 | |
| 7277 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7278 | } |
| 7279 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7280 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7281 | size_t *sq_offset) |
| 7282 | { |
| 7283 | struct io_rings *rings; |
| 7284 | size_t off, sq_array_size; |
| 7285 | |
| 7286 | off = struct_size(rings, cqes, cq_entries); |
| 7287 | if (off == SIZE_MAX) |
| 7288 | return SIZE_MAX; |
| 7289 | |
| 7290 | #ifdef CONFIG_SMP |
| 7291 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 7292 | if (off == 0) |
| 7293 | return SIZE_MAX; |
| 7294 | #endif |
| 7295 | |
| 7296 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 7297 | if (sq_array_size == SIZE_MAX) |
| 7298 | return SIZE_MAX; |
| 7299 | |
| 7300 | if (check_add_overflow(off, sq_array_size, &off)) |
| 7301 | return SIZE_MAX; |
| 7302 | |
| 7303 | if (sq_offset) |
| 7304 | *sq_offset = off; |
| 7305 | |
| 7306 | return off; |
| 7307 | } |
| 7308 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7309 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 7310 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7311 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7312 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7313 | pages = (size_t)1 << get_order( |
| 7314 | rings_size(sq_entries, cq_entries, NULL)); |
| 7315 | pages += (size_t)1 << get_order( |
| 7316 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7317 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7318 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7319 | } |
| 7320 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7321 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 7322 | { |
| 7323 | int i, j; |
| 7324 | |
| 7325 | if (!ctx->user_bufs) |
| 7326 | return -ENXIO; |
| 7327 | |
| 7328 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7329 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7330 | |
| 7331 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7332 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7333 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7334 | io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7335 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7336 | imu->nr_bvecs = 0; |
| 7337 | } |
| 7338 | |
| 7339 | kfree(ctx->user_bufs); |
| 7340 | ctx->user_bufs = NULL; |
| 7341 | ctx->nr_user_bufs = 0; |
| 7342 | return 0; |
| 7343 | } |
| 7344 | |
| 7345 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 7346 | void __user *arg, unsigned index) |
| 7347 | { |
| 7348 | struct iovec __user *src; |
| 7349 | |
| 7350 | #ifdef CONFIG_COMPAT |
| 7351 | if (ctx->compat) { |
| 7352 | struct compat_iovec __user *ciovs; |
| 7353 | struct compat_iovec ciov; |
| 7354 | |
| 7355 | ciovs = (struct compat_iovec __user *) arg; |
| 7356 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 7357 | return -EFAULT; |
| 7358 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 7359 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7360 | dst->iov_len = ciov.iov_len; |
| 7361 | return 0; |
| 7362 | } |
| 7363 | #endif |
| 7364 | src = (struct iovec __user *) arg; |
| 7365 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 7366 | return -EFAULT; |
| 7367 | return 0; |
| 7368 | } |
| 7369 | |
| 7370 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7371 | unsigned nr_args) |
| 7372 | { |
| 7373 | struct vm_area_struct **vmas = NULL; |
| 7374 | struct page **pages = NULL; |
| 7375 | int i, j, got_pages = 0; |
| 7376 | int ret = -EINVAL; |
| 7377 | |
| 7378 | if (ctx->user_bufs) |
| 7379 | return -EBUSY; |
| 7380 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 7381 | return -EINVAL; |
| 7382 | |
| 7383 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 7384 | GFP_KERNEL); |
| 7385 | if (!ctx->user_bufs) |
| 7386 | return -ENOMEM; |
| 7387 | |
| 7388 | for (i = 0; i < nr_args; i++) { |
| 7389 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7390 | unsigned long off, start, end, ubuf; |
| 7391 | int pret, nr_pages; |
| 7392 | struct iovec iov; |
| 7393 | size_t size; |
| 7394 | |
| 7395 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 7396 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 7397 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7398 | |
| 7399 | /* |
| 7400 | * Don't impose further limits on the size and buffer |
| 7401 | * constraints here, we'll -EINVAL later when IO is |
| 7402 | * submitted if they are wrong. |
| 7403 | */ |
| 7404 | ret = -EFAULT; |
| 7405 | if (!iov.iov_base || !iov.iov_len) |
| 7406 | goto err; |
| 7407 | |
| 7408 | /* arbitrary limit, but we need something */ |
| 7409 | if (iov.iov_len > SZ_1G) |
| 7410 | goto err; |
| 7411 | |
| 7412 | ubuf = (unsigned long) iov.iov_base; |
| 7413 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 7414 | start = ubuf >> PAGE_SHIFT; |
| 7415 | nr_pages = end - start; |
| 7416 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7417 | ret = io_account_mem(ctx, nr_pages, ACCT_PINNED); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7418 | if (ret) |
| 7419 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7420 | |
| 7421 | ret = 0; |
| 7422 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 7423 | kvfree(vmas); |
| 7424 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7425 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7426 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7427 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7428 | sizeof(struct vm_area_struct *), |
| 7429 | GFP_KERNEL); |
| 7430 | if (!pages || !vmas) { |
| 7431 | ret = -ENOMEM; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7432 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7433 | goto err; |
| 7434 | } |
| 7435 | got_pages = nr_pages; |
| 7436 | } |
| 7437 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7438 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7439 | GFP_KERNEL); |
| 7440 | ret = -ENOMEM; |
| 7441 | if (!imu->bvec) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7442 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7443 | goto err; |
| 7444 | } |
| 7445 | |
| 7446 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7447 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 7448 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 7449 | FOLL_WRITE | FOLL_LONGTERM, |
| 7450 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7451 | if (pret == nr_pages) { |
| 7452 | /* don't support file backed memory */ |
| 7453 | for (j = 0; j < nr_pages; j++) { |
| 7454 | struct vm_area_struct *vma = vmas[j]; |
| 7455 | |
| 7456 | if (vma->vm_file && |
| 7457 | !is_file_hugepages(vma->vm_file)) { |
| 7458 | ret = -EOPNOTSUPP; |
| 7459 | break; |
| 7460 | } |
| 7461 | } |
| 7462 | } else { |
| 7463 | ret = pret < 0 ? pret : -EFAULT; |
| 7464 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7465 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7466 | if (ret) { |
| 7467 | /* |
| 7468 | * if we did partial map, or found file backed vmas, |
| 7469 | * release any pages we did get |
| 7470 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 7471 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7472 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7473 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7474 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7475 | goto err; |
| 7476 | } |
| 7477 | |
| 7478 | off = ubuf & ~PAGE_MASK; |
| 7479 | size = iov.iov_len; |
| 7480 | for (j = 0; j < nr_pages; j++) { |
| 7481 | size_t vec_len; |
| 7482 | |
| 7483 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 7484 | imu->bvec[j].bv_page = pages[j]; |
| 7485 | imu->bvec[j].bv_len = vec_len; |
| 7486 | imu->bvec[j].bv_offset = off; |
| 7487 | off = 0; |
| 7488 | size -= vec_len; |
| 7489 | } |
| 7490 | /* store original address for later verification */ |
| 7491 | imu->ubuf = ubuf; |
| 7492 | imu->len = iov.iov_len; |
| 7493 | imu->nr_bvecs = nr_pages; |
| 7494 | |
| 7495 | ctx->nr_user_bufs++; |
| 7496 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7497 | kvfree(pages); |
| 7498 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7499 | return 0; |
| 7500 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7501 | kvfree(pages); |
| 7502 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7503 | io_sqe_buffer_unregister(ctx); |
| 7504 | return ret; |
| 7505 | } |
| 7506 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7507 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 7508 | { |
| 7509 | __s32 __user *fds = arg; |
| 7510 | int fd; |
| 7511 | |
| 7512 | if (ctx->cq_ev_fd) |
| 7513 | return -EBUSY; |
| 7514 | |
| 7515 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 7516 | return -EFAULT; |
| 7517 | |
| 7518 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 7519 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 7520 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 7521 | ctx->cq_ev_fd = NULL; |
| 7522 | return ret; |
| 7523 | } |
| 7524 | |
| 7525 | return 0; |
| 7526 | } |
| 7527 | |
| 7528 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 7529 | { |
| 7530 | if (ctx->cq_ev_fd) { |
| 7531 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 7532 | ctx->cq_ev_fd = NULL; |
| 7533 | return 0; |
| 7534 | } |
| 7535 | |
| 7536 | return -ENXIO; |
| 7537 | } |
| 7538 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7539 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 7540 | { |
| 7541 | struct io_ring_ctx *ctx = data; |
| 7542 | struct io_buffer *buf = p; |
| 7543 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 7544 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7545 | return 0; |
| 7546 | } |
| 7547 | |
| 7548 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 7549 | { |
| 7550 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 7551 | idr_destroy(&ctx->io_buffer_idr); |
| 7552 | } |
| 7553 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7554 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 7555 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7556 | io_finish_async(ctx); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7557 | if (ctx->sqo_mm) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7558 | mmdrop(ctx->sqo_mm); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7559 | ctx->sqo_mm = NULL; |
| 7560 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7561 | |
| 7562 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7563 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7564 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7565 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7566 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 7567 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7568 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7569 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7570 | if (ctx->ring_sock) { |
| 7571 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7572 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7573 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7574 | #endif |
| 7575 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7576 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7577 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7578 | |
| 7579 | percpu_ref_exit(&ctx->refs); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7580 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 7581 | ACCT_LOCKED); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7582 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 7583 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 7584 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 7585 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7586 | kfree(ctx); |
| 7587 | } |
| 7588 | |
| 7589 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 7590 | { |
| 7591 | struct io_ring_ctx *ctx = file->private_data; |
| 7592 | __poll_t mask = 0; |
| 7593 | |
| 7594 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 7595 | /* |
| 7596 | * synchronizes with barrier from wq_has_sleeper call in |
| 7597 | * io_commit_cqring |
| 7598 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7599 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7600 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 7601 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7602 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 7603 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7604 | mask |= EPOLLIN | EPOLLRDNORM; |
| 7605 | |
| 7606 | return mask; |
| 7607 | } |
| 7608 | |
| 7609 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 7610 | { |
| 7611 | struct io_ring_ctx *ctx = file->private_data; |
| 7612 | |
| 7613 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 7614 | } |
| 7615 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7616 | static int io_remove_personalities(int id, void *p, void *data) |
| 7617 | { |
| 7618 | struct io_ring_ctx *ctx = data; |
| 7619 | const struct cred *cred; |
| 7620 | |
| 7621 | cred = idr_remove(&ctx->personality_idr, id); |
| 7622 | if (cred) |
| 7623 | put_cred(cred); |
| 7624 | return 0; |
| 7625 | } |
| 7626 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7627 | static void io_ring_exit_work(struct work_struct *work) |
| 7628 | { |
| 7629 | struct io_ring_ctx *ctx; |
| 7630 | |
| 7631 | ctx = container_of(work, struct io_ring_ctx, exit_work); |
| 7632 | if (ctx->rings) |
| 7633 | io_cqring_overflow_flush(ctx, true); |
| 7634 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 7635 | /* |
| 7636 | * If we're doing polled IO and end up having requests being |
| 7637 | * submitted async (out-of-line), then completions can come in while |
| 7638 | * we're waiting for refs to drop. We need to reap these manually, |
| 7639 | * as nobody else will be looking for them. |
| 7640 | */ |
| 7641 | while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) { |
| 7642 | io_iopoll_reap_events(ctx); |
| 7643 | if (ctx->rings) |
| 7644 | io_cqring_overflow_flush(ctx, true); |
| 7645 | } |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7646 | io_ring_ctx_free(ctx); |
| 7647 | } |
| 7648 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7649 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 7650 | { |
| 7651 | mutex_lock(&ctx->uring_lock); |
| 7652 | percpu_ref_kill(&ctx->refs); |
| 7653 | mutex_unlock(&ctx->uring_lock); |
| 7654 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 7655 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 7656 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7657 | |
| 7658 | if (ctx->io_wq) |
| 7659 | io_wq_cancel_all(ctx->io_wq); |
| 7660 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7661 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 7662 | /* if we failed setting up the ctx, we might not have any rings */ |
| 7663 | if (ctx->rings) |
| 7664 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7665 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7666 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
| 7667 | queue_work(system_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | static int io_uring_release(struct inode *inode, struct file *file) |
| 7671 | { |
| 7672 | struct io_ring_ctx *ctx = file->private_data; |
| 7673 | |
| 7674 | file->private_data = NULL; |
| 7675 | io_ring_ctx_wait_and_kill(ctx); |
| 7676 | return 0; |
| 7677 | } |
| 7678 | |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7679 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
| 7680 | { |
| 7681 | struct files_struct *files = data; |
| 7682 | |
| 7683 | return work->files == files; |
| 7684 | } |
| 7685 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7686 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 7687 | struct files_struct *files) |
| 7688 | { |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7689 | if (list_empty_careful(&ctx->inflight_list)) |
| 7690 | return; |
| 7691 | |
| 7692 | /* cancel all at once, should be faster than doing it one by one*/ |
| 7693 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); |
| 7694 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7695 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7696 | struct io_kiocb *cancel_req = NULL, *req; |
| 7697 | DEFINE_WAIT(wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7698 | |
| 7699 | spin_lock_irq(&ctx->inflight_lock); |
| 7700 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7701 | if (req->work.files != files) |
| 7702 | continue; |
| 7703 | /* req is being completed, ignore */ |
| 7704 | if (!refcount_inc_not_zero(&req->refs)) |
| 7705 | continue; |
| 7706 | cancel_req = req; |
| 7707 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7708 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7709 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7710 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7711 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7712 | spin_unlock_irq(&ctx->inflight_lock); |
| 7713 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7714 | /* We need to keep going until we don't find a matching req */ |
| 7715 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7716 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7717 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7718 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 7719 | spin_lock_irq(&ctx->completion_lock); |
| 7720 | list_del(&cancel_req->list); |
| 7721 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 7722 | if (list_empty(&ctx->cq_overflow_list)) { |
| 7723 | clear_bit(0, &ctx->sq_check_overflow); |
| 7724 | clear_bit(0, &ctx->cq_check_overflow); |
| 7725 | } |
| 7726 | spin_unlock_irq(&ctx->completion_lock); |
| 7727 | |
| 7728 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 7729 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 7730 | |
| 7731 | /* |
| 7732 | * Put inflight ref and overflow ref. If that's |
| 7733 | * all we had, then we're done with this request. |
| 7734 | */ |
| 7735 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
Pavel Begunkov | 4518a3c | 2020-05-26 20:34:02 +0300 | [diff] [blame] | 7736 | io_free_req(cancel_req); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7737 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7738 | continue; |
| 7739 | } |
Pavel Begunkov | 7b53d59 | 2020-05-30 14:19:15 +0300 | [diff] [blame] | 7740 | } else { |
| 7741 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 7742 | io_put_req(cancel_req); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7743 | } |
| 7744 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7745 | schedule(); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7746 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7747 | } |
| 7748 | } |
| 7749 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7750 | 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] | 7751 | { |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7752 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7753 | struct task_struct *task = data; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7754 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7755 | return req->task == task; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7756 | } |
| 7757 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7758 | static int io_uring_flush(struct file *file, void *data) |
| 7759 | { |
| 7760 | struct io_ring_ctx *ctx = file->private_data; |
| 7761 | |
| 7762 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7763 | |
| 7764 | /* |
| 7765 | * If the task is going away, cancel work it may have pending |
| 7766 | */ |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7767 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 7768 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7769 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7770 | return 0; |
| 7771 | } |
| 7772 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7773 | static void *io_uring_validate_mmap_request(struct file *file, |
| 7774 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7775 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7776 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7777 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7778 | struct page *page; |
| 7779 | void *ptr; |
| 7780 | |
| 7781 | switch (offset) { |
| 7782 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7783 | case IORING_OFF_CQ_RING: |
| 7784 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7785 | break; |
| 7786 | case IORING_OFF_SQES: |
| 7787 | ptr = ctx->sq_sqes; |
| 7788 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7789 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7790 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7791 | } |
| 7792 | |
| 7793 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 7794 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7795 | return ERR_PTR(-EINVAL); |
| 7796 | |
| 7797 | return ptr; |
| 7798 | } |
| 7799 | |
| 7800 | #ifdef CONFIG_MMU |
| 7801 | |
| 7802 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7803 | { |
| 7804 | size_t sz = vma->vm_end - vma->vm_start; |
| 7805 | unsigned long pfn; |
| 7806 | void *ptr; |
| 7807 | |
| 7808 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 7809 | if (IS_ERR(ptr)) |
| 7810 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7811 | |
| 7812 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 7813 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 7814 | } |
| 7815 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7816 | #else /* !CONFIG_MMU */ |
| 7817 | |
| 7818 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7819 | { |
| 7820 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 7821 | } |
| 7822 | |
| 7823 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 7824 | { |
| 7825 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 7826 | } |
| 7827 | |
| 7828 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 7829 | unsigned long addr, unsigned long len, |
| 7830 | unsigned long pgoff, unsigned long flags) |
| 7831 | { |
| 7832 | void *ptr; |
| 7833 | |
| 7834 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 7835 | if (IS_ERR(ptr)) |
| 7836 | return PTR_ERR(ptr); |
| 7837 | |
| 7838 | return (unsigned long) ptr; |
| 7839 | } |
| 7840 | |
| 7841 | #endif /* !CONFIG_MMU */ |
| 7842 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7843 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 7844 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 7845 | size_t, sigsz) |
| 7846 | { |
| 7847 | struct io_ring_ctx *ctx; |
| 7848 | long ret = -EBADF; |
| 7849 | int submitted = 0; |
| 7850 | struct fd f; |
| 7851 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7852 | if (current->task_works) |
| 7853 | task_work_run(); |
| 7854 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7855 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7856 | return -EINVAL; |
| 7857 | |
| 7858 | f = fdget(fd); |
| 7859 | if (!f.file) |
| 7860 | return -EBADF; |
| 7861 | |
| 7862 | ret = -EOPNOTSUPP; |
| 7863 | if (f.file->f_op != &io_uring_fops) |
| 7864 | goto out_fput; |
| 7865 | |
| 7866 | ret = -ENXIO; |
| 7867 | ctx = f.file->private_data; |
| 7868 | if (!percpu_ref_tryget(&ctx->refs)) |
| 7869 | goto out_fput; |
| 7870 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7871 | /* |
| 7872 | * For SQ polling, the thread will do all submissions and completions. |
| 7873 | * Just return the requested submit count, and wake the thread if |
| 7874 | * we were asked to. |
| 7875 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7876 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7877 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7878 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 7879 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7880 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 7881 | wake_up(&ctx->sqo_wait); |
| 7882 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7883 | } else if (to_submit) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7884 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 7885 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7886 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7887 | |
| 7888 | if (submitted != to_submit) |
| 7889 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7890 | } |
| 7891 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7892 | unsigned nr_events = 0; |
| 7893 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7894 | min_complete = min(min_complete, ctx->cq_entries); |
| 7895 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 7896 | /* |
| 7897 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 7898 | * space applications don't need to do io completion events |
| 7899 | * polling again, they can rely on io_sq_thread to do polling |
| 7900 | * work, which can reduce cpu usage and uring_lock contention. |
| 7901 | */ |
| 7902 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 7903 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7904 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7905 | } else { |
| 7906 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 7907 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7908 | } |
| 7909 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7910 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 7911 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7912 | out_fput: |
| 7913 | fdput(f); |
| 7914 | return submitted ? submitted : ret; |
| 7915 | } |
| 7916 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7917 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7918 | static int io_uring_show_cred(int id, void *p, void *data) |
| 7919 | { |
| 7920 | const struct cred *cred = p; |
| 7921 | struct seq_file *m = data; |
| 7922 | struct user_namespace *uns = seq_user_ns(m); |
| 7923 | struct group_info *gi; |
| 7924 | kernel_cap_t cap; |
| 7925 | unsigned __capi; |
| 7926 | int g; |
| 7927 | |
| 7928 | seq_printf(m, "%5d\n", id); |
| 7929 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 7930 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 7931 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 7932 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 7933 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 7934 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 7935 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 7936 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 7937 | seq_puts(m, "\n\tGroups:\t"); |
| 7938 | gi = cred->group_info; |
| 7939 | for (g = 0; g < gi->ngroups; g++) { |
| 7940 | seq_put_decimal_ull(m, g ? " " : "", |
| 7941 | from_kgid_munged(uns, gi->gid[g])); |
| 7942 | } |
| 7943 | seq_puts(m, "\n\tCapEff:\t"); |
| 7944 | cap = cred->cap_effective; |
| 7945 | CAP_FOR_EACH_U32(__capi) |
| 7946 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 7947 | seq_putc(m, '\n'); |
| 7948 | return 0; |
| 7949 | } |
| 7950 | |
| 7951 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 7952 | { |
| 7953 | int i; |
| 7954 | |
| 7955 | mutex_lock(&ctx->uring_lock); |
| 7956 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 7957 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7958 | struct fixed_file_table *table; |
| 7959 | struct file *f; |
| 7960 | |
| 7961 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7962 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 7963 | if (f) |
| 7964 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 7965 | else |
| 7966 | seq_printf(m, "%5u: <none>\n", i); |
| 7967 | } |
| 7968 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 7969 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7970 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 7971 | |
| 7972 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 7973 | (unsigned int) buf->len); |
| 7974 | } |
| 7975 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 7976 | seq_printf(m, "Personalities:\n"); |
| 7977 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 7978 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7979 | seq_printf(m, "PollList:\n"); |
| 7980 | spin_lock_irq(&ctx->completion_lock); |
| 7981 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 7982 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 7983 | struct io_kiocb *req; |
| 7984 | |
| 7985 | hlist_for_each_entry(req, list, hash_node) |
| 7986 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 7987 | req->task->task_works != NULL); |
| 7988 | } |
| 7989 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7990 | mutex_unlock(&ctx->uring_lock); |
| 7991 | } |
| 7992 | |
| 7993 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 7994 | { |
| 7995 | struct io_ring_ctx *ctx = f->private_data; |
| 7996 | |
| 7997 | if (percpu_ref_tryget(&ctx->refs)) { |
| 7998 | __io_uring_show_fdinfo(ctx, m); |
| 7999 | percpu_ref_put(&ctx->refs); |
| 8000 | } |
| 8001 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8002 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8003 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8004 | static const struct file_operations io_uring_fops = { |
| 8005 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8006 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8007 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8008 | #ifndef CONFIG_MMU |
| 8009 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 8010 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 8011 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8012 | .poll = io_uring_poll, |
| 8013 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8014 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8015 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8016 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8017 | }; |
| 8018 | |
| 8019 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 8020 | struct io_uring_params *p) |
| 8021 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8022 | struct io_rings *rings; |
| 8023 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8024 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8025 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 8026 | if (size == SIZE_MAX) |
| 8027 | return -EOVERFLOW; |
| 8028 | |
| 8029 | rings = io_mem_alloc(size); |
| 8030 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8031 | return -ENOMEM; |
| 8032 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8033 | ctx->rings = rings; |
| 8034 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 8035 | rings->sq_ring_mask = p->sq_entries - 1; |
| 8036 | rings->cq_ring_mask = p->cq_entries - 1; |
| 8037 | rings->sq_ring_entries = p->sq_entries; |
| 8038 | rings->cq_ring_entries = p->cq_entries; |
| 8039 | ctx->sq_mask = rings->sq_ring_mask; |
| 8040 | ctx->cq_mask = rings->cq_ring_mask; |
| 8041 | ctx->sq_entries = rings->sq_ring_entries; |
| 8042 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8043 | |
| 8044 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8045 | if (size == SIZE_MAX) { |
| 8046 | io_mem_free(ctx->rings); |
| 8047 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8048 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8049 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8050 | |
| 8051 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8052 | if (!ctx->sq_sqes) { |
| 8053 | io_mem_free(ctx->rings); |
| 8054 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8055 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8056 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8057 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8058 | return 0; |
| 8059 | } |
| 8060 | |
| 8061 | /* |
| 8062 | * Allocate an anonymous fd, this is what constitutes the application |
| 8063 | * visible backing of an io_uring instance. The application mmaps this |
| 8064 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 8065 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 8066 | */ |
| 8067 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 8068 | { |
| 8069 | struct file *file; |
| 8070 | int ret; |
| 8071 | |
| 8072 | #if defined(CONFIG_UNIX) |
| 8073 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 8074 | &ctx->ring_sock); |
| 8075 | if (ret) |
| 8076 | return ret; |
| 8077 | #endif |
| 8078 | |
| 8079 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 8080 | if (ret < 0) |
| 8081 | goto err; |
| 8082 | |
| 8083 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 8084 | O_RDWR | O_CLOEXEC); |
| 8085 | if (IS_ERR(file)) { |
| 8086 | put_unused_fd(ret); |
| 8087 | ret = PTR_ERR(file); |
| 8088 | goto err; |
| 8089 | } |
| 8090 | |
| 8091 | #if defined(CONFIG_UNIX) |
| 8092 | ctx->ring_sock->file = file; |
| 8093 | #endif |
| 8094 | fd_install(ret, file); |
| 8095 | return ret; |
| 8096 | err: |
| 8097 | #if defined(CONFIG_UNIX) |
| 8098 | sock_release(ctx->ring_sock); |
| 8099 | ctx->ring_sock = NULL; |
| 8100 | #endif |
| 8101 | return ret; |
| 8102 | } |
| 8103 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8104 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 8105 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8106 | { |
| 8107 | struct user_struct *user = NULL; |
| 8108 | struct io_ring_ctx *ctx; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8109 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8110 | int ret; |
| 8111 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8112 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8113 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8114 | if (entries > IORING_MAX_ENTRIES) { |
| 8115 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8116 | return -EINVAL; |
| 8117 | entries = IORING_MAX_ENTRIES; |
| 8118 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8119 | |
| 8120 | /* |
| 8121 | * Use twice as many entries for the CQ ring. It's possible for the |
| 8122 | * application to drive a higher depth than the size of the SQ ring, |
| 8123 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8124 | * some flexibility in overcommitting a bit. If the application has |
| 8125 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 8126 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8127 | */ |
| 8128 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8129 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 8130 | /* |
| 8131 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 8132 | * to a power-of-two, if it isn't already. We do NOT impose |
| 8133 | * any cq vs sq ring sizing. |
| 8134 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8135 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8136 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8137 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 8138 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8139 | return -EINVAL; |
| 8140 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 8141 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8142 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 8143 | } else { |
| 8144 | p->cq_entries = 2 * p->sq_entries; |
| 8145 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8146 | |
| 8147 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8148 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8149 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8150 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8151 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8152 | ring_pages(p->sq_entries, p->cq_entries)); |
| 8153 | if (ret) { |
| 8154 | free_uid(user); |
| 8155 | return ret; |
| 8156 | } |
| 8157 | } |
| 8158 | |
| 8159 | ctx = io_ring_ctx_alloc(p); |
| 8160 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8161 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8162 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8163 | p->cq_entries)); |
| 8164 | free_uid(user); |
| 8165 | return -ENOMEM; |
| 8166 | } |
| 8167 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8168 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 8169 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8170 | |
| 8171 | ret = io_allocate_scq_urings(ctx, p); |
| 8172 | if (ret) |
| 8173 | goto err; |
| 8174 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8175 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8176 | if (ret) |
| 8177 | goto err; |
| 8178 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8179 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8180 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 8181 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 8182 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 8183 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 8184 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 8185 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 8186 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8187 | |
| 8188 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8189 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 8190 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 8191 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 8192 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 8193 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 8194 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 8195 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 8196 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8197 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 8198 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8199 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
| 8200 | IORING_FEAT_POLL_32BITS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8201 | |
| 8202 | if (copy_to_user(params, p, sizeof(*p))) { |
| 8203 | ret = -EFAULT; |
| 8204 | goto err; |
| 8205 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 8206 | /* |
| 8207 | * Install ring fd as the very last thing, so we don't risk someone |
| 8208 | * having closed it before we finish setup |
| 8209 | */ |
| 8210 | ret = io_uring_get_fd(ctx); |
| 8211 | if (ret < 0) |
| 8212 | goto err; |
| 8213 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8214 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8215 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 8216 | ACCT_LOCKED); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8217 | ctx->limit_mem = limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8218 | return ret; |
| 8219 | err: |
| 8220 | io_ring_ctx_wait_and_kill(ctx); |
| 8221 | return ret; |
| 8222 | } |
| 8223 | |
| 8224 | /* |
| 8225 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 8226 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 8227 | * params structure passed in. |
| 8228 | */ |
| 8229 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 8230 | { |
| 8231 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8232 | int i; |
| 8233 | |
| 8234 | if (copy_from_user(&p, params, sizeof(p))) |
| 8235 | return -EFAULT; |
| 8236 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 8237 | if (p.resv[i]) |
| 8238 | return -EINVAL; |
| 8239 | } |
| 8240 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8241 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8242 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8243 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8244 | return -EINVAL; |
| 8245 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8246 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8247 | } |
| 8248 | |
| 8249 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 8250 | struct io_uring_params __user *, params) |
| 8251 | { |
| 8252 | return io_uring_setup(entries, params); |
| 8253 | } |
| 8254 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8255 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 8256 | { |
| 8257 | struct io_uring_probe *p; |
| 8258 | size_t size; |
| 8259 | int i, ret; |
| 8260 | |
| 8261 | size = struct_size(p, ops, nr_args); |
| 8262 | if (size == SIZE_MAX) |
| 8263 | return -EOVERFLOW; |
| 8264 | p = kzalloc(size, GFP_KERNEL); |
| 8265 | if (!p) |
| 8266 | return -ENOMEM; |
| 8267 | |
| 8268 | ret = -EFAULT; |
| 8269 | if (copy_from_user(p, arg, size)) |
| 8270 | goto out; |
| 8271 | ret = -EINVAL; |
| 8272 | if (memchr_inv(p, 0, size)) |
| 8273 | goto out; |
| 8274 | |
| 8275 | p->last_op = IORING_OP_LAST - 1; |
| 8276 | if (nr_args > IORING_OP_LAST) |
| 8277 | nr_args = IORING_OP_LAST; |
| 8278 | |
| 8279 | for (i = 0; i < nr_args; i++) { |
| 8280 | p->ops[i].op = i; |
| 8281 | if (!io_op_defs[i].not_supported) |
| 8282 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 8283 | } |
| 8284 | p->ops_len = i; |
| 8285 | |
| 8286 | ret = 0; |
| 8287 | if (copy_to_user(arg, p, size)) |
| 8288 | ret = -EFAULT; |
| 8289 | out: |
| 8290 | kfree(p); |
| 8291 | return ret; |
| 8292 | } |
| 8293 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8294 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 8295 | { |
| 8296 | const struct cred *creds = get_current_cred(); |
| 8297 | int id; |
| 8298 | |
| 8299 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 8300 | USHRT_MAX, GFP_KERNEL); |
| 8301 | if (id < 0) |
| 8302 | put_cred(creds); |
| 8303 | return id; |
| 8304 | } |
| 8305 | |
| 8306 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 8307 | { |
| 8308 | const struct cred *old_creds; |
| 8309 | |
| 8310 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 8311 | if (old_creds) { |
| 8312 | put_cred(old_creds); |
| 8313 | return 0; |
| 8314 | } |
| 8315 | |
| 8316 | return -EINVAL; |
| 8317 | } |
| 8318 | |
| 8319 | static bool io_register_op_must_quiesce(int op) |
| 8320 | { |
| 8321 | switch (op) { |
| 8322 | case IORING_UNREGISTER_FILES: |
| 8323 | case IORING_REGISTER_FILES_UPDATE: |
| 8324 | case IORING_REGISTER_PROBE: |
| 8325 | case IORING_REGISTER_PERSONALITY: |
| 8326 | case IORING_UNREGISTER_PERSONALITY: |
| 8327 | return false; |
| 8328 | default: |
| 8329 | return true; |
| 8330 | } |
| 8331 | } |
| 8332 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8333 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 8334 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8335 | __releases(ctx->uring_lock) |
| 8336 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8337 | { |
| 8338 | int ret; |
| 8339 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 8340 | /* |
| 8341 | * We're inside the ring mutex, if the ref is already dying, then |
| 8342 | * someone else killed the ctx or is already going through |
| 8343 | * io_uring_register(). |
| 8344 | */ |
| 8345 | if (percpu_ref_is_dying(&ctx->refs)) |
| 8346 | return -ENXIO; |
| 8347 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8348 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8349 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8350 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8351 | /* |
| 8352 | * Drop uring mutex before waiting for references to exit. If |
| 8353 | * another thread is currently inside io_uring_enter() it might |
| 8354 | * need to grab the uring_lock to make progress. If we hold it |
| 8355 | * here across the drain wait, then we can deadlock. It's safe |
| 8356 | * to drop the mutex here, since no new references will come in |
| 8357 | * after we've killed the percpu ref. |
| 8358 | */ |
| 8359 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8360 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8361 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8362 | if (ret) { |
| 8363 | percpu_ref_resurrect(&ctx->refs); |
| 8364 | ret = -EINTR; |
| 8365 | goto out; |
| 8366 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8367 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8368 | |
| 8369 | switch (opcode) { |
| 8370 | case IORING_REGISTER_BUFFERS: |
| 8371 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 8372 | break; |
| 8373 | case IORING_UNREGISTER_BUFFERS: |
| 8374 | ret = -EINVAL; |
| 8375 | if (arg || nr_args) |
| 8376 | break; |
| 8377 | ret = io_sqe_buffer_unregister(ctx); |
| 8378 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8379 | case IORING_REGISTER_FILES: |
| 8380 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 8381 | break; |
| 8382 | case IORING_UNREGISTER_FILES: |
| 8383 | ret = -EINVAL; |
| 8384 | if (arg || nr_args) |
| 8385 | break; |
| 8386 | ret = io_sqe_files_unregister(ctx); |
| 8387 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8388 | case IORING_REGISTER_FILES_UPDATE: |
| 8389 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 8390 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8391 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8392 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8393 | ret = -EINVAL; |
| 8394 | if (nr_args != 1) |
| 8395 | break; |
| 8396 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8397 | if (ret) |
| 8398 | break; |
| 8399 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 8400 | ctx->eventfd_async = 1; |
| 8401 | else |
| 8402 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8403 | break; |
| 8404 | case IORING_UNREGISTER_EVENTFD: |
| 8405 | ret = -EINVAL; |
| 8406 | if (arg || nr_args) |
| 8407 | break; |
| 8408 | ret = io_eventfd_unregister(ctx); |
| 8409 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8410 | case IORING_REGISTER_PROBE: |
| 8411 | ret = -EINVAL; |
| 8412 | if (!arg || nr_args > 256) |
| 8413 | break; |
| 8414 | ret = io_probe(ctx, arg, nr_args); |
| 8415 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8416 | case IORING_REGISTER_PERSONALITY: |
| 8417 | ret = -EINVAL; |
| 8418 | if (arg || nr_args) |
| 8419 | break; |
| 8420 | ret = io_register_personality(ctx); |
| 8421 | break; |
| 8422 | case IORING_UNREGISTER_PERSONALITY: |
| 8423 | ret = -EINVAL; |
| 8424 | if (arg) |
| 8425 | break; |
| 8426 | ret = io_unregister_personality(ctx, nr_args); |
| 8427 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8428 | default: |
| 8429 | ret = -EINVAL; |
| 8430 | break; |
| 8431 | } |
| 8432 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8433 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8434 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8435 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8436 | out: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8437 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8438 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8439 | return ret; |
| 8440 | } |
| 8441 | |
| 8442 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 8443 | void __user *, arg, unsigned int, nr_args) |
| 8444 | { |
| 8445 | struct io_ring_ctx *ctx; |
| 8446 | long ret = -EBADF; |
| 8447 | struct fd f; |
| 8448 | |
| 8449 | f = fdget(fd); |
| 8450 | if (!f.file) |
| 8451 | return -EBADF; |
| 8452 | |
| 8453 | ret = -EOPNOTSUPP; |
| 8454 | if (f.file->f_op != &io_uring_fops) |
| 8455 | goto out_fput; |
| 8456 | |
| 8457 | ctx = f.file->private_data; |
| 8458 | |
| 8459 | mutex_lock(&ctx->uring_lock); |
| 8460 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 8461 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8462 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 8463 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8464 | out_fput: |
| 8465 | fdput(f); |
| 8466 | return ret; |
| 8467 | } |
| 8468 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8469 | static int __init io_uring_init(void) |
| 8470 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8471 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 8472 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 8473 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 8474 | } while (0) |
| 8475 | |
| 8476 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 8477 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 8478 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 8479 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 8480 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 8481 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 8482 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 8483 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 8484 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 8485 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8486 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8487 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 8488 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 8489 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 8490 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 8491 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8492 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 8493 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8494 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 8495 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 8496 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 8497 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 8498 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 8499 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 8500 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 8501 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8502 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8503 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 8504 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 8505 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8506 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8507 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 8508 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 8509 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8510 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 8511 | return 0; |
| 8512 | }; |
| 8513 | __initcall(io_uring_init); |