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> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
| 49 | |
| 50 | #include <linux/sched/signal.h> |
| 51 | #include <linux/fs.h> |
| 52 | #include <linux/file.h> |
| 53 | #include <linux/fdtable.h> |
| 54 | #include <linux/mm.h> |
| 55 | #include <linux/mman.h> |
| 56 | #include <linux/mmu_context.h> |
| 57 | #include <linux/percpu.h> |
| 58 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 59 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 73 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 74 | #define CREATE_TRACE_POINTS |
| 75 | #include <trace/events/io_uring.h> |
| 76 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 77 | #include <uapi/linux/io_uring.h> |
| 78 | |
| 79 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 80 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 82 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 83 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 87 | */ |
| 88 | #define IORING_FILE_TABLE_SHIFT 9 |
| 89 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 90 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 91 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 92 | |
| 93 | struct io_uring { |
| 94 | u32 head ____cacheline_aligned_in_smp; |
| 95 | u32 tail ____cacheline_aligned_in_smp; |
| 96 | }; |
| 97 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 98 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 99 | * This data is shared with the application through the mmap at offsets |
| 100 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 101 | * |
| 102 | * The offsets to the member fields are published through struct |
| 103 | * io_sqring_offsets when calling io_uring_setup. |
| 104 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 105 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Head and tail offsets into the ring; the offsets need to be |
| 108 | * masked to get valid indices. |
| 109 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 110 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 111 | * and the application controls tail of the sq ring and the head of the |
| 112 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 113 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 114 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 116 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 117 | * ring_entries - 1) |
| 118 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | u32 sq_ring_mask, cq_ring_mask; |
| 120 | /* Ring sizes (constant, power of 2) */ |
| 121 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Number of invalid entries dropped by the kernel due to |
| 124 | * invalid index stored in array |
| 125 | * |
| 126 | * Written by the kernel, shouldn't be modified by the |
| 127 | * application (i.e. get number of "new events" by comparing to |
| 128 | * cached value). |
| 129 | * |
| 130 | * After a new SQ head value was read by the application this |
| 131 | * counter includes all submissions that were dropped reaching |
| 132 | * the new SQ head (and possibly more). |
| 133 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 134 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 135 | /* |
| 136 | * Runtime flags |
| 137 | * |
| 138 | * Written by the kernel, shouldn't be modified by the |
| 139 | * application. |
| 140 | * |
| 141 | * The application needs a full memory barrier before checking |
| 142 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 143 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 144 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Number of completion events lost because the queue was full; |
| 147 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 148 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | * the completion queue. |
| 150 | * |
| 151 | * Written by the kernel, shouldn't be modified by the |
| 152 | * application (i.e. get number of "new events" by comparing to |
| 153 | * cached value). |
| 154 | * |
| 155 | * As completion events come in out of order this counter is not |
| 156 | * ordered with any other data. |
| 157 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 158 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 159 | /* |
| 160 | * Ring buffer of completion events. |
| 161 | * |
| 162 | * The kernel writes completion events fresh every time they are |
| 163 | * produced, so the application is allowed to modify pending |
| 164 | * entries. |
| 165 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 166 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 169 | struct io_mapped_ubuf { |
| 170 | u64 ubuf; |
| 171 | size_t len; |
| 172 | struct bio_vec *bvec; |
| 173 | unsigned int nr_bvecs; |
| 174 | }; |
| 175 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 176 | struct fixed_file_table { |
| 177 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 180 | struct io_ring_ctx { |
| 181 | struct { |
| 182 | struct percpu_ref refs; |
| 183 | } ____cacheline_aligned_in_smp; |
| 184 | |
| 185 | struct { |
| 186 | unsigned int flags; |
| 187 | bool compat; |
| 188 | bool account_mem; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 189 | bool cq_overflow_flushed; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 190 | bool drain_next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 191 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 192 | /* |
| 193 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 194 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 195 | * |
| 196 | * This indirection could e.g. be used to assign fixed |
| 197 | * io_uring_sqe entries to operations and only submit them to |
| 198 | * the queue when needed. |
| 199 | * |
| 200 | * The kernel modifies neither the indices array nor the entries |
| 201 | * array. |
| 202 | */ |
| 203 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 204 | unsigned cached_sq_head; |
| 205 | unsigned sq_entries; |
| 206 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 207 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 208 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 209 | atomic_t cached_cq_overflow; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 210 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 211 | |
| 212 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 213 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 214 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 215 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 216 | wait_queue_head_t inflight_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 217 | } ____cacheline_aligned_in_smp; |
| 218 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 219 | struct io_rings *rings; |
| 220 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 221 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 222 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 223 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 224 | struct mm_struct *sqo_mm; |
| 225 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 226 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 227 | /* |
| 228 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 229 | * readers must ensure that ->refs is alive as long as the file* is |
| 230 | * used. Only updated through io_uring_register(2). |
| 231 | */ |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 232 | struct fixed_file_table *file_table; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 233 | unsigned nr_user_files; |
| 234 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 235 | /* if used, fixed mapped user buffers */ |
| 236 | unsigned nr_user_bufs; |
| 237 | struct io_mapped_ubuf *user_bufs; |
| 238 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 239 | struct user_struct *user; |
| 240 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 241 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 242 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 243 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 244 | struct completion *completions; |
| 245 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 246 | /* if all else fails... */ |
| 247 | struct io_kiocb *fallback_req; |
| 248 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 249 | #if defined(CONFIG_UNIX) |
| 250 | struct socket *ring_sock; |
| 251 | #endif |
| 252 | |
| 253 | struct { |
| 254 | unsigned cached_cq_tail; |
| 255 | unsigned cq_entries; |
| 256 | unsigned cq_mask; |
| 257 | atomic_t cq_timeouts; |
| 258 | struct wait_queue_head cq_wait; |
| 259 | struct fasync_struct *cq_fasync; |
| 260 | struct eventfd_ctx *cq_ev_fd; |
| 261 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 262 | |
| 263 | struct { |
| 264 | struct mutex uring_lock; |
| 265 | wait_queue_head_t wait; |
| 266 | } ____cacheline_aligned_in_smp; |
| 267 | |
| 268 | struct { |
| 269 | spinlock_t completion_lock; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 270 | bool poll_multi_file; |
| 271 | /* |
| 272 | * ->poll_list is protected by the ctx->uring_lock for |
| 273 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 274 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 275 | * manipulate the list, hence no extra locking is needed there. |
| 276 | */ |
| 277 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 278 | struct hlist_head *cancel_hash; |
| 279 | unsigned cancel_hash_bits; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 280 | |
| 281 | spinlock_t inflight_lock; |
| 282 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 283 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 286 | /* |
| 287 | * First field must be the file pointer in all the |
| 288 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 289 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 290 | struct io_poll_iocb { |
| 291 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 292 | union { |
| 293 | struct wait_queue_head *head; |
| 294 | u64 addr; |
| 295 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 296 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 297 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 298 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 299 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 300 | }; |
| 301 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 302 | struct io_timeout_data { |
| 303 | struct io_kiocb *req; |
| 304 | struct hrtimer timer; |
| 305 | struct timespec64 ts; |
| 306 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 307 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 310 | struct io_accept { |
| 311 | struct file *file; |
| 312 | struct sockaddr __user *addr; |
| 313 | int __user *addr_len; |
| 314 | int flags; |
| 315 | }; |
| 316 | |
| 317 | struct io_sync { |
| 318 | struct file *file; |
| 319 | loff_t len; |
| 320 | loff_t off; |
| 321 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 322 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 325 | struct io_cancel { |
| 326 | struct file *file; |
| 327 | u64 addr; |
| 328 | }; |
| 329 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 330 | struct io_timeout { |
| 331 | struct file *file; |
| 332 | u64 addr; |
| 333 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 334 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 337 | struct io_rw { |
| 338 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 339 | struct kiocb kiocb; |
| 340 | u64 addr; |
| 341 | u64 len; |
| 342 | }; |
| 343 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 344 | struct io_connect { |
| 345 | struct file *file; |
| 346 | struct sockaddr __user *addr; |
| 347 | int addr_len; |
| 348 | }; |
| 349 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 350 | struct io_sr_msg { |
| 351 | struct file *file; |
| 352 | struct user_msghdr __user *msg; |
| 353 | int msg_flags; |
| 354 | }; |
| 355 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 356 | struct io_async_connect { |
| 357 | struct sockaddr_storage address; |
| 358 | }; |
| 359 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 360 | struct io_async_msghdr { |
| 361 | struct iovec fast_iov[UIO_FASTIOV]; |
| 362 | struct iovec *iov; |
| 363 | struct sockaddr __user *uaddr; |
| 364 | struct msghdr msg; |
| 365 | }; |
| 366 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 367 | struct io_async_rw { |
| 368 | struct iovec fast_iov[UIO_FASTIOV]; |
| 369 | struct iovec *iov; |
| 370 | ssize_t nr_segs; |
| 371 | ssize_t size; |
| 372 | }; |
| 373 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 374 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 375 | union { |
| 376 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 377 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 378 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 379 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 380 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 383 | /* |
| 384 | * NOTE! Each of the iocb union members has the file pointer |
| 385 | * as the first entry in their struct definition. So you can |
| 386 | * access the file pointer through any of the sub-structs, |
| 387 | * or directly as just 'ki_filp' in this struct. |
| 388 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 389 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 390 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 391 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 392 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 393 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 394 | struct io_accept accept; |
| 395 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 396 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 397 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 398 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 399 | struct io_sr_msg sr_msg; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 400 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 401 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 402 | struct io_async_ctx *io; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 403 | struct file *ring_file; |
| 404 | int ring_fd; |
| 405 | bool has_user; |
| 406 | bool in_async; |
| 407 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 408 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 409 | |
| 410 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 411 | union { |
| 412 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 413 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 414 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 415 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 416 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 417 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 418 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 419 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 420 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 421 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 422 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 423 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 424 | #define REQ_F_LINK 64 /* linked sqes */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 425 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 426 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 427 | #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 428 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 429 | #define REQ_F_ISREG 2048 /* regular file */ |
| 430 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 431 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 432 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
| 433 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 434 | #define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 435 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 436 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 437 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 438 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 439 | struct list_head inflight_entry; |
| 440 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 441 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 445 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 446 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 447 | struct io_submit_state { |
| 448 | struct blk_plug plug; |
| 449 | |
| 450 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 451 | * io_kiocb alloc cache |
| 452 | */ |
| 453 | void *reqs[IO_IOPOLL_BATCH]; |
| 454 | unsigned int free_reqs; |
| 455 | unsigned int cur_req; |
| 456 | |
| 457 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 458 | * File reference cache |
| 459 | */ |
| 460 | struct file *file; |
| 461 | unsigned int fd; |
| 462 | unsigned int has_refs; |
| 463 | unsigned int used_refs; |
| 464 | unsigned int ios_left; |
| 465 | }; |
| 466 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 467 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 468 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 469 | static void __io_free_req(struct io_kiocb *req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 470 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 471 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 472 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 473 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 474 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 475 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 476 | static struct kmem_cache *req_cachep; |
| 477 | |
| 478 | static const struct file_operations io_uring_fops; |
| 479 | |
| 480 | struct sock *io_uring_get_socket(struct file *file) |
| 481 | { |
| 482 | #if defined(CONFIG_UNIX) |
| 483 | if (file->f_op == &io_uring_fops) { |
| 484 | struct io_ring_ctx *ctx = file->private_data; |
| 485 | |
| 486 | return ctx->ring_sock->sk; |
| 487 | } |
| 488 | #endif |
| 489 | return NULL; |
| 490 | } |
| 491 | EXPORT_SYMBOL(io_uring_get_socket); |
| 492 | |
| 493 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 494 | { |
| 495 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 496 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 497 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 501 | { |
| 502 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 503 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 504 | |
| 505 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 506 | if (!ctx) |
| 507 | return NULL; |
| 508 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 509 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 510 | if (!ctx->fallback_req) |
| 511 | goto err; |
| 512 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 513 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 514 | if (!ctx->completions) |
| 515 | goto err; |
| 516 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 517 | /* |
| 518 | * Use 5 bits less than the max cq entries, that should give us around |
| 519 | * 32 entries per hash list if totally full and uniformly spread. |
| 520 | */ |
| 521 | hash_bits = ilog2(p->cq_entries); |
| 522 | hash_bits -= 5; |
| 523 | if (hash_bits <= 0) |
| 524 | hash_bits = 1; |
| 525 | ctx->cancel_hash_bits = hash_bits; |
| 526 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 527 | GFP_KERNEL); |
| 528 | if (!ctx->cancel_hash) |
| 529 | goto err; |
| 530 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 531 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 532 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 533 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 534 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 535 | |
| 536 | ctx->flags = p->flags; |
| 537 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 538 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 539 | init_completion(&ctx->completions[0]); |
| 540 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 541 | mutex_init(&ctx->uring_lock); |
| 542 | init_waitqueue_head(&ctx->wait); |
| 543 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 544 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 545 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 546 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 547 | init_waitqueue_head(&ctx->inflight_wait); |
| 548 | spin_lock_init(&ctx->inflight_lock); |
| 549 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 550 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 551 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 552 | if (ctx->fallback_req) |
| 553 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 554 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 555 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 556 | kfree(ctx); |
| 557 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 560 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 561 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 562 | struct io_ring_ctx *ctx = req->ctx; |
| 563 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 564 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 565 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 566 | } |
| 567 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 568 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 569 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 570 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 571 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 572 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 573 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 577 | { |
| 578 | struct io_kiocb *req; |
| 579 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 580 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 581 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 582 | list_del_init(&req->list); |
| 583 | return req; |
| 584 | } |
| 585 | |
| 586 | return NULL; |
| 587 | } |
| 588 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 589 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 590 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 591 | struct io_kiocb *req; |
| 592 | |
| 593 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 594 | if (req) { |
| 595 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 596 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 597 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 598 | list_del_init(&req->list); |
| 599 | return req; |
| 600 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 604 | } |
| 605 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 606 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 607 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 608 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 609 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 610 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 611 | /* order cqe stores with ring update */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 612 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 613 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 614 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 615 | wake_up_interruptible(&ctx->cq_wait); |
| 616 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 621 | static inline bool io_req_needs_user(struct io_kiocb *req) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 622 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 623 | return !(req->opcode == IORING_OP_READ_FIXED || |
| 624 | req->opcode == IORING_OP_WRITE_FIXED); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 625 | } |
| 626 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 627 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 628 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 629 | { |
| 630 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 631 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 632 | switch (req->opcode) { |
| 633 | case IORING_OP_WRITEV: |
| 634 | case IORING_OP_WRITE_FIXED: |
| 635 | /* only regular files should be hashed for writes */ |
| 636 | if (req->flags & REQ_F_ISREG) |
| 637 | do_hashed = true; |
| 638 | /* fall-through */ |
| 639 | case IORING_OP_READV: |
| 640 | case IORING_OP_READ_FIXED: |
| 641 | case IORING_OP_SENDMSG: |
| 642 | case IORING_OP_RECVMSG: |
| 643 | case IORING_OP_ACCEPT: |
| 644 | case IORING_OP_POLL_ADD: |
| 645 | case IORING_OP_CONNECT: |
| 646 | /* |
| 647 | * We know REQ_F_ISREG is not set on some of these |
| 648 | * opcodes, but this enables us to keep the check in |
| 649 | * just one place. |
| 650 | */ |
| 651 | if (!(req->flags & REQ_F_ISREG)) |
| 652 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 653 | break; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 654 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 655 | if (io_req_needs_user(req)) |
| 656 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 657 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 658 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 659 | return do_hashed; |
| 660 | } |
| 661 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 662 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 663 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 664 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 665 | struct io_kiocb *link; |
| 666 | bool do_hashed; |
| 667 | |
| 668 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 669 | |
| 670 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 671 | req->flags); |
| 672 | if (!do_hashed) { |
| 673 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 674 | } else { |
| 675 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 676 | file_inode(req->file)); |
| 677 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 678 | |
| 679 | if (link) |
| 680 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 681 | } |
| 682 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 683 | static void io_kill_timeout(struct io_kiocb *req) |
| 684 | { |
| 685 | int ret; |
| 686 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 687 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 688 | if (ret != -1) { |
| 689 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 690 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 691 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 692 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 697 | { |
| 698 | struct io_kiocb *req, *tmp; |
| 699 | |
| 700 | spin_lock_irq(&ctx->completion_lock); |
| 701 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 702 | io_kill_timeout(req); |
| 703 | spin_unlock_irq(&ctx->completion_lock); |
| 704 | } |
| 705 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 706 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 707 | { |
| 708 | struct io_kiocb *req; |
| 709 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 710 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 711 | io_kill_timeout(req); |
| 712 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 713 | __io_commit_cqring(ctx); |
| 714 | |
| 715 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 716 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 717 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 721 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 722 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 723 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 724 | unsigned tail; |
| 725 | |
| 726 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 727 | /* |
| 728 | * writes to the cq entry need to come after reading head; the |
| 729 | * control dependency is enough as we're using WRITE_ONCE to |
| 730 | * fill the cq entry |
| 731 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 732 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 733 | return NULL; |
| 734 | |
| 735 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 736 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 739 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 740 | { |
| 741 | if (waitqueue_active(&ctx->wait)) |
| 742 | wake_up(&ctx->wait); |
| 743 | if (waitqueue_active(&ctx->sqo_wait)) |
| 744 | wake_up(&ctx->sqo_wait); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 745 | if (ctx->cq_ev_fd) |
| 746 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 747 | } |
| 748 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 749 | /* Returns true if there are no backlogged entries after the flush */ |
| 750 | 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] | 751 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 752 | struct io_rings *rings = ctx->rings; |
| 753 | struct io_uring_cqe *cqe; |
| 754 | struct io_kiocb *req; |
| 755 | unsigned long flags; |
| 756 | LIST_HEAD(list); |
| 757 | |
| 758 | if (!force) { |
| 759 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 760 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 761 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 762 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 763 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 767 | |
| 768 | /* if force is set, the ring is going away. always drop after that */ |
| 769 | if (force) |
| 770 | ctx->cq_overflow_flushed = true; |
| 771 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 772 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 773 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 774 | cqe = io_get_cqring(ctx); |
| 775 | if (!cqe && !force) |
| 776 | break; |
| 777 | |
| 778 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 779 | list); |
| 780 | list_move(&req->list, &list); |
| 781 | if (cqe) { |
| 782 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 783 | WRITE_ONCE(cqe->res, req->result); |
| 784 | WRITE_ONCE(cqe->flags, 0); |
| 785 | } else { |
| 786 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 787 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | io_commit_cqring(ctx); |
| 792 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 793 | io_cqring_ev_posted(ctx); |
| 794 | |
| 795 | while (!list_empty(&list)) { |
| 796 | req = list_first_entry(&list, struct io_kiocb, list); |
| 797 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 798 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 799 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 800 | |
| 801 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 804 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 805 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 806 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 807 | struct io_uring_cqe *cqe; |
| 808 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 809 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 810 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 811 | /* |
| 812 | * If we can't get a cq entry, userspace overflowed the |
| 813 | * submission (by quite a lot). Increment the overflow count in |
| 814 | * the ring. |
| 815 | */ |
| 816 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 817 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 818 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 819 | WRITE_ONCE(cqe->res, res); |
| 820 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 821 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 822 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 823 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 824 | } else { |
| 825 | refcount_inc(&req->refs); |
| 826 | req->result = res; |
| 827 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 831 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 832 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 833 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 834 | unsigned long flags; |
| 835 | |
| 836 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 837 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 838 | io_commit_cqring(ctx); |
| 839 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 840 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 841 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 844 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 845 | { |
| 846 | return req == (struct io_kiocb *) |
| 847 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 848 | } |
| 849 | |
| 850 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 851 | { |
| 852 | struct io_kiocb *req; |
| 853 | |
| 854 | req = ctx->fallback_req; |
| 855 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 856 | return req; |
| 857 | |
| 858 | return NULL; |
| 859 | } |
| 860 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 861 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 862 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 863 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 864 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 865 | struct io_kiocb *req; |
| 866 | |
| 867 | if (!percpu_ref_tryget(&ctx->refs)) |
| 868 | return NULL; |
| 869 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 870 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 871 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 872 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 873 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 874 | } else if (!state->free_reqs) { |
| 875 | size_t sz; |
| 876 | int ret; |
| 877 | |
| 878 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 879 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 880 | |
| 881 | /* |
| 882 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 883 | * retry single alloc to be on the safe side. |
| 884 | */ |
| 885 | if (unlikely(ret <= 0)) { |
| 886 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 887 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 888 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 889 | ret = 1; |
| 890 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 891 | state->free_reqs = ret - 1; |
| 892 | state->cur_req = 1; |
| 893 | req = state->reqs[0]; |
| 894 | } else { |
| 895 | req = state->reqs[state->cur_req]; |
| 896 | state->free_reqs--; |
| 897 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 900 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 901 | req->io = NULL; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 902 | req->ring_file = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 903 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 904 | req->ctx = ctx; |
| 905 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 906 | /* one is dropped after submission, the other at completion */ |
| 907 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 908 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 909 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 910 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 911 | fallback: |
| 912 | req = io_get_fallback_req(ctx); |
| 913 | if (req) |
| 914 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 915 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
| 918 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 919 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
| 920 | { |
| 921 | if (*nr) { |
| 922 | kmem_cache_free_bulk(req_cachep, *nr, reqs); |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 923 | percpu_ref_put_many(&ctx->refs, *nr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 924 | *nr = 0; |
| 925 | } |
| 926 | } |
| 927 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 928 | static void __io_free_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 929 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 930 | struct io_ring_ctx *ctx = req->ctx; |
| 931 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 932 | if (req->io) |
| 933 | kfree(req->io); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 934 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
| 935 | fput(req->file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 936 | if (req->flags & REQ_F_INFLIGHT) { |
| 937 | unsigned long flags; |
| 938 | |
| 939 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 940 | list_del(&req->inflight_entry); |
| 941 | if (waitqueue_active(&ctx->inflight_wait)) |
| 942 | wake_up(&ctx->inflight_wait); |
| 943 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 944 | } |
| 945 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 946 | if (likely(!io_is_fallback_req(req))) |
| 947 | kmem_cache_free(req_cachep, req); |
| 948 | else |
| 949 | clear_bit_unlock(0, (unsigned long *) ctx->fallback_req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 950 | } |
| 951 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 952 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 953 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 954 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 955 | int ret; |
| 956 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 957 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 958 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 959 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 960 | io_commit_cqring(ctx); |
| 961 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 962 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 963 | return true; |
| 964 | } |
| 965 | |
| 966 | return false; |
| 967 | } |
| 968 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 969 | 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] | 970 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 971 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 972 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 973 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 974 | /* Already got next link */ |
| 975 | if (req->flags & REQ_F_LINK_NEXT) |
| 976 | return; |
| 977 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 978 | /* |
| 979 | * The list should never be empty when we are called here. But could |
| 980 | * potentially happen if the chain is messed up, check to be on the |
| 981 | * safe side. |
| 982 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 983 | while (!list_empty(&req->link_list)) { |
| 984 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 985 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 986 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 987 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 988 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 989 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 990 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 991 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 992 | continue; |
| 993 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 994 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 995 | list_del_init(&req->link_list); |
| 996 | if (!list_empty(&nxt->link_list)) |
| 997 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 998 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 999 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1000 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1001 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1002 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1003 | if (wake_ev) |
| 1004 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1009 | */ |
| 1010 | static void io_fail_links(struct io_kiocb *req) |
| 1011 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1012 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1013 | unsigned long flags; |
| 1014 | |
| 1015 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1016 | |
| 1017 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1018 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1019 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1020 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1021 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1022 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1023 | |
| 1024 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1025 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1026 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1027 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1028 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1029 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1030 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1031 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1032 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1033 | |
| 1034 | io_commit_cqring(ctx); |
| 1035 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1036 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1037 | } |
| 1038 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1039 | 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] | 1040 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1041 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1042 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1043 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1044 | /* |
| 1045 | * If LINK is set, we have dependent requests in this chain. If we |
| 1046 | * didn't fail this request, queue the first one up, moving any other |
| 1047 | * dependencies to the next request. In case of failure, fail the rest |
| 1048 | * of the chain. |
| 1049 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1050 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1051 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1052 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1053 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1054 | struct io_ring_ctx *ctx = req->ctx; |
| 1055 | unsigned long flags; |
| 1056 | |
| 1057 | /* |
| 1058 | * If this is a timeout link, we could be racing with the |
| 1059 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1060 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1061 | */ |
| 1062 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1063 | io_req_link_next(req, nxt); |
| 1064 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1065 | } else { |
| 1066 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1067 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1068 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1069 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1070 | static void io_free_req(struct io_kiocb *req) |
| 1071 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1072 | struct io_kiocb *nxt = NULL; |
| 1073 | |
| 1074 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1075 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1076 | |
| 1077 | if (nxt) |
| 1078 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1081 | /* |
| 1082 | * Drop reference to request, return next in chain (if there is one) if this |
| 1083 | * was the last reference to this request. |
| 1084 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1085 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1086 | 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] | 1087 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1088 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1089 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1090 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1091 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1094 | static void io_put_req(struct io_kiocb *req) |
| 1095 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1096 | if (refcount_dec_and_test(&req->refs)) |
| 1097 | io_free_req(req); |
| 1098 | } |
| 1099 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1100 | /* |
| 1101 | * Must only be used if we don't need to care about links, usually from |
| 1102 | * within the completion handling itself. |
| 1103 | */ |
| 1104 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1105 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1106 | /* drop both submit and complete references */ |
| 1107 | if (refcount_sub_and_test(2, &req->refs)) |
| 1108 | __io_free_req(req); |
| 1109 | } |
| 1110 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1111 | static void io_double_put_req(struct io_kiocb *req) |
| 1112 | { |
| 1113 | /* drop both submit and complete references */ |
| 1114 | if (refcount_sub_and_test(2, &req->refs)) |
| 1115 | io_free_req(req); |
| 1116 | } |
| 1117 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1118 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1119 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1120 | struct io_rings *rings = ctx->rings; |
| 1121 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * noflush == true is from the waitqueue handler, just ensure we wake |
| 1124 | * up the task, and the next invocation will flush the entries. We |
| 1125 | * cannot safely to it from here. |
| 1126 | */ |
| 1127 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1128 | return -1U; |
| 1129 | |
| 1130 | io_cqring_overflow_flush(ctx, false); |
| 1131 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1132 | /* See comment at the top of this file */ |
| 1133 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1134 | return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1135 | } |
| 1136 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1137 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1138 | { |
| 1139 | struct io_rings *rings = ctx->rings; |
| 1140 | |
| 1141 | /* make sure SQ entry isn't read before tail */ |
| 1142 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1143 | } |
| 1144 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1145 | /* |
| 1146 | * Find and free completed poll iocbs |
| 1147 | */ |
| 1148 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1149 | struct list_head *done) |
| 1150 | { |
| 1151 | void *reqs[IO_IOPOLL_BATCH]; |
| 1152 | struct io_kiocb *req; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1153 | int to_free; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1154 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1155 | to_free = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1156 | while (!list_empty(done)) { |
| 1157 | req = list_first_entry(done, struct io_kiocb, list); |
| 1158 | list_del(&req->list); |
| 1159 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1160 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1161 | (*nr_events)++; |
| 1162 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1163 | if (refcount_dec_and_test(&req->refs)) { |
| 1164 | /* If we're not using fixed files, we have to pair the |
| 1165 | * completion part with the file put. Use regular |
| 1166 | * completions for those, only batch free for fixed |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1167 | * file and non-linked commands. |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1168 | */ |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1169 | if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == |
| 1170 | REQ_F_FIXED_FILE) && !io_is_fallback_req(req) && |
| 1171 | !req->io) { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1172 | reqs[to_free++] = req; |
| 1173 | if (to_free == ARRAY_SIZE(reqs)) |
| 1174 | io_free_req_many(ctx, reqs, &to_free); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1175 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1176 | io_free_req(req); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1177 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1178 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1179 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1180 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1181 | io_commit_cqring(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1182 | io_free_req_many(ctx, reqs, &to_free); |
| 1183 | } |
| 1184 | |
| 1185 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1186 | long min) |
| 1187 | { |
| 1188 | struct io_kiocb *req, *tmp; |
| 1189 | LIST_HEAD(done); |
| 1190 | bool spin; |
| 1191 | int ret; |
| 1192 | |
| 1193 | /* |
| 1194 | * Only spin for completions if we don't have multiple devices hanging |
| 1195 | * off our complete list, and we're under the requested amount. |
| 1196 | */ |
| 1197 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1198 | |
| 1199 | ret = 0; |
| 1200 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1201 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* |
| 1204 | * Move completed entries to our local list. If we find a |
| 1205 | * request that requires polling, break out and complete |
| 1206 | * the done list first, if we have entries there. |
| 1207 | */ |
| 1208 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1209 | list_move_tail(&req->list, &done); |
| 1210 | continue; |
| 1211 | } |
| 1212 | if (!list_empty(&done)) |
| 1213 | break; |
| 1214 | |
| 1215 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1216 | if (ret < 0) |
| 1217 | break; |
| 1218 | |
| 1219 | if (ret && spin) |
| 1220 | spin = false; |
| 1221 | ret = 0; |
| 1222 | } |
| 1223 | |
| 1224 | if (!list_empty(&done)) |
| 1225 | io_iopoll_complete(ctx, nr_events, &done); |
| 1226 | |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1231 | * 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] | 1232 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1233 | * as a non-spinning completion check. |
| 1234 | */ |
| 1235 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1236 | long min) |
| 1237 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1238 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1239 | int ret; |
| 1240 | |
| 1241 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1242 | if (ret < 0) |
| 1243 | return ret; |
| 1244 | if (!min || *nr_events >= min) |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | return 1; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * We can't just wait for polled events to come to us, we have to actively |
| 1253 | * find and complete them. |
| 1254 | */ |
| 1255 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1256 | { |
| 1257 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1258 | return; |
| 1259 | |
| 1260 | mutex_lock(&ctx->uring_lock); |
| 1261 | while (!list_empty(&ctx->poll_list)) { |
| 1262 | unsigned int nr_events = 0; |
| 1263 | |
| 1264 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1265 | |
| 1266 | /* |
| 1267 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1268 | * in this case we need to ensure that we reap all events. |
| 1269 | */ |
| 1270 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1271 | } |
| 1272 | mutex_unlock(&ctx->uring_lock); |
| 1273 | } |
| 1274 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1275 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1276 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1277 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1278 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1279 | |
| 1280 | do { |
| 1281 | int tmin = 0; |
| 1282 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1283 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1284 | * Don't enter poll loop if we already have events pending. |
| 1285 | * If we do, we can potentially be spinning for commands that |
| 1286 | * already triggered a CQE (eg in error). |
| 1287 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1288 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1289 | break; |
| 1290 | |
| 1291 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1292 | * If a submit got punted to a workqueue, we can have the |
| 1293 | * application entering polling for a command before it gets |
| 1294 | * issued. That app will hold the uring_lock for the duration |
| 1295 | * of the poll right here, so we need to take a breather every |
| 1296 | * now and then to ensure that the issue has a chance to add |
| 1297 | * the poll to the issued list. Otherwise we can spin here |
| 1298 | * forever, while the workqueue is stuck trying to acquire the |
| 1299 | * very same mutex. |
| 1300 | */ |
| 1301 | if (!(++iters & 7)) { |
| 1302 | mutex_unlock(&ctx->uring_lock); |
| 1303 | mutex_lock(&ctx->uring_lock); |
| 1304 | } |
| 1305 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1306 | if (*nr_events < min) |
| 1307 | tmin = min - *nr_events; |
| 1308 | |
| 1309 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1310 | if (ret <= 0) |
| 1311 | break; |
| 1312 | ret = 0; |
| 1313 | } while (min && !*nr_events && !need_resched()); |
| 1314 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1315 | return ret; |
| 1316 | } |
| 1317 | |
| 1318 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1319 | long min) |
| 1320 | { |
| 1321 | int ret; |
| 1322 | |
| 1323 | /* |
| 1324 | * We disallow the app entering submit/complete with polling, but we |
| 1325 | * still need to lock the ring to prevent racing with polled issue |
| 1326 | * that got punted to a workqueue. |
| 1327 | */ |
| 1328 | mutex_lock(&ctx->uring_lock); |
| 1329 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1330 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1331 | return ret; |
| 1332 | } |
| 1333 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1334 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1335 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1336 | /* |
| 1337 | * Tell lockdep we inherited freeze protection from submission |
| 1338 | * thread. |
| 1339 | */ |
| 1340 | if (req->flags & REQ_F_ISREG) { |
| 1341 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1342 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1343 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1344 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1345 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1348 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1349 | { |
| 1350 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1351 | req->flags |= REQ_F_FAIL_LINK; |
| 1352 | } |
| 1353 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1354 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1355 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1356 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1357 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1358 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1359 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1360 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1361 | if (res != req->result) |
| 1362 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1363 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1367 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1368 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1369 | |
| 1370 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1371 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1374 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1375 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1376 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1377 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1378 | |
| 1379 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1380 | io_put_req_find_next(req, &nxt); |
| 1381 | |
| 1382 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1385 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1386 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1387 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1388 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1389 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1390 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1391 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1392 | if (res != req->result) |
| 1393 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1394 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1395 | if (res != -EAGAIN) |
| 1396 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1401 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1402 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1403 | * accessing the kiocb cookie. |
| 1404 | */ |
| 1405 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1406 | { |
| 1407 | struct io_ring_ctx *ctx = req->ctx; |
| 1408 | |
| 1409 | /* |
| 1410 | * Track whether we have multiple files in our lists. This will impact |
| 1411 | * how we do polling eventually, not spinning if we're on potentially |
| 1412 | * different devices. |
| 1413 | */ |
| 1414 | if (list_empty(&ctx->poll_list)) { |
| 1415 | ctx->poll_multi_file = false; |
| 1416 | } else if (!ctx->poll_multi_file) { |
| 1417 | struct io_kiocb *list_req; |
| 1418 | |
| 1419 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1420 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1421 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1422 | ctx->poll_multi_file = true; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * For fast devices, IO may have already completed. If it has, add |
| 1427 | * it to the front so we find it first. |
| 1428 | */ |
| 1429 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1430 | list_add(&req->list, &ctx->poll_list); |
| 1431 | else |
| 1432 | list_add_tail(&req->list, &ctx->poll_list); |
| 1433 | } |
| 1434 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1435 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1436 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1437 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1438 | int diff = state->has_refs - state->used_refs; |
| 1439 | |
| 1440 | if (diff) |
| 1441 | fput_many(state->file, diff); |
| 1442 | state->file = NULL; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /* |
| 1447 | * Get as many references to a file as we have IOs left in this submission, |
| 1448 | * assuming most submissions are for one file, or at least that each file |
| 1449 | * has more than one submission. |
| 1450 | */ |
| 1451 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1452 | { |
| 1453 | if (!state) |
| 1454 | return fget(fd); |
| 1455 | |
| 1456 | if (state->file) { |
| 1457 | if (state->fd == fd) { |
| 1458 | state->used_refs++; |
| 1459 | state->ios_left--; |
| 1460 | return state->file; |
| 1461 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1462 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1463 | } |
| 1464 | state->file = fget_many(fd, state->ios_left); |
| 1465 | if (!state->file) |
| 1466 | return NULL; |
| 1467 | |
| 1468 | state->fd = fd; |
| 1469 | state->has_refs = state->ios_left; |
| 1470 | state->used_refs = 1; |
| 1471 | state->ios_left--; |
| 1472 | return state->file; |
| 1473 | } |
| 1474 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1477 | * any file. For now, just ensure that anything potentially problematic is done |
| 1478 | * inline. |
| 1479 | */ |
| 1480 | static bool io_file_supports_async(struct file *file) |
| 1481 | { |
| 1482 | umode_t mode = file_inode(file)->i_mode; |
| 1483 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1484 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1485 | return true; |
| 1486 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1487 | return true; |
| 1488 | |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1492 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1493 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1494 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1495 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1496 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1497 | unsigned ioprio; |
| 1498 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1499 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1500 | if (!req->file) |
| 1501 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1502 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1503 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1504 | req->flags |= REQ_F_ISREG; |
| 1505 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1506 | kiocb->ki_pos = READ_ONCE(sqe->off); |
| 1507 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1508 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1509 | |
| 1510 | ioprio = READ_ONCE(sqe->ioprio); |
| 1511 | if (ioprio) { |
| 1512 | ret = ioprio_check_cap(ioprio); |
| 1513 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1514 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1515 | |
| 1516 | kiocb->ki_ioprio = ioprio; |
| 1517 | } else |
| 1518 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1519 | |
| 1520 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1521 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1522 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1523 | |
| 1524 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1525 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1526 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1527 | req->flags |= REQ_F_NOWAIT; |
| 1528 | |
| 1529 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1530 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1531 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1532 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1533 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1534 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1535 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1536 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1537 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1538 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1539 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1540 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1541 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1542 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1543 | kiocb->ki_complete = io_complete_rw; |
| 1544 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1545 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1546 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1547 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1548 | /* we own ->private, reuse it for the buffer index */ |
| 1549 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1550 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1551 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1555 | { |
| 1556 | switch (ret) { |
| 1557 | case -EIOCBQUEUED: |
| 1558 | break; |
| 1559 | case -ERESTARTSYS: |
| 1560 | case -ERESTARTNOINTR: |
| 1561 | case -ERESTARTNOHAND: |
| 1562 | case -ERESTART_RESTARTBLOCK: |
| 1563 | /* |
| 1564 | * We can't just restart the syscall, since previously |
| 1565 | * submitted sqes may already be in progress. Just fail this |
| 1566 | * IO with EINTR. |
| 1567 | */ |
| 1568 | ret = -EINTR; |
| 1569 | /* fall through */ |
| 1570 | default: |
| 1571 | kiocb->ki_complete(kiocb, ret, 0); |
| 1572 | } |
| 1573 | } |
| 1574 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1575 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1576 | bool in_async) |
| 1577 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1578 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1579 | *nxt = __io_complete_rw(kiocb, ret); |
| 1580 | else |
| 1581 | io_rw_done(kiocb, ret); |
| 1582 | } |
| 1583 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1584 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1585 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1586 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1587 | struct io_ring_ctx *ctx = req->ctx; |
| 1588 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1589 | struct io_mapped_ubuf *imu; |
| 1590 | unsigned index, buf_index; |
| 1591 | size_t offset; |
| 1592 | u64 buf_addr; |
| 1593 | |
| 1594 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1595 | if (unlikely(!ctx->user_bufs)) |
| 1596 | return -EFAULT; |
| 1597 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1598 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1599 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1600 | return -EFAULT; |
| 1601 | |
| 1602 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1603 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1604 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1605 | |
| 1606 | /* overflow */ |
| 1607 | if (buf_addr + len < buf_addr) |
| 1608 | return -EFAULT; |
| 1609 | /* not inside the mapped region */ |
| 1610 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1611 | return -EFAULT; |
| 1612 | |
| 1613 | /* |
| 1614 | * May not be a start of buffer, set size appropriately |
| 1615 | * and advance us to the beginning. |
| 1616 | */ |
| 1617 | offset = buf_addr - imu->ubuf; |
| 1618 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1619 | |
| 1620 | if (offset) { |
| 1621 | /* |
| 1622 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1623 | * using the latter parts of a big fixed buffer - it iterates |
| 1624 | * over each segment manually. We can cheat a bit here, because |
| 1625 | * we know that: |
| 1626 | * |
| 1627 | * 1) it's a BVEC iter, we set it up |
| 1628 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1629 | * first and last bvec |
| 1630 | * |
| 1631 | * So just find our index, and adjust the iterator afterwards. |
| 1632 | * If the offset is within the first bvec (or the whole first |
| 1633 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1634 | * since we can just skip the first segment, which may not |
| 1635 | * be PAGE_SIZE aligned. |
| 1636 | */ |
| 1637 | const struct bio_vec *bvec = imu->bvec; |
| 1638 | |
| 1639 | if (offset <= bvec->bv_len) { |
| 1640 | iov_iter_advance(iter, offset); |
| 1641 | } else { |
| 1642 | unsigned long seg_skip; |
| 1643 | |
| 1644 | /* skip first vec */ |
| 1645 | offset -= bvec->bv_len; |
| 1646 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1647 | |
| 1648 | iter->bvec = bvec + seg_skip; |
| 1649 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1650 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1651 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1655 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1658 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1659 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1660 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1661 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1662 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1663 | u8 opcode; |
| 1664 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1665 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1666 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1667 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1668 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1669 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1670 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1671 | /* buffer index only valid with fixed read/write */ |
| 1672 | if (req->rw.kiocb.private) |
| 1673 | return -EINVAL; |
| 1674 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1675 | if (req->io) { |
| 1676 | struct io_async_rw *iorw = &req->io->rw; |
| 1677 | |
| 1678 | *iovec = iorw->iov; |
| 1679 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 1680 | if (iorw->iov == iorw->fast_iov) |
| 1681 | *iovec = NULL; |
| 1682 | return iorw->size; |
| 1683 | } |
| 1684 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1685 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1686 | return -EFAULT; |
| 1687 | |
| 1688 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1689 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1690 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 1691 | iovec, iter); |
| 1692 | #endif |
| 1693 | |
| 1694 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 1695 | } |
| 1696 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1697 | /* |
| 1698 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 1699 | * by looping over ->read() or ->write() manually. |
| 1700 | */ |
| 1701 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 1702 | struct iov_iter *iter) |
| 1703 | { |
| 1704 | ssize_t ret = 0; |
| 1705 | |
| 1706 | /* |
| 1707 | * Don't support polled IO through this interface, and we can't |
| 1708 | * support non-blocking either. For the latter, this just causes |
| 1709 | * the kiocb to be handled from an async context. |
| 1710 | */ |
| 1711 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1712 | return -EOPNOTSUPP; |
| 1713 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 1714 | return -EAGAIN; |
| 1715 | |
| 1716 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1717 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1718 | ssize_t nr; |
| 1719 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1720 | if (!iov_iter_is_bvec(iter)) { |
| 1721 | iovec = iov_iter_iovec(iter); |
| 1722 | } else { |
| 1723 | /* fixed buffers import bvec */ |
| 1724 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 1725 | + iter->iov_offset; |
| 1726 | iovec.iov_len = min(iter->count, |
| 1727 | iter->bvec->bv_len - iter->iov_offset); |
| 1728 | } |
| 1729 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1730 | if (rw == READ) { |
| 1731 | nr = file->f_op->read(file, iovec.iov_base, |
| 1732 | iovec.iov_len, &kiocb->ki_pos); |
| 1733 | } else { |
| 1734 | nr = file->f_op->write(file, iovec.iov_base, |
| 1735 | iovec.iov_len, &kiocb->ki_pos); |
| 1736 | } |
| 1737 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1738 | if (iov_iter_is_bvec(iter)) |
| 1739 | kunmap(iter->bvec->bv_page); |
| 1740 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1741 | if (nr < 0) { |
| 1742 | if (!ret) |
| 1743 | ret = nr; |
| 1744 | break; |
| 1745 | } |
| 1746 | ret += nr; |
| 1747 | if (nr != iovec.iov_len) |
| 1748 | break; |
| 1749 | iov_iter_advance(iter, nr); |
| 1750 | } |
| 1751 | |
| 1752 | return ret; |
| 1753 | } |
| 1754 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1755 | 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] | 1756 | struct iovec *iovec, struct iovec *fast_iov, |
| 1757 | struct iov_iter *iter) |
| 1758 | { |
| 1759 | req->io->rw.nr_segs = iter->nr_segs; |
| 1760 | req->io->rw.size = io_size; |
| 1761 | req->io->rw.iov = iovec; |
| 1762 | if (!req->io->rw.iov) { |
| 1763 | req->io->rw.iov = req->io->rw.fast_iov; |
| 1764 | memcpy(req->io->rw.iov, fast_iov, |
| 1765 | sizeof(struct iovec) * iter->nr_segs); |
| 1766 | } |
| 1767 | } |
| 1768 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1769 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1770 | { |
| 1771 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 1772 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | static void io_rw_async(struct io_wq_work **workptr) |
| 1776 | { |
| 1777 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 1778 | struct iovec *iov = NULL; |
| 1779 | |
| 1780 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 1781 | iov = req->io->rw.iov; |
| 1782 | io_wq_submit_work(workptr); |
| 1783 | kfree(iov); |
| 1784 | } |
| 1785 | |
| 1786 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 1787 | struct iovec *iovec, struct iovec *fast_iov, |
| 1788 | struct iov_iter *iter) |
| 1789 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 1790 | if (req->opcode == IORING_OP_READ_FIXED || |
| 1791 | req->opcode == IORING_OP_WRITE_FIXED) |
| 1792 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1793 | if (!req->io && io_alloc_async_ctx(req)) |
| 1794 | return -ENOMEM; |
| 1795 | |
| 1796 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 1797 | req->work.func = io_rw_async; |
| 1798 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1801 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1802 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1803 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1804 | struct io_async_ctx *io; |
| 1805 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1806 | ssize_t ret; |
| 1807 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1808 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 1809 | if (ret) |
| 1810 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1811 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1812 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 1813 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1814 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1815 | if (!req->io) |
| 1816 | return 0; |
| 1817 | |
| 1818 | io = req->io; |
| 1819 | io->rw.iov = io->rw.fast_iov; |
| 1820 | req->io = NULL; |
| 1821 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 1822 | req->io = io; |
| 1823 | if (ret < 0) |
| 1824 | return ret; |
| 1825 | |
| 1826 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 1827 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1830 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1831 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1832 | { |
| 1833 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1834 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1835 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1836 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1837 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1838 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1839 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 1840 | if (ret < 0) |
| 1841 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1842 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 1843 | /* Ensure we clear previously set non-block flag */ |
| 1844 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1845 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 1846 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 1847 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1848 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1849 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1850 | req->result = io_size; |
| 1851 | |
| 1852 | /* |
| 1853 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 1854 | * we know to async punt it even if it was opened O_NONBLOCK |
| 1855 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1856 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1857 | req->flags |= REQ_F_MUST_PUNT; |
| 1858 | goto copy_iov; |
| 1859 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1860 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1861 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1862 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1863 | if (!ret) { |
| 1864 | ssize_t ret2; |
| 1865 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1866 | if (req->file->f_op->read_iter) |
| 1867 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1868 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1869 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1870 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1871 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1872 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1873 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1874 | } else { |
| 1875 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1876 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1877 | inline_vecs, &iter); |
| 1878 | if (ret) |
| 1879 | goto out_free; |
| 1880 | return -EAGAIN; |
| 1881 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1882 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1883 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1884 | if (!io_wq_current_is_worker()) |
| 1885 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1886 | return ret; |
| 1887 | } |
| 1888 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1889 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1890 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1891 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1892 | struct io_async_ctx *io; |
| 1893 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1894 | ssize_t ret; |
| 1895 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1896 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 1897 | if (ret) |
| 1898 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1899 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1900 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 1901 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1902 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1903 | if (!req->io) |
| 1904 | return 0; |
| 1905 | |
| 1906 | io = req->io; |
| 1907 | io->rw.iov = io->rw.fast_iov; |
| 1908 | req->io = NULL; |
| 1909 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 1910 | req->io = io; |
| 1911 | if (ret < 0) |
| 1912 | return ret; |
| 1913 | |
| 1914 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 1915 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1918 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1919 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1920 | { |
| 1921 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1922 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1923 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1924 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1925 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1926 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1927 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 1928 | if (ret < 0) |
| 1929 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1930 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 1931 | /* Ensure we clear previously set non-block flag */ |
| 1932 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1933 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 1934 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 1935 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1936 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1937 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1938 | req->result = io_size; |
| 1939 | |
| 1940 | /* |
| 1941 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 1942 | * we know to async punt it even if it was opened O_NONBLOCK |
| 1943 | */ |
| 1944 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 1945 | req->flags |= REQ_F_MUST_PUNT; |
| 1946 | goto copy_iov; |
| 1947 | } |
| 1948 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1949 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 1950 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 1951 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1952 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1953 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1954 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1955 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1956 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1957 | ssize_t ret2; |
| 1958 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1959 | /* |
| 1960 | * Open-code file_start_write here to grab freeze protection, |
| 1961 | * which will be released by another thread in |
| 1962 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 1963 | * released so that it doesn't complain about the held lock when |
| 1964 | * we return to userspace. |
| 1965 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1966 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1967 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1968 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1969 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1970 | SB_FREEZE_WRITE); |
| 1971 | } |
| 1972 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1973 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1974 | if (req->file->f_op->write_iter) |
| 1975 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1976 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1977 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1978 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1979 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1980 | } else { |
| 1981 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1982 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1983 | inline_vecs, &iter); |
| 1984 | if (ret) |
| 1985 | goto out_free; |
| 1986 | return -EAGAIN; |
| 1987 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1988 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1989 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1990 | if (!io_wq_current_is_worker()) |
| 1991 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1992 | return ret; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 1997 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1998 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1999 | { |
| 2000 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2001 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2002 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2003 | return -EINVAL; |
| 2004 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2005 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2006 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
| 2009 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2010 | 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] | 2011 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2012 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2013 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2014 | if (!req->file) |
| 2015 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2016 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2017 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2018 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2019 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2020 | return -EINVAL; |
| 2021 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2022 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2023 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2024 | return -EINVAL; |
| 2025 | |
| 2026 | req->sync.off = READ_ONCE(sqe->off); |
| 2027 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2028 | return 0; |
| 2029 | } |
| 2030 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2031 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2032 | { |
| 2033 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2034 | req_set_fail_links(req); |
| 2035 | io_cqring_add_event(req, -ECANCELED); |
| 2036 | io_put_req(req); |
| 2037 | return true; |
| 2038 | } |
| 2039 | |
| 2040 | return false; |
| 2041 | } |
| 2042 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2043 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2044 | { |
| 2045 | struct io_wq_work *work = *workptr; |
| 2046 | struct io_kiocb *link = work->data; |
| 2047 | |
| 2048 | io_queue_linked_timeout(link); |
| 2049 | work->func = io_wq_submit_work; |
| 2050 | } |
| 2051 | |
| 2052 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2053 | { |
| 2054 | struct io_kiocb *link; |
| 2055 | |
| 2056 | io_prep_async_work(nxt, &link); |
| 2057 | *workptr = &nxt->work; |
| 2058 | if (link) { |
| 2059 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2060 | nxt->work.func = io_link_work_cb; |
| 2061 | nxt->work.data = link; |
| 2062 | } |
| 2063 | } |
| 2064 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2065 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2066 | { |
| 2067 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2068 | loff_t end = req->sync.off + req->sync.len; |
| 2069 | struct io_kiocb *nxt = NULL; |
| 2070 | int ret; |
| 2071 | |
| 2072 | if (io_req_cancelled(req)) |
| 2073 | return; |
| 2074 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2075 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2076 | end > 0 ? end : LLONG_MAX, |
| 2077 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2078 | if (ret < 0) |
| 2079 | req_set_fail_links(req); |
| 2080 | io_cqring_add_event(req, ret); |
| 2081 | io_put_req_find_next(req, &nxt); |
| 2082 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2083 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2086 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2087 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2088 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2089 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2090 | |
| 2091 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2092 | if (force_nonblock) { |
| 2093 | io_put_req(req); |
| 2094 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2095 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2096 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2097 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2098 | work = old_work = &req->work; |
| 2099 | io_fsync_finish(&work); |
| 2100 | if (work && work != old_work) |
| 2101 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2105 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2106 | { |
| 2107 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2108 | struct io_kiocb *nxt = NULL; |
| 2109 | int ret; |
| 2110 | |
| 2111 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2112 | req->sync.len); |
| 2113 | if (ret < 0) |
| 2114 | req_set_fail_links(req); |
| 2115 | io_cqring_add_event(req, ret); |
| 2116 | io_put_req_find_next(req, &nxt); |
| 2117 | if (nxt) |
| 2118 | io_wq_assign_next(workptr, nxt); |
| 2119 | } |
| 2120 | |
| 2121 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2122 | const struct io_uring_sqe *sqe) |
| 2123 | { |
| 2124 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2125 | return -EINVAL; |
| 2126 | |
| 2127 | req->sync.off = READ_ONCE(sqe->off); |
| 2128 | req->sync.len = READ_ONCE(sqe->addr); |
| 2129 | req->sync.mode = READ_ONCE(sqe->len); |
| 2130 | return 0; |
| 2131 | } |
| 2132 | |
| 2133 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2134 | bool force_nonblock) |
| 2135 | { |
| 2136 | struct io_wq_work *work, *old_work; |
| 2137 | |
| 2138 | /* fallocate always requiring blocking context */ |
| 2139 | if (force_nonblock) { |
| 2140 | io_put_req(req); |
| 2141 | req->work.func = io_fallocate_finish; |
| 2142 | return -EAGAIN; |
| 2143 | } |
| 2144 | |
| 2145 | work = old_work = &req->work; |
| 2146 | io_fallocate_finish(&work); |
| 2147 | if (work && work != old_work) |
| 2148 | *nxt = container_of(work, struct io_kiocb, work); |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2153 | 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] | 2154 | { |
| 2155 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2156 | |
| 2157 | if (!req->file) |
| 2158 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2159 | |
| 2160 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2161 | return -EINVAL; |
| 2162 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2163 | return -EINVAL; |
| 2164 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2165 | req->sync.off = READ_ONCE(sqe->off); |
| 2166 | req->sync.len = READ_ONCE(sqe->len); |
| 2167 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2172 | { |
| 2173 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2174 | struct io_kiocb *nxt = NULL; |
| 2175 | int ret; |
| 2176 | |
| 2177 | if (io_req_cancelled(req)) |
| 2178 | return; |
| 2179 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2180 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2181 | req->sync.flags); |
| 2182 | if (ret < 0) |
| 2183 | req_set_fail_links(req); |
| 2184 | io_cqring_add_event(req, ret); |
| 2185 | io_put_req_find_next(req, &nxt); |
| 2186 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2187 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2188 | } |
| 2189 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2190 | static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2191 | bool force_nonblock) |
| 2192 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2193 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2194 | |
| 2195 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2196 | if (force_nonblock) { |
| 2197 | io_put_req(req); |
| 2198 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2199 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2200 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2201 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2202 | work = old_work = &req->work; |
| 2203 | io_sync_file_range_finish(&work); |
| 2204 | if (work && work != old_work) |
| 2205 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2209 | #if defined(CONFIG_NET) |
| 2210 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2211 | { |
| 2212 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2213 | struct iovec *iov = NULL; |
| 2214 | |
| 2215 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2216 | iov = req->io->msg.iov; |
| 2217 | io_wq_submit_work(workptr); |
| 2218 | kfree(iov); |
| 2219 | } |
| 2220 | #endif |
| 2221 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2222 | 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] | 2223 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2224 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2225 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2226 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2227 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2228 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2229 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2230 | |
| 2231 | if (!io) |
| 2232 | return 0; |
| 2233 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2234 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2235 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2236 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2237 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2238 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2239 | #endif |
| 2240 | } |
| 2241 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2242 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2243 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2244 | { |
| 2245 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2246 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2247 | struct socket *sock; |
| 2248 | int ret; |
| 2249 | |
| 2250 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2251 | return -EINVAL; |
| 2252 | |
| 2253 | sock = sock_from_file(req->file, &ret); |
| 2254 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2255 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2256 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2257 | unsigned flags; |
| 2258 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2259 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2260 | kmsg = &req->io->msg; |
| 2261 | kmsg->msg.msg_name = &addr; |
| 2262 | /* if iov is set, it's allocated already */ |
| 2263 | if (!kmsg->iov) |
| 2264 | kmsg->iov = kmsg->fast_iov; |
| 2265 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2266 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2267 | struct io_sr_msg *sr = &req->sr_msg; |
| 2268 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2269 | kmsg = &io.msg; |
| 2270 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2271 | |
| 2272 | io.msg.iov = io.msg.fast_iov; |
| 2273 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2274 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2275 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2276 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2279 | flags = req->sr_msg.msg_flags; |
| 2280 | if (flags & MSG_DONTWAIT) |
| 2281 | req->flags |= REQ_F_NOWAIT; |
| 2282 | else if (force_nonblock) |
| 2283 | flags |= MSG_DONTWAIT; |
| 2284 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2285 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2286 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2287 | if (req->io) |
| 2288 | return -EAGAIN; |
| 2289 | if (io_alloc_async_ctx(req)) |
| 2290 | return -ENOMEM; |
| 2291 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2292 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2293 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2294 | } |
| 2295 | if (ret == -ERESTARTSYS) |
| 2296 | ret = -EINTR; |
| 2297 | } |
| 2298 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2299 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2300 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2301 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2302 | if (ret < 0) |
| 2303 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2304 | io_put_req_find_next(req, nxt); |
| 2305 | return 0; |
| 2306 | #else |
| 2307 | return -EOPNOTSUPP; |
| 2308 | #endif |
| 2309 | } |
| 2310 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2311 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 2312 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2313 | { |
| 2314 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2315 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2316 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2317 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2318 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2319 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2320 | |
| 2321 | if (!io) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2322 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2323 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2324 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2325 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2326 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2327 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2328 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2329 | #endif |
| 2330 | } |
| 2331 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2332 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2333 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2334 | { |
| 2335 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2336 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2337 | struct socket *sock; |
| 2338 | int ret; |
| 2339 | |
| 2340 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2341 | return -EINVAL; |
| 2342 | |
| 2343 | sock = sock_from_file(req->file, &ret); |
| 2344 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2345 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2346 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2347 | unsigned flags; |
| 2348 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2349 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2350 | kmsg = &req->io->msg; |
| 2351 | kmsg->msg.msg_name = &addr; |
| 2352 | /* if iov is set, it's allocated already */ |
| 2353 | if (!kmsg->iov) |
| 2354 | kmsg->iov = kmsg->fast_iov; |
| 2355 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2356 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2357 | struct io_sr_msg *sr = &req->sr_msg; |
| 2358 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2359 | kmsg = &io.msg; |
| 2360 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2361 | |
| 2362 | io.msg.iov = io.msg.fast_iov; |
| 2363 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2364 | sr->msg_flags, &io.msg.uaddr, |
| 2365 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2366 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2367 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2368 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2369 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2370 | flags = req->sr_msg.msg_flags; |
| 2371 | if (flags & MSG_DONTWAIT) |
| 2372 | req->flags |= REQ_F_NOWAIT; |
| 2373 | else if (force_nonblock) |
| 2374 | flags |= MSG_DONTWAIT; |
| 2375 | |
| 2376 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 2377 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2378 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2379 | if (req->io) |
| 2380 | return -EAGAIN; |
| 2381 | if (io_alloc_async_ctx(req)) |
| 2382 | return -ENOMEM; |
| 2383 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2384 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2385 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2386 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 2387 | if (ret == -ERESTARTSYS) |
| 2388 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2389 | } |
| 2390 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2391 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2392 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2393 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2394 | if (ret < 0) |
| 2395 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2396 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2397 | return 0; |
| 2398 | #else |
| 2399 | return -EOPNOTSUPP; |
| 2400 | #endif |
| 2401 | } |
| 2402 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2403 | 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] | 2404 | { |
| 2405 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2406 | struct io_accept *accept = &req->accept; |
| 2407 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2408 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 2409 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 2410 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2411 | return -EINVAL; |
| 2412 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 2413 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2414 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2415 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2416 | return 0; |
| 2417 | #else |
| 2418 | return -EOPNOTSUPP; |
| 2419 | #endif |
| 2420 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2421 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2422 | #if defined(CONFIG_NET) |
| 2423 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2424 | bool force_nonblock) |
| 2425 | { |
| 2426 | struct io_accept *accept = &req->accept; |
| 2427 | unsigned file_flags; |
| 2428 | int ret; |
| 2429 | |
| 2430 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 2431 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 2432 | accept->addr_len, accept->flags); |
| 2433 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2434 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 2435 | if (ret == -ERESTARTSYS) |
| 2436 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2437 | if (ret < 0) |
| 2438 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2439 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2440 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2441 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | static void io_accept_finish(struct io_wq_work **workptr) |
| 2445 | { |
| 2446 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2447 | struct io_kiocb *nxt = NULL; |
| 2448 | |
| 2449 | if (io_req_cancelled(req)) |
| 2450 | return; |
| 2451 | __io_accept(req, &nxt, false); |
| 2452 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2453 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2454 | } |
| 2455 | #endif |
| 2456 | |
| 2457 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2458 | bool force_nonblock) |
| 2459 | { |
| 2460 | #if defined(CONFIG_NET) |
| 2461 | int ret; |
| 2462 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2463 | ret = __io_accept(req, nxt, force_nonblock); |
| 2464 | if (ret == -EAGAIN && force_nonblock) { |
| 2465 | req->work.func = io_accept_finish; |
| 2466 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2467 | io_put_req(req); |
| 2468 | return -EAGAIN; |
| 2469 | } |
| 2470 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2471 | #else |
| 2472 | return -EOPNOTSUPP; |
| 2473 | #endif |
| 2474 | } |
| 2475 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2476 | 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] | 2477 | { |
| 2478 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2479 | struct io_connect *conn = &req->connect; |
| 2480 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2481 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2482 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 2483 | return -EINVAL; |
| 2484 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 2485 | return -EINVAL; |
| 2486 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2487 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2488 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 2489 | |
| 2490 | if (!io) |
| 2491 | return 0; |
| 2492 | |
| 2493 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2494 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2495 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2496 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2497 | #endif |
| 2498 | } |
| 2499 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2500 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2501 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2502 | { |
| 2503 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2504 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2505 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2506 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2507 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2508 | if (req->io) { |
| 2509 | io = req->io; |
| 2510 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2511 | ret = move_addr_to_kernel(req->connect.addr, |
| 2512 | req->connect.addr_len, |
| 2513 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2514 | if (ret) |
| 2515 | goto out; |
| 2516 | io = &__io; |
| 2517 | } |
| 2518 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2519 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 2520 | |
| 2521 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 2522 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 2523 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2524 | if (req->io) |
| 2525 | return -EAGAIN; |
| 2526 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2527 | ret = -ENOMEM; |
| 2528 | goto out; |
| 2529 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2530 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2531 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2532 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2533 | if (ret == -ERESTARTSYS) |
| 2534 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2535 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2536 | if (ret < 0) |
| 2537 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2538 | io_cqring_add_event(req, ret); |
| 2539 | io_put_req_find_next(req, nxt); |
| 2540 | return 0; |
| 2541 | #else |
| 2542 | return -EOPNOTSUPP; |
| 2543 | #endif |
| 2544 | } |
| 2545 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2546 | static void io_poll_remove_one(struct io_kiocb *req) |
| 2547 | { |
| 2548 | struct io_poll_iocb *poll = &req->poll; |
| 2549 | |
| 2550 | spin_lock(&poll->head->lock); |
| 2551 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2552 | if (!list_empty(&poll->wait.entry)) { |
| 2553 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2554 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2555 | } |
| 2556 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2557 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 2561 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2562 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2563 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2564 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2565 | |
| 2566 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2567 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 2568 | struct hlist_head *list; |
| 2569 | |
| 2570 | list = &ctx->cancel_hash[i]; |
| 2571 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 2572 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2573 | } |
| 2574 | spin_unlock_irq(&ctx->completion_lock); |
| 2575 | } |
| 2576 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2577 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 2578 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2579 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2580 | struct io_kiocb *req; |
| 2581 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2582 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 2583 | hlist_for_each_entry(req, list, hash_node) { |
| 2584 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2585 | io_poll_remove_one(req); |
| 2586 | return 0; |
| 2587 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | return -ENOENT; |
| 2591 | } |
| 2592 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2593 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 2594 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2595 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2596 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2597 | return -EINVAL; |
| 2598 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 2599 | sqe->poll_events) |
| 2600 | return -EINVAL; |
| 2601 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2602 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
| 2607 | * Find a running poll command that matches one specified in sqe->addr, |
| 2608 | * and remove it if found. |
| 2609 | */ |
| 2610 | static int io_poll_remove(struct io_kiocb *req) |
| 2611 | { |
| 2612 | struct io_ring_ctx *ctx = req->ctx; |
| 2613 | u64 addr; |
| 2614 | int ret; |
| 2615 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2616 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2617 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2618 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2619 | spin_unlock_irq(&ctx->completion_lock); |
| 2620 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2621 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2622 | if (ret < 0) |
| 2623 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2624 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2625 | return 0; |
| 2626 | } |
| 2627 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2628 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2629 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2630 | struct io_ring_ctx *ctx = req->ctx; |
| 2631 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2632 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2633 | if (error) |
| 2634 | io_cqring_fill_event(req, error); |
| 2635 | else |
| 2636 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2637 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2640 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2641 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2642 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2643 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 2644 | struct io_poll_iocb *poll = &req->poll; |
| 2645 | struct poll_table_struct pt = { ._key = poll->events }; |
| 2646 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2647 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2648 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2649 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2650 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2651 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2652 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2653 | ret = -ECANCELED; |
| 2654 | } else if (READ_ONCE(poll->canceled)) { |
| 2655 | ret = -ECANCELED; |
| 2656 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2657 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2658 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2659 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 2660 | |
| 2661 | /* |
| 2662 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 2663 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 2664 | * synchronize with them. In the cancellation case the list_del_init |
| 2665 | * itself is not actually needed, but harmless so we keep it in to |
| 2666 | * avoid further branches in the fast path. |
| 2667 | */ |
| 2668 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2669 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2670 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2671 | spin_unlock_irq(&ctx->completion_lock); |
| 2672 | return; |
| 2673 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2674 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2675 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2676 | spin_unlock_irq(&ctx->completion_lock); |
| 2677 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2678 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2679 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2680 | if (ret < 0) |
| 2681 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2682 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2683 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2684 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 2688 | void *key) |
| 2689 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 2690 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2691 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 2692 | struct io_ring_ctx *ctx = req->ctx; |
| 2693 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2694 | unsigned long flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2695 | |
| 2696 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2697 | if (mask && !(mask & poll->events)) |
| 2698 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2699 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2700 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2701 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 2702 | /* |
| 2703 | * Run completion inline if we can. We're using trylock here because |
| 2704 | * we are violating the completion_lock -> poll wq lock ordering. |
| 2705 | * If we have a link timeout we're going to need the completion_lock |
| 2706 | * for finalizing the request, mark us as having grabbed that already. |
| 2707 | */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2708 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2709 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2710 | io_poll_complete(req, mask, 0); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 2711 | req->flags |= REQ_F_COMP_LOCKED; |
| 2712 | io_put_req(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2713 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2714 | |
| 2715 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2716 | } else { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2717 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2718 | } |
| 2719 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2720 | return 1; |
| 2721 | } |
| 2722 | |
| 2723 | struct io_poll_table { |
| 2724 | struct poll_table_struct pt; |
| 2725 | struct io_kiocb *req; |
| 2726 | int error; |
| 2727 | }; |
| 2728 | |
| 2729 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 2730 | struct poll_table_struct *p) |
| 2731 | { |
| 2732 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 2733 | |
| 2734 | if (unlikely(pt->req->poll.head)) { |
| 2735 | pt->error = -EINVAL; |
| 2736 | return; |
| 2737 | } |
| 2738 | |
| 2739 | pt->error = 0; |
| 2740 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2741 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2744 | static void io_poll_req_insert(struct io_kiocb *req) |
| 2745 | { |
| 2746 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2747 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2748 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2749 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 2750 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2753 | 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] | 2754 | { |
| 2755 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2756 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2757 | |
| 2758 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2759 | return -EINVAL; |
| 2760 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 2761 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2762 | if (!poll->file) |
| 2763 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2764 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2765 | events = READ_ONCE(sqe->poll_events); |
| 2766 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2767 | return 0; |
| 2768 | } |
| 2769 | |
| 2770 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 2771 | { |
| 2772 | struct io_poll_iocb *poll = &req->poll; |
| 2773 | struct io_ring_ctx *ctx = req->ctx; |
| 2774 | struct io_poll_table ipt; |
| 2775 | bool cancel = false; |
| 2776 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 2777 | |
| 2778 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 2779 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2780 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2781 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2782 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2783 | poll->canceled = false; |
| 2784 | |
| 2785 | ipt.pt._qproc = io_poll_queue_proc; |
| 2786 | ipt.pt._key = poll->events; |
| 2787 | ipt.req = req; |
| 2788 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 2789 | |
| 2790 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2791 | INIT_LIST_HEAD(&poll->wait.entry); |
| 2792 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 2793 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2794 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 2795 | INIT_LIST_HEAD(&req->list); |
| 2796 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2797 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2798 | |
| 2799 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2800 | if (likely(poll->head)) { |
| 2801 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2802 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2803 | if (ipt.error) |
| 2804 | cancel = true; |
| 2805 | ipt.error = 0; |
| 2806 | mask = 0; |
| 2807 | } |
| 2808 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 2809 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2810 | else if (cancel) |
| 2811 | WRITE_ONCE(poll->canceled, true); |
| 2812 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2813 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2814 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2815 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2816 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2817 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2818 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2819 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2820 | spin_unlock_irq(&ctx->completion_lock); |
| 2821 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2822 | if (mask) { |
| 2823 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2824 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2825 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2826 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2829 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 2830 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2831 | struct io_timeout_data *data = container_of(timer, |
| 2832 | struct io_timeout_data, timer); |
| 2833 | struct io_kiocb *req = data->req; |
| 2834 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2835 | unsigned long flags; |
| 2836 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2837 | atomic_inc(&ctx->cq_timeouts); |
| 2838 | |
| 2839 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 2840 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2841 | * We could be racing with timeout deletion. If the list is empty, |
| 2842 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 2843 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 2844 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2845 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2846 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2847 | /* |
| 2848 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2849 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2850 | * pointer will be increased, otherwise other timeout reqs may |
| 2851 | * return in advance without waiting for enough wait_nr. |
| 2852 | */ |
| 2853 | prev = req; |
| 2854 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 2855 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2856 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2857 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 2858 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2859 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2860 | io_commit_cqring(ctx); |
| 2861 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2862 | |
| 2863 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2864 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2865 | io_put_req(req); |
| 2866 | return HRTIMER_NORESTART; |
| 2867 | } |
| 2868 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2869 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 2870 | { |
| 2871 | struct io_kiocb *req; |
| 2872 | int ret = -ENOENT; |
| 2873 | |
| 2874 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 2875 | if (user_data == req->user_data) { |
| 2876 | list_del_init(&req->list); |
| 2877 | ret = 0; |
| 2878 | break; |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | if (ret == -ENOENT) |
| 2883 | return ret; |
| 2884 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 2885 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2886 | if (ret == -1) |
| 2887 | return -EALREADY; |
| 2888 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2889 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2890 | io_cqring_fill_event(req, -ECANCELED); |
| 2891 | io_put_req(req); |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2895 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 2896 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 2897 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 2898 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2899 | return -EINVAL; |
| 2900 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 2901 | return -EINVAL; |
| 2902 | |
| 2903 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 2904 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 2905 | if (req->timeout.flags) |
| 2906 | return -EINVAL; |
| 2907 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 2908 | return 0; |
| 2909 | } |
| 2910 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2911 | /* |
| 2912 | * Remove or update an existing timeout command |
| 2913 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2914 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2915 | { |
| 2916 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2917 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2918 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2919 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 2920 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2921 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2922 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2923 | io_commit_cqring(ctx); |
| 2924 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2925 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2926 | if (ret < 0) |
| 2927 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2928 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2929 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2930 | } |
| 2931 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2932 | 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] | 2933 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2934 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2935 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 2936 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2937 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2938 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2939 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2940 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 2941 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 2942 | if (sqe->off && is_timeout_link) |
| 2943 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 2944 | flags = READ_ONCE(sqe->timeout_flags); |
| 2945 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2946 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 2947 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 2948 | req->timeout.count = READ_ONCE(sqe->off); |
| 2949 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2950 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 2951 | return -ENOMEM; |
| 2952 | |
| 2953 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2954 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2955 | req->flags |= REQ_F_TIMEOUT; |
| 2956 | |
| 2957 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2958 | return -EFAULT; |
| 2959 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2960 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2961 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2962 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2963 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2964 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2965 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 2966 | return 0; |
| 2967 | } |
| 2968 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2969 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2970 | { |
| 2971 | unsigned count; |
| 2972 | struct io_ring_ctx *ctx = req->ctx; |
| 2973 | struct io_timeout_data *data; |
| 2974 | struct list_head *entry; |
| 2975 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2976 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 2977 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2978 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2979 | /* |
| 2980 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2981 | * timeout event to be satisfied. If it isn't set, then this is |
| 2982 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2983 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 2984 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2985 | if (!count) { |
| 2986 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 2987 | spin_lock_irq(&ctx->completion_lock); |
| 2988 | entry = ctx->timeout_list.prev; |
| 2989 | goto add; |
| 2990 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2991 | |
| 2992 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 2993 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2994 | |
| 2995 | /* |
| 2996 | * Insertion sort, ensuring the first entry in the list is always |
| 2997 | * the one we need first. |
| 2998 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2999 | spin_lock_irq(&ctx->completion_lock); |
| 3000 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3001 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3002 | unsigned nxt_sq_head; |
| 3003 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3004 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3005 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3006 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3007 | continue; |
| 3008 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3009 | /* |
| 3010 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3011 | * long to store it. |
| 3012 | */ |
| 3013 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3014 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3015 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3016 | |
| 3017 | /* |
| 3018 | * cached_sq_head may overflow, and it will never overflow twice |
| 3019 | * once there is some timeout req still be valid. |
| 3020 | */ |
| 3021 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3022 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3023 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3024 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3025 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3026 | |
| 3027 | /* |
| 3028 | * Sequence of reqs after the insert one and itself should |
| 3029 | * be adjusted because each timeout req consumes a slot. |
| 3030 | */ |
| 3031 | span++; |
| 3032 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3033 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3034 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3035 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3036 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3037 | data->timer.function = io_timeout_fn; |
| 3038 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3039 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3040 | return 0; |
| 3041 | } |
| 3042 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3043 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3044 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3045 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3046 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3047 | return req->user_data == (unsigned long) data; |
| 3048 | } |
| 3049 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3050 | 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] | 3051 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3052 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3053 | int ret = 0; |
| 3054 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3055 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3056 | switch (cancel_ret) { |
| 3057 | case IO_WQ_CANCEL_OK: |
| 3058 | ret = 0; |
| 3059 | break; |
| 3060 | case IO_WQ_CANCEL_RUNNING: |
| 3061 | ret = -EALREADY; |
| 3062 | break; |
| 3063 | case IO_WQ_CANCEL_NOTFOUND: |
| 3064 | ret = -ENOENT; |
| 3065 | break; |
| 3066 | } |
| 3067 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3068 | return ret; |
| 3069 | } |
| 3070 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3071 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3072 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3073 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3074 | { |
| 3075 | unsigned long flags; |
| 3076 | int ret; |
| 3077 | |
| 3078 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3079 | if (ret != -ENOENT) { |
| 3080 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3081 | goto done; |
| 3082 | } |
| 3083 | |
| 3084 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3085 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3086 | if (ret != -ENOENT) |
| 3087 | goto done; |
| 3088 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3089 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3090 | if (!ret) |
| 3091 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3092 | io_cqring_fill_event(req, ret); |
| 3093 | io_commit_cqring(ctx); |
| 3094 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3095 | io_cqring_ev_posted(ctx); |
| 3096 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3097 | if (ret < 0) |
| 3098 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3099 | io_put_req_find_next(req, nxt); |
| 3100 | } |
| 3101 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3102 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3103 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3104 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3105 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3106 | return -EINVAL; |
| 3107 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3108 | sqe->cancel_flags) |
| 3109 | return -EINVAL; |
| 3110 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3111 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3112 | return 0; |
| 3113 | } |
| 3114 | |
| 3115 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3116 | { |
| 3117 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3118 | |
| 3119 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3120 | return 0; |
| 3121 | } |
| 3122 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3123 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3124 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3125 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3126 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3127 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3128 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3129 | case IORING_OP_NOP: |
| 3130 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3131 | case IORING_OP_READV: |
| 3132 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3133 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3134 | break; |
| 3135 | case IORING_OP_WRITEV: |
| 3136 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3137 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3138 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3139 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3140 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3141 | break; |
| 3142 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3143 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3144 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3145 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3146 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3147 | break; |
| 3148 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3149 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3150 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3151 | case IORING_OP_SENDMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3152 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3153 | break; |
| 3154 | case IORING_OP_RECVMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3155 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3156 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3157 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3158 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3159 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3160 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3161 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3162 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3163 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3164 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3165 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3166 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3167 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3168 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3169 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3170 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3171 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3172 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3173 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3174 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3175 | case IORING_OP_FALLOCATE: |
| 3176 | ret = io_fallocate_prep(req, sqe); |
| 3177 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3178 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3179 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 3180 | req->opcode); |
| 3181 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3182 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3185 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3188 | 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] | 3189 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3190 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3191 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3192 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 3193 | /* Still need defer if there is pending req in defer list. */ |
| 3194 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3195 | return 0; |
| 3196 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3197 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3198 | return -EAGAIN; |
| 3199 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3200 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3201 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3202 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3203 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3204 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 3205 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3206 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3207 | return 0; |
| 3208 | } |
| 3209 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 3210 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3211 | list_add_tail(&req->list, &ctx->defer_list); |
| 3212 | spin_unlock_irq(&ctx->completion_lock); |
| 3213 | return -EIOCBQUEUED; |
| 3214 | } |
| 3215 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3216 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3217 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3218 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3219 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3220 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3221 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3222 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3223 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3224 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3225 | break; |
| 3226 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3227 | case IORING_OP_READ_FIXED: |
| 3228 | if (sqe) { |
| 3229 | ret = io_read_prep(req, sqe, force_nonblock); |
| 3230 | if (ret < 0) |
| 3231 | break; |
| 3232 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 3233 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3234 | break; |
| 3235 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3236 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3237 | if (sqe) { |
| 3238 | ret = io_write_prep(req, sqe, force_nonblock); |
| 3239 | if (ret < 0) |
| 3240 | break; |
| 3241 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 3242 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3243 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3244 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3245 | if (sqe) { |
| 3246 | ret = io_prep_fsync(req, sqe); |
| 3247 | if (ret < 0) |
| 3248 | break; |
| 3249 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3250 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3251 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3252 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3253 | if (sqe) { |
| 3254 | ret = io_poll_add_prep(req, sqe); |
| 3255 | if (ret) |
| 3256 | break; |
| 3257 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3258 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3259 | break; |
| 3260 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3261 | if (sqe) { |
| 3262 | ret = io_poll_remove_prep(req, sqe); |
| 3263 | if (ret < 0) |
| 3264 | break; |
| 3265 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3266 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3267 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3268 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3269 | if (sqe) { |
| 3270 | ret = io_prep_sfr(req, sqe); |
| 3271 | if (ret < 0) |
| 3272 | break; |
| 3273 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3274 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3275 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3276 | case IORING_OP_SENDMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3277 | if (sqe) { |
| 3278 | ret = io_sendmsg_prep(req, sqe); |
| 3279 | if (ret < 0) |
| 3280 | break; |
| 3281 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3282 | ret = io_sendmsg(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3283 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 3284 | case IORING_OP_RECVMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3285 | if (sqe) { |
| 3286 | ret = io_recvmsg_prep(req, sqe); |
| 3287 | if (ret) |
| 3288 | break; |
| 3289 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3290 | ret = io_recvmsg(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 3291 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3292 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3293 | if (sqe) { |
| 3294 | ret = io_timeout_prep(req, sqe, false); |
| 3295 | if (ret) |
| 3296 | break; |
| 3297 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3298 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3299 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3300 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3301 | if (sqe) { |
| 3302 | ret = io_timeout_remove_prep(req, sqe); |
| 3303 | if (ret) |
| 3304 | break; |
| 3305 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3306 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3307 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3308 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3309 | if (sqe) { |
| 3310 | ret = io_accept_prep(req, sqe); |
| 3311 | if (ret) |
| 3312 | break; |
| 3313 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3314 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3315 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3316 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3317 | if (sqe) { |
| 3318 | ret = io_connect_prep(req, sqe); |
| 3319 | if (ret) |
| 3320 | break; |
| 3321 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3322 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3323 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3324 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3325 | if (sqe) { |
| 3326 | ret = io_async_cancel_prep(req, sqe); |
| 3327 | if (ret) |
| 3328 | break; |
| 3329 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3330 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3331 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3332 | case IORING_OP_FALLOCATE: |
| 3333 | if (sqe) { |
| 3334 | ret = io_fallocate_prep(req, sqe); |
| 3335 | if (ret) |
| 3336 | break; |
| 3337 | } |
| 3338 | ret = io_fallocate(req, nxt, force_nonblock); |
| 3339 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3340 | default: |
| 3341 | ret = -EINVAL; |
| 3342 | break; |
| 3343 | } |
| 3344 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3345 | if (ret) |
| 3346 | return ret; |
| 3347 | |
| 3348 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3349 | const bool in_async = io_wq_current_is_worker(); |
| 3350 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3351 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3352 | return -EAGAIN; |
| 3353 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3354 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 3355 | if (in_async) |
| 3356 | mutex_lock(&ctx->uring_lock); |
| 3357 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3358 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3359 | |
| 3360 | if (in_async) |
| 3361 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3367 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3368 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3369 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3370 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3371 | struct io_kiocb *nxt = NULL; |
| 3372 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3373 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3374 | if (work->flags & IO_WQ_WORK_CANCEL) |
| 3375 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3376 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3377 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 3378 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 3379 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3380 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3381 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3382 | /* |
| 3383 | * We can get EAGAIN for polled IO even though we're |
| 3384 | * forcing a sync submission from here, since we can't |
| 3385 | * wait for request slots on the block side. |
| 3386 | */ |
| 3387 | if (ret != -EAGAIN) |
| 3388 | break; |
| 3389 | cond_resched(); |
| 3390 | } while (1); |
| 3391 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3392 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3393 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3394 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 3395 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3396 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3397 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3398 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 3399 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3400 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3401 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3402 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3403 | if (!ret && nxt) |
| 3404 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3405 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3406 | |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 3407 | static bool io_req_op_valid(int op) |
| 3408 | { |
| 3409 | return op >= IORING_OP_NOP && op < IORING_OP_LAST; |
| 3410 | } |
| 3411 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3412 | static int io_req_needs_file(struct io_kiocb *req) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3413 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3414 | switch (req->opcode) { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3415 | case IORING_OP_NOP: |
| 3416 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 5683e54 | 2019-11-14 00:59:19 +0300 | [diff] [blame] | 3417 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | a320e9f | 2019-11-14 00:11:01 +0300 | [diff] [blame] | 3418 | case IORING_OP_TIMEOUT_REMOVE: |
| 3419 | case IORING_OP_ASYNC_CANCEL: |
| 3420 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 3421 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3422 | default: |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3423 | if (io_req_op_valid(req->opcode)) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 3424 | return 1; |
| 3425 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3426 | } |
| 3427 | } |
| 3428 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3429 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 3430 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3431 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3432 | struct fixed_file_table *table; |
| 3433 | |
| 3434 | table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT]; |
| 3435 | return table->files[index & IORING_FILE_TABLE_MASK]; |
| 3436 | } |
| 3437 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3438 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 3439 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3440 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3441 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3442 | unsigned flags; |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 3443 | int fd, ret; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3444 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3445 | flags = READ_ONCE(sqe->flags); |
| 3446 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3447 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3448 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3449 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3450 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3451 | ret = io_req_needs_file(req); |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 3452 | if (ret <= 0) |
| 3453 | return ret; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3454 | |
| 3455 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3456 | if (unlikely(!ctx->file_table || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3457 | (unsigned) fd >= ctx->nr_user_files)) |
| 3458 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 3459 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3460 | req->file = io_file_from_index(ctx, fd); |
| 3461 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3462 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3463 | req->flags |= REQ_F_FIXED_FILE; |
| 3464 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 3465 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3466 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 3467 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3468 | req->file = io_file_get(state, fd); |
| 3469 | if (unlikely(!req->file)) |
| 3470 | return -EBADF; |
| 3471 | } |
| 3472 | |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3476 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3477 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 3478 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3479 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 3480 | |
| 3481 | rcu_read_lock(); |
| 3482 | spin_lock_irq(&ctx->inflight_lock); |
| 3483 | /* |
| 3484 | * We use the f_ops->flush() handler to ensure that we can flush |
| 3485 | * out work accessing these files if the fd is closed. Check if |
| 3486 | * the fd has changed since we started down this path, and disallow |
| 3487 | * this operation if it has. |
| 3488 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 3489 | if (fcheck(req->ring_fd) == req->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 3490 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 3491 | req->flags |= REQ_F_INFLIGHT; |
| 3492 | req->work.files = current->files; |
| 3493 | ret = 0; |
| 3494 | } |
| 3495 | spin_unlock_irq(&ctx->inflight_lock); |
| 3496 | rcu_read_unlock(); |
| 3497 | |
| 3498 | return ret; |
| 3499 | } |
| 3500 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3501 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 3502 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3503 | struct io_timeout_data *data = container_of(timer, |
| 3504 | struct io_timeout_data, timer); |
| 3505 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3506 | struct io_ring_ctx *ctx = req->ctx; |
| 3507 | struct io_kiocb *prev = NULL; |
| 3508 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3509 | |
| 3510 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3511 | |
| 3512 | /* |
| 3513 | * We don't expect the list to be empty, that will only happen if we |
| 3514 | * race with the completion of the linked work. |
| 3515 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 3516 | if (!list_empty(&req->link_list)) { |
| 3517 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 3518 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 3519 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 3520 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 3521 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 3522 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3523 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3527 | |
| 3528 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3529 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3530 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 3531 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3532 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3533 | } else { |
| 3534 | io_cqring_add_event(req, -ETIME); |
| 3535 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3536 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3537 | return HRTIMER_NORESTART; |
| 3538 | } |
| 3539 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3540 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3541 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3542 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3543 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3544 | /* |
| 3545 | * If the list is now empty, then our linked request finished before |
| 3546 | * we got a chance to setup the timer |
| 3547 | */ |
| 3548 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 3549 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3550 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3551 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3552 | data->timer.function = io_link_timeout_fn; |
| 3553 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 3554 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3555 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3556 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3557 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3558 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3559 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3560 | } |
| 3561 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3562 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3563 | { |
| 3564 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3565 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3566 | if (!(req->flags & REQ_F_LINK)) |
| 3567 | return NULL; |
| 3568 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 3569 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 3570 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3571 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3572 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3573 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3574 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3575 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3578 | 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] | 3579 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 3580 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 3581 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3582 | int ret; |
| 3583 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 3584 | again: |
| 3585 | linked_timeout = io_prep_linked_timeout(req); |
| 3586 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3587 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 3588 | |
| 3589 | /* |
| 3590 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 3591 | * doesn't support non-blocking read/write attempts |
| 3592 | */ |
| 3593 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 3594 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 3595 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 3596 | ret = io_grab_files(req); |
| 3597 | if (ret) |
| 3598 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3599 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 3600 | |
| 3601 | /* |
| 3602 | * Queued up for async execution, worker will release |
| 3603 | * submit reference when the iocb is actually submitted. |
| 3604 | */ |
| 3605 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 3606 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3607 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3608 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 3609 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3610 | /* drop submission reference */ |
| 3611 | io_put_req(req); |
| 3612 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 3613 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3614 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 3615 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3616 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 3617 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3620 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3621 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3622 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3623 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3624 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3625 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 3626 | done_req: |
| 3627 | if (nxt) { |
| 3628 | req = nxt; |
| 3629 | nxt = NULL; |
| 3630 | goto again; |
| 3631 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3634 | 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] | 3635 | { |
| 3636 | int ret; |
| 3637 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3638 | if (unlikely(req->ctx->drain_next)) { |
| 3639 | req->flags |= REQ_F_IO_DRAIN; |
| 3640 | req->ctx->drain_next = false; |
| 3641 | } |
| 3642 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK); |
| 3643 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3644 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3645 | if (ret) { |
| 3646 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3647 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3648 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3649 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3650 | } |
Jens Axboe | 0e0702d | 2019-11-14 21:42:10 -0700 | [diff] [blame] | 3651 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3652 | __io_queue_sqe(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3653 | } |
| 3654 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3655 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3656 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3657 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3658 | io_cqring_add_event(req, -ECANCELED); |
| 3659 | io_double_put_req(req); |
| 3660 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3661 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3662 | } |
| 3663 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3664 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 3665 | IOSQE_IO_HARDLINK) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3666 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3667 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3668 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3669 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3670 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3671 | int ret; |
| 3672 | |
| 3673 | /* enforce forwards compatibility on users */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3674 | if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3675 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3676 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3677 | } |
| 3678 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3679 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3680 | if (unlikely(ret)) { |
| 3681 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3682 | io_cqring_add_event(req, ret); |
| 3683 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 3684 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3685 | } |
| 3686 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3687 | /* |
| 3688 | * If we already have a head request, queue this one for async |
| 3689 | * submittal once the head completes. If we don't have a head but |
| 3690 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 3691 | * submitted sync once the chain is complete. If none of those |
| 3692 | * conditions are true (normal request), then just queue it. |
| 3693 | */ |
| 3694 | if (*link) { |
| 3695 | struct io_kiocb *prev = *link; |
| 3696 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3697 | if (sqe->flags & IOSQE_IO_DRAIN) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3698 | (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; |
| 3699 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3700 | if (sqe->flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3701 | req->flags |= REQ_F_HARDLINK; |
| 3702 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3703 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3704 | ret = -EAGAIN; |
| 3705 | goto err_req; |
| 3706 | } |
| 3707 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3708 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3709 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3710 | /* fail even hard links since we don't submit */ |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3711 | prev->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3712 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3713 | } |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 3714 | trace_io_uring_link(ctx, req, prev); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 3715 | list_add_tail(&req->link_list, &prev->link_list); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3716 | } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3717 | req->flags |= REQ_F_LINK; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3718 | if (sqe->flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3719 | req->flags |= REQ_F_HARDLINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3720 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3721 | INIT_LIST_HEAD(&req->link_list); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3722 | ret = io_req_defer_prep(req, sqe); |
| 3723 | if (ret) |
| 3724 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3725 | *link = req; |
| 3726 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3727 | io_queue_sqe(req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3728 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 3729 | |
| 3730 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3731 | } |
| 3732 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3733 | /* |
| 3734 | * Batched submission is done, ensure local IO is flushed out. |
| 3735 | */ |
| 3736 | static void io_submit_state_end(struct io_submit_state *state) |
| 3737 | { |
| 3738 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 3739 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 3740 | if (state->free_reqs) |
| 3741 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 3742 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | /* |
| 3746 | * Start submission side cache. |
| 3747 | */ |
| 3748 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 3749 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3750 | { |
| 3751 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 3752 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3753 | state->file = NULL; |
| 3754 | state->ios_left = max_ios; |
| 3755 | } |
| 3756 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3757 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 3758 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3759 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3760 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3761 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3762 | /* |
| 3763 | * Ensure any loads from the SQEs are done at this point, |
| 3764 | * since once we write the new head, the application could |
| 3765 | * write new data to them. |
| 3766 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3767 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3772 | * 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] | 3773 | * that is mapped by userspace. This means that care needs to be taken to |
| 3774 | * ensure that reads are stable, as we cannot rely on userspace always |
| 3775 | * being a good citizen. If members of the sqe are validated and then later |
| 3776 | * used, it's important that those reads are done through READ_ONCE() to |
| 3777 | * prevent a re-load down the line. |
| 3778 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3779 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 3780 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3781 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3782 | struct io_rings *rings = ctx->rings; |
| 3783 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3784 | unsigned head; |
| 3785 | |
| 3786 | /* |
| 3787 | * The cached sq head (or cq tail) serves two purposes: |
| 3788 | * |
| 3789 | * 1) allows us to batch the cost of updating the user visible |
| 3790 | * head updates. |
| 3791 | * 2) allows the kernel side to track the head on its own, even |
| 3792 | * though the application is the one updating it. |
| 3793 | */ |
| 3794 | head = ctx->cached_sq_head; |
Stefan Bühler | e523a29 | 2019-04-19 11:57:44 +0200 | [diff] [blame] | 3795 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 3796 | if (unlikely(head == smp_load_acquire(&rings->sq.tail))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3797 | return false; |
| 3798 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3799 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 3800 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 3801 | /* |
| 3802 | * All io need record the previous position, if LINK vs DARIN, |
| 3803 | * it can be used to mark the position of the first IO in the |
| 3804 | * link list. |
| 3805 | */ |
| 3806 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3807 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 3808 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 3809 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3810 | ctx->cached_sq_head++; |
| 3811 | return true; |
| 3812 | } |
| 3813 | |
| 3814 | /* drop invalid entries */ |
| 3815 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 3816 | ctx->cached_sq_dropped++; |
| 3817 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3818 | return false; |
| 3819 | } |
| 3820 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3821 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 3822 | struct file *ring_file, int ring_fd, |
| 3823 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3824 | { |
| 3825 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3826 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3827 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 3828 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3829 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 3830 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
| 3831 | if (!list_empty(&ctx->cq_overflow_list) && |
| 3832 | !io_cqring_overflow_flush(ctx, false)) |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3833 | return -EBUSY; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3834 | |
| 3835 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 3836 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3837 | statep = &state; |
| 3838 | } |
| 3839 | |
| 3840 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3841 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3842 | struct io_kiocb *req; |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3843 | unsigned int sqe_flags; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3844 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3845 | req = io_get_req(ctx, statep); |
| 3846 | if (unlikely(!req)) { |
| 3847 | if (!submitted) |
| 3848 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3849 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3850 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3851 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3852 | __io_free_req(req); |
| 3853 | break; |
| 3854 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3855 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3856 | if (io_req_needs_user(req) && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 3857 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 3858 | if (!mm_fault) { |
| 3859 | use_mm(ctx->sqo_mm); |
| 3860 | *mm = ctx->sqo_mm; |
| 3861 | } |
| 3862 | } |
| 3863 | |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 3864 | submitted++; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3865 | sqe_flags = sqe->flags; |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3866 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 3867 | req->ring_file = ring_file; |
| 3868 | req->ring_fd = ring_fd; |
| 3869 | req->has_user = *mm != NULL; |
| 3870 | req->in_async = async; |
| 3871 | req->needs_fixed_file = async; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3872 | trace_io_uring_submit_sqe(ctx, req->user_data, true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3873 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 3874 | break; |
Pavel Begunkov | e5eb636 | 2019-11-06 00:22:15 +0300 | [diff] [blame] | 3875 | /* |
| 3876 | * If previous wasn't linked and we have a linked command, |
| 3877 | * that's the end of the chain. Submit the previous link. |
| 3878 | */ |
Pavel Begunkov | ffbb8d6 | 2019-12-17 20:57:05 +0300 | [diff] [blame] | 3879 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3880 | io_queue_link_head(link); |
Pavel Begunkov | e5eb636 | 2019-11-06 00:22:15 +0300 | [diff] [blame] | 3881 | link = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3882 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3883 | } |
| 3884 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3885 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3886 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3887 | if (statep) |
| 3888 | io_submit_state_end(&state); |
| 3889 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 3890 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 3891 | io_commit_sqring(ctx); |
| 3892 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3893 | return submitted; |
| 3894 | } |
| 3895 | |
| 3896 | static int io_sq_thread(void *data) |
| 3897 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3898 | struct io_ring_ctx *ctx = data; |
| 3899 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 3900 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3901 | mm_segment_t old_fs; |
| 3902 | DEFINE_WAIT(wait); |
| 3903 | unsigned inflight; |
| 3904 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3905 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3906 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 3907 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 3908 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3909 | old_fs = get_fs(); |
| 3910 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 3911 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3912 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3913 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3914 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3915 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3916 | |
| 3917 | if (inflight) { |
| 3918 | unsigned nr_events = 0; |
| 3919 | |
| 3920 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 3921 | /* |
| 3922 | * inflight is the count of the maximum possible |
| 3923 | * entries we submitted, but it can be smaller |
| 3924 | * if we dropped some of them. If we don't have |
| 3925 | * poll entries available, then we know that we |
| 3926 | * have nothing left to poll for. Reset the |
| 3927 | * inflight count to zero in that case. |
| 3928 | */ |
| 3929 | mutex_lock(&ctx->uring_lock); |
| 3930 | if (!list_empty(&ctx->poll_list)) |
| 3931 | __io_iopoll_check(ctx, &nr_events, 0); |
| 3932 | else |
| 3933 | inflight = 0; |
| 3934 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3935 | } else { |
| 3936 | /* |
| 3937 | * Normal IO, just pretend everything completed. |
| 3938 | * We don't have to poll completions for that. |
| 3939 | */ |
| 3940 | nr_events = inflight; |
| 3941 | } |
| 3942 | |
| 3943 | inflight -= nr_events; |
| 3944 | if (!inflight) |
| 3945 | timeout = jiffies + ctx->sq_thread_idle; |
| 3946 | } |
| 3947 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3948 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3949 | |
| 3950 | /* |
| 3951 | * If submit got -EBUSY, flag us as needing the application |
| 3952 | * to enter the kernel to reap and flush events. |
| 3953 | */ |
| 3954 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3955 | /* |
| 3956 | * We're polling. If we're within the defined idle |
| 3957 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3958 | * to sleep. The exception is if we got EBUSY doing |
| 3959 | * more IO, we should wait for the application to |
| 3960 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3961 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3962 | if (inflight || |
| 3963 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 3964 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3965 | continue; |
| 3966 | } |
| 3967 | |
| 3968 | /* |
| 3969 | * Drop cur_mm before scheduling, we can't hold it for |
| 3970 | * long periods (or over schedule()). Do this before |
| 3971 | * adding ourselves to the waitqueue, as the unuse/drop |
| 3972 | * may sleep. |
| 3973 | */ |
| 3974 | if (cur_mm) { |
| 3975 | unuse_mm(cur_mm); |
| 3976 | mmput(cur_mm); |
| 3977 | cur_mm = NULL; |
| 3978 | } |
| 3979 | |
| 3980 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 3981 | TASK_INTERRUPTIBLE); |
| 3982 | |
| 3983 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3984 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 3985 | /* make sure to read SQ tail after writing flags */ |
| 3986 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3987 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3988 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3989 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3990 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3991 | finish_wait(&ctx->sqo_wait, &wait); |
| 3992 | break; |
| 3993 | } |
| 3994 | if (signal_pending(current)) |
| 3995 | flush_signals(current); |
| 3996 | schedule(); |
| 3997 | finish_wait(&ctx->sqo_wait, &wait); |
| 3998 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3999 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4000 | continue; |
| 4001 | } |
| 4002 | finish_wait(&ctx->sqo_wait, &wait); |
| 4003 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4004 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4007 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4008 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4009 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4010 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4011 | if (ret > 0) |
| 4012 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | set_fs(old_fs); |
| 4016 | if (cur_mm) { |
| 4017 | unuse_mm(cur_mm); |
| 4018 | mmput(cur_mm); |
| 4019 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4020 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4021 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4022 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4023 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4024 | return 0; |
| 4025 | } |
| 4026 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4027 | struct io_wait_queue { |
| 4028 | struct wait_queue_entry wq; |
| 4029 | struct io_ring_ctx *ctx; |
| 4030 | unsigned to_wait; |
| 4031 | unsigned nr_timeouts; |
| 4032 | }; |
| 4033 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4034 | 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] | 4035 | { |
| 4036 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4037 | |
| 4038 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4039 | * 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] | 4040 | * started waiting. For timeouts, we always want to return to userspace, |
| 4041 | * regardless of event count. |
| 4042 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4043 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4044 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4045 | } |
| 4046 | |
| 4047 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4048 | int wake_flags, void *key) |
| 4049 | { |
| 4050 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4051 | wq); |
| 4052 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4053 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4054 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4055 | return -1; |
| 4056 | |
| 4057 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 4058 | } |
| 4059 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4060 | /* |
| 4061 | * Wait until events become available, if we don't already have some. The |
| 4062 | * application must reap them itself, as they reside on the shared cq ring. |
| 4063 | */ |
| 4064 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 4065 | const sigset_t __user *sig, size_t sigsz) |
| 4066 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4067 | struct io_wait_queue iowq = { |
| 4068 | .wq = { |
| 4069 | .private = current, |
| 4070 | .func = io_wake_function, |
| 4071 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 4072 | }, |
| 4073 | .ctx = ctx, |
| 4074 | .to_wait = min_events, |
| 4075 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4076 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4077 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4078 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4079 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4080 | return 0; |
| 4081 | |
| 4082 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4083 | #ifdef CONFIG_COMPAT |
| 4084 | if (in_compat_syscall()) |
| 4085 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4086 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4087 | else |
| 4088 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4089 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4090 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4091 | if (ret) |
| 4092 | return ret; |
| 4093 | } |
| 4094 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4095 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4096 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4097 | do { |
| 4098 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 4099 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4100 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4101 | break; |
| 4102 | schedule(); |
| 4103 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4104 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4105 | break; |
| 4106 | } |
| 4107 | } while (1); |
| 4108 | finish_wait(&ctx->wait, &iowq.wq); |
| 4109 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4110 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4111 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4112 | 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] | 4113 | } |
| 4114 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4115 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 4116 | { |
| 4117 | #if defined(CONFIG_UNIX) |
| 4118 | if (ctx->ring_sock) { |
| 4119 | struct sock *sock = ctx->ring_sock->sk; |
| 4120 | struct sk_buff *skb; |
| 4121 | |
| 4122 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 4123 | kfree_skb(skb); |
| 4124 | } |
| 4125 | #else |
| 4126 | int i; |
| 4127 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4128 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 4129 | struct file *file; |
| 4130 | |
| 4131 | file = io_file_from_index(ctx, i); |
| 4132 | if (file) |
| 4133 | fput(file); |
| 4134 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4135 | #endif |
| 4136 | } |
| 4137 | |
| 4138 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 4139 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4140 | unsigned nr_tables, i; |
| 4141 | |
| 4142 | if (!ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4143 | return -ENXIO; |
| 4144 | |
| 4145 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4146 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 4147 | for (i = 0; i < nr_tables; i++) |
| 4148 | kfree(ctx->file_table[i].files); |
| 4149 | kfree(ctx->file_table); |
| 4150 | ctx->file_table = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4151 | ctx->nr_user_files = 0; |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4155 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 4156 | { |
| 4157 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4158 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4159 | /* |
| 4160 | * The park is a bit of a work-around, without it we get |
| 4161 | * warning spews on shutdown with SQPOLL set and affinity |
| 4162 | * set to a single CPU. |
| 4163 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4164 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4165 | kthread_stop(ctx->sqo_thread); |
| 4166 | ctx->sqo_thread = NULL; |
| 4167 | } |
| 4168 | } |
| 4169 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4170 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 4171 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4172 | io_sq_thread_stop(ctx); |
| 4173 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4174 | if (ctx->io_wq) { |
| 4175 | io_wq_destroy(ctx->io_wq); |
| 4176 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | #if defined(CONFIG_UNIX) |
| 4181 | static void io_destruct_skb(struct sk_buff *skb) |
| 4182 | { |
| 4183 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; |
| 4184 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4185 | if (ctx->io_wq) |
| 4186 | io_wq_flush(ctx->io_wq); |
Jens Axboe | 8a99734 | 2019-10-09 14:40:13 -0600 | [diff] [blame] | 4187 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4188 | unix_destruct_scm(skb); |
| 4189 | } |
| 4190 | |
| 4191 | /* |
| 4192 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 4193 | * the io_uring can be safely unregistered on process exit, even if we have |
| 4194 | * loops in the file referencing. |
| 4195 | */ |
| 4196 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 4197 | { |
| 4198 | struct sock *sk = ctx->ring_sock->sk; |
| 4199 | struct scm_fp_list *fpl; |
| 4200 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4201 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4202 | |
| 4203 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 4204 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 4205 | |
| 4206 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 4207 | return -EMFILE; |
| 4208 | } |
| 4209 | |
| 4210 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 4211 | if (!fpl) |
| 4212 | return -ENOMEM; |
| 4213 | |
| 4214 | skb = alloc_skb(0, GFP_KERNEL); |
| 4215 | if (!skb) { |
| 4216 | kfree(fpl); |
| 4217 | return -ENOMEM; |
| 4218 | } |
| 4219 | |
| 4220 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4221 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4222 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4223 | fpl->user = get_uid(ctx->user); |
| 4224 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4225 | struct file *file = io_file_from_index(ctx, i + offset); |
| 4226 | |
| 4227 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4228 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4229 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4230 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 4231 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4234 | if (nr_files) { |
| 4235 | fpl->max = SCM_MAX_FD; |
| 4236 | fpl->count = nr_files; |
| 4237 | UNIXCB(skb).fp = fpl; |
| 4238 | skb->destructor = io_destruct_skb; |
| 4239 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 4240 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4241 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4242 | for (i = 0; i < nr_files; i++) |
| 4243 | fput(fpl->fp[i]); |
| 4244 | } else { |
| 4245 | kfree_skb(skb); |
| 4246 | kfree(fpl); |
| 4247 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4248 | |
| 4249 | return 0; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 4254 | * causes regular reference counting to break down. We rely on the UNIX |
| 4255 | * garbage collection to take care of this problem for us. |
| 4256 | */ |
| 4257 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 4258 | { |
| 4259 | unsigned left, total; |
| 4260 | int ret = 0; |
| 4261 | |
| 4262 | total = 0; |
| 4263 | left = ctx->nr_user_files; |
| 4264 | while (left) { |
| 4265 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4266 | |
| 4267 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 4268 | if (ret) |
| 4269 | break; |
| 4270 | left -= this_files; |
| 4271 | total += this_files; |
| 4272 | } |
| 4273 | |
| 4274 | if (!ret) |
| 4275 | return 0; |
| 4276 | |
| 4277 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4278 | struct file *file = io_file_from_index(ctx, total); |
| 4279 | |
| 4280 | if (file) |
| 4281 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4282 | total++; |
| 4283 | } |
| 4284 | |
| 4285 | return ret; |
| 4286 | } |
| 4287 | #else |
| 4288 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 4289 | { |
| 4290 | return 0; |
| 4291 | } |
| 4292 | #endif |
| 4293 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4294 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 4295 | unsigned nr_files) |
| 4296 | { |
| 4297 | int i; |
| 4298 | |
| 4299 | for (i = 0; i < nr_tables; i++) { |
| 4300 | struct fixed_file_table *table = &ctx->file_table[i]; |
| 4301 | unsigned this_files; |
| 4302 | |
| 4303 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 4304 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 4305 | GFP_KERNEL); |
| 4306 | if (!table->files) |
| 4307 | break; |
| 4308 | nr_files -= this_files; |
| 4309 | } |
| 4310 | |
| 4311 | if (i == nr_tables) |
| 4312 | return 0; |
| 4313 | |
| 4314 | for (i = 0; i < nr_tables; i++) { |
| 4315 | struct fixed_file_table *table = &ctx->file_table[i]; |
| 4316 | kfree(table->files); |
| 4317 | } |
| 4318 | return 1; |
| 4319 | } |
| 4320 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4321 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 4322 | unsigned nr_args) |
| 4323 | { |
| 4324 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4325 | unsigned nr_tables; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4326 | int fd, ret = 0; |
| 4327 | unsigned i; |
| 4328 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4329 | if (ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4330 | return -EBUSY; |
| 4331 | if (!nr_args) |
| 4332 | return -EINVAL; |
| 4333 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 4334 | return -EMFILE; |
| 4335 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4336 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 4337 | ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table), |
| 4338 | GFP_KERNEL); |
| 4339 | if (!ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4340 | return -ENOMEM; |
| 4341 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4342 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 4343 | kfree(ctx->file_table); |
Jens Axboe | 46568e9 | 2019-11-10 08:40:53 -0700 | [diff] [blame] | 4344 | ctx->file_table = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4345 | return -ENOMEM; |
| 4346 | } |
| 4347 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4348 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4349 | struct fixed_file_table *table; |
| 4350 | unsigned index; |
| 4351 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4352 | ret = -EFAULT; |
| 4353 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 4354 | break; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4355 | /* allow sparse sets */ |
| 4356 | if (fd == -1) { |
| 4357 | ret = 0; |
| 4358 | continue; |
| 4359 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4360 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4361 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
| 4362 | index = i & IORING_FILE_TABLE_MASK; |
| 4363 | table->files[index] = fget(fd); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4364 | |
| 4365 | ret = -EBADF; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4366 | if (!table->files[index]) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4367 | break; |
| 4368 | /* |
| 4369 | * Don't allow io_uring instances to be registered. If UNIX |
| 4370 | * isn't enabled, then this causes a reference cycle and this |
| 4371 | * instance can never get freed. If UNIX is enabled we'll |
| 4372 | * handle it just fine, but there's still no point in allowing |
| 4373 | * a ring fd as it doesn't support regular read/write anyway. |
| 4374 | */ |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4375 | if (table->files[index]->f_op == &io_uring_fops) { |
| 4376 | fput(table->files[index]); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4377 | break; |
| 4378 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4379 | ret = 0; |
| 4380 | } |
| 4381 | |
| 4382 | if (ret) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4383 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 4384 | struct file *file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4385 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4386 | file = io_file_from_index(ctx, i); |
| 4387 | if (file) |
| 4388 | fput(file); |
| 4389 | } |
| 4390 | for (i = 0; i < nr_tables; i++) |
| 4391 | kfree(ctx->file_table[i].files); |
| 4392 | |
| 4393 | kfree(ctx->file_table); |
| 4394 | ctx->file_table = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4395 | ctx->nr_user_files = 0; |
| 4396 | return ret; |
| 4397 | } |
| 4398 | |
| 4399 | ret = io_sqe_files_scm(ctx); |
| 4400 | if (ret) |
| 4401 | io_sqe_files_unregister(ctx); |
| 4402 | |
| 4403 | return ret; |
| 4404 | } |
| 4405 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4406 | static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index) |
| 4407 | { |
| 4408 | #if defined(CONFIG_UNIX) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4409 | struct file *file = io_file_from_index(ctx, index); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4410 | struct sock *sock = ctx->ring_sock->sk; |
| 4411 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 4412 | struct sk_buff *skb; |
| 4413 | int i; |
| 4414 | |
| 4415 | __skb_queue_head_init(&list); |
| 4416 | |
| 4417 | /* |
| 4418 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 4419 | * remove this entry and rearrange the file array. |
| 4420 | */ |
| 4421 | skb = skb_dequeue(head); |
| 4422 | while (skb) { |
| 4423 | struct scm_fp_list *fp; |
| 4424 | |
| 4425 | fp = UNIXCB(skb).fp; |
| 4426 | for (i = 0; i < fp->count; i++) { |
| 4427 | int left; |
| 4428 | |
| 4429 | if (fp->fp[i] != file) |
| 4430 | continue; |
| 4431 | |
| 4432 | unix_notinflight(fp->user, fp->fp[i]); |
| 4433 | left = fp->count - 1 - i; |
| 4434 | if (left) { |
| 4435 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 4436 | left * sizeof(struct file *)); |
| 4437 | } |
| 4438 | fp->count--; |
| 4439 | if (!fp->count) { |
| 4440 | kfree_skb(skb); |
| 4441 | skb = NULL; |
| 4442 | } else { |
| 4443 | __skb_queue_tail(&list, skb); |
| 4444 | } |
| 4445 | fput(file); |
| 4446 | file = NULL; |
| 4447 | break; |
| 4448 | } |
| 4449 | |
| 4450 | if (!file) |
| 4451 | break; |
| 4452 | |
| 4453 | __skb_queue_tail(&list, skb); |
| 4454 | |
| 4455 | skb = skb_dequeue(head); |
| 4456 | } |
| 4457 | |
| 4458 | if (skb_peek(&list)) { |
| 4459 | spin_lock_irq(&head->lock); |
| 4460 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 4461 | __skb_queue_tail(head, skb); |
| 4462 | spin_unlock_irq(&head->lock); |
| 4463 | } |
| 4464 | #else |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4465 | fput(io_file_from_index(ctx, index)); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4466 | #endif |
| 4467 | } |
| 4468 | |
| 4469 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 4470 | int index) |
| 4471 | { |
| 4472 | #if defined(CONFIG_UNIX) |
| 4473 | struct sock *sock = ctx->ring_sock->sk; |
| 4474 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 4475 | struct sk_buff *skb; |
| 4476 | |
| 4477 | /* |
| 4478 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 4479 | * file set. If there's no room, fall back to allocating a new skb |
| 4480 | * and filling it in. |
| 4481 | */ |
| 4482 | spin_lock_irq(&head->lock); |
| 4483 | skb = skb_peek(head); |
| 4484 | if (skb) { |
| 4485 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 4486 | |
| 4487 | if (fpl->count < SCM_MAX_FD) { |
| 4488 | __skb_unlink(skb, head); |
| 4489 | spin_unlock_irq(&head->lock); |
| 4490 | fpl->fp[fpl->count] = get_file(file); |
| 4491 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 4492 | fpl->count++; |
| 4493 | spin_lock_irq(&head->lock); |
| 4494 | __skb_queue_head(head, skb); |
| 4495 | } else { |
| 4496 | skb = NULL; |
| 4497 | } |
| 4498 | } |
| 4499 | spin_unlock_irq(&head->lock); |
| 4500 | |
| 4501 | if (skb) { |
| 4502 | fput(file); |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
| 4506 | return __io_sqe_files_scm(ctx, 1, index); |
| 4507 | #else |
| 4508 | return 0; |
| 4509 | #endif |
| 4510 | } |
| 4511 | |
| 4512 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 4513 | unsigned nr_args) |
| 4514 | { |
| 4515 | struct io_uring_files_update up; |
| 4516 | __s32 __user *fds; |
| 4517 | int fd, i, err; |
| 4518 | __u32 done; |
| 4519 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4520 | if (!ctx->file_table) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4521 | return -ENXIO; |
| 4522 | if (!nr_args) |
| 4523 | return -EINVAL; |
| 4524 | if (copy_from_user(&up, arg, sizeof(up))) |
| 4525 | return -EFAULT; |
Eugene Syromiatnikov | 1292e97 | 2020-01-15 17:35:38 +0100 | [diff] [blame] | 4526 | if (up.resv) |
| 4527 | return -EINVAL; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4528 | if (check_add_overflow(up.offset, nr_args, &done)) |
| 4529 | return -EOVERFLOW; |
| 4530 | if (done > ctx->nr_user_files) |
| 4531 | return -EINVAL; |
| 4532 | |
| 4533 | done = 0; |
Eugene Syromiatnikov | 1292e97 | 2020-01-15 17:35:38 +0100 | [diff] [blame] | 4534 | fds = u64_to_user_ptr(up.fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4535 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4536 | struct fixed_file_table *table; |
| 4537 | unsigned index; |
| 4538 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4539 | err = 0; |
| 4540 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 4541 | err = -EFAULT; |
| 4542 | break; |
| 4543 | } |
| 4544 | i = array_index_nospec(up.offset, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4545 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
| 4546 | index = i & IORING_FILE_TABLE_MASK; |
| 4547 | if (table->files[index]) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4548 | io_sqe_file_unregister(ctx, i); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4549 | table->files[index] = NULL; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4550 | } |
| 4551 | if (fd != -1) { |
| 4552 | struct file *file; |
| 4553 | |
| 4554 | file = fget(fd); |
| 4555 | if (!file) { |
| 4556 | err = -EBADF; |
| 4557 | break; |
| 4558 | } |
| 4559 | /* |
| 4560 | * Don't allow io_uring instances to be registered. If |
| 4561 | * UNIX isn't enabled, then this causes a reference |
| 4562 | * cycle and this instance can never get freed. If UNIX |
| 4563 | * is enabled we'll handle it just fine, but there's |
| 4564 | * still no point in allowing a ring fd as it doesn't |
| 4565 | * support regular read/write anyway. |
| 4566 | */ |
| 4567 | if (file->f_op == &io_uring_fops) { |
| 4568 | fput(file); |
| 4569 | err = -EBADF; |
| 4570 | break; |
| 4571 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4572 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4573 | err = io_sqe_file_register(ctx, file, i); |
| 4574 | if (err) |
| 4575 | break; |
| 4576 | } |
| 4577 | nr_args--; |
| 4578 | done++; |
| 4579 | up.offset++; |
| 4580 | } |
| 4581 | |
| 4582 | return done ? done : err; |
| 4583 | } |
| 4584 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 4585 | static void io_put_work(struct io_wq_work *work) |
| 4586 | { |
| 4587 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 4588 | |
| 4589 | io_put_req(req); |
| 4590 | } |
| 4591 | |
| 4592 | static void io_get_work(struct io_wq_work *work) |
| 4593 | { |
| 4594 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 4595 | |
| 4596 | refcount_inc(&req->refs); |
| 4597 | } |
| 4598 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4599 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 4600 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4601 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4602 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4603 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4604 | int ret; |
| 4605 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4606 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4607 | mmgrab(current->mm); |
| 4608 | ctx->sqo_mm = current->mm; |
| 4609 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4610 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 4611 | ret = -EPERM; |
| 4612 | if (!capable(CAP_SYS_ADMIN)) |
| 4613 | goto err; |
| 4614 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 4615 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 4616 | if (!ctx->sq_thread_idle) |
| 4617 | ctx->sq_thread_idle = HZ; |
| 4618 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4619 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 4620 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4621 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 4622 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 4623 | if (cpu >= nr_cpu_ids) |
| 4624 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 4625 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 4626 | goto err; |
| 4627 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4628 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 4629 | ctx, cpu, |
| 4630 | "io_uring-sq"); |
| 4631 | } else { |
| 4632 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 4633 | "io_uring-sq"); |
| 4634 | } |
| 4635 | if (IS_ERR(ctx->sqo_thread)) { |
| 4636 | ret = PTR_ERR(ctx->sqo_thread); |
| 4637 | ctx->sqo_thread = NULL; |
| 4638 | goto err; |
| 4639 | } |
| 4640 | wake_up_process(ctx->sqo_thread); |
| 4641 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 4642 | /* Can't have SQ_AFF without SQPOLL */ |
| 4643 | ret = -EINVAL; |
| 4644 | goto err; |
| 4645 | } |
| 4646 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4647 | data.mm = ctx->sqo_mm; |
| 4648 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4649 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4650 | data.get_work = io_get_work; |
| 4651 | data.put_work = io_put_work; |
| 4652 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4653 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 4654 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4655 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 4656 | if (IS_ERR(ctx->io_wq)) { |
| 4657 | ret = PTR_ERR(ctx->io_wq); |
| 4658 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4659 | goto err; |
| 4660 | } |
| 4661 | |
| 4662 | return 0; |
| 4663 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 4664 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4665 | mmdrop(ctx->sqo_mm); |
| 4666 | ctx->sqo_mm = NULL; |
| 4667 | return ret; |
| 4668 | } |
| 4669 | |
| 4670 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 4671 | { |
| 4672 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 4673 | } |
| 4674 | |
| 4675 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 4676 | { |
| 4677 | unsigned long page_limit, cur_pages, new_pages; |
| 4678 | |
| 4679 | /* Don't allow more pages than we can safely lock */ |
| 4680 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 4681 | |
| 4682 | do { |
| 4683 | cur_pages = atomic_long_read(&user->locked_vm); |
| 4684 | new_pages = cur_pages + nr_pages; |
| 4685 | if (new_pages > page_limit) |
| 4686 | return -ENOMEM; |
| 4687 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 4688 | new_pages) != cur_pages); |
| 4689 | |
| 4690 | return 0; |
| 4691 | } |
| 4692 | |
| 4693 | static void io_mem_free(void *ptr) |
| 4694 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 4695 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4696 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 4697 | if (!ptr) |
| 4698 | return; |
| 4699 | |
| 4700 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4701 | if (put_page_testzero(page)) |
| 4702 | free_compound_page(page); |
| 4703 | } |
| 4704 | |
| 4705 | static void *io_mem_alloc(size_t size) |
| 4706 | { |
| 4707 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 4708 | __GFP_NORETRY; |
| 4709 | |
| 4710 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 4711 | } |
| 4712 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4713 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 4714 | size_t *sq_offset) |
| 4715 | { |
| 4716 | struct io_rings *rings; |
| 4717 | size_t off, sq_array_size; |
| 4718 | |
| 4719 | off = struct_size(rings, cqes, cq_entries); |
| 4720 | if (off == SIZE_MAX) |
| 4721 | return SIZE_MAX; |
| 4722 | |
| 4723 | #ifdef CONFIG_SMP |
| 4724 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 4725 | if (off == 0) |
| 4726 | return SIZE_MAX; |
| 4727 | #endif |
| 4728 | |
| 4729 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 4730 | if (sq_array_size == SIZE_MAX) |
| 4731 | return SIZE_MAX; |
| 4732 | |
| 4733 | if (check_add_overflow(off, sq_array_size, &off)) |
| 4734 | return SIZE_MAX; |
| 4735 | |
| 4736 | if (sq_offset) |
| 4737 | *sq_offset = off; |
| 4738 | |
| 4739 | return off; |
| 4740 | } |
| 4741 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4742 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 4743 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4744 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4745 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4746 | pages = (size_t)1 << get_order( |
| 4747 | rings_size(sq_entries, cq_entries, NULL)); |
| 4748 | pages += (size_t)1 << get_order( |
| 4749 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4750 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4751 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4754 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 4755 | { |
| 4756 | int i, j; |
| 4757 | |
| 4758 | if (!ctx->user_bufs) |
| 4759 | return -ENXIO; |
| 4760 | |
| 4761 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 4762 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 4763 | |
| 4764 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 4765 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4766 | |
| 4767 | if (ctx->account_mem) |
| 4768 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4769 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4770 | imu->nr_bvecs = 0; |
| 4771 | } |
| 4772 | |
| 4773 | kfree(ctx->user_bufs); |
| 4774 | ctx->user_bufs = NULL; |
| 4775 | ctx->nr_user_bufs = 0; |
| 4776 | return 0; |
| 4777 | } |
| 4778 | |
| 4779 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 4780 | void __user *arg, unsigned index) |
| 4781 | { |
| 4782 | struct iovec __user *src; |
| 4783 | |
| 4784 | #ifdef CONFIG_COMPAT |
| 4785 | if (ctx->compat) { |
| 4786 | struct compat_iovec __user *ciovs; |
| 4787 | struct compat_iovec ciov; |
| 4788 | |
| 4789 | ciovs = (struct compat_iovec __user *) arg; |
| 4790 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 4791 | return -EFAULT; |
| 4792 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4793 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4794 | dst->iov_len = ciov.iov_len; |
| 4795 | return 0; |
| 4796 | } |
| 4797 | #endif |
| 4798 | src = (struct iovec __user *) arg; |
| 4799 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 4800 | return -EFAULT; |
| 4801 | return 0; |
| 4802 | } |
| 4803 | |
| 4804 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 4805 | unsigned nr_args) |
| 4806 | { |
| 4807 | struct vm_area_struct **vmas = NULL; |
| 4808 | struct page **pages = NULL; |
| 4809 | int i, j, got_pages = 0; |
| 4810 | int ret = -EINVAL; |
| 4811 | |
| 4812 | if (ctx->user_bufs) |
| 4813 | return -EBUSY; |
| 4814 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 4815 | return -EINVAL; |
| 4816 | |
| 4817 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 4818 | GFP_KERNEL); |
| 4819 | if (!ctx->user_bufs) |
| 4820 | return -ENOMEM; |
| 4821 | |
| 4822 | for (i = 0; i < nr_args; i++) { |
| 4823 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 4824 | unsigned long off, start, end, ubuf; |
| 4825 | int pret, nr_pages; |
| 4826 | struct iovec iov; |
| 4827 | size_t size; |
| 4828 | |
| 4829 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 4830 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 4831 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4832 | |
| 4833 | /* |
| 4834 | * Don't impose further limits on the size and buffer |
| 4835 | * constraints here, we'll -EINVAL later when IO is |
| 4836 | * submitted if they are wrong. |
| 4837 | */ |
| 4838 | ret = -EFAULT; |
| 4839 | if (!iov.iov_base || !iov.iov_len) |
| 4840 | goto err; |
| 4841 | |
| 4842 | /* arbitrary limit, but we need something */ |
| 4843 | if (iov.iov_len > SZ_1G) |
| 4844 | goto err; |
| 4845 | |
| 4846 | ubuf = (unsigned long) iov.iov_base; |
| 4847 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 4848 | start = ubuf >> PAGE_SHIFT; |
| 4849 | nr_pages = end - start; |
| 4850 | |
| 4851 | if (ctx->account_mem) { |
| 4852 | ret = io_account_mem(ctx->user, nr_pages); |
| 4853 | if (ret) |
| 4854 | goto err; |
| 4855 | } |
| 4856 | |
| 4857 | ret = 0; |
| 4858 | if (!pages || nr_pages > got_pages) { |
| 4859 | kfree(vmas); |
| 4860 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4861 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4862 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4863 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4864 | sizeof(struct vm_area_struct *), |
| 4865 | GFP_KERNEL); |
| 4866 | if (!pages || !vmas) { |
| 4867 | ret = -ENOMEM; |
| 4868 | if (ctx->account_mem) |
| 4869 | io_unaccount_mem(ctx->user, nr_pages); |
| 4870 | goto err; |
| 4871 | } |
| 4872 | got_pages = nr_pages; |
| 4873 | } |
| 4874 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4875 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4876 | GFP_KERNEL); |
| 4877 | ret = -ENOMEM; |
| 4878 | if (!imu->bvec) { |
| 4879 | if (ctx->account_mem) |
| 4880 | io_unaccount_mem(ctx->user, nr_pages); |
| 4881 | goto err; |
| 4882 | } |
| 4883 | |
| 4884 | ret = 0; |
| 4885 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 4886 | pret = get_user_pages(ubuf, nr_pages, |
| 4887 | FOLL_WRITE | FOLL_LONGTERM, |
| 4888 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4889 | if (pret == nr_pages) { |
| 4890 | /* don't support file backed memory */ |
| 4891 | for (j = 0; j < nr_pages; j++) { |
| 4892 | struct vm_area_struct *vma = vmas[j]; |
| 4893 | |
| 4894 | if (vma->vm_file && |
| 4895 | !is_file_hugepages(vma->vm_file)) { |
| 4896 | ret = -EOPNOTSUPP; |
| 4897 | break; |
| 4898 | } |
| 4899 | } |
| 4900 | } else { |
| 4901 | ret = pret < 0 ? pret : -EFAULT; |
| 4902 | } |
| 4903 | up_read(¤t->mm->mmap_sem); |
| 4904 | if (ret) { |
| 4905 | /* |
| 4906 | * if we did partial map, or found file backed vmas, |
| 4907 | * release any pages we did get |
| 4908 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 4909 | if (pret > 0) |
| 4910 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4911 | if (ctx->account_mem) |
| 4912 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4913 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4914 | goto err; |
| 4915 | } |
| 4916 | |
| 4917 | off = ubuf & ~PAGE_MASK; |
| 4918 | size = iov.iov_len; |
| 4919 | for (j = 0; j < nr_pages; j++) { |
| 4920 | size_t vec_len; |
| 4921 | |
| 4922 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 4923 | imu->bvec[j].bv_page = pages[j]; |
| 4924 | imu->bvec[j].bv_len = vec_len; |
| 4925 | imu->bvec[j].bv_offset = off; |
| 4926 | off = 0; |
| 4927 | size -= vec_len; |
| 4928 | } |
| 4929 | /* store original address for later verification */ |
| 4930 | imu->ubuf = ubuf; |
| 4931 | imu->len = iov.iov_len; |
| 4932 | imu->nr_bvecs = nr_pages; |
| 4933 | |
| 4934 | ctx->nr_user_bufs++; |
| 4935 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4936 | kvfree(pages); |
| 4937 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4938 | return 0; |
| 4939 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4940 | kvfree(pages); |
| 4941 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4942 | io_sqe_buffer_unregister(ctx); |
| 4943 | return ret; |
| 4944 | } |
| 4945 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 4946 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 4947 | { |
| 4948 | __s32 __user *fds = arg; |
| 4949 | int fd; |
| 4950 | |
| 4951 | if (ctx->cq_ev_fd) |
| 4952 | return -EBUSY; |
| 4953 | |
| 4954 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 4955 | return -EFAULT; |
| 4956 | |
| 4957 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 4958 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 4959 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 4960 | ctx->cq_ev_fd = NULL; |
| 4961 | return ret; |
| 4962 | } |
| 4963 | |
| 4964 | return 0; |
| 4965 | } |
| 4966 | |
| 4967 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 4968 | { |
| 4969 | if (ctx->cq_ev_fd) { |
| 4970 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 4971 | ctx->cq_ev_fd = NULL; |
| 4972 | return 0; |
| 4973 | } |
| 4974 | |
| 4975 | return -ENXIO; |
| 4976 | } |
| 4977 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4978 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 4979 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4980 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4981 | if (ctx->sqo_mm) |
| 4982 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4983 | |
| 4984 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4985 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4986 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 4987 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4988 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4989 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 4990 | if (ctx->ring_sock) { |
| 4991 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4992 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 4993 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4994 | #endif |
| 4995 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4996 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4997 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4998 | |
| 4999 | percpu_ref_exit(&ctx->refs); |
| 5000 | if (ctx->account_mem) |
| 5001 | io_unaccount_mem(ctx->user, |
| 5002 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 5003 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5004 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5005 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5006 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 5007 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5008 | kfree(ctx); |
| 5009 | } |
| 5010 | |
| 5011 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 5012 | { |
| 5013 | struct io_ring_ctx *ctx = file->private_data; |
| 5014 | __poll_t mask = 0; |
| 5015 | |
| 5016 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 5017 | /* |
| 5018 | * synchronizes with barrier from wq_has_sleeper call in |
| 5019 | * io_commit_cqring |
| 5020 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5021 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5022 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 5023 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5024 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 5025 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5026 | mask |= EPOLLIN | EPOLLRDNORM; |
| 5027 | |
| 5028 | return mask; |
| 5029 | } |
| 5030 | |
| 5031 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 5032 | { |
| 5033 | struct io_ring_ctx *ctx = file->private_data; |
| 5034 | |
| 5035 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 5036 | } |
| 5037 | |
| 5038 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 5039 | { |
| 5040 | mutex_lock(&ctx->uring_lock); |
| 5041 | percpu_ref_kill(&ctx->refs); |
| 5042 | mutex_unlock(&ctx->uring_lock); |
| 5043 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5044 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5045 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5046 | |
| 5047 | if (ctx->io_wq) |
| 5048 | io_wq_cancel_all(ctx->io_wq); |
| 5049 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5050 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 5051 | /* if we failed setting up the ctx, we might not have any rings */ |
| 5052 | if (ctx->rings) |
| 5053 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5054 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5055 | io_ring_ctx_free(ctx); |
| 5056 | } |
| 5057 | |
| 5058 | static int io_uring_release(struct inode *inode, struct file *file) |
| 5059 | { |
| 5060 | struct io_ring_ctx *ctx = file->private_data; |
| 5061 | |
| 5062 | file->private_data = NULL; |
| 5063 | io_ring_ctx_wait_and_kill(ctx); |
| 5064 | return 0; |
| 5065 | } |
| 5066 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5067 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 5068 | struct files_struct *files) |
| 5069 | { |
| 5070 | struct io_kiocb *req; |
| 5071 | DEFINE_WAIT(wait); |
| 5072 | |
| 5073 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5074 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5075 | |
| 5076 | spin_lock_irq(&ctx->inflight_lock); |
| 5077 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5078 | if (req->work.files != files) |
| 5079 | continue; |
| 5080 | /* req is being completed, ignore */ |
| 5081 | if (!refcount_inc_not_zero(&req->refs)) |
| 5082 | continue; |
| 5083 | cancel_req = req; |
| 5084 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5085 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5086 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5087 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5088 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5089 | spin_unlock_irq(&ctx->inflight_lock); |
| 5090 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5091 | /* We need to keep going until we don't find a matching req */ |
| 5092 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5093 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 5094 | |
| 5095 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 5096 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5097 | schedule(); |
| 5098 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5099 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | static int io_uring_flush(struct file *file, void *data) |
| 5103 | { |
| 5104 | struct io_ring_ctx *ctx = file->private_data; |
| 5105 | |
| 5106 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5107 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 5108 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5109 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5110 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5111 | return 0; |
| 5112 | } |
| 5113 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5114 | static void *io_uring_validate_mmap_request(struct file *file, |
| 5115 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5116 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5117 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5118 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5119 | struct page *page; |
| 5120 | void *ptr; |
| 5121 | |
| 5122 | switch (offset) { |
| 5123 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5124 | case IORING_OFF_CQ_RING: |
| 5125 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5126 | break; |
| 5127 | case IORING_OFF_SQES: |
| 5128 | ptr = ctx->sq_sqes; |
| 5129 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5130 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5131 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 5135 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5136 | return ERR_PTR(-EINVAL); |
| 5137 | |
| 5138 | return ptr; |
| 5139 | } |
| 5140 | |
| 5141 | #ifdef CONFIG_MMU |
| 5142 | |
| 5143 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 5144 | { |
| 5145 | size_t sz = vma->vm_end - vma->vm_start; |
| 5146 | unsigned long pfn; |
| 5147 | void *ptr; |
| 5148 | |
| 5149 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 5150 | if (IS_ERR(ptr)) |
| 5151 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5152 | |
| 5153 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 5154 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 5155 | } |
| 5156 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5157 | #else /* !CONFIG_MMU */ |
| 5158 | |
| 5159 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 5160 | { |
| 5161 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 5162 | } |
| 5163 | |
| 5164 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 5165 | { |
| 5166 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 5167 | } |
| 5168 | |
| 5169 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 5170 | unsigned long addr, unsigned long len, |
| 5171 | unsigned long pgoff, unsigned long flags) |
| 5172 | { |
| 5173 | void *ptr; |
| 5174 | |
| 5175 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 5176 | if (IS_ERR(ptr)) |
| 5177 | return PTR_ERR(ptr); |
| 5178 | |
| 5179 | return (unsigned long) ptr; |
| 5180 | } |
| 5181 | |
| 5182 | #endif /* !CONFIG_MMU */ |
| 5183 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5184 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 5185 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 5186 | size_t, sigsz) |
| 5187 | { |
| 5188 | struct io_ring_ctx *ctx; |
| 5189 | long ret = -EBADF; |
| 5190 | int submitted = 0; |
| 5191 | struct fd f; |
| 5192 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5193 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5194 | return -EINVAL; |
| 5195 | |
| 5196 | f = fdget(fd); |
| 5197 | if (!f.file) |
| 5198 | return -EBADF; |
| 5199 | |
| 5200 | ret = -EOPNOTSUPP; |
| 5201 | if (f.file->f_op != &io_uring_fops) |
| 5202 | goto out_fput; |
| 5203 | |
| 5204 | ret = -ENXIO; |
| 5205 | ctx = f.file->private_data; |
| 5206 | if (!percpu_ref_tryget(&ctx->refs)) |
| 5207 | goto out_fput; |
| 5208 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5209 | /* |
| 5210 | * For SQ polling, the thread will do all submissions and completions. |
| 5211 | * Just return the requested submit count, and wake the thread if |
| 5212 | * we were asked to. |
| 5213 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 5214 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5215 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5216 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 5217 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5218 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 5219 | wake_up(&ctx->sqo_wait); |
| 5220 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 5221 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5222 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5223 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 5224 | if (current->mm != ctx->sqo_mm || |
| 5225 | current_cred() != ctx->creds) { |
| 5226 | ret = -EPERM; |
| 5227 | goto out; |
| 5228 | } |
| 5229 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5230 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5231 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5232 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 5233 | cur_mm = ctx->sqo_mm; |
| 5234 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 5235 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5236 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 5237 | |
| 5238 | if (submitted != to_submit) |
| 5239 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5240 | } |
| 5241 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5242 | unsigned nr_events = 0; |
| 5243 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5244 | min_complete = min(min_complete, ctx->cq_entries); |
| 5245 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5246 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5247 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5248 | } else { |
| 5249 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 5250 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5251 | } |
| 5252 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 5253 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 5254 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5255 | out_fput: |
| 5256 | fdput(f); |
| 5257 | return submitted ? submitted : ret; |
| 5258 | } |
| 5259 | |
| 5260 | static const struct file_operations io_uring_fops = { |
| 5261 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5262 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5263 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5264 | #ifndef CONFIG_MMU |
| 5265 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 5266 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 5267 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5268 | .poll = io_uring_poll, |
| 5269 | .fasync = io_uring_fasync, |
| 5270 | }; |
| 5271 | |
| 5272 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 5273 | struct io_uring_params *p) |
| 5274 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5275 | struct io_rings *rings; |
| 5276 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5277 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5278 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 5279 | if (size == SIZE_MAX) |
| 5280 | return -EOVERFLOW; |
| 5281 | |
| 5282 | rings = io_mem_alloc(size); |
| 5283 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5284 | return -ENOMEM; |
| 5285 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5286 | ctx->rings = rings; |
| 5287 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 5288 | rings->sq_ring_mask = p->sq_entries - 1; |
| 5289 | rings->cq_ring_mask = p->cq_entries - 1; |
| 5290 | rings->sq_ring_entries = p->sq_entries; |
| 5291 | rings->cq_ring_entries = p->cq_entries; |
| 5292 | ctx->sq_mask = rings->sq_ring_mask; |
| 5293 | ctx->cq_mask = rings->cq_ring_mask; |
| 5294 | ctx->sq_entries = rings->sq_ring_entries; |
| 5295 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5296 | |
| 5297 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 5298 | if (size == SIZE_MAX) { |
| 5299 | io_mem_free(ctx->rings); |
| 5300 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5301 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 5302 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5303 | |
| 5304 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 5305 | if (!ctx->sq_sqes) { |
| 5306 | io_mem_free(ctx->rings); |
| 5307 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5308 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 5309 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5310 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5311 | return 0; |
| 5312 | } |
| 5313 | |
| 5314 | /* |
| 5315 | * Allocate an anonymous fd, this is what constitutes the application |
| 5316 | * visible backing of an io_uring instance. The application mmaps this |
| 5317 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 5318 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 5319 | */ |
| 5320 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 5321 | { |
| 5322 | struct file *file; |
| 5323 | int ret; |
| 5324 | |
| 5325 | #if defined(CONFIG_UNIX) |
| 5326 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 5327 | &ctx->ring_sock); |
| 5328 | if (ret) |
| 5329 | return ret; |
| 5330 | #endif |
| 5331 | |
| 5332 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 5333 | if (ret < 0) |
| 5334 | goto err; |
| 5335 | |
| 5336 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 5337 | O_RDWR | O_CLOEXEC); |
| 5338 | if (IS_ERR(file)) { |
| 5339 | put_unused_fd(ret); |
| 5340 | ret = PTR_ERR(file); |
| 5341 | goto err; |
| 5342 | } |
| 5343 | |
| 5344 | #if defined(CONFIG_UNIX) |
| 5345 | ctx->ring_sock->file = file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5346 | ctx->ring_sock->sk->sk_user_data = ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5347 | #endif |
| 5348 | fd_install(ret, file); |
| 5349 | return ret; |
| 5350 | err: |
| 5351 | #if defined(CONFIG_UNIX) |
| 5352 | sock_release(ctx->ring_sock); |
| 5353 | ctx->ring_sock = NULL; |
| 5354 | #endif |
| 5355 | return ret; |
| 5356 | } |
| 5357 | |
| 5358 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 5359 | { |
| 5360 | struct user_struct *user = NULL; |
| 5361 | struct io_ring_ctx *ctx; |
| 5362 | bool account_mem; |
| 5363 | int ret; |
| 5364 | |
| 5365 | if (!entries || entries > IORING_MAX_ENTRIES) |
| 5366 | return -EINVAL; |
| 5367 | |
| 5368 | /* |
| 5369 | * Use twice as many entries for the CQ ring. It's possible for the |
| 5370 | * application to drive a higher depth than the size of the SQ ring, |
| 5371 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 5372 | * some flexibility in overcommitting a bit. If the application has |
| 5373 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 5374 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5375 | */ |
| 5376 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 5377 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 5378 | /* |
| 5379 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 5380 | * to a power-of-two, if it isn't already. We do NOT impose |
| 5381 | * any cq vs sq ring sizing. |
| 5382 | */ |
| 5383 | if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES) |
| 5384 | return -EINVAL; |
| 5385 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 5386 | } else { |
| 5387 | p->cq_entries = 2 * p->sq_entries; |
| 5388 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5389 | |
| 5390 | user = get_uid(current_user()); |
| 5391 | account_mem = !capable(CAP_IPC_LOCK); |
| 5392 | |
| 5393 | if (account_mem) { |
| 5394 | ret = io_account_mem(user, |
| 5395 | ring_pages(p->sq_entries, p->cq_entries)); |
| 5396 | if (ret) { |
| 5397 | free_uid(user); |
| 5398 | return ret; |
| 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | ctx = io_ring_ctx_alloc(p); |
| 5403 | if (!ctx) { |
| 5404 | if (account_mem) |
| 5405 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 5406 | p->cq_entries)); |
| 5407 | free_uid(user); |
| 5408 | return -ENOMEM; |
| 5409 | } |
| 5410 | ctx->compat = in_compat_syscall(); |
| 5411 | ctx->account_mem = account_mem; |
| 5412 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 5413 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5414 | |
| 5415 | ret = io_allocate_scq_urings(ctx, p); |
| 5416 | if (ret) |
| 5417 | goto err; |
| 5418 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5419 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5420 | if (ret) |
| 5421 | goto err; |
| 5422 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5423 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5424 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 5425 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 5426 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 5427 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 5428 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 5429 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 5430 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5431 | |
| 5432 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5433 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 5434 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 5435 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 5436 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 5437 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 5438 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 5439 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 5440 | /* |
| 5441 | * Install ring fd as the very last thing, so we don't risk someone |
| 5442 | * having closed it before we finish setup |
| 5443 | */ |
| 5444 | ret = io_uring_get_fd(ctx); |
| 5445 | if (ret < 0) |
| 5446 | goto err; |
| 5447 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 5448 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 5449 | IORING_FEAT_SUBMIT_STABLE; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5450 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5451 | return ret; |
| 5452 | err: |
| 5453 | io_ring_ctx_wait_and_kill(ctx); |
| 5454 | return ret; |
| 5455 | } |
| 5456 | |
| 5457 | /* |
| 5458 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 5459 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 5460 | * params structure passed in. |
| 5461 | */ |
| 5462 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 5463 | { |
| 5464 | struct io_uring_params p; |
| 5465 | long ret; |
| 5466 | int i; |
| 5467 | |
| 5468 | if (copy_from_user(&p, params, sizeof(p))) |
| 5469 | return -EFAULT; |
| 5470 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 5471 | if (p.resv[i]) |
| 5472 | return -EINVAL; |
| 5473 | } |
| 5474 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5475 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 5476 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5477 | return -EINVAL; |
| 5478 | |
| 5479 | ret = io_uring_create(entries, &p); |
| 5480 | if (ret < 0) |
| 5481 | return ret; |
| 5482 | |
| 5483 | if (copy_to_user(params, &p, sizeof(p))) |
| 5484 | return -EFAULT; |
| 5485 | |
| 5486 | return ret; |
| 5487 | } |
| 5488 | |
| 5489 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 5490 | struct io_uring_params __user *, params) |
| 5491 | { |
| 5492 | return io_uring_setup(entries, params); |
| 5493 | } |
| 5494 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5495 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 5496 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 5497 | __releases(ctx->uring_lock) |
| 5498 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5499 | { |
| 5500 | int ret; |
| 5501 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 5502 | /* |
| 5503 | * We're inside the ring mutex, if the ref is already dying, then |
| 5504 | * someone else killed the ctx or is already going through |
| 5505 | * io_uring_register(). |
| 5506 | */ |
| 5507 | if (percpu_ref_is_dying(&ctx->refs)) |
| 5508 | return -ENXIO; |
| 5509 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5510 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 5511 | |
| 5512 | /* |
| 5513 | * Drop uring mutex before waiting for references to exit. If another |
| 5514 | * thread is currently inside io_uring_enter() it might need to grab |
| 5515 | * the uring_lock to make progress. If we hold it here across the drain |
| 5516 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 5517 | * no new references will come in after we've killed the percpu ref. |
| 5518 | */ |
| 5519 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5520 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 5521 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5522 | |
| 5523 | switch (opcode) { |
| 5524 | case IORING_REGISTER_BUFFERS: |
| 5525 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 5526 | break; |
| 5527 | case IORING_UNREGISTER_BUFFERS: |
| 5528 | ret = -EINVAL; |
| 5529 | if (arg || nr_args) |
| 5530 | break; |
| 5531 | ret = io_sqe_buffer_unregister(ctx); |
| 5532 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5533 | case IORING_REGISTER_FILES: |
| 5534 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 5535 | break; |
| 5536 | case IORING_UNREGISTER_FILES: |
| 5537 | ret = -EINVAL; |
| 5538 | if (arg || nr_args) |
| 5539 | break; |
| 5540 | ret = io_sqe_files_unregister(ctx); |
| 5541 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5542 | case IORING_REGISTER_FILES_UPDATE: |
| 5543 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 5544 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 5545 | case IORING_REGISTER_EVENTFD: |
| 5546 | ret = -EINVAL; |
| 5547 | if (nr_args != 1) |
| 5548 | break; |
| 5549 | ret = io_eventfd_register(ctx, arg); |
| 5550 | break; |
| 5551 | case IORING_UNREGISTER_EVENTFD: |
| 5552 | ret = -EINVAL; |
| 5553 | if (arg || nr_args) |
| 5554 | break; |
| 5555 | ret = io_eventfd_unregister(ctx); |
| 5556 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5557 | default: |
| 5558 | ret = -EINVAL; |
| 5559 | break; |
| 5560 | } |
| 5561 | |
| 5562 | /* bring the ctx back to life */ |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5563 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5564 | percpu_ref_reinit(&ctx->refs); |
| 5565 | return ret; |
| 5566 | } |
| 5567 | |
| 5568 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 5569 | void __user *, arg, unsigned int, nr_args) |
| 5570 | { |
| 5571 | struct io_ring_ctx *ctx; |
| 5572 | long ret = -EBADF; |
| 5573 | struct fd f; |
| 5574 | |
| 5575 | f = fdget(fd); |
| 5576 | if (!f.file) |
| 5577 | return -EBADF; |
| 5578 | |
| 5579 | ret = -EOPNOTSUPP; |
| 5580 | if (f.file->f_op != &io_uring_fops) |
| 5581 | goto out_fput; |
| 5582 | |
| 5583 | ctx = f.file->private_data; |
| 5584 | |
| 5585 | mutex_lock(&ctx->uring_lock); |
| 5586 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 5587 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5588 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 5589 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5590 | out_fput: |
| 5591 | fdput(f); |
| 5592 | return ret; |
| 5593 | } |
| 5594 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5595 | static int __init io_uring_init(void) |
| 5596 | { |
| 5597 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 5598 | return 0; |
| 5599 | }; |
| 5600 | __initcall(io_uring_init); |