Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 47 | #include <net/compat.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 48 | #include <linux/refcount.h> |
| 49 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 50 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/sched/signal.h> |
| 53 | #include <linux/fs.h> |
| 54 | #include <linux/file.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/mm.h> |
| 57 | #include <linux/mman.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 79 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 80 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 81 | #include <linux/pagemap.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 82 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 83 | #define CREATE_TRACE_POINTS |
| 84 | #include <trace/events/io_uring.h> |
| 85 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 86 | #include <uapi/linux/io_uring.h> |
| 87 | |
| 88 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 89 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 90 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 91 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 96 | */ |
| 97 | #define IORING_FILE_TABLE_SHIFT 9 |
| 98 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 99 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 100 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 101 | |
| 102 | struct io_uring { |
| 103 | u32 head ____cacheline_aligned_in_smp; |
| 104 | u32 tail ____cacheline_aligned_in_smp; |
| 105 | }; |
| 106 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 107 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 108 | * This data is shared with the application through the mmap at offsets |
| 109 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 110 | * |
| 111 | * The offsets to the member fields are published through struct |
| 112 | * io_sqring_offsets when calling io_uring_setup. |
| 113 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 114 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
| 116 | * Head and tail offsets into the ring; the offsets need to be |
| 117 | * masked to get valid indices. |
| 118 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 120 | * and the application controls tail of the sq ring and the head of the |
| 121 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 125 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 126 | * ring_entries - 1) |
| 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | u32 sq_ring_mask, cq_ring_mask; |
| 129 | /* Ring sizes (constant, power of 2) */ |
| 130 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Number of invalid entries dropped by the kernel due to |
| 133 | * invalid index stored in array |
| 134 | * |
| 135 | * Written by the kernel, shouldn't be modified by the |
| 136 | * application (i.e. get number of "new events" by comparing to |
| 137 | * cached value). |
| 138 | * |
| 139 | * After a new SQ head value was read by the application this |
| 140 | * counter includes all submissions that were dropped reaching |
| 141 | * the new SQ head (and possibly more). |
| 142 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 144 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 145 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 146 | * |
| 147 | * Written by the kernel, shouldn't be modified by the |
| 148 | * application. |
| 149 | * |
| 150 | * The application needs a full memory barrier before checking |
| 151 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 152 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 153 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 155 | * Runtime CQ flags |
| 156 | * |
| 157 | * Written by the application, shouldn't be modified by the |
| 158 | * kernel. |
| 159 | */ |
| 160 | u32 cq_flags; |
| 161 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | * Number of completion events lost because the queue was full; |
| 163 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 164 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 165 | * the completion queue. |
| 166 | * |
| 167 | * Written by the kernel, shouldn't be modified by the |
| 168 | * application (i.e. get number of "new events" by comparing to |
| 169 | * cached value). |
| 170 | * |
| 171 | * As completion events come in out of order this counter is not |
| 172 | * ordered with any other data. |
| 173 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 174 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Ring buffer of completion events. |
| 177 | * |
| 178 | * The kernel writes completion events fresh every time they are |
| 179 | * produced, so the application is allowed to modify pending |
| 180 | * entries. |
| 181 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 182 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 185 | struct io_mapped_ubuf { |
| 186 | u64 ubuf; |
| 187 | size_t len; |
| 188 | struct bio_vec *bvec; |
| 189 | unsigned int nr_bvecs; |
| 190 | }; |
| 191 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 192 | struct fixed_file_table { |
| 193 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 196 | struct fixed_file_ref_node { |
| 197 | struct percpu_ref refs; |
| 198 | struct list_head node; |
| 199 | struct list_head file_list; |
| 200 | struct fixed_file_data *file_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 201 | struct llist_node llist; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 202 | }; |
| 203 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 204 | struct fixed_file_data { |
| 205 | struct fixed_file_table *table; |
| 206 | struct io_ring_ctx *ctx; |
| 207 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 208 | struct percpu_ref *cur_refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 209 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 210 | struct completion done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 211 | struct list_head ref_list; |
| 212 | spinlock_t lock; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 215 | struct io_buffer { |
| 216 | struct list_head list; |
| 217 | __u64 addr; |
| 218 | __s32 len; |
| 219 | __u16 bid; |
| 220 | }; |
| 221 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 222 | struct io_ring_ctx { |
| 223 | struct { |
| 224 | struct percpu_ref refs; |
| 225 | } ____cacheline_aligned_in_smp; |
| 226 | |
| 227 | struct { |
| 228 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 229 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 230 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 231 | unsigned int cq_overflow_flushed: 1; |
| 232 | unsigned int drain_next: 1; |
| 233 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 235 | /* |
| 236 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 237 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 238 | * |
| 239 | * This indirection could e.g. be used to assign fixed |
| 240 | * io_uring_sqe entries to operations and only submit them to |
| 241 | * the queue when needed. |
| 242 | * |
| 243 | * The kernel modifies neither the indices array nor the entries |
| 244 | * array. |
| 245 | */ |
| 246 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 247 | unsigned cached_sq_head; |
| 248 | unsigned sq_entries; |
| 249 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 250 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 251 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 252 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 253 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 254 | |
| 255 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 256 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 257 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 258 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 259 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 260 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | } ____cacheline_aligned_in_smp; |
| 262 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 263 | struct io_rings *rings; |
| 264 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 265 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 266 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 267 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 268 | struct mm_struct *sqo_mm; |
| 269 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 270 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 271 | /* |
| 272 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 273 | * readers must ensure that ->refs is alive as long as the file* is |
| 274 | * used. Only updated through io_uring_register(2). |
| 275 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 276 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 277 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 278 | int ring_fd; |
| 279 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 280 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 281 | /* if used, fixed mapped user buffers */ |
| 282 | unsigned nr_user_bufs; |
| 283 | struct io_mapped_ubuf *user_bufs; |
| 284 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 285 | struct user_struct *user; |
| 286 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 287 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 288 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 289 | struct completion ref_comp; |
| 290 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 291 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 292 | /* if all else fails... */ |
| 293 | struct io_kiocb *fallback_req; |
| 294 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 295 | #if defined(CONFIG_UNIX) |
| 296 | struct socket *ring_sock; |
| 297 | #endif |
| 298 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 299 | struct idr io_buffer_idr; |
| 300 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 301 | struct idr personality_idr; |
| 302 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 303 | struct { |
| 304 | unsigned cached_cq_tail; |
| 305 | unsigned cq_entries; |
| 306 | unsigned cq_mask; |
| 307 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 308 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 309 | struct wait_queue_head cq_wait; |
| 310 | struct fasync_struct *cq_fasync; |
| 311 | struct eventfd_ctx *cq_ev_fd; |
| 312 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 313 | |
| 314 | struct { |
| 315 | struct mutex uring_lock; |
| 316 | wait_queue_head_t wait; |
| 317 | } ____cacheline_aligned_in_smp; |
| 318 | |
| 319 | struct { |
| 320 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 321 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 322 | /* |
| 323 | * ->poll_list is protected by the ctx->uring_lock for |
| 324 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 325 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 326 | * manipulate the list, hence no extra locking is needed there. |
| 327 | */ |
| 328 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 329 | struct hlist_head *cancel_hash; |
| 330 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 331 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 332 | |
| 333 | spinlock_t inflight_lock; |
| 334 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 335 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 336 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 337 | struct delayed_work file_put_work; |
| 338 | struct llist_head file_put_llist; |
| 339 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 340 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 343 | /* |
| 344 | * First field must be the file pointer in all the |
| 345 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 346 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 347 | struct io_poll_iocb { |
| 348 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 349 | union { |
| 350 | struct wait_queue_head *head; |
| 351 | u64 addr; |
| 352 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 353 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 354 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 355 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 356 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 359 | struct io_close { |
| 360 | struct file *file; |
| 361 | struct file *put_file; |
| 362 | int fd; |
| 363 | }; |
| 364 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 365 | struct io_timeout_data { |
| 366 | struct io_kiocb *req; |
| 367 | struct hrtimer timer; |
| 368 | struct timespec64 ts; |
| 369 | enum hrtimer_mode mode; |
| 370 | }; |
| 371 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 372 | struct io_accept { |
| 373 | struct file *file; |
| 374 | struct sockaddr __user *addr; |
| 375 | int __user *addr_len; |
| 376 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 377 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | struct io_sync { |
| 381 | struct file *file; |
| 382 | loff_t len; |
| 383 | loff_t off; |
| 384 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 385 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 388 | struct io_cancel { |
| 389 | struct file *file; |
| 390 | u64 addr; |
| 391 | }; |
| 392 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 393 | struct io_timeout { |
| 394 | struct file *file; |
| 395 | u64 addr; |
| 396 | int flags; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 397 | u32 off; |
| 398 | u32 target_seq; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 401 | struct io_rw { |
| 402 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 403 | struct kiocb kiocb; |
| 404 | u64 addr; |
| 405 | u64 len; |
| 406 | }; |
| 407 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 408 | struct io_connect { |
| 409 | struct file *file; |
| 410 | struct sockaddr __user *addr; |
| 411 | int addr_len; |
| 412 | }; |
| 413 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 414 | struct io_sr_msg { |
| 415 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 416 | union { |
| 417 | struct user_msghdr __user *msg; |
| 418 | void __user *buf; |
| 419 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 420 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 421 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 422 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 423 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 426 | struct io_open { |
| 427 | struct file *file; |
| 428 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 429 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 430 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 431 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 432 | }; |
| 433 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 434 | struct io_files_update { |
| 435 | struct file *file; |
| 436 | u64 arg; |
| 437 | u32 nr_args; |
| 438 | u32 offset; |
| 439 | }; |
| 440 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 441 | struct io_fadvise { |
| 442 | struct file *file; |
| 443 | u64 offset; |
| 444 | u32 len; |
| 445 | u32 advice; |
| 446 | }; |
| 447 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 448 | struct io_madvise { |
| 449 | struct file *file; |
| 450 | u64 addr; |
| 451 | u32 len; |
| 452 | u32 advice; |
| 453 | }; |
| 454 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 455 | struct io_epoll { |
| 456 | struct file *file; |
| 457 | int epfd; |
| 458 | int op; |
| 459 | int fd; |
| 460 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 463 | struct io_splice { |
| 464 | struct file *file_out; |
| 465 | struct file *file_in; |
| 466 | loff_t off_out; |
| 467 | loff_t off_in; |
| 468 | u64 len; |
| 469 | unsigned int flags; |
| 470 | }; |
| 471 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 472 | struct io_provide_buf { |
| 473 | struct file *file; |
| 474 | __u64 addr; |
| 475 | __s32 len; |
| 476 | __u32 bgid; |
| 477 | __u16 nbufs; |
| 478 | __u16 bid; |
| 479 | }; |
| 480 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 481 | struct io_statx { |
| 482 | struct file *file; |
| 483 | int dfd; |
| 484 | unsigned int mask; |
| 485 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 486 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 487 | struct statx __user *buffer; |
| 488 | }; |
| 489 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 490 | struct io_async_connect { |
| 491 | struct sockaddr_storage address; |
| 492 | }; |
| 493 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 494 | struct io_async_msghdr { |
| 495 | struct iovec fast_iov[UIO_FASTIOV]; |
| 496 | struct iovec *iov; |
| 497 | struct sockaddr __user *uaddr; |
| 498 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 499 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 502 | struct io_async_rw { |
| 503 | struct iovec fast_iov[UIO_FASTIOV]; |
| 504 | struct iovec *iov; |
| 505 | ssize_t nr_segs; |
| 506 | ssize_t size; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 507 | struct wait_page_queue wpq; |
| 508 | struct callback_head task_work; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 511 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 512 | union { |
| 513 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 514 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 515 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 516 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 517 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 520 | enum { |
| 521 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 522 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 523 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 524 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 525 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 526 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 527 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 528 | REQ_F_LINK_HEAD_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 529 | REQ_F_LINK_NEXT_BIT, |
| 530 | REQ_F_FAIL_LINK_BIT, |
| 531 | REQ_F_INFLIGHT_BIT, |
| 532 | REQ_F_CUR_POS_BIT, |
| 533 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 534 | REQ_F_LINK_TIMEOUT_BIT, |
| 535 | REQ_F_TIMEOUT_BIT, |
| 536 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 537 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 538 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 539 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 540 | REQ_F_OVERFLOW_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 541 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 542 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 543 | REQ_F_NO_FILE_TABLE_BIT, |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 544 | REQ_F_QUEUE_TIMEOUT_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 545 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 546 | REQ_F_TASK_PINNED_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 547 | |
| 548 | /* not a real bit, just to check we're not overflowing the space */ |
| 549 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | enum { |
| 553 | /* ctx owns file */ |
| 554 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 555 | /* drain existing IO first */ |
| 556 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 557 | /* linked sqes */ |
| 558 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 559 | /* doesn't sever on completion < 0 */ |
| 560 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 561 | /* IOSQE_ASYNC */ |
| 562 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 563 | /* IOSQE_BUFFER_SELECT */ |
| 564 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 565 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 566 | /* head of a link */ |
| 567 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 568 | /* already grabbed next link */ |
| 569 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 570 | /* fail rest of links */ |
| 571 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 572 | /* on inflight list */ |
| 573 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 574 | /* read/write uses file position */ |
| 575 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 576 | /* must not punt to workers */ |
| 577 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 578 | /* has linked timeout */ |
| 579 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 580 | /* timeout request */ |
| 581 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 582 | /* regular file */ |
| 583 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 584 | /* no timeout sequence */ |
| 585 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 586 | /* completion under lock */ |
| 587 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 588 | /* needs cleanup */ |
| 589 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 590 | /* in overflow list */ |
| 591 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 592 | /* already went through poll handler */ |
| 593 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 594 | /* buffer already selected */ |
| 595 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 596 | /* doesn't need file table for this request */ |
| 597 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 598 | /* needs to queue linked timeout */ |
| 599 | REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 600 | /* io_wq_work is initialized */ |
| 601 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 602 | /* req->task is refcounted */ |
| 603 | REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | struct async_poll { |
| 607 | struct io_poll_iocb poll; |
| 608 | struct io_wq_work work; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 609 | }; |
| 610 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 611 | /* |
| 612 | * NOTE! Each of the iocb union members has the file pointer |
| 613 | * as the first entry in their struct definition. So you can |
| 614 | * access the file pointer through any of the sub-structs, |
| 615 | * or directly as just 'ki_filp' in this struct. |
| 616 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 617 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 618 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 619 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 620 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 621 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 622 | struct io_accept accept; |
| 623 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 624 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 625 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 626 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 627 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 628 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 629 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 630 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 631 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 632 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 633 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 634 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 635 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 636 | struct io_statx statx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 637 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 638 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 639 | struct io_async_ctx *io; |
Pavel Begunkov | c398ecb | 2020-04-09 08:17:59 +0300 | [diff] [blame] | 640 | int cflags; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 641 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 642 | /* polled IO has completed */ |
| 643 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 644 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 645 | u16 buf_index; |
| 646 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 647 | struct io_ring_ctx *ctx; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 648 | struct list_head list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 649 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 650 | refcount_t refs; |
Jens Axboe | 3537b6a | 2020-04-03 11:19:06 -0600 | [diff] [blame] | 651 | struct task_struct *task; |
| 652 | unsigned long fsize; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 653 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 654 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 655 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 656 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 657 | struct list_head link_list; |
| 658 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 659 | struct list_head inflight_entry; |
| 660 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 661 | struct percpu_ref *fixed_file_refs; |
| 662 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 663 | union { |
| 664 | /* |
| 665 | * Only commands that never go async can use the below fields, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 666 | * obviously. Right now only IORING_OP_POLL_ADD uses them, and |
| 667 | * async armed poll handlers for regular commands. The latter |
| 668 | * restore the work, if needed. |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 669 | */ |
| 670 | struct { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 671 | struct callback_head task_work; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 672 | struct hlist_node hash_node; |
| 673 | struct async_poll *apoll; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 674 | }; |
| 675 | struct io_wq_work work; |
| 676 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 677 | }; |
| 678 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 679 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 680 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 681 | struct io_comp_state { |
| 682 | unsigned int nr; |
| 683 | struct list_head list; |
| 684 | struct io_ring_ctx *ctx; |
| 685 | }; |
| 686 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 687 | struct io_submit_state { |
| 688 | struct blk_plug plug; |
| 689 | |
| 690 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 691 | * io_kiocb alloc cache |
| 692 | */ |
| 693 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 694 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 695 | |
| 696 | /* |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 697 | * Batch completion logic |
| 698 | */ |
| 699 | struct io_comp_state comp; |
| 700 | |
| 701 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 702 | * File reference cache |
| 703 | */ |
| 704 | struct file *file; |
| 705 | unsigned int fd; |
| 706 | unsigned int has_refs; |
| 707 | unsigned int used_refs; |
| 708 | unsigned int ios_left; |
| 709 | }; |
| 710 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 711 | struct io_op_def { |
| 712 | /* needs req->io allocated for deferral/async */ |
| 713 | unsigned async_ctx : 1; |
| 714 | /* needs current->mm setup, does mm access */ |
| 715 | unsigned needs_mm : 1; |
| 716 | /* needs req->file assigned */ |
| 717 | unsigned needs_file : 1; |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 718 | /* don't fail if file grab fails */ |
| 719 | unsigned needs_file_no_error : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 720 | /* hash wq insertion if file is a regular file */ |
| 721 | unsigned hash_reg_file : 1; |
| 722 | /* unbound wq insertion if file is a non-regular file */ |
| 723 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 724 | /* opcode is not supported by this kernel */ |
| 725 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 726 | /* needs file table */ |
| 727 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 728 | /* needs ->fs */ |
| 729 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 730 | /* set if opcode supports polled "wait" */ |
| 731 | unsigned pollin : 1; |
| 732 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 733 | /* op supports buffer selection */ |
| 734 | unsigned buffer_select : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 738 | [IORING_OP_NOP] = {}, |
| 739 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 740 | .async_ctx = 1, |
| 741 | .needs_mm = 1, |
| 742 | .needs_file = 1, |
| 743 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 744 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 745 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 746 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 747 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 748 | .async_ctx = 1, |
| 749 | .needs_mm = 1, |
| 750 | .needs_file = 1, |
| 751 | .hash_reg_file = 1, |
| 752 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 753 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 754 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 755 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 756 | .needs_file = 1, |
| 757 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 758 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 759 | .needs_file = 1, |
| 760 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 761 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 762 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 763 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 764 | .needs_file = 1, |
| 765 | .hash_reg_file = 1, |
| 766 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 767 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 768 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 769 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 770 | .needs_file = 1, |
| 771 | .unbound_nonreg_file = 1, |
| 772 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 773 | [IORING_OP_POLL_REMOVE] = {}, |
| 774 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 775 | .needs_file = 1, |
| 776 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 777 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 778 | .async_ctx = 1, |
| 779 | .needs_mm = 1, |
| 780 | .needs_file = 1, |
| 781 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 782 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 783 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 784 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 785 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 786 | .async_ctx = 1, |
| 787 | .needs_mm = 1, |
| 788 | .needs_file = 1, |
| 789 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 790 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 791 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 792 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 793 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 794 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 795 | .async_ctx = 1, |
| 796 | .needs_mm = 1, |
| 797 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 798 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 799 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 800 | .needs_mm = 1, |
| 801 | .needs_file = 1, |
| 802 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 803 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 804 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 805 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 806 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 807 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 808 | .async_ctx = 1, |
| 809 | .needs_mm = 1, |
| 810 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 811 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 812 | .async_ctx = 1, |
| 813 | .needs_mm = 1, |
| 814 | .needs_file = 1, |
| 815 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 816 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 817 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 818 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 819 | .needs_file = 1, |
| 820 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 821 | [IORING_OP_OPENAT] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 822 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 823 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 824 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 825 | [IORING_OP_CLOSE] = { |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 826 | .needs_file = 1, |
| 827 | .needs_file_no_error = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 828 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 829 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 830 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 831 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 832 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 833 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 834 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 835 | .needs_mm = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 836 | .needs_fs = 1, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 837 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 838 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 839 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 840 | .needs_mm = 1, |
| 841 | .needs_file = 1, |
| 842 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 843 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 844 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 845 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 846 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 847 | .needs_mm = 1, |
| 848 | .needs_file = 1, |
| 849 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 850 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 851 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 852 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 853 | .needs_file = 1, |
| 854 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 855 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 856 | .needs_mm = 1, |
| 857 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 858 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 859 | .needs_mm = 1, |
| 860 | .needs_file = 1, |
| 861 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 862 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 863 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 864 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 865 | .needs_mm = 1, |
| 866 | .needs_file = 1, |
| 867 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 868 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 869 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 870 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 871 | [IORING_OP_OPENAT2] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 872 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 873 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 874 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 875 | [IORING_OP_EPOLL_CTL] = { |
| 876 | .unbound_nonreg_file = 1, |
| 877 | .file_table = 1, |
| 878 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 879 | [IORING_OP_SPLICE] = { |
| 880 | .needs_file = 1, |
| 881 | .hash_reg_file = 1, |
| 882 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 883 | }, |
| 884 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 885 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 886 | [IORING_OP_TEE] = { |
| 887 | .needs_file = 1, |
| 888 | .hash_reg_file = 1, |
| 889 | .unbound_nonreg_file = 1, |
| 890 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 893 | enum io_mem_account { |
| 894 | ACCT_LOCKED, |
| 895 | ACCT_PINNED, |
| 896 | }; |
| 897 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 898 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 899 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 900 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 901 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 902 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 903 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 904 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 905 | struct io_uring_files_update *ip, |
| 906 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 907 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2237d76 | 2020-06-26 13:44:16 -0600 | [diff] [blame] | 908 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 909 | struct io_comp_state *cs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 910 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 911 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 912 | int fd, struct file **out_file, bool fixed); |
| 913 | static void __io_queue_sqe(struct io_kiocb *req, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 914 | const struct io_uring_sqe *sqe, |
| 915 | struct io_comp_state *cs); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 916 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 917 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 918 | struct iovec **iovec, struct iov_iter *iter, |
| 919 | bool needs_lock); |
| 920 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 921 | struct iovec *iovec, struct iovec *fast_iov, |
| 922 | struct iov_iter *iter); |
| 923 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 924 | static struct kmem_cache *req_cachep; |
| 925 | |
| 926 | static const struct file_operations io_uring_fops; |
| 927 | |
| 928 | struct sock *io_uring_get_socket(struct file *file) |
| 929 | { |
| 930 | #if defined(CONFIG_UNIX) |
| 931 | if (file->f_op == &io_uring_fops) { |
| 932 | struct io_ring_ctx *ctx = file->private_data; |
| 933 | |
| 934 | return ctx->ring_sock->sk; |
| 935 | } |
| 936 | #endif |
| 937 | return NULL; |
| 938 | } |
| 939 | EXPORT_SYMBOL(io_uring_get_socket); |
| 940 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 941 | static void io_get_req_task(struct io_kiocb *req) |
| 942 | { |
| 943 | if (req->flags & REQ_F_TASK_PINNED) |
| 944 | return; |
| 945 | get_task_struct(req->task); |
| 946 | req->flags |= REQ_F_TASK_PINNED; |
| 947 | } |
| 948 | |
| 949 | /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */ |
| 950 | static void __io_put_req_task(struct io_kiocb *req) |
| 951 | { |
| 952 | if (req->flags & REQ_F_TASK_PINNED) |
| 953 | put_task_struct(req->task); |
| 954 | } |
| 955 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 956 | static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx) |
| 957 | { |
| 958 | struct mm_struct *mm = current->mm; |
| 959 | |
| 960 | if (mm) { |
| 961 | kthread_unuse_mm(mm); |
| 962 | mmput(mm); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 967 | { |
| 968 | if (!current->mm) { |
| 969 | if (unlikely(!mmget_not_zero(ctx->sqo_mm))) |
| 970 | return -EFAULT; |
| 971 | kthread_use_mm(ctx->sqo_mm); |
| 972 | } |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, |
| 978 | struct io_kiocb *req) |
| 979 | { |
| 980 | if (!io_op_defs[req->opcode].needs_mm) |
| 981 | return 0; |
| 982 | return __io_sq_thread_acquire_mm(ctx); |
| 983 | } |
| 984 | |
| 985 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 986 | { |
| 987 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 988 | req->flags |= REQ_F_FAIL_LINK; |
| 989 | } |
| 990 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 991 | static void io_file_put_work(struct work_struct *work); |
| 992 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 993 | /* |
| 994 | * Note: must call io_req_init_async() for the first time you |
| 995 | * touch any members of io_wq_work. |
| 996 | */ |
| 997 | static inline void io_req_init_async(struct io_kiocb *req) |
| 998 | { |
| 999 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1000 | return; |
| 1001 | |
| 1002 | memset(&req->work, 0, sizeof(req->work)); |
| 1003 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1004 | } |
| 1005 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 1006 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 1007 | { |
| 1008 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 1009 | } |
| 1010 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1011 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1012 | { |
| 1013 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1014 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1015 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1019 | { |
| 1020 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1021 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1022 | |
| 1023 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1024 | if (!ctx) |
| 1025 | return NULL; |
| 1026 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1027 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 1028 | if (!ctx->fallback_req) |
| 1029 | goto err; |
| 1030 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1031 | /* |
| 1032 | * Use 5 bits less than the max cq entries, that should give us around |
| 1033 | * 32 entries per hash list if totally full and uniformly spread. |
| 1034 | */ |
| 1035 | hash_bits = ilog2(p->cq_entries); |
| 1036 | hash_bits -= 5; |
| 1037 | if (hash_bits <= 0) |
| 1038 | hash_bits = 1; |
| 1039 | ctx->cancel_hash_bits = hash_bits; |
| 1040 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1041 | GFP_KERNEL); |
| 1042 | if (!ctx->cancel_hash) |
| 1043 | goto err; |
| 1044 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1045 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1046 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1047 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1048 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1049 | |
| 1050 | ctx->flags = p->flags; |
Jens Axboe | 583863e | 2020-05-17 09:20:00 -0600 | [diff] [blame] | 1051 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1052 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1053 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1054 | init_completion(&ctx->ref_comp); |
| 1055 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1056 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1057 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1058 | mutex_init(&ctx->uring_lock); |
| 1059 | init_waitqueue_head(&ctx->wait); |
| 1060 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1061 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1062 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1063 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1064 | init_waitqueue_head(&ctx->inflight_wait); |
| 1065 | spin_lock_init(&ctx->inflight_lock); |
| 1066 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1067 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1068 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1069 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1070 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1071 | if (ctx->fallback_req) |
| 1072 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1073 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1074 | kfree(ctx); |
| 1075 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1078 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1079 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1080 | struct io_ring_ctx *ctx = req->ctx; |
| 1081 | |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 1082 | return req->sequence != ctx->cached_cq_tail |
| 1083 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1084 | } |
| 1085 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1086 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1087 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1088 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1089 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1090 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1091 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1092 | } |
| 1093 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1094 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1095 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1096 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1097 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1098 | /* order cqe stores with ring update */ |
| 1099 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1100 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1101 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 1102 | wake_up_interruptible(&ctx->cq_wait); |
| 1103 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1107 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 1108 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1109 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1110 | if (!req->work.mm && def->needs_mm) { |
| 1111 | mmgrab(current->mm); |
| 1112 | req->work.mm = current->mm; |
| 1113 | } |
| 1114 | if (!req->work.creds) |
| 1115 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1116 | if (!req->work.fs && def->needs_fs) { |
| 1117 | spin_lock(¤t->fs->lock); |
| 1118 | if (!current->fs->in_exec) { |
| 1119 | req->work.fs = current->fs; |
| 1120 | req->work.fs->users++; |
| 1121 | } else { |
| 1122 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1123 | } |
| 1124 | spin_unlock(¤t->fs->lock); |
| 1125 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 1129 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1130 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1131 | return; |
| 1132 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1133 | if (req->work.mm) { |
| 1134 | mmdrop(req->work.mm); |
| 1135 | req->work.mm = NULL; |
| 1136 | } |
| 1137 | if (req->work.creds) { |
| 1138 | put_cred(req->work.creds); |
| 1139 | req->work.creds = NULL; |
| 1140 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1141 | if (req->work.fs) { |
| 1142 | struct fs_struct *fs = req->work.fs; |
| 1143 | |
| 1144 | spin_lock(&req->work.fs->lock); |
| 1145 | if (--fs->users) |
| 1146 | fs = NULL; |
| 1147 | spin_unlock(&req->work.fs->lock); |
| 1148 | if (fs) |
| 1149 | free_fs_struct(fs); |
| 1150 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1151 | } |
| 1152 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1153 | static inline void io_prep_async_work(struct io_kiocb *req, |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1154 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1155 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1156 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1157 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1158 | if (req->flags & REQ_F_ISREG) { |
| 1159 | if (def->hash_reg_file) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1160 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1161 | } else { |
| 1162 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1163 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1164 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1165 | |
Pavel Begunkov | 59960b9 | 2020-06-15 16:36:30 +0300 | [diff] [blame] | 1166 | io_req_init_async(req); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1167 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1168 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1169 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1170 | } |
| 1171 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1172 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1173 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1174 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1175 | struct io_kiocb *link; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1176 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1177 | io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1178 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1179 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1180 | &req->work, req->flags); |
| 1181 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1182 | |
| 1183 | if (link) |
| 1184 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1185 | } |
| 1186 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1187 | static void io_kill_timeout(struct io_kiocb *req) |
| 1188 | { |
| 1189 | int ret; |
| 1190 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1191 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1192 | if (ret != -1) { |
| 1193 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1194 | list_del_init(&req->list); |
Pavel Begunkov | f0e20b8 | 2020-03-07 01:15:22 +0300 | [diff] [blame] | 1195 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1196 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1197 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1202 | { |
| 1203 | struct io_kiocb *req, *tmp; |
| 1204 | |
| 1205 | spin_lock_irq(&ctx->completion_lock); |
| 1206 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1207 | io_kill_timeout(req); |
| 1208 | spin_unlock_irq(&ctx->completion_lock); |
| 1209 | } |
| 1210 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1211 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1212 | { |
| 1213 | do { |
| 1214 | struct io_kiocb *req = list_first_entry(&ctx->defer_list, |
| 1215 | struct io_kiocb, list); |
| 1216 | |
| 1217 | if (req_need_defer(req)) |
| 1218 | break; |
| 1219 | list_del_init(&req->list); |
| 1220 | io_queue_async_work(req); |
| 1221 | } while (!list_empty(&ctx->defer_list)); |
| 1222 | } |
| 1223 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1224 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1225 | { |
| 1226 | while (!list_empty(&ctx->timeout_list)) { |
| 1227 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
| 1228 | struct io_kiocb, list); |
| 1229 | |
| 1230 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 1231 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1232 | if (req->timeout.target_seq != ctx->cached_cq_tail |
| 1233 | - atomic_read(&ctx->cq_timeouts)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1234 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1235 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1236 | list_del_init(&req->list); |
| 1237 | io_kill_timeout(req); |
| 1238 | } |
| 1239 | } |
| 1240 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1241 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1242 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1243 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1244 | __io_commit_cqring(ctx); |
| 1245 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1246 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1247 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1248 | } |
| 1249 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1250 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1251 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1252 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1253 | unsigned tail; |
| 1254 | |
| 1255 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1256 | /* |
| 1257 | * writes to the cq entry need to come after reading head; the |
| 1258 | * control dependency is enough as we're using WRITE_ONCE to |
| 1259 | * fill the cq entry |
| 1260 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1261 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1262 | return NULL; |
| 1263 | |
| 1264 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1265 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1268 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1269 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1270 | if (!ctx->cq_ev_fd) |
| 1271 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1272 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1273 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1274 | if (!ctx->eventfd_async) |
| 1275 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1276 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1279 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1280 | { |
| 1281 | if (waitqueue_active(&ctx->wait)) |
| 1282 | wake_up(&ctx->wait); |
| 1283 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1284 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1285 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1286 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1287 | } |
| 1288 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1289 | /* Returns true if there are no backlogged entries after the flush */ |
| 1290 | 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] | 1291 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1292 | struct io_rings *rings = ctx->rings; |
| 1293 | struct io_uring_cqe *cqe; |
| 1294 | struct io_kiocb *req; |
| 1295 | unsigned long flags; |
| 1296 | LIST_HEAD(list); |
| 1297 | |
| 1298 | if (!force) { |
| 1299 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1300 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1301 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1302 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1303 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1307 | |
| 1308 | /* if force is set, the ring is going away. always drop after that */ |
| 1309 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1310 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1311 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1312 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1313 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1314 | cqe = io_get_cqring(ctx); |
| 1315 | if (!cqe && !force) |
| 1316 | break; |
| 1317 | |
| 1318 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1319 | list); |
| 1320 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1321 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1322 | if (cqe) { |
| 1323 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1324 | WRITE_ONCE(cqe->res, req->result); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1325 | WRITE_ONCE(cqe->flags, req->cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1326 | } else { |
| 1327 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1328 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1333 | if (cqe) { |
| 1334 | clear_bit(0, &ctx->sq_check_overflow); |
| 1335 | clear_bit(0, &ctx->cq_check_overflow); |
| 1336 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1337 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1338 | io_cqring_ev_posted(ctx); |
| 1339 | |
| 1340 | while (!list_empty(&list)) { |
| 1341 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1342 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1343 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1344 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1345 | |
| 1346 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1349 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1350 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1351 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1352 | struct io_uring_cqe *cqe; |
| 1353 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1354 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1355 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1356 | /* |
| 1357 | * If we can't get a cq entry, userspace overflowed the |
| 1358 | * submission (by quite a lot). Increment the overflow count in |
| 1359 | * the ring. |
| 1360 | */ |
| 1361 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1362 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1363 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1364 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1365 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1366 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1367 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1368 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1369 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1370 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1371 | set_bit(0, &ctx->sq_check_overflow); |
| 1372 | set_bit(0, &ctx->cq_check_overflow); |
| 1373 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1374 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1375 | refcount_inc(&req->refs); |
| 1376 | req->result = res; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1377 | req->cflags = cflags; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1378 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1382 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1383 | { |
| 1384 | __io_cqring_fill_event(req, res, 0); |
| 1385 | } |
| 1386 | |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1387 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1388 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1389 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1390 | unsigned long flags; |
| 1391 | |
| 1392 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1393 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1394 | io_commit_cqring(ctx); |
| 1395 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1396 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1397 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1400 | static void io_submit_flush_completions(struct io_comp_state *cs) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1401 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1402 | struct io_ring_ctx *ctx = cs->ctx; |
| 1403 | |
| 1404 | spin_lock_irq(&ctx->completion_lock); |
| 1405 | while (!list_empty(&cs->list)) { |
| 1406 | struct io_kiocb *req; |
| 1407 | |
| 1408 | req = list_first_entry(&cs->list, struct io_kiocb, list); |
| 1409 | list_del(&req->list); |
| 1410 | io_cqring_fill_event(req, req->result); |
| 1411 | if (!(req->flags & REQ_F_LINK_HEAD)) { |
| 1412 | req->flags |= REQ_F_COMP_LOCKED; |
| 1413 | io_put_req(req); |
| 1414 | } else { |
| 1415 | spin_unlock_irq(&ctx->completion_lock); |
| 1416 | io_put_req(req); |
| 1417 | spin_lock_irq(&ctx->completion_lock); |
| 1418 | } |
| 1419 | } |
| 1420 | io_commit_cqring(ctx); |
| 1421 | spin_unlock_irq(&ctx->completion_lock); |
| 1422 | |
| 1423 | io_cqring_ev_posted(ctx); |
| 1424 | cs->nr = 0; |
| 1425 | } |
| 1426 | |
| 1427 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| 1428 | struct io_comp_state *cs) |
| 1429 | { |
| 1430 | if (!cs) { |
| 1431 | io_cqring_add_event(req, res, cflags); |
| 1432 | io_put_req(req); |
| 1433 | } else { |
| 1434 | req->result = res; |
| 1435 | list_add_tail(&req->list, &cs->list); |
| 1436 | if (++cs->nr >= 32) |
| 1437 | io_submit_flush_completions(cs); |
| 1438 | } |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static void io_req_complete(struct io_kiocb *req, long res) |
| 1442 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1443 | __io_req_complete(req, res, 0, NULL); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1446 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1447 | { |
| 1448 | return req == (struct io_kiocb *) |
| 1449 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1450 | } |
| 1451 | |
| 1452 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1453 | { |
| 1454 | struct io_kiocb *req; |
| 1455 | |
| 1456 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1457 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1458 | return req; |
| 1459 | |
| 1460 | return NULL; |
| 1461 | } |
| 1462 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1463 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1464 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1465 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1466 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1467 | struct io_kiocb *req; |
| 1468 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1469 | if (!state->free_reqs) { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1470 | size_t sz; |
| 1471 | int ret; |
| 1472 | |
| 1473 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1474 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1475 | |
| 1476 | /* |
| 1477 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1478 | * retry single alloc to be on the safe side. |
| 1479 | */ |
| 1480 | if (unlikely(ret <= 0)) { |
| 1481 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1482 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1483 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1484 | ret = 1; |
| 1485 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1486 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1487 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1488 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1489 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1490 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1493 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1494 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1495 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1498 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1499 | bool fixed) |
| 1500 | { |
| 1501 | if (fixed) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 1502 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1503 | else |
| 1504 | fput(file); |
| 1505 | } |
| 1506 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1507 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1508 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1509 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1510 | io_cleanup_req(req); |
| 1511 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1512 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1513 | if (req->file) |
| 1514 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 1515 | __io_put_req_task(req); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1516 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | static void __io_free_req(struct io_kiocb *req) |
| 1520 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1521 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1522 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1523 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1524 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1525 | unsigned long flags; |
| 1526 | |
| 1527 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1528 | list_del(&req->inflight_entry); |
| 1529 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1530 | wake_up(&ctx->inflight_wait); |
| 1531 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1532 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1533 | |
| 1534 | percpu_ref_put(&req->ctx->refs); |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 1535 | if (likely(!io_is_fallback_req(req))) |
| 1536 | kmem_cache_free(req_cachep, req); |
| 1537 | else |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1538 | clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1539 | } |
| 1540 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1541 | struct req_batch { |
| 1542 | void *reqs[IO_IOPOLL_BATCH]; |
| 1543 | int to_free; |
| 1544 | int need_iter; |
| 1545 | }; |
| 1546 | |
| 1547 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1548 | { |
| 1549 | if (!rb->to_free) |
| 1550 | return; |
| 1551 | if (rb->need_iter) { |
| 1552 | int i, inflight = 0; |
| 1553 | unsigned long flags; |
| 1554 | |
| 1555 | for (i = 0; i < rb->to_free; i++) { |
| 1556 | struct io_kiocb *req = rb->reqs[i]; |
| 1557 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1558 | if (req->flags & REQ_F_INFLIGHT) |
| 1559 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1560 | __io_req_aux_free(req); |
| 1561 | } |
| 1562 | if (!inflight) |
| 1563 | goto do_free; |
| 1564 | |
| 1565 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1566 | for (i = 0; i < rb->to_free; i++) { |
| 1567 | struct io_kiocb *req = rb->reqs[i]; |
| 1568 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1569 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1570 | list_del(&req->inflight_entry); |
| 1571 | if (!--inflight) |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1576 | |
| 1577 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1578 | wake_up(&ctx->inflight_wait); |
| 1579 | } |
| 1580 | do_free: |
| 1581 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1582 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1583 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1584 | } |
| 1585 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1586 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1587 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1588 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1589 | int ret; |
| 1590 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1591 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1592 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1593 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1594 | io_commit_cqring(ctx); |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1595 | req->flags &= ~REQ_F_LINK_HEAD; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1596 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1597 | return true; |
| 1598 | } |
| 1599 | |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1603 | 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] | 1604 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1605 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1606 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1607 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1608 | /* Already got next link */ |
| 1609 | if (req->flags & REQ_F_LINK_NEXT) |
| 1610 | return; |
| 1611 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1612 | /* |
| 1613 | * The list should never be empty when we are called here. But could |
| 1614 | * potentially happen if the chain is messed up, check to be on the |
| 1615 | * safe side. |
| 1616 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1617 | while (!list_empty(&req->link_list)) { |
| 1618 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1619 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1620 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1621 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1622 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1623 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1624 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1625 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1626 | continue; |
| 1627 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1628 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1629 | list_del_init(&req->link_list); |
| 1630 | if (!list_empty(&nxt->link_list)) |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1631 | nxt->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1632 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1633 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1634 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1635 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1636 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1637 | if (wake_ev) |
| 1638 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /* |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1642 | * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1643 | */ |
| 1644 | static void io_fail_links(struct io_kiocb *req) |
| 1645 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1646 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1647 | unsigned long flags; |
| 1648 | |
| 1649 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1650 | |
| 1651 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1652 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1653 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1654 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1655 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1656 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1657 | |
| 1658 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1659 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1660 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1661 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1662 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1663 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1664 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1665 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1666 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1667 | |
| 1668 | io_commit_cqring(ctx); |
| 1669 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1670 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1671 | } |
| 1672 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1673 | 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] | 1674 | { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1675 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1676 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1677 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1678 | /* |
| 1679 | * If LINK is set, we have dependent requests in this chain. If we |
| 1680 | * didn't fail this request, queue the first one up, moving any other |
| 1681 | * dependencies to the next request. In case of failure, fail the rest |
| 1682 | * of the chain. |
| 1683 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1684 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1685 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1686 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1687 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1688 | struct io_ring_ctx *ctx = req->ctx; |
| 1689 | unsigned long flags; |
| 1690 | |
| 1691 | /* |
| 1692 | * If this is a timeout link, we could be racing with the |
| 1693 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1694 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1695 | */ |
| 1696 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1697 | io_req_link_next(req, nxt); |
| 1698 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1699 | } else { |
| 1700 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1701 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1702 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1703 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1704 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1705 | { |
| 1706 | struct io_ring_ctx *ctx = req->ctx; |
| 1707 | |
| 1708 | spin_lock_irq(&ctx->completion_lock); |
| 1709 | io_cqring_fill_event(req, error); |
| 1710 | io_commit_cqring(ctx); |
| 1711 | spin_unlock_irq(&ctx->completion_lock); |
| 1712 | |
| 1713 | io_cqring_ev_posted(ctx); |
| 1714 | req_set_fail_links(req); |
| 1715 | io_double_put_req(req); |
| 1716 | } |
| 1717 | |
| 1718 | static void io_req_task_cancel(struct callback_head *cb) |
| 1719 | { |
| 1720 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1721 | |
| 1722 | __io_req_task_cancel(req, -ECANCELED); |
| 1723 | } |
| 1724 | |
| 1725 | static void __io_req_task_submit(struct io_kiocb *req) |
| 1726 | { |
| 1727 | struct io_ring_ctx *ctx = req->ctx; |
| 1728 | |
| 1729 | __set_current_state(TASK_RUNNING); |
| 1730 | if (!__io_sq_thread_acquire_mm(ctx)) { |
| 1731 | mutex_lock(&ctx->uring_lock); |
| 1732 | __io_queue_sqe(req, NULL, NULL); |
| 1733 | mutex_unlock(&ctx->uring_lock); |
| 1734 | } else { |
| 1735 | __io_req_task_cancel(req, -EFAULT); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | static void io_req_task_submit(struct callback_head *cb) |
| 1740 | { |
| 1741 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1742 | |
| 1743 | __io_req_task_submit(req); |
| 1744 | } |
| 1745 | |
| 1746 | static void io_req_task_queue(struct io_kiocb *req) |
| 1747 | { |
| 1748 | struct task_struct *tsk = req->task; |
| 1749 | int ret; |
| 1750 | |
| 1751 | init_task_work(&req->task_work, io_req_task_submit); |
| 1752 | |
| 1753 | ret = task_work_add(tsk, &req->task_work, true); |
| 1754 | if (unlikely(ret)) { |
| 1755 | init_task_work(&req->task_work, io_req_task_cancel); |
| 1756 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 1757 | task_work_add(tsk, &req->task_work, true); |
| 1758 | } |
| 1759 | wake_up_process(tsk); |
| 1760 | } |
| 1761 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1762 | static void io_free_req(struct io_kiocb *req) |
| 1763 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1764 | struct io_kiocb *nxt = NULL; |
| 1765 | |
| 1766 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1767 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1768 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1769 | if (nxt) { |
| 1770 | if (nxt->flags & REQ_F_WORK_INITIALIZED) |
| 1771 | io_queue_async_work(nxt); |
| 1772 | else |
| 1773 | io_req_task_queue(nxt); |
| 1774 | } |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1775 | } |
| 1776 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1777 | /* |
| 1778 | * Drop reference to request, return next in chain (if there is one) if this |
| 1779 | * was the last reference to this request. |
| 1780 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1781 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1782 | 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] | 1783 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1784 | if (refcount_dec_and_test(&req->refs)) { |
| 1785 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1786 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1787 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1790 | static void io_put_req(struct io_kiocb *req) |
| 1791 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1792 | if (refcount_dec_and_test(&req->refs)) |
| 1793 | io_free_req(req); |
| 1794 | } |
| 1795 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1796 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1797 | { |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1798 | struct io_kiocb *link, *nxt = NULL; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1799 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1800 | /* |
| 1801 | * A ref is owned by io-wq in which context we're. So, if that's the |
| 1802 | * last one, it's safe to steal next work. False negatives are Ok, |
| 1803 | * it just will be re-punted async in io_put_work() |
| 1804 | */ |
| 1805 | if (refcount_read(&req->refs) != 1) |
| 1806 | return NULL; |
| 1807 | |
| 1808 | io_req_find_next(req, &nxt); |
| 1809 | if (!nxt) |
| 1810 | return NULL; |
| 1811 | |
| 1812 | if ((nxt->flags & REQ_F_ISREG) && io_op_defs[nxt->opcode].hash_reg_file) |
| 1813 | io_wq_hash_work(&nxt->work, file_inode(nxt->file)); |
| 1814 | |
| 1815 | link = io_prep_linked_timeout(nxt); |
| 1816 | if (link) |
| 1817 | nxt->flags |= REQ_F_QUEUE_TIMEOUT; |
| 1818 | return &nxt->work; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1819 | } |
| 1820 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1821 | /* |
| 1822 | * Must only be used if we don't need to care about links, usually from |
| 1823 | * within the completion handling itself. |
| 1824 | */ |
| 1825 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1826 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1827 | /* drop both submit and complete references */ |
| 1828 | if (refcount_sub_and_test(2, &req->refs)) |
| 1829 | __io_free_req(req); |
| 1830 | } |
| 1831 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1832 | static void io_double_put_req(struct io_kiocb *req) |
| 1833 | { |
| 1834 | /* drop both submit and complete references */ |
| 1835 | if (refcount_sub_and_test(2, &req->refs)) |
| 1836 | io_free_req(req); |
| 1837 | } |
| 1838 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1839 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1840 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1841 | struct io_rings *rings = ctx->rings; |
| 1842 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1843 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1844 | /* |
| 1845 | * noflush == true is from the waitqueue handler, just ensure |
| 1846 | * we wake up the task, and the next invocation will flush the |
| 1847 | * entries. We cannot safely to it from here. |
| 1848 | */ |
| 1849 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1850 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1851 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1852 | io_cqring_overflow_flush(ctx, false); |
| 1853 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1854 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1855 | /* See comment at the top of this file */ |
| 1856 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1857 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1858 | } |
| 1859 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1860 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1861 | { |
| 1862 | struct io_rings *rings = ctx->rings; |
| 1863 | |
| 1864 | /* make sure SQ entry isn't read before tail */ |
| 1865 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1866 | } |
| 1867 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1868 | static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1869 | { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1870 | if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req)) |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1871 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1872 | |
Jens Axboe | 9d9e88a | 2020-05-13 12:53:19 -0600 | [diff] [blame] | 1873 | if (req->file || req->io) |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1874 | rb->need_iter++; |
| 1875 | |
| 1876 | rb->reqs[rb->to_free++] = req; |
| 1877 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1878 | io_free_req_many(req->ctx, rb); |
| 1879 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1882 | static int io_put_kbuf(struct io_kiocb *req) |
| 1883 | { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1884 | struct io_buffer *kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1885 | int cflags; |
| 1886 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1887 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1888 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1889 | cflags |= IORING_CQE_F_BUFFER; |
| 1890 | req->rw.addr = 0; |
| 1891 | kfree(kbuf); |
| 1892 | return cflags; |
| 1893 | } |
| 1894 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1895 | static void io_iopoll_queue(struct list_head *again) |
| 1896 | { |
| 1897 | struct io_kiocb *req; |
| 1898 | |
| 1899 | do { |
| 1900 | req = list_first_entry(again, struct io_kiocb, list); |
| 1901 | list_del(&req->list); |
Pavel Begunkov | d60b5fb | 2020-06-25 12:37:11 +0300 | [diff] [blame] | 1902 | |
| 1903 | /* shouldn't happen unless io_uring is dying, cancel reqs */ |
| 1904 | if (unlikely(!current->mm)) { |
Jens Axboe | 2237d76 | 2020-06-26 13:44:16 -0600 | [diff] [blame] | 1905 | io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL); |
Pavel Begunkov | d60b5fb | 2020-06-25 12:37:11 +0300 | [diff] [blame] | 1906 | io_put_req(req); |
| 1907 | continue; |
| 1908 | } |
| 1909 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1910 | refcount_inc(&req->refs); |
| 1911 | io_queue_async_work(req); |
| 1912 | } while (!list_empty(again)); |
| 1913 | } |
| 1914 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1915 | /* |
| 1916 | * Find and free completed poll iocbs |
| 1917 | */ |
| 1918 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1919 | struct list_head *done) |
| 1920 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1921 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1922 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1923 | LIST_HEAD(again); |
| 1924 | |
| 1925 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 1926 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1927 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1928 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1929 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1930 | int cflags = 0; |
| 1931 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1932 | req = list_first_entry(done, struct io_kiocb, list); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1933 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 1934 | req->iopoll_completed = 0; |
| 1935 | list_move_tail(&req->list, &again); |
| 1936 | continue; |
| 1937 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1938 | list_del(&req->list); |
| 1939 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1940 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1941 | cflags = io_put_kbuf(req); |
| 1942 | |
| 1943 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1944 | (*nr_events)++; |
| 1945 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1946 | if (refcount_dec_and_test(&req->refs) && |
| 1947 | !io_req_multi_free(&rb, req)) |
| 1948 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1949 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1950 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1951 | io_commit_cqring(ctx); |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 1952 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 1953 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1954 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1955 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1956 | if (!list_empty(&again)) |
| 1957 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1960 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1961 | long min) |
| 1962 | { |
| 1963 | struct io_kiocb *req, *tmp; |
| 1964 | LIST_HEAD(done); |
| 1965 | bool spin; |
| 1966 | int ret; |
| 1967 | |
| 1968 | /* |
| 1969 | * Only spin for completions if we don't have multiple devices hanging |
| 1970 | * off our complete list, and we're under the requested amount. |
| 1971 | */ |
| 1972 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1973 | |
| 1974 | ret = 0; |
| 1975 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1976 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1977 | |
| 1978 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 1979 | * Move completed and retryable entries to our local lists. |
| 1980 | * If we find a request that requires polling, break out |
| 1981 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1982 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 1983 | if (READ_ONCE(req->iopoll_completed)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1984 | list_move_tail(&req->list, &done); |
| 1985 | continue; |
| 1986 | } |
| 1987 | if (!list_empty(&done)) |
| 1988 | break; |
| 1989 | |
| 1990 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1991 | if (ret < 0) |
| 1992 | break; |
| 1993 | |
| 1994 | if (ret && spin) |
| 1995 | spin = false; |
| 1996 | ret = 0; |
| 1997 | } |
| 1998 | |
| 1999 | if (!list_empty(&done)) |
| 2000 | io_iopoll_complete(ctx, nr_events, &done); |
| 2001 | |
| 2002 | return ret; |
| 2003 | } |
| 2004 | |
| 2005 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2006 | * 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] | 2007 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2008 | * as a non-spinning completion check. |
| 2009 | */ |
| 2010 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2011 | long min) |
| 2012 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2013 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2014 | int ret; |
| 2015 | |
| 2016 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2017 | if (ret < 0) |
| 2018 | return ret; |
| 2019 | if (!min || *nr_events >= min) |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| 2023 | return 1; |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * We can't just wait for polled events to come to us, we have to actively |
| 2028 | * find and complete them. |
| 2029 | */ |
| 2030 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 2031 | { |
| 2032 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2033 | return; |
| 2034 | |
| 2035 | mutex_lock(&ctx->uring_lock); |
| 2036 | while (!list_empty(&ctx->poll_list)) { |
| 2037 | unsigned int nr_events = 0; |
| 2038 | |
| 2039 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2040 | |
| 2041 | /* |
| 2042 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2043 | * in this case we need to ensure that we reap all events. |
| 2044 | */ |
| 2045 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2046 | } |
| 2047 | mutex_unlock(&ctx->uring_lock); |
| 2048 | } |
| 2049 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2050 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 2051 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2052 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2053 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2054 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2055 | /* |
| 2056 | * We disallow the app entering submit/complete with polling, but we |
| 2057 | * still need to lock the ring to prevent racing with polled issue |
| 2058 | * that got punted to a workqueue. |
| 2059 | */ |
| 2060 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2061 | do { |
| 2062 | int tmin = 0; |
| 2063 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2064 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2065 | * Don't enter poll loop if we already have events pending. |
| 2066 | * If we do, we can potentially be spinning for commands that |
| 2067 | * already triggered a CQE (eg in error). |
| 2068 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2069 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2070 | break; |
| 2071 | |
| 2072 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2073 | * If a submit got punted to a workqueue, we can have the |
| 2074 | * application entering polling for a command before it gets |
| 2075 | * issued. That app will hold the uring_lock for the duration |
| 2076 | * of the poll right here, so we need to take a breather every |
| 2077 | * now and then to ensure that the issue has a chance to add |
| 2078 | * the poll to the issued list. Otherwise we can spin here |
| 2079 | * forever, while the workqueue is stuck trying to acquire the |
| 2080 | * very same mutex. |
| 2081 | */ |
| 2082 | if (!(++iters & 7)) { |
| 2083 | mutex_unlock(&ctx->uring_lock); |
| 2084 | mutex_lock(&ctx->uring_lock); |
| 2085 | } |
| 2086 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2087 | if (*nr_events < min) |
| 2088 | tmin = min - *nr_events; |
| 2089 | |
| 2090 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 2091 | if (ret <= 0) |
| 2092 | break; |
| 2093 | ret = 0; |
| 2094 | } while (min && !*nr_events && !need_resched()); |
| 2095 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2096 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2097 | return ret; |
| 2098 | } |
| 2099 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2100 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2101 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2102 | /* |
| 2103 | * Tell lockdep we inherited freeze protection from submission |
| 2104 | * thread. |
| 2105 | */ |
| 2106 | if (req->flags & REQ_F_ISREG) { |
| 2107 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2108 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2109 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2110 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2111 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2114 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 2115 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2116 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2117 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2118 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2119 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2120 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2121 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2122 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2123 | if (res != req->result) |
| 2124 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2125 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2126 | cflags = io_put_kbuf(req); |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2127 | __io_req_complete(req, res, cflags, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2128 | } |
| 2129 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2130 | #ifdef CONFIG_BLOCK |
| 2131 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2132 | { |
| 2133 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2134 | ssize_t ret = -ECANCELED; |
| 2135 | struct iov_iter iter; |
| 2136 | int rw; |
| 2137 | |
| 2138 | if (error) { |
| 2139 | ret = error; |
| 2140 | goto end_req; |
| 2141 | } |
| 2142 | |
| 2143 | switch (req->opcode) { |
| 2144 | case IORING_OP_READV: |
| 2145 | case IORING_OP_READ_FIXED: |
| 2146 | case IORING_OP_READ: |
| 2147 | rw = READ; |
| 2148 | break; |
| 2149 | case IORING_OP_WRITEV: |
| 2150 | case IORING_OP_WRITE_FIXED: |
| 2151 | case IORING_OP_WRITE: |
| 2152 | rw = WRITE; |
| 2153 | break; |
| 2154 | default: |
| 2155 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2156 | req->opcode); |
| 2157 | goto end_req; |
| 2158 | } |
| 2159 | |
| 2160 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2161 | if (ret < 0) |
| 2162 | goto end_req; |
| 2163 | ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter); |
| 2164 | if (!ret) |
| 2165 | return true; |
| 2166 | kfree(iovec); |
| 2167 | end_req: |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2168 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 2169 | io_req_complete(req, ret); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2170 | return false; |
| 2171 | } |
| 2172 | |
| 2173 | static void io_rw_resubmit(struct callback_head *cb) |
| 2174 | { |
| 2175 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2176 | struct io_ring_ctx *ctx = req->ctx; |
| 2177 | int err; |
| 2178 | |
| 2179 | __set_current_state(TASK_RUNNING); |
| 2180 | |
| 2181 | err = io_sq_thread_acquire_mm(ctx, req); |
| 2182 | |
| 2183 | if (io_resubmit_prep(req, err)) { |
| 2184 | refcount_inc(&req->refs); |
| 2185 | io_queue_async_work(req); |
| 2186 | } |
| 2187 | } |
| 2188 | #endif |
| 2189 | |
| 2190 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2191 | { |
| 2192 | #ifdef CONFIG_BLOCK |
| 2193 | struct task_struct *tsk; |
| 2194 | int ret; |
| 2195 | |
| 2196 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2197 | return false; |
| 2198 | |
| 2199 | tsk = req->task; |
| 2200 | init_task_work(&req->task_work, io_rw_resubmit); |
| 2201 | ret = task_work_add(tsk, &req->task_work, true); |
| 2202 | if (!ret) |
| 2203 | return true; |
| 2204 | #endif |
| 2205 | return false; |
| 2206 | } |
| 2207 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2208 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2209 | struct io_comp_state *cs) |
| 2210 | { |
| 2211 | if (!io_rw_reissue(req, res)) |
| 2212 | io_complete_rw_common(&req->rw.kiocb, res, cs); |
| 2213 | } |
| 2214 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2215 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2216 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2217 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2218 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2219 | __io_complete_rw(req, res, res2, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2222 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2223 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2224 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2225 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2226 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2227 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2228 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2229 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2230 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2231 | |
| 2232 | WRITE_ONCE(req->result, res); |
| 2233 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2234 | smp_wmb(); |
| 2235 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2240 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2241 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2242 | * accessing the kiocb cookie. |
| 2243 | */ |
| 2244 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 2245 | { |
| 2246 | struct io_ring_ctx *ctx = req->ctx; |
| 2247 | |
| 2248 | /* |
| 2249 | * Track whether we have multiple files in our lists. This will impact |
| 2250 | * how we do polling eventually, not spinning if we're on potentially |
| 2251 | * different devices. |
| 2252 | */ |
| 2253 | if (list_empty(&ctx->poll_list)) { |
| 2254 | ctx->poll_multi_file = false; |
| 2255 | } else if (!ctx->poll_multi_file) { |
| 2256 | struct io_kiocb *list_req; |
| 2257 | |
| 2258 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 2259 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2260 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2261 | ctx->poll_multi_file = true; |
| 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * For fast devices, IO may have already completed. If it has, add |
| 2266 | * it to the front so we find it first. |
| 2267 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2268 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2269 | list_add(&req->list, &ctx->poll_list); |
| 2270 | else |
| 2271 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2272 | |
| 2273 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2274 | wq_has_sleeper(&ctx->sqo_wait)) |
| 2275 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2278 | static void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2279 | { |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2280 | int diff = state->has_refs - state->used_refs; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2281 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2282 | if (diff) |
| 2283 | fput_many(state->file, diff); |
| 2284 | state->file = NULL; |
| 2285 | } |
| 2286 | |
| 2287 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2288 | { |
| 2289 | if (state->file) |
| 2290 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * Get as many references to a file as we have IOs left in this submission, |
| 2295 | * assuming most submissions are for one file, or at least that each file |
| 2296 | * has more than one submission. |
| 2297 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2298 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2299 | { |
| 2300 | if (!state) |
| 2301 | return fget(fd); |
| 2302 | |
| 2303 | if (state->file) { |
| 2304 | if (state->fd == fd) { |
| 2305 | state->used_refs++; |
| 2306 | state->ios_left--; |
| 2307 | return state->file; |
| 2308 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2309 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2310 | } |
| 2311 | state->file = fget_many(fd, state->ios_left); |
| 2312 | if (!state->file) |
| 2313 | return NULL; |
| 2314 | |
| 2315 | state->fd = fd; |
| 2316 | state->has_refs = state->ios_left; |
| 2317 | state->used_refs = 1; |
| 2318 | state->ios_left--; |
| 2319 | return state->file; |
| 2320 | } |
| 2321 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2322 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2323 | { |
| 2324 | #ifdef CONFIG_BLOCK |
| 2325 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); |
| 2326 | #else |
| 2327 | return true; |
| 2328 | #endif |
| 2329 | } |
| 2330 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2331 | /* |
| 2332 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2333 | * any file. For now, just ensure that anything potentially problematic is done |
| 2334 | * inline. |
| 2335 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2336 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2337 | { |
| 2338 | umode_t mode = file_inode(file)->i_mode; |
| 2339 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2340 | if (S_ISBLK(mode)) { |
| 2341 | if (io_bdev_nowait(file->f_inode->i_bdev)) |
| 2342 | return true; |
| 2343 | return false; |
| 2344 | } |
| 2345 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2346 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2347 | if (S_ISREG(mode)) { |
| 2348 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
| 2349 | file->f_op != &io_uring_fops) |
| 2350 | return true; |
| 2351 | return false; |
| 2352 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2353 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2354 | /* any ->read/write should understand O_NONBLOCK */ |
| 2355 | if (file->f_flags & O_NONBLOCK) |
| 2356 | return true; |
| 2357 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2358 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2359 | return false; |
| 2360 | |
| 2361 | if (rw == READ) |
| 2362 | return file->f_op->read_iter != NULL; |
| 2363 | |
| 2364 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2367 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2368 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2369 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2370 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2371 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2372 | unsigned ioprio; |
| 2373 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2374 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2375 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2376 | req->flags |= REQ_F_ISREG; |
| 2377 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2378 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2379 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2380 | req->flags |= REQ_F_CUR_POS; |
| 2381 | kiocb->ki_pos = req->file->f_pos; |
| 2382 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2383 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2384 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2385 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2386 | if (unlikely(ret)) |
| 2387 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2388 | |
| 2389 | ioprio = READ_ONCE(sqe->ioprio); |
| 2390 | if (ioprio) { |
| 2391 | ret = ioprio_check_cap(ioprio); |
| 2392 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2393 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2394 | |
| 2395 | kiocb->ki_ioprio = ioprio; |
| 2396 | } else |
| 2397 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2398 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2399 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2400 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2401 | req->flags |= REQ_F_NOWAIT; |
| 2402 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2403 | if (kiocb->ki_flags & IOCB_DIRECT) |
| 2404 | io_get_req_task(req); |
| 2405 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2406 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2407 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2408 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2409 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2410 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2411 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2412 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2413 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2414 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2415 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 2416 | req->result = 0; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2417 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2418 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2419 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2420 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2421 | kiocb->ki_complete = io_complete_rw; |
| 2422 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2423 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2424 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2425 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2426 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2427 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2431 | { |
| 2432 | switch (ret) { |
| 2433 | case -EIOCBQUEUED: |
| 2434 | break; |
| 2435 | case -ERESTARTSYS: |
| 2436 | case -ERESTARTNOINTR: |
| 2437 | case -ERESTARTNOHAND: |
| 2438 | case -ERESTART_RESTARTBLOCK: |
| 2439 | /* |
| 2440 | * We can't just restart the syscall, since previously |
| 2441 | * submitted sqes may already be in progress. Just fail this |
| 2442 | * IO with EINTR. |
| 2443 | */ |
| 2444 | ret = -EINTR; |
| 2445 | /* fall through */ |
| 2446 | default: |
| 2447 | kiocb->ki_complete(kiocb, ret, 0); |
| 2448 | } |
| 2449 | } |
| 2450 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2451 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| 2452 | struct io_comp_state *cs) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2453 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2454 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2455 | |
| 2456 | if (req->flags & REQ_F_CUR_POS) |
| 2457 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2458 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2459 | __io_complete_rw(req, ret, 0, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2460 | else |
| 2461 | io_rw_done(kiocb, ret); |
| 2462 | } |
| 2463 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2464 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2465 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2466 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2467 | struct io_ring_ctx *ctx = req->ctx; |
| 2468 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2469 | struct io_mapped_ubuf *imu; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2470 | u16 index, buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2471 | size_t offset; |
| 2472 | u64 buf_addr; |
| 2473 | |
| 2474 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2475 | if (unlikely(!ctx->user_bufs)) |
| 2476 | return -EFAULT; |
| 2477 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2478 | buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2479 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2480 | return -EFAULT; |
| 2481 | |
| 2482 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2483 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2484 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2485 | |
| 2486 | /* overflow */ |
| 2487 | if (buf_addr + len < buf_addr) |
| 2488 | return -EFAULT; |
| 2489 | /* not inside the mapped region */ |
| 2490 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2491 | return -EFAULT; |
| 2492 | |
| 2493 | /* |
| 2494 | * May not be a start of buffer, set size appropriately |
| 2495 | * and advance us to the beginning. |
| 2496 | */ |
| 2497 | offset = buf_addr - imu->ubuf; |
| 2498 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2499 | |
| 2500 | if (offset) { |
| 2501 | /* |
| 2502 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2503 | * using the latter parts of a big fixed buffer - it iterates |
| 2504 | * over each segment manually. We can cheat a bit here, because |
| 2505 | * we know that: |
| 2506 | * |
| 2507 | * 1) it's a BVEC iter, we set it up |
| 2508 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2509 | * first and last bvec |
| 2510 | * |
| 2511 | * So just find our index, and adjust the iterator afterwards. |
| 2512 | * If the offset is within the first bvec (or the whole first |
| 2513 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2514 | * since we can just skip the first segment, which may not |
| 2515 | * be PAGE_SIZE aligned. |
| 2516 | */ |
| 2517 | const struct bio_vec *bvec = imu->bvec; |
| 2518 | |
| 2519 | if (offset <= bvec->bv_len) { |
| 2520 | iov_iter_advance(iter, offset); |
| 2521 | } else { |
| 2522 | unsigned long seg_skip; |
| 2523 | |
| 2524 | /* skip first vec */ |
| 2525 | offset -= bvec->bv_len; |
| 2526 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2527 | |
| 2528 | iter->bvec = bvec + seg_skip; |
| 2529 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2530 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2531 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2535 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2538 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2539 | { |
| 2540 | if (needs_lock) |
| 2541 | mutex_unlock(&ctx->uring_lock); |
| 2542 | } |
| 2543 | |
| 2544 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2545 | { |
| 2546 | /* |
| 2547 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2548 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2549 | * The only exception is when we've detached the request and issue it |
| 2550 | * from an async worker thread, grab the lock for that case. |
| 2551 | */ |
| 2552 | if (needs_lock) |
| 2553 | mutex_lock(&ctx->uring_lock); |
| 2554 | } |
| 2555 | |
| 2556 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2557 | int bgid, struct io_buffer *kbuf, |
| 2558 | bool needs_lock) |
| 2559 | { |
| 2560 | struct io_buffer *head; |
| 2561 | |
| 2562 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2563 | return kbuf; |
| 2564 | |
| 2565 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2566 | |
| 2567 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2568 | |
| 2569 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2570 | if (head) { |
| 2571 | if (!list_empty(&head->list)) { |
| 2572 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2573 | list); |
| 2574 | list_del(&kbuf->list); |
| 2575 | } else { |
| 2576 | kbuf = head; |
| 2577 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2578 | } |
| 2579 | if (*len > kbuf->len) |
| 2580 | *len = kbuf->len; |
| 2581 | } else { |
| 2582 | kbuf = ERR_PTR(-ENOBUFS); |
| 2583 | } |
| 2584 | |
| 2585 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2586 | |
| 2587 | return kbuf; |
| 2588 | } |
| 2589 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2590 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2591 | bool needs_lock) |
| 2592 | { |
| 2593 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2594 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2595 | |
| 2596 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2597 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2598 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2599 | if (IS_ERR(kbuf)) |
| 2600 | return kbuf; |
| 2601 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2602 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2603 | return u64_to_user_ptr(kbuf->addr); |
| 2604 | } |
| 2605 | |
| 2606 | #ifdef CONFIG_COMPAT |
| 2607 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2608 | bool needs_lock) |
| 2609 | { |
| 2610 | struct compat_iovec __user *uiov; |
| 2611 | compat_ssize_t clen; |
| 2612 | void __user *buf; |
| 2613 | ssize_t len; |
| 2614 | |
| 2615 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2616 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2617 | return -EFAULT; |
| 2618 | if (__get_user(clen, &uiov->iov_len)) |
| 2619 | return -EFAULT; |
| 2620 | if (clen < 0) |
| 2621 | return -EINVAL; |
| 2622 | |
| 2623 | len = clen; |
| 2624 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2625 | if (IS_ERR(buf)) |
| 2626 | return PTR_ERR(buf); |
| 2627 | iov[0].iov_base = buf; |
| 2628 | iov[0].iov_len = (compat_size_t) len; |
| 2629 | return 0; |
| 2630 | } |
| 2631 | #endif |
| 2632 | |
| 2633 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2634 | bool needs_lock) |
| 2635 | { |
| 2636 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2637 | void __user *buf; |
| 2638 | ssize_t len; |
| 2639 | |
| 2640 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2641 | return -EFAULT; |
| 2642 | |
| 2643 | len = iov[0].iov_len; |
| 2644 | if (len < 0) |
| 2645 | return -EINVAL; |
| 2646 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2647 | if (IS_ERR(buf)) |
| 2648 | return PTR_ERR(buf); |
| 2649 | iov[0].iov_base = buf; |
| 2650 | iov[0].iov_len = len; |
| 2651 | return 0; |
| 2652 | } |
| 2653 | |
| 2654 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2655 | bool needs_lock) |
| 2656 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2657 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2658 | struct io_buffer *kbuf; |
| 2659 | |
| 2660 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2661 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2662 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2663 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2664 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2665 | if (!req->rw.len) |
| 2666 | return 0; |
| 2667 | else if (req->rw.len > 1) |
| 2668 | return -EINVAL; |
| 2669 | |
| 2670 | #ifdef CONFIG_COMPAT |
| 2671 | if (req->ctx->compat) |
| 2672 | return io_compat_import(req, iov, needs_lock); |
| 2673 | #endif |
| 2674 | |
| 2675 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2676 | } |
| 2677 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2678 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2679 | struct iovec **iovec, struct iov_iter *iter, |
| 2680 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2681 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2682 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2683 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2684 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2685 | u8 opcode; |
| 2686 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2687 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2688 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2689 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2690 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2691 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2692 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2693 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2694 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2695 | return -EINVAL; |
| 2696 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2697 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2698 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2699 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
| 2700 | if (IS_ERR(buf)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2701 | *iovec = NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2702 | return PTR_ERR(buf); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2703 | } |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2704 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2707 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2708 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2709 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2712 | if (req->io) { |
| 2713 | struct io_async_rw *iorw = &req->io->rw; |
| 2714 | |
| 2715 | *iovec = iorw->iov; |
| 2716 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2717 | if (iorw->iov == iorw->fast_iov) |
| 2718 | *iovec = NULL; |
| 2719 | return iorw->size; |
| 2720 | } |
| 2721 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2722 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2723 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2724 | if (!ret) { |
| 2725 | ret = (*iovec)->iov_len; |
| 2726 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 2727 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2728 | *iovec = NULL; |
| 2729 | return ret; |
| 2730 | } |
| 2731 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2732 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2733 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2734 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2735 | iovec, iter); |
| 2736 | #endif |
| 2737 | |
| 2738 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2739 | } |
| 2740 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2741 | /* |
| 2742 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2743 | * by looping over ->read() or ->write() manually. |
| 2744 | */ |
| 2745 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2746 | struct iov_iter *iter) |
| 2747 | { |
| 2748 | ssize_t ret = 0; |
| 2749 | |
| 2750 | /* |
| 2751 | * Don't support polled IO through this interface, and we can't |
| 2752 | * support non-blocking either. For the latter, this just causes |
| 2753 | * the kiocb to be handled from an async context. |
| 2754 | */ |
| 2755 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2756 | return -EOPNOTSUPP; |
| 2757 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2758 | return -EAGAIN; |
| 2759 | |
| 2760 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2761 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2762 | ssize_t nr; |
| 2763 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2764 | if (!iov_iter_is_bvec(iter)) { |
| 2765 | iovec = iov_iter_iovec(iter); |
| 2766 | } else { |
| 2767 | /* fixed buffers import bvec */ |
| 2768 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2769 | + iter->iov_offset; |
| 2770 | iovec.iov_len = min(iter->count, |
| 2771 | iter->bvec->bv_len - iter->iov_offset); |
| 2772 | } |
| 2773 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2774 | if (rw == READ) { |
| 2775 | nr = file->f_op->read(file, iovec.iov_base, |
| 2776 | iovec.iov_len, &kiocb->ki_pos); |
| 2777 | } else { |
| 2778 | nr = file->f_op->write(file, iovec.iov_base, |
| 2779 | iovec.iov_len, &kiocb->ki_pos); |
| 2780 | } |
| 2781 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2782 | if (iov_iter_is_bvec(iter)) |
| 2783 | kunmap(iter->bvec->bv_page); |
| 2784 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2785 | if (nr < 0) { |
| 2786 | if (!ret) |
| 2787 | ret = nr; |
| 2788 | break; |
| 2789 | } |
| 2790 | ret += nr; |
| 2791 | if (nr != iovec.iov_len) |
| 2792 | break; |
| 2793 | iov_iter_advance(iter, nr); |
| 2794 | } |
| 2795 | |
| 2796 | return ret; |
| 2797 | } |
| 2798 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2799 | 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] | 2800 | struct iovec *iovec, struct iovec *fast_iov, |
| 2801 | struct iov_iter *iter) |
| 2802 | { |
| 2803 | req->io->rw.nr_segs = iter->nr_segs; |
| 2804 | req->io->rw.size = io_size; |
| 2805 | req->io->rw.iov = iovec; |
| 2806 | if (!req->io->rw.iov) { |
| 2807 | req->io->rw.iov = req->io->rw.fast_iov; |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 2808 | if (req->io->rw.iov != fast_iov) |
| 2809 | memcpy(req->io->rw.iov, fast_iov, |
| 2810 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2811 | } else { |
| 2812 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2816 | static inline int __io_alloc_async_ctx(struct io_kiocb *req) |
| 2817 | { |
| 2818 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
| 2819 | return req->io == NULL; |
| 2820 | } |
| 2821 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2822 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2823 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2824 | if (!io_op_defs[req->opcode].async_ctx) |
| 2825 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2826 | |
| 2827 | return __io_alloc_async_ctx(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2830 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2831 | struct iovec *iovec, struct iovec *fast_iov, |
| 2832 | struct iov_iter *iter) |
| 2833 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2834 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2835 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2836 | if (!req->io) { |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2837 | if (__io_alloc_async_ctx(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2838 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2839 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2840 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2841 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2842 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2845 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2846 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2847 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2848 | struct io_async_ctx *io; |
| 2849 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2850 | ssize_t ret; |
| 2851 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2852 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2853 | if (ret) |
| 2854 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2855 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2856 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2857 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2858 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2859 | /* either don't need iovec imported or already have it */ |
| 2860 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2861 | return 0; |
| 2862 | |
| 2863 | io = req->io; |
| 2864 | io->rw.iov = io->rw.fast_iov; |
| 2865 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2866 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2867 | req->io = io; |
| 2868 | if (ret < 0) |
| 2869 | return ret; |
| 2870 | |
| 2871 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2872 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2875 | static void io_async_buf_cancel(struct callback_head *cb) |
| 2876 | { |
| 2877 | struct io_async_rw *rw; |
| 2878 | struct io_kiocb *req; |
| 2879 | |
| 2880 | rw = container_of(cb, struct io_async_rw, task_work); |
| 2881 | req = rw->wpq.wait.private; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2882 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static void io_async_buf_retry(struct callback_head *cb) |
| 2886 | { |
| 2887 | struct io_async_rw *rw; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2888 | struct io_kiocb *req; |
| 2889 | |
| 2890 | rw = container_of(cb, struct io_async_rw, task_work); |
| 2891 | req = rw->wpq.wait.private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2892 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2893 | __io_req_task_submit(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 2897 | int sync, void *arg) |
| 2898 | { |
| 2899 | struct wait_page_queue *wpq; |
| 2900 | struct io_kiocb *req = wait->private; |
| 2901 | struct io_async_rw *rw = &req->io->rw; |
| 2902 | struct wait_page_key *key = arg; |
| 2903 | struct task_struct *tsk; |
| 2904 | int ret; |
| 2905 | |
| 2906 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 2907 | |
| 2908 | ret = wake_page_match(wpq, key); |
| 2909 | if (ret != 1) |
| 2910 | return ret; |
| 2911 | |
| 2912 | list_del_init(&wait->entry); |
| 2913 | |
| 2914 | init_task_work(&rw->task_work, io_async_buf_retry); |
| 2915 | /* submit ref gets dropped, acquire a new one */ |
| 2916 | refcount_inc(&req->refs); |
| 2917 | tsk = req->task; |
| 2918 | ret = task_work_add(tsk, &rw->task_work, true); |
| 2919 | if (unlikely(ret)) { |
| 2920 | /* queue just for cancelation */ |
| 2921 | init_task_work(&rw->task_work, io_async_buf_cancel); |
| 2922 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 2923 | task_work_add(tsk, &rw->task_work, true); |
| 2924 | } |
| 2925 | wake_up_process(tsk); |
| 2926 | return 1; |
| 2927 | } |
| 2928 | |
| 2929 | static bool io_rw_should_retry(struct io_kiocb *req) |
| 2930 | { |
| 2931 | struct kiocb *kiocb = &req->rw.kiocb; |
| 2932 | int ret; |
| 2933 | |
| 2934 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 2935 | if (req->flags & REQ_F_NOWAIT) |
| 2936 | return false; |
| 2937 | |
| 2938 | /* already tried, or we're doing O_DIRECT */ |
| 2939 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ)) |
| 2940 | return false; |
| 2941 | /* |
| 2942 | * just use poll if we can, and don't attempt if the fs doesn't |
| 2943 | * support callback based unlocks |
| 2944 | */ |
| 2945 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 2946 | return false; |
| 2947 | |
| 2948 | /* |
| 2949 | * If request type doesn't require req->io to defer in general, |
| 2950 | * we need to allocate it here |
| 2951 | */ |
| 2952 | if (!req->io && __io_alloc_async_ctx(req)) |
| 2953 | return false; |
| 2954 | |
| 2955 | ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq, |
| 2956 | io_async_buf_func, req); |
| 2957 | if (!ret) { |
| 2958 | io_get_req_task(req); |
| 2959 | return true; |
| 2960 | } |
| 2961 | |
| 2962 | return false; |
| 2963 | } |
| 2964 | |
| 2965 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 2966 | { |
| 2967 | if (req->file->f_op->read_iter) |
| 2968 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
| 2969 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); |
| 2970 | } |
| 2971 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2972 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
| 2973 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2974 | { |
| 2975 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2976 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2977 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2978 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2979 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2980 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2981 | ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2982 | if (ret < 0) |
| 2983 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2984 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2985 | /* Ensure we clear previously set non-block flag */ |
| 2986 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2987 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2988 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2989 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2990 | io_size = ret; |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 2991 | if (req->flags & REQ_F_LINK_HEAD) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2992 | req->result = io_size; |
| 2993 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 2994 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2995 | if (force_nonblock && !io_file_supports_async(req->file, READ)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2996 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2997 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2998 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2999 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3000 | if (!ret) { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 3001 | unsigned long nr_segs = iter.nr_segs; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 3002 | ssize_t ret2 = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3003 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3004 | ret2 = io_iter_do_read(req, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3005 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 3006 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 3007 | if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) { |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3008 | kiocb_done(kiocb, ret2, cs); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3009 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 3010 | iter.count = iov_count; |
| 3011 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3012 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3013 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3014 | inline_vecs, &iter); |
| 3015 | if (ret) |
| 3016 | goto out_free; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3017 | /* if we can retry, do so with the callbacks armed */ |
| 3018 | if (io_rw_should_retry(req)) { |
| 3019 | ret2 = io_iter_do_read(req, &iter); |
| 3020 | if (ret2 == -EIOCBQUEUED) { |
| 3021 | goto out_free; |
| 3022 | } else if (ret2 != -EAGAIN) { |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3023 | kiocb_done(kiocb, ret2, cs); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3024 | goto out_free; |
| 3025 | } |
| 3026 | } |
| 3027 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3028 | return -EAGAIN; |
| 3029 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3030 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3031 | out_free: |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3032 | if (!(req->flags & REQ_F_NEED_CLEANUP)) |
| 3033 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3034 | return ret; |
| 3035 | } |
| 3036 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3037 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3038 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3039 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3040 | struct io_async_ctx *io; |
| 3041 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3042 | ssize_t ret; |
| 3043 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3044 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 3045 | if (ret) |
| 3046 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3047 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3048 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3049 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3050 | |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3051 | req->fsize = rlimit(RLIMIT_FSIZE); |
| 3052 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3053 | /* either don't need iovec imported or already have it */ |
| 3054 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3055 | return 0; |
| 3056 | |
| 3057 | io = req->io; |
| 3058 | io->rw.iov = io->rw.fast_iov; |
| 3059 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3060 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3061 | req->io = io; |
| 3062 | if (ret < 0) |
| 3063 | return ret; |
| 3064 | |
| 3065 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 3066 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3067 | } |
| 3068 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3069 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
| 3070 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3071 | { |
| 3072 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3073 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3074 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3075 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3076 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3077 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3078 | ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3079 | if (ret < 0) |
| 3080 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3081 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3082 | /* Ensure we clear previously set non-block flag */ |
| 3083 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3084 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3085 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 3086 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3087 | io_size = ret; |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 3088 | if (req->flags & REQ_F_LINK_HEAD) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3089 | req->result = io_size; |
| 3090 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3091 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3092 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3093 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3094 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3095 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3096 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3097 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3098 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3099 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3100 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3101 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3102 | if (!ret) { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 3103 | unsigned long nr_segs = iter.nr_segs; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3104 | ssize_t ret2; |
| 3105 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3106 | /* |
| 3107 | * Open-code file_start_write here to grab freeze protection, |
| 3108 | * which will be released by another thread in |
| 3109 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3110 | * released so that it doesn't complain about the held lock when |
| 3111 | * we return to userspace. |
| 3112 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 3113 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3114 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3115 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3116 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3117 | SB_FREEZE_WRITE); |
| 3118 | } |
| 3119 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3120 | |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3121 | if (!force_nonblock) |
| 3122 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize; |
| 3123 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3124 | if (req->file->f_op->write_iter) |
| 3125 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3126 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3127 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3128 | |
| 3129 | if (!force_nonblock) |
| 3130 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 3131 | |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 3132 | /* |
Chucheng Luo | bff6035 | 2020-03-25 11:31:38 +0800 | [diff] [blame] | 3133 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 3134 | * retry them without IOCB_NOWAIT. |
| 3135 | */ |
| 3136 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3137 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3138 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3139 | kiocb_done(kiocb, ret2, cs); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3140 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 3141 | iter.count = iov_count; |
| 3142 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3143 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3144 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3145 | inline_vecs, &iter); |
| 3146 | if (ret) |
| 3147 | goto out_free; |
| 3148 | return -EAGAIN; |
| 3149 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3150 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3151 | out_free: |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3152 | if (!(req->flags & REQ_F_NEED_CLEANUP)) |
| 3153 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3154 | return ret; |
| 3155 | } |
| 3156 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3157 | static int __io_splice_prep(struct io_kiocb *req, |
| 3158 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3159 | { |
| 3160 | struct io_splice* sp = &req->splice; |
| 3161 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 3162 | int ret; |
| 3163 | |
| 3164 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3165 | return 0; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3166 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3167 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3168 | |
| 3169 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3170 | sp->len = READ_ONCE(sqe->len); |
| 3171 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3172 | |
| 3173 | if (unlikely(sp->flags & ~valid_flags)) |
| 3174 | return -EINVAL; |
| 3175 | |
| 3176 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 3177 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3178 | if (ret) |
| 3179 | return ret; |
| 3180 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3181 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3182 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3183 | /* |
| 3184 | * Splice operation will be punted aync, and here need to |
| 3185 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3186 | */ |
| 3187 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3188 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3189 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3190 | |
| 3191 | return 0; |
| 3192 | } |
| 3193 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3194 | static int io_tee_prep(struct io_kiocb *req, |
| 3195 | const struct io_uring_sqe *sqe) |
| 3196 | { |
| 3197 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3198 | return -EINVAL; |
| 3199 | return __io_splice_prep(req, sqe); |
| 3200 | } |
| 3201 | |
| 3202 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3203 | { |
| 3204 | struct io_splice *sp = &req->splice; |
| 3205 | struct file *in = sp->file_in; |
| 3206 | struct file *out = sp->file_out; |
| 3207 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3208 | long ret = 0; |
| 3209 | |
| 3210 | if (force_nonblock) |
| 3211 | return -EAGAIN; |
| 3212 | if (sp->len) |
| 3213 | ret = do_tee(in, out, sp->len, flags); |
| 3214 | |
| 3215 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3216 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3217 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3218 | if (ret != sp->len) |
| 3219 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3220 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3221 | return 0; |
| 3222 | } |
| 3223 | |
| 3224 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3225 | { |
| 3226 | struct io_splice* sp = &req->splice; |
| 3227 | |
| 3228 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3229 | sp->off_out = READ_ONCE(sqe->off); |
| 3230 | return __io_splice_prep(req, sqe); |
| 3231 | } |
| 3232 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3233 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3234 | { |
| 3235 | struct io_splice *sp = &req->splice; |
| 3236 | struct file *in = sp->file_in; |
| 3237 | struct file *out = sp->file_out; |
| 3238 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3239 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3240 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3241 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3242 | if (force_nonblock) |
| 3243 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3244 | |
| 3245 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3246 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3247 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3248 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3249 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3250 | |
| 3251 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3252 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3253 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3254 | if (ret != sp->len) |
| 3255 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3256 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3257 | return 0; |
| 3258 | } |
| 3259 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3260 | /* |
| 3261 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3262 | */ |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3263 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3264 | { |
| 3265 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3266 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3267 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3268 | return -EINVAL; |
| 3269 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3270 | __io_req_complete(req, 0, 0, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3271 | return 0; |
| 3272 | } |
| 3273 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3274 | 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] | 3275 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3276 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3277 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3278 | if (!req->file) |
| 3279 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3280 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3281 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3282 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3283 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3284 | return -EINVAL; |
| 3285 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3286 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3287 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3288 | return -EINVAL; |
| 3289 | |
| 3290 | req->sync.off = READ_ONCE(sqe->off); |
| 3291 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
| 3294 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3295 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3296 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3297 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3298 | int ret; |
| 3299 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3300 | /* fsync always requires a blocking context */ |
| 3301 | if (force_nonblock) |
| 3302 | return -EAGAIN; |
| 3303 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3304 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3305 | end > 0 ? end : LLONG_MAX, |
| 3306 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3307 | if (ret < 0) |
| 3308 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3309 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3310 | return 0; |
| 3311 | } |
| 3312 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3313 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3314 | const struct io_uring_sqe *sqe) |
| 3315 | { |
| 3316 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3317 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3318 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3319 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3320 | |
| 3321 | req->sync.off = READ_ONCE(sqe->off); |
| 3322 | req->sync.len = READ_ONCE(sqe->addr); |
| 3323 | req->sync.mode = READ_ONCE(sqe->len); |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3324 | req->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3325 | return 0; |
| 3326 | } |
| 3327 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3328 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3329 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3330 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3331 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3332 | /* fallocate always requiring blocking context */ |
| 3333 | if (force_nonblock) |
| 3334 | return -EAGAIN; |
| 3335 | |
| 3336 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize; |
| 3337 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3338 | req->sync.len); |
| 3339 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 3340 | if (ret < 0) |
| 3341 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3342 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
| 3345 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3346 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3347 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3348 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3349 | int ret; |
| 3350 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3351 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3352 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3353 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3354 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3355 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3356 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3357 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3358 | /* open.how should be already initialised */ |
| 3359 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3360 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3361 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3362 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3363 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3364 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3365 | if (IS_ERR(req->open.filename)) { |
| 3366 | ret = PTR_ERR(req->open.filename); |
| 3367 | req->open.filename = NULL; |
| 3368 | return ret; |
| 3369 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3370 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3371 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3372 | return 0; |
| 3373 | } |
| 3374 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3375 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3376 | { |
| 3377 | u64 flags, mode; |
| 3378 | |
| 3379 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3380 | return 0; |
| 3381 | mode = READ_ONCE(sqe->len); |
| 3382 | flags = READ_ONCE(sqe->open_flags); |
| 3383 | req->open.how = build_open_how(flags, mode); |
| 3384 | return __io_openat_prep(req, sqe); |
| 3385 | } |
| 3386 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3387 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3388 | { |
| 3389 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3390 | size_t len; |
| 3391 | int ret; |
| 3392 | |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3393 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3394 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3395 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3396 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3397 | if (len < OPEN_HOW_SIZE_VER0) |
| 3398 | return -EINVAL; |
| 3399 | |
| 3400 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3401 | len); |
| 3402 | if (ret) |
| 3403 | return ret; |
| 3404 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3405 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3408 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3409 | { |
| 3410 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3411 | struct file *file; |
| 3412 | int ret; |
| 3413 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 3414 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3415 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3416 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3417 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3418 | if (ret) |
| 3419 | goto err; |
| 3420 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3421 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3422 | if (ret < 0) |
| 3423 | goto err; |
| 3424 | |
| 3425 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 3426 | if (IS_ERR(file)) { |
| 3427 | put_unused_fd(ret); |
| 3428 | ret = PTR_ERR(file); |
| 3429 | } else { |
| 3430 | fsnotify_open(file); |
| 3431 | fd_install(ret, file); |
| 3432 | } |
| 3433 | err: |
| 3434 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3435 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3436 | if (ret < 0) |
| 3437 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3438 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3439 | return 0; |
| 3440 | } |
| 3441 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3442 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3443 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3444 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3447 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3448 | const struct io_uring_sqe *sqe) |
| 3449 | { |
| 3450 | struct io_provide_buf *p = &req->pbuf; |
| 3451 | u64 tmp; |
| 3452 | |
| 3453 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3454 | return -EINVAL; |
| 3455 | |
| 3456 | tmp = READ_ONCE(sqe->fd); |
| 3457 | if (!tmp || tmp > USHRT_MAX) |
| 3458 | return -EINVAL; |
| 3459 | |
| 3460 | memset(p, 0, sizeof(*p)); |
| 3461 | p->nbufs = tmp; |
| 3462 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3463 | return 0; |
| 3464 | } |
| 3465 | |
| 3466 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3467 | int bgid, unsigned nbufs) |
| 3468 | { |
| 3469 | unsigned i = 0; |
| 3470 | |
| 3471 | /* shouldn't happen */ |
| 3472 | if (!nbufs) |
| 3473 | return 0; |
| 3474 | |
| 3475 | /* the head kbuf is the list itself */ |
| 3476 | while (!list_empty(&buf->list)) { |
| 3477 | struct io_buffer *nxt; |
| 3478 | |
| 3479 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3480 | list_del(&nxt->list); |
| 3481 | kfree(nxt); |
| 3482 | if (++i == nbufs) |
| 3483 | return i; |
| 3484 | } |
| 3485 | i++; |
| 3486 | kfree(buf); |
| 3487 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3488 | |
| 3489 | return i; |
| 3490 | } |
| 3491 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3492 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3493 | struct io_comp_state *cs) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3494 | { |
| 3495 | struct io_provide_buf *p = &req->pbuf; |
| 3496 | struct io_ring_ctx *ctx = req->ctx; |
| 3497 | struct io_buffer *head; |
| 3498 | int ret = 0; |
| 3499 | |
| 3500 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3501 | |
| 3502 | lockdep_assert_held(&ctx->uring_lock); |
| 3503 | |
| 3504 | ret = -ENOENT; |
| 3505 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3506 | if (head) |
| 3507 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 3508 | |
| 3509 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3510 | if (ret < 0) |
| 3511 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3512 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3513 | return 0; |
| 3514 | } |
| 3515 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3516 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3517 | const struct io_uring_sqe *sqe) |
| 3518 | { |
| 3519 | struct io_provide_buf *p = &req->pbuf; |
| 3520 | u64 tmp; |
| 3521 | |
| 3522 | if (sqe->ioprio || sqe->rw_flags) |
| 3523 | return -EINVAL; |
| 3524 | |
| 3525 | tmp = READ_ONCE(sqe->fd); |
| 3526 | if (!tmp || tmp > USHRT_MAX) |
| 3527 | return -E2BIG; |
| 3528 | p->nbufs = tmp; |
| 3529 | p->addr = READ_ONCE(sqe->addr); |
| 3530 | p->len = READ_ONCE(sqe->len); |
| 3531 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3532 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3533 | return -EFAULT; |
| 3534 | |
| 3535 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3536 | tmp = READ_ONCE(sqe->off); |
| 3537 | if (tmp > USHRT_MAX) |
| 3538 | return -E2BIG; |
| 3539 | p->bid = tmp; |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
| 3543 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3544 | { |
| 3545 | struct io_buffer *buf; |
| 3546 | u64 addr = pbuf->addr; |
| 3547 | int i, bid = pbuf->bid; |
| 3548 | |
| 3549 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3550 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3551 | if (!buf) |
| 3552 | break; |
| 3553 | |
| 3554 | buf->addr = addr; |
| 3555 | buf->len = pbuf->len; |
| 3556 | buf->bid = bid; |
| 3557 | addr += pbuf->len; |
| 3558 | bid++; |
| 3559 | if (!*head) { |
| 3560 | INIT_LIST_HEAD(&buf->list); |
| 3561 | *head = buf; |
| 3562 | } else { |
| 3563 | list_add_tail(&buf->list, &(*head)->list); |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | return i ? i : -ENOMEM; |
| 3568 | } |
| 3569 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3570 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3571 | struct io_comp_state *cs) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3572 | { |
| 3573 | struct io_provide_buf *p = &req->pbuf; |
| 3574 | struct io_ring_ctx *ctx = req->ctx; |
| 3575 | struct io_buffer *head, *list; |
| 3576 | int ret = 0; |
| 3577 | |
| 3578 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3579 | |
| 3580 | lockdep_assert_held(&ctx->uring_lock); |
| 3581 | |
| 3582 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3583 | |
| 3584 | ret = io_add_buffers(p, &head); |
| 3585 | if (ret < 0) |
| 3586 | goto out; |
| 3587 | |
| 3588 | if (!list) { |
| 3589 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3590 | GFP_KERNEL); |
| 3591 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3592 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3593 | goto out; |
| 3594 | } |
| 3595 | } |
| 3596 | out: |
| 3597 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3598 | if (ret < 0) |
| 3599 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3600 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3601 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3604 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3605 | const struct io_uring_sqe *sqe) |
| 3606 | { |
| 3607 | #if defined(CONFIG_EPOLL) |
| 3608 | if (sqe->ioprio || sqe->buf_index) |
| 3609 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3610 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3611 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3612 | |
| 3613 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3614 | req->epoll.op = READ_ONCE(sqe->len); |
| 3615 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3616 | |
| 3617 | if (ep_op_has_event(req->epoll.op)) { |
| 3618 | struct epoll_event __user *ev; |
| 3619 | |
| 3620 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3621 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3622 | return -EFAULT; |
| 3623 | } |
| 3624 | |
| 3625 | return 0; |
| 3626 | #else |
| 3627 | return -EOPNOTSUPP; |
| 3628 | #endif |
| 3629 | } |
| 3630 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3631 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
| 3632 | struct io_comp_state *cs) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3633 | { |
| 3634 | #if defined(CONFIG_EPOLL) |
| 3635 | struct io_epoll *ie = &req->epoll; |
| 3636 | int ret; |
| 3637 | |
| 3638 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3639 | if (force_nonblock && ret == -EAGAIN) |
| 3640 | return -EAGAIN; |
| 3641 | |
| 3642 | if (ret < 0) |
| 3643 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3644 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3645 | return 0; |
| 3646 | #else |
| 3647 | return -EOPNOTSUPP; |
| 3648 | #endif |
| 3649 | } |
| 3650 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3651 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3652 | { |
| 3653 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3654 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3655 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3656 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3657 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3658 | |
| 3659 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3660 | req->madvise.len = READ_ONCE(sqe->len); |
| 3661 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3662 | return 0; |
| 3663 | #else |
| 3664 | return -EOPNOTSUPP; |
| 3665 | #endif |
| 3666 | } |
| 3667 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3668 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3669 | { |
| 3670 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3671 | struct io_madvise *ma = &req->madvise; |
| 3672 | int ret; |
| 3673 | |
| 3674 | if (force_nonblock) |
| 3675 | return -EAGAIN; |
| 3676 | |
| 3677 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3678 | if (ret < 0) |
| 3679 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3680 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3681 | return 0; |
| 3682 | #else |
| 3683 | return -EOPNOTSUPP; |
| 3684 | #endif |
| 3685 | } |
| 3686 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3687 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3688 | { |
| 3689 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3690 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3691 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3692 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3693 | |
| 3694 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3695 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3696 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3697 | return 0; |
| 3698 | } |
| 3699 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3700 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3701 | { |
| 3702 | struct io_fadvise *fa = &req->fadvise; |
| 3703 | int ret; |
| 3704 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3705 | if (force_nonblock) { |
| 3706 | switch (fa->advice) { |
| 3707 | case POSIX_FADV_NORMAL: |
| 3708 | case POSIX_FADV_RANDOM: |
| 3709 | case POSIX_FADV_SEQUENTIAL: |
| 3710 | break; |
| 3711 | default: |
| 3712 | return -EAGAIN; |
| 3713 | } |
| 3714 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3715 | |
| 3716 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3717 | if (ret < 0) |
| 3718 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3719 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3720 | return 0; |
| 3721 | } |
| 3722 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3723 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3724 | { |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3725 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3726 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3727 | if (sqe->ioprio || sqe->buf_index) |
| 3728 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3729 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3730 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3731 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3732 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 3733 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3734 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3735 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3736 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3737 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3738 | return 0; |
| 3739 | } |
| 3740 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3741 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3742 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3743 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3744 | int ret; |
| 3745 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3746 | if (force_nonblock) { |
| 3747 | /* only need file table for an actual valid fd */ |
| 3748 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 3749 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3750 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3751 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3752 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3753 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 3754 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3755 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3756 | if (ret < 0) |
| 3757 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3758 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3759 | return 0; |
| 3760 | } |
| 3761 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3762 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3763 | { |
| 3764 | /* |
| 3765 | * If we queue this for async, it must not be cancellable. That would |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3766 | * leave the 'file' in an undeterminate state, and here need to modify |
| 3767 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3768 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3769 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3770 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3771 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3772 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3773 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3774 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 3775 | sqe->rw_flags || sqe->buf_index) |
| 3776 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3777 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3778 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3779 | |
| 3780 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 3781 | if ((req->file && req->file->f_op == &io_uring_fops) || |
| 3782 | req->close.fd == req->ctx->ring_fd) |
| 3783 | return -EBADF; |
| 3784 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3785 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3786 | return 0; |
| 3787 | } |
| 3788 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3789 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
| 3790 | struct io_comp_state *cs) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3791 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3792 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3793 | int ret; |
| 3794 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3795 | /* might be already done during nonblock submission */ |
| 3796 | if (!close->put_file) { |
| 3797 | ret = __close_fd_get_file(close->fd, &close->put_file); |
| 3798 | if (ret < 0) |
| 3799 | return (ret == -ENOENT) ? -EBADF : ret; |
| 3800 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3801 | |
| 3802 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3803 | if (close->put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3804 | /* was never set, but play safe */ |
| 3805 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3806 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3807 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3808 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3809 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3810 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3811 | /* No ->flush() or already async, safely close from here */ |
| 3812 | ret = filp_close(close->put_file, req->work.files); |
| 3813 | if (ret < 0) |
| 3814 | req_set_fail_links(req); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3815 | fput(close->put_file); |
| 3816 | close->put_file = NULL; |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3817 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3818 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3821 | 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] | 3822 | { |
| 3823 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3824 | |
| 3825 | if (!req->file) |
| 3826 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3827 | |
| 3828 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3829 | return -EINVAL; |
| 3830 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3831 | return -EINVAL; |
| 3832 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3833 | req->sync.off = READ_ONCE(sqe->off); |
| 3834 | req->sync.len = READ_ONCE(sqe->len); |
| 3835 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3836 | return 0; |
| 3837 | } |
| 3838 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3839 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3840 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3841 | int ret; |
| 3842 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3843 | /* sync_file_range always requires a blocking context */ |
| 3844 | if (force_nonblock) |
| 3845 | return -EAGAIN; |
| 3846 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3847 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3848 | req->sync.flags); |
| 3849 | if (ret < 0) |
| 3850 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3851 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3852 | return 0; |
| 3853 | } |
| 3854 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 3855 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3856 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3857 | struct io_async_msghdr *kmsg) |
| 3858 | { |
| 3859 | if (req->io) |
| 3860 | return -EAGAIN; |
| 3861 | if (io_alloc_async_ctx(req)) { |
| 3862 | if (kmsg->iov != kmsg->fast_iov) |
| 3863 | kfree(kmsg->iov); |
| 3864 | return -ENOMEM; |
| 3865 | } |
| 3866 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3867 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3868 | return -EAGAIN; |
| 3869 | } |
| 3870 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3871 | 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] | 3872 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3873 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3874 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3875 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3876 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 3877 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3878 | return -EINVAL; |
| 3879 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3880 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3881 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3882 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3883 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3884 | #ifdef CONFIG_COMPAT |
| 3885 | if (req->ctx->compat) |
| 3886 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3887 | #endif |
| 3888 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3889 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3890 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3891 | /* iovec is already imported */ |
| 3892 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3893 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3894 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3895 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3896 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3897 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3898 | if (!ret) |
| 3899 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3900 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3901 | } |
| 3902 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3903 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 3904 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3905 | { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3906 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3907 | struct socket *sock; |
| 3908 | int ret; |
| 3909 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3910 | sock = sock_from_file(req->file, &ret); |
| 3911 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3912 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3913 | unsigned flags; |
| 3914 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3915 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3916 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3917 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3918 | /* if iov is set, it's allocated already */ |
| 3919 | if (!kmsg->iov) |
| 3920 | kmsg->iov = kmsg->fast_iov; |
| 3921 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3922 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3923 | struct io_sr_msg *sr = &req->sr_msg; |
| 3924 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3925 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3926 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3927 | |
| 3928 | io.msg.iov = io.msg.fast_iov; |
| 3929 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3930 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3931 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3932 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3933 | } |
| 3934 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3935 | flags = req->sr_msg.msg_flags; |
| 3936 | if (flags & MSG_DONTWAIT) |
| 3937 | req->flags |= REQ_F_NOWAIT; |
| 3938 | else if (force_nonblock) |
| 3939 | flags |= MSG_DONTWAIT; |
| 3940 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3941 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3942 | if (force_nonblock && ret == -EAGAIN) |
| 3943 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3944 | if (ret == -ERESTARTSYS) |
| 3945 | ret = -EINTR; |
| 3946 | } |
| 3947 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3948 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3949 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3950 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3951 | if (ret < 0) |
| 3952 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3953 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3954 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3955 | } |
| 3956 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3957 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 3958 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3959 | { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3960 | struct socket *sock; |
| 3961 | int ret; |
| 3962 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3963 | sock = sock_from_file(req->file, &ret); |
| 3964 | if (sock) { |
| 3965 | struct io_sr_msg *sr = &req->sr_msg; |
| 3966 | struct msghdr msg; |
| 3967 | struct iovec iov; |
| 3968 | unsigned flags; |
| 3969 | |
| 3970 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3971 | &msg.msg_iter); |
| 3972 | if (ret) |
| 3973 | return ret; |
| 3974 | |
| 3975 | msg.msg_name = NULL; |
| 3976 | msg.msg_control = NULL; |
| 3977 | msg.msg_controllen = 0; |
| 3978 | msg.msg_namelen = 0; |
| 3979 | |
| 3980 | flags = req->sr_msg.msg_flags; |
| 3981 | if (flags & MSG_DONTWAIT) |
| 3982 | req->flags |= REQ_F_NOWAIT; |
| 3983 | else if (force_nonblock) |
| 3984 | flags |= MSG_DONTWAIT; |
| 3985 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3986 | msg.msg_flags = flags; |
| 3987 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3988 | if (force_nonblock && ret == -EAGAIN) |
| 3989 | return -EAGAIN; |
| 3990 | if (ret == -ERESTARTSYS) |
| 3991 | ret = -EINTR; |
| 3992 | } |
| 3993 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3994 | if (ret < 0) |
| 3995 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3996 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3997 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3998 | } |
| 3999 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4000 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 4001 | { |
| 4002 | struct io_sr_msg *sr = &req->sr_msg; |
| 4003 | struct iovec __user *uiov; |
| 4004 | size_t iov_len; |
| 4005 | int ret; |
| 4006 | |
| 4007 | ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr, |
| 4008 | &uiov, &iov_len); |
| 4009 | if (ret) |
| 4010 | return ret; |
| 4011 | |
| 4012 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4013 | if (iov_len > 1) |
| 4014 | return -EINVAL; |
| 4015 | if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov))) |
| 4016 | return -EFAULT; |
| 4017 | sr->len = io->msg.iov[0].iov_len; |
| 4018 | iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1, |
| 4019 | sr->len); |
| 4020 | io->msg.iov = NULL; |
| 4021 | } else { |
| 4022 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
| 4023 | &io->msg.iov, &io->msg.msg.msg_iter); |
| 4024 | if (ret > 0) |
| 4025 | ret = 0; |
| 4026 | } |
| 4027 | |
| 4028 | return ret; |
| 4029 | } |
| 4030 | |
| 4031 | #ifdef CONFIG_COMPAT |
| 4032 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4033 | struct io_async_ctx *io) |
| 4034 | { |
| 4035 | struct compat_msghdr __user *msg_compat; |
| 4036 | struct io_sr_msg *sr = &req->sr_msg; |
| 4037 | struct compat_iovec __user *uiov; |
| 4038 | compat_uptr_t ptr; |
| 4039 | compat_size_t len; |
| 4040 | int ret; |
| 4041 | |
| 4042 | msg_compat = (struct compat_msghdr __user *) sr->msg; |
| 4043 | ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr, |
| 4044 | &ptr, &len); |
| 4045 | if (ret) |
| 4046 | return ret; |
| 4047 | |
| 4048 | uiov = compat_ptr(ptr); |
| 4049 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4050 | compat_ssize_t clen; |
| 4051 | |
| 4052 | if (len > 1) |
| 4053 | return -EINVAL; |
| 4054 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4055 | return -EFAULT; |
| 4056 | if (__get_user(clen, &uiov->iov_len)) |
| 4057 | return -EFAULT; |
| 4058 | if (clen < 0) |
| 4059 | return -EINVAL; |
| 4060 | sr->len = io->msg.iov[0].iov_len; |
| 4061 | io->msg.iov = NULL; |
| 4062 | } else { |
| 4063 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, |
| 4064 | &io->msg.iov, |
| 4065 | &io->msg.msg.msg_iter); |
| 4066 | if (ret < 0) |
| 4067 | return ret; |
| 4068 | } |
| 4069 | |
| 4070 | return 0; |
| 4071 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4072 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4073 | |
| 4074 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 4075 | { |
| 4076 | io->msg.iov = io->msg.fast_iov; |
| 4077 | |
| 4078 | #ifdef CONFIG_COMPAT |
| 4079 | if (req->ctx->compat) |
| 4080 | return __io_compat_recvmsg_copy_hdr(req, io); |
| 4081 | #endif |
| 4082 | |
| 4083 | return __io_recvmsg_copy_hdr(req, io); |
| 4084 | } |
| 4085 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4086 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
| 4087 | int *cflags, bool needs_lock) |
| 4088 | { |
| 4089 | struct io_sr_msg *sr = &req->sr_msg; |
| 4090 | struct io_buffer *kbuf; |
| 4091 | |
| 4092 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 4093 | return NULL; |
| 4094 | |
| 4095 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4096 | if (IS_ERR(kbuf)) |
| 4097 | return kbuf; |
| 4098 | |
| 4099 | sr->kbuf = kbuf; |
| 4100 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 4101 | |
| 4102 | *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 4103 | *cflags |= IORING_CQE_F_BUFFER; |
| 4104 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4107 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4108 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4109 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4110 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4111 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4112 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4113 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4114 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4115 | return -EINVAL; |
| 4116 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4117 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 4118 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4119 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4120 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4121 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4122 | #ifdef CONFIG_COMPAT |
| 4123 | if (req->ctx->compat) |
| 4124 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4125 | #endif |
| 4126 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4127 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4128 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 4129 | /* iovec is already imported */ |
| 4130 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 4131 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4132 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4133 | ret = io_recvmsg_copy_hdr(req, io); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4134 | if (!ret) |
| 4135 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4136 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4137 | } |
| 4138 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4139 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4140 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4141 | { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4142 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4143 | struct socket *sock; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4144 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4145 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4146 | sock = sock_from_file(req->file, &ret); |
| 4147 | if (sock) { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4148 | struct io_buffer *kbuf; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4149 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4150 | unsigned flags; |
| 4151 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4152 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4153 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 4154 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4155 | /* if iov is set, it's allocated already */ |
| 4156 | if (!kmsg->iov) |
| 4157 | kmsg->iov = kmsg->fast_iov; |
| 4158 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4159 | } else { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4160 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 4161 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4162 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4163 | ret = io_recvmsg_copy_hdr(req, &io); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4164 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4165 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4166 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4167 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4168 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 4169 | if (IS_ERR(kbuf)) { |
| 4170 | return PTR_ERR(kbuf); |
| 4171 | } else if (kbuf) { |
| 4172 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4173 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4174 | 1, req->sr_msg.len); |
| 4175 | } |
| 4176 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4177 | flags = req->sr_msg.msg_flags; |
| 4178 | if (flags & MSG_DONTWAIT) |
| 4179 | req->flags |= REQ_F_NOWAIT; |
| 4180 | else if (force_nonblock) |
| 4181 | flags |= MSG_DONTWAIT; |
| 4182 | |
| 4183 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 4184 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4185 | if (force_nonblock && ret == -EAGAIN) |
| 4186 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 4187 | if (ret == -ERESTARTSYS) |
| 4188 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4189 | } |
| 4190 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 4191 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4192 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4193 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4194 | if (ret < 0) |
| 4195 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4196 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4197 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4198 | } |
| 4199 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4200 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4201 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4202 | { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4203 | struct io_buffer *kbuf = NULL; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4204 | struct socket *sock; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4205 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4206 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4207 | sock = sock_from_file(req->file, &ret); |
| 4208 | if (sock) { |
| 4209 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4210 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4211 | struct msghdr msg; |
| 4212 | struct iovec iov; |
| 4213 | unsigned flags; |
| 4214 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4215 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 4216 | if (IS_ERR(kbuf)) |
| 4217 | return PTR_ERR(kbuf); |
| 4218 | else if (kbuf) |
| 4219 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4220 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4221 | ret = import_single_range(READ, buf, sr->len, &iov, |
| 4222 | &msg.msg_iter); |
| 4223 | if (ret) { |
| 4224 | kfree(kbuf); |
| 4225 | return ret; |
| 4226 | } |
| 4227 | |
| 4228 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4229 | msg.msg_name = NULL; |
| 4230 | msg.msg_control = NULL; |
| 4231 | msg.msg_controllen = 0; |
| 4232 | msg.msg_namelen = 0; |
| 4233 | msg.msg_iocb = NULL; |
| 4234 | msg.msg_flags = 0; |
| 4235 | |
| 4236 | flags = req->sr_msg.msg_flags; |
| 4237 | if (flags & MSG_DONTWAIT) |
| 4238 | req->flags |= REQ_F_NOWAIT; |
| 4239 | else if (force_nonblock) |
| 4240 | flags |= MSG_DONTWAIT; |
| 4241 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4242 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4243 | if (force_nonblock && ret == -EAGAIN) |
| 4244 | return -EAGAIN; |
| 4245 | if (ret == -ERESTARTSYS) |
| 4246 | ret = -EINTR; |
| 4247 | } |
| 4248 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4249 | kfree(kbuf); |
| 4250 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4251 | if (ret < 0) |
| 4252 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4253 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4254 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4257 | 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] | 4258 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4259 | struct io_accept *accept = &req->accept; |
| 4260 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4261 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4262 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4263 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4264 | return -EINVAL; |
| 4265 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4266 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4267 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4268 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4269 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4270 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4271 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4272 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4273 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4274 | struct io_comp_state *cs) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4275 | { |
| 4276 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4277 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4278 | int ret; |
| 4279 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4280 | if (req->file->f_flags & O_NONBLOCK) |
| 4281 | req->flags |= REQ_F_NOWAIT; |
| 4282 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4283 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4284 | accept->addr_len, accept->flags, |
| 4285 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4286 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4287 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4288 | if (ret < 0) { |
| 4289 | if (ret == -ERESTARTSYS) |
| 4290 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4291 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4292 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4293 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4294 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4297 | 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] | 4298 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4299 | struct io_connect *conn = &req->connect; |
| 4300 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4301 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4302 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4303 | return -EINVAL; |
| 4304 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4305 | return -EINVAL; |
| 4306 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4307 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4308 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 4309 | |
| 4310 | if (!io) |
| 4311 | return 0; |
| 4312 | |
| 4313 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4314 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4315 | } |
| 4316 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4317 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4318 | struct io_comp_state *cs) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4319 | { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4320 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4321 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4322 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4323 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4324 | if (req->io) { |
| 4325 | io = req->io; |
| 4326 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4327 | ret = move_addr_to_kernel(req->connect.addr, |
| 4328 | req->connect.addr_len, |
| 4329 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4330 | if (ret) |
| 4331 | goto out; |
| 4332 | io = &__io; |
| 4333 | } |
| 4334 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4335 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4336 | |
| 4337 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 4338 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4339 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4340 | if (req->io) |
| 4341 | return -EAGAIN; |
| 4342 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4343 | ret = -ENOMEM; |
| 4344 | goto out; |
| 4345 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4346 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4347 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4348 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4349 | if (ret == -ERESTARTSYS) |
| 4350 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4351 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4352 | if (ret < 0) |
| 4353 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4354 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4355 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4356 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4357 | #else /* !CONFIG_NET */ |
| 4358 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4359 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4360 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4363 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4364 | struct io_comp_state *cs) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4365 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4366 | return -EOPNOTSUPP; |
| 4367 | } |
| 4368 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4369 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4370 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4371 | { |
| 4372 | return -EOPNOTSUPP; |
| 4373 | } |
| 4374 | |
| 4375 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4376 | const struct io_uring_sqe *sqe) |
| 4377 | { |
| 4378 | return -EOPNOTSUPP; |
| 4379 | } |
| 4380 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4381 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4382 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4383 | { |
| 4384 | return -EOPNOTSUPP; |
| 4385 | } |
| 4386 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4387 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4388 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4389 | { |
| 4390 | return -EOPNOTSUPP; |
| 4391 | } |
| 4392 | |
| 4393 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4394 | { |
| 4395 | return -EOPNOTSUPP; |
| 4396 | } |
| 4397 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4398 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4399 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4400 | { |
| 4401 | return -EOPNOTSUPP; |
| 4402 | } |
| 4403 | |
| 4404 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4405 | { |
| 4406 | return -EOPNOTSUPP; |
| 4407 | } |
| 4408 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame^] | 4409 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4410 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4411 | { |
| 4412 | return -EOPNOTSUPP; |
| 4413 | } |
| 4414 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4415 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4416 | struct io_poll_table { |
| 4417 | struct poll_table_struct pt; |
| 4418 | struct io_kiocb *req; |
| 4419 | int error; |
| 4420 | }; |
| 4421 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4422 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4423 | __poll_t mask, task_work_func_t func) |
| 4424 | { |
| 4425 | struct task_struct *tsk; |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4426 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4427 | |
| 4428 | /* for instances that support it check for an event match first: */ |
| 4429 | if (mask && !(mask & poll->events)) |
| 4430 | return 0; |
| 4431 | |
| 4432 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4433 | |
| 4434 | list_del_init(&poll->wait.entry); |
| 4435 | |
| 4436 | tsk = req->task; |
| 4437 | req->result = mask; |
| 4438 | init_task_work(&req->task_work, func); |
| 4439 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4440 | * If this fails, then the task is exiting. When a task exits, the |
| 4441 | * work gets canceled, so just cancel this request as well instead |
| 4442 | * of executing it. We can't safely execute it anyway, as we may not |
| 4443 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4444 | */ |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4445 | ret = task_work_add(tsk, &req->task_work, true); |
| 4446 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4447 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4448 | tsk = io_wq_get_task(req->ctx->io_wq); |
| 4449 | task_work_add(tsk, &req->task_work, true); |
| 4450 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4451 | wake_up_process(tsk); |
| 4452 | return 1; |
| 4453 | } |
| 4454 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4455 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4456 | __acquires(&req->ctx->completion_lock) |
| 4457 | { |
| 4458 | struct io_ring_ctx *ctx = req->ctx; |
| 4459 | |
| 4460 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4461 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4462 | |
| 4463 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4464 | } |
| 4465 | |
| 4466 | spin_lock_irq(&ctx->completion_lock); |
| 4467 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4468 | add_wait_queue(poll->head, &poll->wait); |
| 4469 | return true; |
| 4470 | } |
| 4471 | |
| 4472 | return false; |
| 4473 | } |
| 4474 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4475 | static void io_poll_remove_double(struct io_kiocb *req) |
| 4476 | { |
| 4477 | struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io; |
| 4478 | |
| 4479 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4480 | |
| 4481 | if (poll && poll->head) { |
| 4482 | struct wait_queue_head *head = poll->head; |
| 4483 | |
| 4484 | spin_lock(&head->lock); |
| 4485 | list_del_init(&poll->wait.entry); |
| 4486 | if (poll->wait.private) |
| 4487 | refcount_dec(&req->refs); |
| 4488 | poll->head = NULL; |
| 4489 | spin_unlock(&head->lock); |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4494 | { |
| 4495 | struct io_ring_ctx *ctx = req->ctx; |
| 4496 | |
| 4497 | io_poll_remove_double(req); |
| 4498 | req->poll.done = true; |
| 4499 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4500 | io_commit_cqring(ctx); |
| 4501 | } |
| 4502 | |
| 4503 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4504 | { |
| 4505 | struct io_ring_ctx *ctx = req->ctx; |
| 4506 | |
| 4507 | if (io_poll_rewait(req, &req->poll)) { |
| 4508 | spin_unlock_irq(&ctx->completion_lock); |
| 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | hash_del(&req->hash_node); |
| 4513 | io_poll_complete(req, req->result, 0); |
| 4514 | req->flags |= REQ_F_COMP_LOCKED; |
| 4515 | io_put_req_find_next(req, nxt); |
| 4516 | spin_unlock_irq(&ctx->completion_lock); |
| 4517 | |
| 4518 | io_cqring_ev_posted(ctx); |
| 4519 | } |
| 4520 | |
| 4521 | static void io_poll_task_func(struct callback_head *cb) |
| 4522 | { |
| 4523 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4524 | struct io_kiocb *nxt = NULL; |
| 4525 | |
| 4526 | io_poll_task_handler(req, &nxt); |
| 4527 | if (nxt) { |
| 4528 | struct io_ring_ctx *ctx = nxt->ctx; |
| 4529 | |
| 4530 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 4531 | __io_queue_sqe(nxt, NULL, NULL); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4532 | mutex_unlock(&ctx->uring_lock); |
| 4533 | } |
| 4534 | } |
| 4535 | |
| 4536 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4537 | int sync, void *key) |
| 4538 | { |
| 4539 | struct io_kiocb *req = wait->private; |
| 4540 | struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io; |
| 4541 | __poll_t mask = key_to_poll(key); |
| 4542 | |
| 4543 | /* for instances that support it check for an event match first: */ |
| 4544 | if (mask && !(mask & poll->events)) |
| 4545 | return 0; |
| 4546 | |
| 4547 | if (req->poll.head) { |
| 4548 | bool done; |
| 4549 | |
| 4550 | spin_lock(&req->poll.head->lock); |
| 4551 | done = list_empty(&req->poll.wait.entry); |
| 4552 | if (!done) |
| 4553 | list_del_init(&req->poll.wait.entry); |
| 4554 | spin_unlock(&req->poll.head->lock); |
| 4555 | if (!done) |
| 4556 | __io_async_wake(req, poll, mask, io_poll_task_func); |
| 4557 | } |
| 4558 | refcount_dec(&req->refs); |
| 4559 | return 1; |
| 4560 | } |
| 4561 | |
| 4562 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4563 | wait_queue_func_t wake_func) |
| 4564 | { |
| 4565 | poll->head = NULL; |
| 4566 | poll->done = false; |
| 4567 | poll->canceled = false; |
| 4568 | poll->events = events; |
| 4569 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4570 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4571 | } |
| 4572 | |
| 4573 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
| 4574 | struct wait_queue_head *head) |
| 4575 | { |
| 4576 | struct io_kiocb *req = pt->req; |
| 4577 | |
| 4578 | /* |
| 4579 | * If poll->head is already set, it's because the file being polled |
| 4580 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4581 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4582 | */ |
| 4583 | if (unlikely(poll->head)) { |
| 4584 | /* already have a 2nd entry, fail a third attempt */ |
| 4585 | if (req->io) { |
| 4586 | pt->error = -EINVAL; |
| 4587 | return; |
| 4588 | } |
| 4589 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4590 | if (!poll) { |
| 4591 | pt->error = -ENOMEM; |
| 4592 | return; |
| 4593 | } |
| 4594 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); |
| 4595 | refcount_inc(&req->refs); |
| 4596 | poll->wait.private = req; |
| 4597 | req->io = (void *) poll; |
| 4598 | } |
| 4599 | |
| 4600 | pt->error = 0; |
| 4601 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4602 | |
| 4603 | if (poll->events & EPOLLEXCLUSIVE) |
| 4604 | add_wait_queue_exclusive(head, &poll->wait); |
| 4605 | else |
| 4606 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4610 | struct poll_table_struct *p) |
| 4611 | { |
| 4612 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4613 | |
| 4614 | __io_queue_proc(&pt->req->apoll->poll, pt, head); |
| 4615 | } |
| 4616 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4617 | static void io_async_task_func(struct callback_head *cb) |
| 4618 | { |
| 4619 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4620 | struct async_poll *apoll = req->apoll; |
| 4621 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4622 | bool canceled = false; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4623 | |
| 4624 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4625 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4626 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4627 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4628 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4629 | } |
| 4630 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4631 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
| 4632 | if (hash_hashed(&req->hash_node)) { |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4633 | hash_del(&req->hash_node); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4634 | } else { |
| 4635 | canceled = READ_ONCE(apoll->poll.canceled); |
| 4636 | if (canceled) { |
| 4637 | io_cqring_fill_event(req, -ECANCELED); |
| 4638 | io_commit_cqring(ctx); |
| 4639 | } |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4640 | } |
| 4641 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4642 | spin_unlock_irq(&ctx->completion_lock); |
| 4643 | |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4644 | /* restore ->work in case we need to retry again */ |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4645 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4646 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4647 | kfree(apoll); |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4648 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4649 | if (!canceled) { |
| 4650 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 4651 | if (io_sq_thread_acquire_mm(ctx, req)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4652 | io_cqring_add_event(req, -EFAULT, 0); |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 4653 | goto end_req; |
| 4654 | } |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4655 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 4656 | __io_queue_sqe(req, NULL, NULL); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4657 | mutex_unlock(&ctx->uring_lock); |
| 4658 | } else { |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4659 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 4660 | end_req: |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4661 | req_set_fail_links(req); |
Xiaoguang Wang | 44575a6 | 2020-04-19 10:06:55 +0800 | [diff] [blame] | 4662 | io_double_put_req(req); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4663 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4667 | void *key) |
| 4668 | { |
| 4669 | struct io_kiocb *req = wait->private; |
| 4670 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 4671 | |
| 4672 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 4673 | key_to_poll(key)); |
| 4674 | |
| 4675 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 4676 | } |
| 4677 | |
| 4678 | static void io_poll_req_insert(struct io_kiocb *req) |
| 4679 | { |
| 4680 | struct io_ring_ctx *ctx = req->ctx; |
| 4681 | struct hlist_head *list; |
| 4682 | |
| 4683 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 4684 | hlist_add_head(&req->hash_node, list); |
| 4685 | } |
| 4686 | |
| 4687 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 4688 | struct io_poll_iocb *poll, |
| 4689 | struct io_poll_table *ipt, __poll_t mask, |
| 4690 | wait_queue_func_t wake_func) |
| 4691 | __acquires(&ctx->completion_lock) |
| 4692 | { |
| 4693 | struct io_ring_ctx *ctx = req->ctx; |
| 4694 | bool cancel = false; |
| 4695 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4696 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 4697 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4698 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4699 | |
| 4700 | ipt->pt._key = mask; |
| 4701 | ipt->req = req; |
| 4702 | ipt->error = -EINVAL; |
| 4703 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4704 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 4705 | |
| 4706 | spin_lock_irq(&ctx->completion_lock); |
| 4707 | if (likely(poll->head)) { |
| 4708 | spin_lock(&poll->head->lock); |
| 4709 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 4710 | if (ipt->error) |
| 4711 | cancel = true; |
| 4712 | ipt->error = 0; |
| 4713 | mask = 0; |
| 4714 | } |
| 4715 | if (mask || ipt->error) |
| 4716 | list_del_init(&poll->wait.entry); |
| 4717 | else if (cancel) |
| 4718 | WRITE_ONCE(poll->canceled, true); |
| 4719 | else if (!poll->done) /* actually waiting for an event */ |
| 4720 | io_poll_req_insert(req); |
| 4721 | spin_unlock(&poll->head->lock); |
| 4722 | } |
| 4723 | |
| 4724 | return mask; |
| 4725 | } |
| 4726 | |
| 4727 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 4728 | { |
| 4729 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 4730 | struct io_ring_ctx *ctx = req->ctx; |
| 4731 | struct async_poll *apoll; |
| 4732 | struct io_poll_table ipt; |
| 4733 | __poll_t mask, ret; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4734 | bool had_io; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4735 | |
| 4736 | if (!req->file || !file_can_poll(req->file)) |
| 4737 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4738 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4739 | return false; |
| 4740 | if (!def->pollin && !def->pollout) |
| 4741 | return false; |
| 4742 | |
| 4743 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 4744 | if (unlikely(!apoll)) |
| 4745 | return false; |
| 4746 | |
| 4747 | req->flags |= REQ_F_POLLED; |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4748 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4749 | memcpy(&apoll->work, &req->work, sizeof(req->work)); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4750 | had_io = req->io != NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4751 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4752 | io_get_req_task(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4753 | req->apoll = apoll; |
| 4754 | INIT_HLIST_NODE(&req->hash_node); |
| 4755 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4756 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4757 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4758 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4759 | if (def->pollout) |
| 4760 | mask |= POLLOUT | POLLWRNORM; |
| 4761 | mask |= POLLERR | POLLPRI; |
| 4762 | |
| 4763 | ipt.pt._qproc = io_async_queue_proc; |
| 4764 | |
| 4765 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 4766 | io_async_wake); |
| 4767 | if (ret) { |
| 4768 | ipt.error = 0; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4769 | /* only remove double add if we did it here */ |
| 4770 | if (!had_io) |
| 4771 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4772 | spin_unlock_irq(&ctx->completion_lock); |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4773 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4774 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4775 | kfree(apoll); |
| 4776 | return false; |
| 4777 | } |
| 4778 | spin_unlock_irq(&ctx->completion_lock); |
| 4779 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 4780 | apoll->poll.events); |
| 4781 | return true; |
| 4782 | } |
| 4783 | |
| 4784 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 4785 | struct io_poll_iocb *poll) |
| 4786 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4787 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4788 | |
| 4789 | spin_lock(&poll->head->lock); |
| 4790 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 4791 | if (!list_empty(&poll->wait.entry)) { |
| 4792 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4793 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4794 | } |
| 4795 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4796 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4797 | return do_complete; |
| 4798 | } |
| 4799 | |
| 4800 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 4801 | { |
| 4802 | bool do_complete; |
| 4803 | |
| 4804 | if (req->opcode == IORING_OP_POLL_ADD) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4805 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4806 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 4807 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4808 | struct async_poll *apoll = req->apoll; |
| 4809 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4810 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4811 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 4812 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4813 | io_put_req(req); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4814 | /* |
| 4815 | * restore ->work because we will call |
| 4816 | * io_req_work_drop_env below when dropping the |
| 4817 | * final reference. |
| 4818 | */ |
Xiaoguang Wang | 405a5d2 | 2020-06-10 19:41:20 +0800 | [diff] [blame] | 4819 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 4820 | memcpy(&req->work, &apoll->work, |
| 4821 | sizeof(req->work)); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4822 | kfree(apoll); |
| 4823 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 4824 | } |
| 4825 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4826 | if (do_complete) { |
| 4827 | io_cqring_fill_event(req, -ECANCELED); |
| 4828 | io_commit_cqring(req->ctx); |
| 4829 | req->flags |= REQ_F_COMP_LOCKED; |
| 4830 | io_put_req(req); |
| 4831 | } |
| 4832 | |
| 4833 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 4837 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4838 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4839 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4840 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4841 | |
| 4842 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4843 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 4844 | struct hlist_head *list; |
| 4845 | |
| 4846 | list = &ctx->cancel_hash[i]; |
| 4847 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4848 | posted += io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4849 | } |
| 4850 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4851 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4852 | if (posted) |
| 4853 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4854 | } |
| 4855 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4856 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 4857 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4858 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4859 | struct io_kiocb *req; |
| 4860 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4861 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 4862 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4863 | if (sqe_addr != req->user_data) |
| 4864 | continue; |
| 4865 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4866 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4867 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | return -ENOENT; |
| 4871 | } |
| 4872 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4873 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 4874 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4875 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4876 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4877 | return -EINVAL; |
| 4878 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 4879 | sqe->poll_events) |
| 4880 | return -EINVAL; |
| 4881 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4882 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4883 | return 0; |
| 4884 | } |
| 4885 | |
| 4886 | /* |
| 4887 | * Find a running poll command that matches one specified in sqe->addr, |
| 4888 | * and remove it if found. |
| 4889 | */ |
| 4890 | static int io_poll_remove(struct io_kiocb *req) |
| 4891 | { |
| 4892 | struct io_ring_ctx *ctx = req->ctx; |
| 4893 | u64 addr; |
| 4894 | int ret; |
| 4895 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4896 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4897 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4898 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4899 | spin_unlock_irq(&ctx->completion_lock); |
| 4900 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4901 | if (ret < 0) |
| 4902 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4903 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4904 | return 0; |
| 4905 | } |
| 4906 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4907 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4908 | void *key) |
| 4909 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 4910 | struct io_kiocb *req = wait->private; |
| 4911 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4912 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4913 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4914 | } |
| 4915 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4916 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4917 | struct poll_table_struct *p) |
| 4918 | { |
| 4919 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4920 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4921 | __io_queue_proc(&pt->req->poll, pt, head); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4924 | 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] | 4925 | { |
| 4926 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4927 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4928 | |
| 4929 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4930 | return -EINVAL; |
| 4931 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 4932 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4933 | if (!poll->file) |
| 4934 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4935 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4936 | events = READ_ONCE(sqe->poll32_events); |
| 4937 | #ifdef __BIG_ENDIAN |
| 4938 | events = swahw32(events); |
| 4939 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4940 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 4941 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4942 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4943 | io_get_req_task(req); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4944 | return 0; |
| 4945 | } |
| 4946 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4947 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4948 | { |
| 4949 | struct io_poll_iocb *poll = &req->poll; |
| 4950 | struct io_ring_ctx *ctx = req->ctx; |
| 4951 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4952 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4953 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4954 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4955 | INIT_LIST_HEAD(&req->list); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4956 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4957 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4958 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 4959 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4960 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4961 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4962 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4963 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4964 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4965 | spin_unlock_irq(&ctx->completion_lock); |
| 4966 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4967 | if (mask) { |
| 4968 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4969 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4970 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4971 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4974 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 4975 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4976 | struct io_timeout_data *data = container_of(timer, |
| 4977 | struct io_timeout_data, timer); |
| 4978 | struct io_kiocb *req = data->req; |
| 4979 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4980 | unsigned long flags; |
| 4981 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4982 | atomic_inc(&ctx->cq_timeouts); |
| 4983 | |
| 4984 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4985 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4986 | * We could be racing with timeout deletion. If the list is empty, |
| 4987 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4988 | */ |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 4989 | if (!list_empty(&req->list)) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4990 | list_del_init(&req->list); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4991 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4992 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4993 | io_commit_cqring(ctx); |
| 4994 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4995 | |
| 4996 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4997 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4998 | io_put_req(req); |
| 4999 | return HRTIMER_NORESTART; |
| 5000 | } |
| 5001 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5002 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5003 | { |
| 5004 | struct io_kiocb *req; |
| 5005 | int ret = -ENOENT; |
| 5006 | |
| 5007 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 5008 | if (user_data == req->user_data) { |
| 5009 | list_del_init(&req->list); |
| 5010 | ret = 0; |
| 5011 | break; |
| 5012 | } |
| 5013 | } |
| 5014 | |
| 5015 | if (ret == -ENOENT) |
| 5016 | return ret; |
| 5017 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5018 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5019 | if (ret == -1) |
| 5020 | return -EALREADY; |
| 5021 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5022 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5023 | io_cqring_fill_event(req, -ECANCELED); |
| 5024 | io_put_req(req); |
| 5025 | return 0; |
| 5026 | } |
| 5027 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5028 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5029 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5030 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5031 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5032 | return -EINVAL; |
| 5033 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 5034 | return -EINVAL; |
| 5035 | |
| 5036 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 5037 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 5038 | if (req->timeout.flags) |
| 5039 | return -EINVAL; |
| 5040 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5041 | return 0; |
| 5042 | } |
| 5043 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5044 | /* |
| 5045 | * Remove or update an existing timeout command |
| 5046 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5047 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5048 | { |
| 5049 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5050 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5051 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5052 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5053 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5054 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5055 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5056 | io_commit_cqring(ctx); |
| 5057 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5058 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5059 | if (ret < 0) |
| 5060 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5061 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5062 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5063 | } |
| 5064 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5065 | 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] | 5066 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5067 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5068 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5069 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5070 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5071 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5072 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5073 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5074 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5075 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5076 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5077 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5078 | flags = READ_ONCE(sqe->timeout_flags); |
| 5079 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5080 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5081 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5082 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5083 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5084 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5085 | return -ENOMEM; |
| 5086 | |
| 5087 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5088 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5089 | req->flags |= REQ_F_TIMEOUT; |
| 5090 | |
| 5091 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5092 | return -EFAULT; |
| 5093 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5094 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5095 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5096 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5097 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5098 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5099 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5100 | return 0; |
| 5101 | } |
| 5102 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5103 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5104 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5105 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5106 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5107 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5108 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5109 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5110 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5111 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5112 | /* |
| 5113 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5114 | * timeout event to be satisfied. If it isn't set, then this is |
| 5115 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5116 | */ |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5117 | if (!off) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5118 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5119 | entry = ctx->timeout_list.prev; |
| 5120 | goto add; |
| 5121 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5122 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5123 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5124 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5125 | |
| 5126 | /* |
| 5127 | * Insertion sort, ensuring the first entry in the list is always |
| 5128 | * the one we need first. |
| 5129 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5130 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 5131 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5132 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5133 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 5134 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5135 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5136 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5137 | break; |
| 5138 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5139 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5140 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5141 | data->timer.function = io_timeout_fn; |
| 5142 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5143 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
| 5146 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5147 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5148 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5149 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5150 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5151 | return req->user_data == (unsigned long) data; |
| 5152 | } |
| 5153 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5154 | 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] | 5155 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5156 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5157 | int ret = 0; |
| 5158 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5159 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5160 | switch (cancel_ret) { |
| 5161 | case IO_WQ_CANCEL_OK: |
| 5162 | ret = 0; |
| 5163 | break; |
| 5164 | case IO_WQ_CANCEL_RUNNING: |
| 5165 | ret = -EALREADY; |
| 5166 | break; |
| 5167 | case IO_WQ_CANCEL_NOTFOUND: |
| 5168 | ret = -ENOENT; |
| 5169 | break; |
| 5170 | } |
| 5171 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5172 | return ret; |
| 5173 | } |
| 5174 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5175 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5176 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5177 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5178 | { |
| 5179 | unsigned long flags; |
| 5180 | int ret; |
| 5181 | |
| 5182 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5183 | if (ret != -ENOENT) { |
| 5184 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5185 | goto done; |
| 5186 | } |
| 5187 | |
| 5188 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5189 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5190 | if (ret != -ENOENT) |
| 5191 | goto done; |
| 5192 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5193 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5194 | if (!ret) |
| 5195 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5196 | io_cqring_fill_event(req, ret); |
| 5197 | io_commit_cqring(ctx); |
| 5198 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5199 | io_cqring_ev_posted(ctx); |
| 5200 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5201 | if (ret < 0) |
| 5202 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5203 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5206 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5207 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5208 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5209 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5210 | return -EINVAL; |
| 5211 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 5212 | sqe->cancel_flags) |
| 5213 | return -EINVAL; |
| 5214 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5215 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5216 | return 0; |
| 5217 | } |
| 5218 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5219 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5220 | { |
| 5221 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5222 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5223 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5224 | return 0; |
| 5225 | } |
| 5226 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5227 | static int io_files_update_prep(struct io_kiocb *req, |
| 5228 | const struct io_uring_sqe *sqe) |
| 5229 | { |
| 5230 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 5231 | return -EINVAL; |
| 5232 | |
| 5233 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5234 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5235 | if (!req->files_update.nr_args) |
| 5236 | return -EINVAL; |
| 5237 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5241 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
| 5242 | struct io_comp_state *cs) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5243 | { |
| 5244 | struct io_ring_ctx *ctx = req->ctx; |
| 5245 | struct io_uring_files_update up; |
| 5246 | int ret; |
| 5247 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5248 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5249 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5250 | |
| 5251 | up.offset = req->files_update.offset; |
| 5252 | up.fds = req->files_update.arg; |
| 5253 | |
| 5254 | mutex_lock(&ctx->uring_lock); |
| 5255 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 5256 | mutex_unlock(&ctx->uring_lock); |
| 5257 | |
| 5258 | if (ret < 0) |
| 5259 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5260 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5261 | return 0; |
| 5262 | } |
| 5263 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5264 | static int io_req_defer_prep(struct io_kiocb *req, |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 5265 | const struct io_uring_sqe *sqe, bool for_async) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5266 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5267 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5268 | |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 5269 | if (!sqe) |
| 5270 | return 0; |
| 5271 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 5272 | if (for_async || (req->flags & REQ_F_WORK_INITIALIZED)) { |
| 5273 | io_req_init_async(req); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5274 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 5275 | if (io_op_defs[req->opcode].file_table) { |
| 5276 | ret = io_grab_files(req); |
| 5277 | if (unlikely(ret)) |
| 5278 | return ret; |
| 5279 | } |
| 5280 | |
| 5281 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5282 | } |
| 5283 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5284 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5285 | case IORING_OP_NOP: |
| 5286 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5287 | case IORING_OP_READV: |
| 5288 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5289 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5290 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5291 | break; |
| 5292 | case IORING_OP_WRITEV: |
| 5293 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5294 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5295 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5296 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5297 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5298 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5299 | break; |
| 5300 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5301 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5302 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5303 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5304 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5305 | break; |
| 5306 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5307 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5308 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5309 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5310 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5311 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5312 | break; |
| 5313 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5314 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5315 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5316 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5317 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5318 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5319 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5320 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5321 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5322 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5323 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5324 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5325 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5326 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5327 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5328 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5329 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5330 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5331 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5332 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5333 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5334 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5335 | case IORING_OP_FALLOCATE: |
| 5336 | ret = io_fallocate_prep(req, sqe); |
| 5337 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5338 | case IORING_OP_OPENAT: |
| 5339 | ret = io_openat_prep(req, sqe); |
| 5340 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5341 | case IORING_OP_CLOSE: |
| 5342 | ret = io_close_prep(req, sqe); |
| 5343 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5344 | case IORING_OP_FILES_UPDATE: |
| 5345 | ret = io_files_update_prep(req, sqe); |
| 5346 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5347 | case IORING_OP_STATX: |
| 5348 | ret = io_statx_prep(req, sqe); |
| 5349 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5350 | case IORING_OP_FADVISE: |
| 5351 | ret = io_fadvise_prep(req, sqe); |
| 5352 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5353 | case IORING_OP_MADVISE: |
| 5354 | ret = io_madvise_prep(req, sqe); |
| 5355 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5356 | case IORING_OP_OPENAT2: |
| 5357 | ret = io_openat2_prep(req, sqe); |
| 5358 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5359 | case IORING_OP_EPOLL_CTL: |
| 5360 | ret = io_epoll_ctl_prep(req, sqe); |
| 5361 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5362 | case IORING_OP_SPLICE: |
| 5363 | ret = io_splice_prep(req, sqe); |
| 5364 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5365 | case IORING_OP_PROVIDE_BUFFERS: |
| 5366 | ret = io_provide_buffers_prep(req, sqe); |
| 5367 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5368 | case IORING_OP_REMOVE_BUFFERS: |
| 5369 | ret = io_remove_buffers_prep(req, sqe); |
| 5370 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5371 | case IORING_OP_TEE: |
| 5372 | ret = io_tee_prep(req, sqe); |
| 5373 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5374 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5375 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5376 | req->opcode); |
| 5377 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5378 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5379 | } |
| 5380 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5381 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5382 | } |
| 5383 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5384 | 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] | 5385 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5386 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5387 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5388 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5389 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 4ee3631 | 2020-05-01 17:09:37 +0300 | [diff] [blame] | 5390 | if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5391 | return 0; |
| 5392 | |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5393 | if (!req->io) { |
| 5394 | if (io_alloc_async_ctx(req)) |
| 5395 | return -EAGAIN; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 5396 | ret = io_req_defer_prep(req, sqe, true); |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5397 | if (ret < 0) |
| 5398 | return ret; |
| 5399 | } |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5400 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5401 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5402 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5403 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5404 | return 0; |
| 5405 | } |
| 5406 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 5407 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5408 | list_add_tail(&req->list, &ctx->defer_list); |
| 5409 | spin_unlock_irq(&ctx->completion_lock); |
| 5410 | return -EIOCBQUEUED; |
| 5411 | } |
| 5412 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5413 | static void io_cleanup_req(struct io_kiocb *req) |
| 5414 | { |
| 5415 | struct io_async_ctx *io = req->io; |
| 5416 | |
| 5417 | switch (req->opcode) { |
| 5418 | case IORING_OP_READV: |
| 5419 | case IORING_OP_READ_FIXED: |
| 5420 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5421 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5422 | kfree((void *)(unsigned long)req->rw.addr); |
| 5423 | /* fallthrough */ |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5424 | case IORING_OP_WRITEV: |
| 5425 | case IORING_OP_WRITE_FIXED: |
| 5426 | case IORING_OP_WRITE: |
| 5427 | if (io->rw.iov != io->rw.fast_iov) |
| 5428 | kfree(io->rw.iov); |
| 5429 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5430 | case IORING_OP_RECVMSG: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5431 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5432 | kfree(req->sr_msg.kbuf); |
| 5433 | /* fallthrough */ |
| 5434 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5435 | if (io->msg.iov != io->msg.fast_iov) |
| 5436 | kfree(io->msg.iov); |
| 5437 | break; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5438 | case IORING_OP_RECV: |
| 5439 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5440 | kfree(req->sr_msg.kbuf); |
| 5441 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 5442 | case IORING_OP_OPENAT: |
| 5443 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 5444 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5445 | case IORING_OP_SPLICE: |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5446 | case IORING_OP_TEE: |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5447 | io_put_file(req, req->splice.file_in, |
| 5448 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5449 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5453 | } |
| 5454 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5455 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5456 | bool force_nonblock, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5457 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5458 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5459 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5460 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5461 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5462 | case IORING_OP_NOP: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5463 | ret = io_nop(req, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5464 | break; |
| 5465 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5466 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5467 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5468 | if (sqe) { |
| 5469 | ret = io_read_prep(req, sqe, force_nonblock); |
| 5470 | if (ret < 0) |
| 5471 | break; |
| 5472 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5473 | ret = io_read(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5474 | break; |
| 5475 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5476 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5477 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5478 | if (sqe) { |
| 5479 | ret = io_write_prep(req, sqe, force_nonblock); |
| 5480 | if (ret < 0) |
| 5481 | break; |
| 5482 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5483 | ret = io_write(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5484 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5485 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5486 | if (sqe) { |
| 5487 | ret = io_prep_fsync(req, sqe); |
| 5488 | if (ret < 0) |
| 5489 | break; |
| 5490 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5491 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5492 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5493 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5494 | if (sqe) { |
| 5495 | ret = io_poll_add_prep(req, sqe); |
| 5496 | if (ret) |
| 5497 | break; |
| 5498 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5499 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5500 | break; |
| 5501 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5502 | if (sqe) { |
| 5503 | ret = io_poll_remove_prep(req, sqe); |
| 5504 | if (ret < 0) |
| 5505 | break; |
| 5506 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5507 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5508 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5509 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5510 | if (sqe) { |
| 5511 | ret = io_prep_sfr(req, sqe); |
| 5512 | if (ret < 0) |
| 5513 | break; |
| 5514 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5515 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5516 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5517 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5518 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5519 | if (sqe) { |
| 5520 | ret = io_sendmsg_prep(req, sqe); |
| 5521 | if (ret < 0) |
| 5522 | break; |
| 5523 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5524 | if (req->opcode == IORING_OP_SENDMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5525 | ret = io_sendmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5526 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5527 | ret = io_send(req, force_nonblock, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5528 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5529 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5530 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5531 | if (sqe) { |
| 5532 | ret = io_recvmsg_prep(req, sqe); |
| 5533 | if (ret) |
| 5534 | break; |
| 5535 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5536 | if (req->opcode == IORING_OP_RECVMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5537 | ret = io_recvmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5538 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5539 | ret = io_recv(req, force_nonblock, cs); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5540 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5541 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5542 | if (sqe) { |
| 5543 | ret = io_timeout_prep(req, sqe, false); |
| 5544 | if (ret) |
| 5545 | break; |
| 5546 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5547 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5548 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5549 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5550 | if (sqe) { |
| 5551 | ret = io_timeout_remove_prep(req, sqe); |
| 5552 | if (ret) |
| 5553 | break; |
| 5554 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5555 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5556 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5557 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5558 | if (sqe) { |
| 5559 | ret = io_accept_prep(req, sqe); |
| 5560 | if (ret) |
| 5561 | break; |
| 5562 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5563 | ret = io_accept(req, force_nonblock, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5564 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5565 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5566 | if (sqe) { |
| 5567 | ret = io_connect_prep(req, sqe); |
| 5568 | if (ret) |
| 5569 | break; |
| 5570 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5571 | ret = io_connect(req, force_nonblock, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5572 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5573 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5574 | if (sqe) { |
| 5575 | ret = io_async_cancel_prep(req, sqe); |
| 5576 | if (ret) |
| 5577 | break; |
| 5578 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5579 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5580 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5581 | case IORING_OP_FALLOCATE: |
| 5582 | if (sqe) { |
| 5583 | ret = io_fallocate_prep(req, sqe); |
| 5584 | if (ret) |
| 5585 | break; |
| 5586 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5587 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5588 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5589 | case IORING_OP_OPENAT: |
| 5590 | if (sqe) { |
| 5591 | ret = io_openat_prep(req, sqe); |
| 5592 | if (ret) |
| 5593 | break; |
| 5594 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5595 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5596 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5597 | case IORING_OP_CLOSE: |
| 5598 | if (sqe) { |
| 5599 | ret = io_close_prep(req, sqe); |
| 5600 | if (ret) |
| 5601 | break; |
| 5602 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5603 | ret = io_close(req, force_nonblock, cs); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5604 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5605 | case IORING_OP_FILES_UPDATE: |
| 5606 | if (sqe) { |
| 5607 | ret = io_files_update_prep(req, sqe); |
| 5608 | if (ret) |
| 5609 | break; |
| 5610 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5611 | ret = io_files_update(req, force_nonblock, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5612 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5613 | case IORING_OP_STATX: |
| 5614 | if (sqe) { |
| 5615 | ret = io_statx_prep(req, sqe); |
| 5616 | if (ret) |
| 5617 | break; |
| 5618 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5619 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5620 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5621 | case IORING_OP_FADVISE: |
| 5622 | if (sqe) { |
| 5623 | ret = io_fadvise_prep(req, sqe); |
| 5624 | if (ret) |
| 5625 | break; |
| 5626 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5627 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5628 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5629 | case IORING_OP_MADVISE: |
| 5630 | if (sqe) { |
| 5631 | ret = io_madvise_prep(req, sqe); |
| 5632 | if (ret) |
| 5633 | break; |
| 5634 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5635 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5636 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5637 | case IORING_OP_OPENAT2: |
| 5638 | if (sqe) { |
| 5639 | ret = io_openat2_prep(req, sqe); |
| 5640 | if (ret) |
| 5641 | break; |
| 5642 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5643 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5644 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5645 | case IORING_OP_EPOLL_CTL: |
| 5646 | if (sqe) { |
| 5647 | ret = io_epoll_ctl_prep(req, sqe); |
| 5648 | if (ret) |
| 5649 | break; |
| 5650 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5651 | ret = io_epoll_ctl(req, force_nonblock, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5652 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5653 | case IORING_OP_SPLICE: |
| 5654 | if (sqe) { |
| 5655 | ret = io_splice_prep(req, sqe); |
| 5656 | if (ret < 0) |
| 5657 | break; |
| 5658 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5659 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5660 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5661 | case IORING_OP_PROVIDE_BUFFERS: |
| 5662 | if (sqe) { |
| 5663 | ret = io_provide_buffers_prep(req, sqe); |
| 5664 | if (ret) |
| 5665 | break; |
| 5666 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5667 | ret = io_provide_buffers(req, force_nonblock, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5668 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5669 | case IORING_OP_REMOVE_BUFFERS: |
| 5670 | if (sqe) { |
| 5671 | ret = io_remove_buffers_prep(req, sqe); |
| 5672 | if (ret) |
| 5673 | break; |
| 5674 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5675 | ret = io_remove_buffers(req, force_nonblock, cs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5676 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5677 | case IORING_OP_TEE: |
| 5678 | if (sqe) { |
| 5679 | ret = io_tee_prep(req, sqe); |
| 5680 | if (ret < 0) |
| 5681 | break; |
| 5682 | } |
| 5683 | ret = io_tee(req, force_nonblock); |
| 5684 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5685 | default: |
| 5686 | ret = -EINVAL; |
| 5687 | break; |
| 5688 | } |
| 5689 | |
| 5690 | if (ret) |
| 5691 | return ret; |
| 5692 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 5693 | /* If the op doesn't have a file, we're not polling for it */ |
| 5694 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5695 | const bool in_async = io_wq_current_is_worker(); |
| 5696 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5697 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 5698 | if (in_async) |
| 5699 | mutex_lock(&ctx->uring_lock); |
| 5700 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5701 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5702 | |
| 5703 | if (in_async) |
| 5704 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | return 0; |
| 5708 | } |
| 5709 | |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5710 | static void io_arm_async_linked_timeout(struct io_kiocb *req) |
| 5711 | { |
| 5712 | struct io_kiocb *link; |
| 5713 | |
| 5714 | /* link head's timeout is queued in io_queue_async_work() */ |
| 5715 | if (!(req->flags & REQ_F_QUEUE_TIMEOUT)) |
| 5716 | return; |
| 5717 | |
| 5718 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 5719 | io_queue_linked_timeout(link); |
| 5720 | } |
| 5721 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 5722 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5723 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5724 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5725 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5726 | |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5727 | io_arm_async_linked_timeout(req); |
| 5728 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5729 | /* if NO_CANCEL is set, we must still run the work */ |
| 5730 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 5731 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5732 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5733 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5734 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5735 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5736 | do { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5737 | ret = io_issue_sqe(req, NULL, false, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5738 | /* |
| 5739 | * We can get EAGAIN for polled IO even though we're |
| 5740 | * forcing a sync submission from here, since we can't |
| 5741 | * wait for request slots on the block side. |
| 5742 | */ |
| 5743 | if (ret != -EAGAIN) |
| 5744 | break; |
| 5745 | cond_resched(); |
| 5746 | } while (1); |
| 5747 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5748 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5749 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5750 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5751 | io_req_complete(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5752 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5753 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 5754 | return io_steal_work(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5755 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5756 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5757 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 5758 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5759 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5760 | struct fixed_file_table *table; |
| 5761 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5762 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 5763 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5764 | } |
| 5765 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5766 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 5767 | int fd, struct file **out_file, bool fixed) |
| 5768 | { |
| 5769 | struct io_ring_ctx *ctx = req->ctx; |
| 5770 | struct file *file; |
| 5771 | |
| 5772 | if (fixed) { |
| 5773 | if (unlikely(!ctx->file_data || |
| 5774 | (unsigned) fd >= ctx->nr_user_files)) |
| 5775 | return -EBADF; |
| 5776 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 5777 | file = io_file_from_index(ctx, fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5778 | if (file) { |
| 5779 | req->fixed_file_refs = ctx->file_data->cur_refs; |
| 5780 | percpu_ref_get(req->fixed_file_refs); |
| 5781 | } |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5782 | } else { |
| 5783 | trace_io_uring_file_get(ctx, fd); |
| 5784 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5785 | } |
| 5786 | |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5787 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
| 5788 | *out_file = file; |
| 5789 | return 0; |
| 5790 | } |
| 5791 | return -EBADF; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5792 | } |
| 5793 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5794 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 5795 | int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5796 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5797 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5798 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 5799 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 5800 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5801 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5802 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5803 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5804 | } |
| 5805 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5806 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5807 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5808 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5809 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5810 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 5811 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5812 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5813 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5814 | return -EBADF; |
| 5815 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5816 | rcu_read_lock(); |
| 5817 | spin_lock_irq(&ctx->inflight_lock); |
| 5818 | /* |
| 5819 | * We use the f_ops->flush() handler to ensure that we can flush |
| 5820 | * out work accessing these files if the fd is closed. Check if |
| 5821 | * the fd has changed since we started down this path, and disallow |
| 5822 | * this operation if it has. |
| 5823 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5824 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5825 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 5826 | req->flags |= REQ_F_INFLIGHT; |
| 5827 | req->work.files = current->files; |
| 5828 | ret = 0; |
| 5829 | } |
| 5830 | spin_unlock_irq(&ctx->inflight_lock); |
| 5831 | rcu_read_unlock(); |
| 5832 | |
| 5833 | return ret; |
| 5834 | } |
| 5835 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5836 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 5837 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5838 | struct io_timeout_data *data = container_of(timer, |
| 5839 | struct io_timeout_data, timer); |
| 5840 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5841 | struct io_ring_ctx *ctx = req->ctx; |
| 5842 | struct io_kiocb *prev = NULL; |
| 5843 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5844 | |
| 5845 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5846 | |
| 5847 | /* |
| 5848 | * We don't expect the list to be empty, that will only happen if we |
| 5849 | * race with the completion of the linked work. |
| 5850 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5851 | if (!list_empty(&req->link_list)) { |
| 5852 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 5853 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5854 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5855 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5856 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 5857 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5858 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5862 | |
| 5863 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5864 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5865 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5866 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5867 | } else { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5868 | io_req_complete(req, -ETIME); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5869 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5870 | return HRTIMER_NORESTART; |
| 5871 | } |
| 5872 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5873 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5874 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5875 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5876 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5877 | /* |
| 5878 | * If the list is now empty, then our linked request finished before |
| 5879 | * we got a chance to setup the timer |
| 5880 | */ |
| 5881 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5882 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5883 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5884 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5885 | data->timer.function = io_link_timeout_fn; |
| 5886 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 5887 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5888 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5889 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5890 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5891 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5892 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5893 | } |
| 5894 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5895 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5896 | { |
| 5897 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5898 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 5899 | if (!(req->flags & REQ_F_LINK_HEAD)) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5900 | return NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5901 | /* for polled retry, if flag is set, we already went through here */ |
| 5902 | if (req->flags & REQ_F_POLLED) |
| 5903 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5904 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5905 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 5906 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5907 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5908 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5909 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5910 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5911 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5914 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 5915 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5916 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5917 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5918 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5919 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5920 | int ret; |
| 5921 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5922 | again: |
| 5923 | linked_timeout = io_prep_linked_timeout(req); |
| 5924 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5925 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
| 5926 | req->work.creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5927 | if (old_creds) |
| 5928 | revert_creds(old_creds); |
| 5929 | if (old_creds == req->work.creds) |
| 5930 | old_creds = NULL; /* restored original creds */ |
| 5931 | else |
| 5932 | old_creds = override_creds(req->work.creds); |
| 5933 | } |
| 5934 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5935 | ret = io_issue_sqe(req, sqe, true, cs); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 5936 | |
| 5937 | /* |
| 5938 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 5939 | * doesn't support non-blocking read/write attempts |
| 5940 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5941 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5942 | if (io_arm_poll_handler(req)) { |
| 5943 | if (linked_timeout) |
| 5944 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5945 | goto exit; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5946 | } |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5947 | punt: |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 5948 | io_req_init_async(req); |
| 5949 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5950 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5951 | ret = io_grab_files(req); |
| 5952 | if (ret) |
| 5953 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5954 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5955 | |
| 5956 | /* |
| 5957 | * Queued up for async execution, worker will release |
| 5958 | * submit reference when the iocb is actually submitted. |
| 5959 | */ |
| 5960 | io_queue_async_work(req); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5961 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5962 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5963 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5964 | err: |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5965 | nxt = NULL; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5966 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 5967 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5968 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5969 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5970 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5971 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5972 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5973 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5974 | } |
| 5975 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5976 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5977 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5978 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5979 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5980 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5981 | if (nxt) { |
| 5982 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5983 | |
| 5984 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 5985 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5986 | goto again; |
| 5987 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5988 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5989 | if (old_creds) |
| 5990 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5991 | } |
| 5992 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5993 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 5994 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5995 | { |
| 5996 | int ret; |
| 5997 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5998 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5999 | if (ret) { |
| 6000 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6001 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6002 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6003 | io_put_req(req); |
| 6004 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6005 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6006 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6007 | if (!req->io) { |
| 6008 | ret = -EAGAIN; |
| 6009 | if (io_alloc_async_ctx(req)) |
| 6010 | goto fail_req; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 6011 | ret = io_req_defer_prep(req, sqe, true); |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6012 | if (unlikely(ret < 0)) |
| 6013 | goto fail_req; |
| 6014 | } |
| 6015 | |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6016 | /* |
| 6017 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 6018 | * to async execution. |
| 6019 | */ |
| 6020 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 6021 | io_queue_async_work(req); |
| 6022 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6023 | __io_queue_sqe(req, sqe, cs); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6024 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6025 | } |
| 6026 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6027 | static inline void io_queue_link_head(struct io_kiocb *req, |
| 6028 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6029 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6030 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6031 | io_put_req(req); |
| 6032 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6033 | } else |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6034 | io_queue_sqe(req, NULL, cs); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6035 | } |
| 6036 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6037 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6038 | struct io_kiocb **link, struct io_comp_state *cs) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6039 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6040 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6041 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6042 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6043 | /* |
| 6044 | * If we already have a head request, queue this one for async |
| 6045 | * submittal once the head completes. If we don't have a head but |
| 6046 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6047 | * submitted sync once the chain is complete. If none of those |
| 6048 | * conditions are true (normal request), then just queue it. |
| 6049 | */ |
| 6050 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6051 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6052 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6053 | /* |
| 6054 | * Taking sequential execution of a link, draining both sides |
| 6055 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6056 | * requests in the link. So, it drains the head and the |
| 6057 | * next after the link request. The last one is done via |
| 6058 | * drain_next flag to persist the effect across calls. |
| 6059 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6060 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6061 | head->flags |= REQ_F_IO_DRAIN; |
| 6062 | ctx->drain_next = 1; |
| 6063 | } |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6064 | if (io_alloc_async_ctx(req)) |
| 6065 | return -EAGAIN; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6066 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 6067 | ret = io_req_defer_prep(req, sqe, false); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6068 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6069 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6070 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6071 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6072 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6073 | trace_io_uring_link(ctx, req, head); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 6074 | io_get_req_task(req); |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6075 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6076 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6077 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6078 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6079 | io_queue_link_head(head, cs); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6080 | *link = NULL; |
| 6081 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6082 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6083 | if (unlikely(ctx->drain_next)) { |
| 6084 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6085 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6086 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6087 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 6088 | req->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6089 | INIT_LIST_HEAD(&req->link_list); |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 6090 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6091 | if (io_alloc_async_ctx(req)) |
| 6092 | return -EAGAIN; |
| 6093 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 6094 | ret = io_req_defer_prep(req, sqe, true); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6095 | if (ret) |
| 6096 | req->flags |= REQ_F_FAIL_LINK; |
| 6097 | *link = req; |
| 6098 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6099 | io_queue_sqe(req, sqe, cs); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6100 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6101 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6102 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6103 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6104 | } |
| 6105 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6106 | /* |
| 6107 | * Batched submission is done, ensure local IO is flushed out. |
| 6108 | */ |
| 6109 | static void io_submit_state_end(struct io_submit_state *state) |
| 6110 | { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6111 | if (!list_empty(&state->comp.list)) |
| 6112 | io_submit_flush_completions(&state->comp); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6113 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6114 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6115 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6116 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | /* |
| 6120 | * Start submission side cache. |
| 6121 | */ |
| 6122 | static void io_submit_state_start(struct io_submit_state *state, |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6123 | struct io_ring_ctx *ctx, unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6124 | { |
| 6125 | blk_start_plug(&state->plug); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 6126 | #ifdef CONFIG_BLOCK |
| 6127 | state->plug.nowait = true; |
| 6128 | #endif |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6129 | state->comp.nr = 0; |
| 6130 | INIT_LIST_HEAD(&state->comp.list); |
| 6131 | state->comp.ctx = ctx; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6132 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6133 | state->file = NULL; |
| 6134 | state->ios_left = max_ios; |
| 6135 | } |
| 6136 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6137 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6138 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6139 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6140 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6141 | /* |
| 6142 | * Ensure any loads from the SQEs are done at this point, |
| 6143 | * since once we write the new head, the application could |
| 6144 | * write new data to them. |
| 6145 | */ |
| 6146 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6150 | * 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] | 6151 | * that is mapped by userspace. This means that care needs to be taken to |
| 6152 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6153 | * being a good citizen. If members of the sqe are validated and then later |
| 6154 | * used, it's important that those reads are done through READ_ONCE() to |
| 6155 | * prevent a re-load down the line. |
| 6156 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6157 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6158 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6159 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6160 | unsigned head; |
| 6161 | |
| 6162 | /* |
| 6163 | * The cached sq head (or cq tail) serves two purposes: |
| 6164 | * |
| 6165 | * 1) allows us to batch the cost of updating the user visible |
| 6166 | * head updates. |
| 6167 | * 2) allows the kernel side to track the head on its own, even |
| 6168 | * though the application is the one updating it. |
| 6169 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6170 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6171 | if (likely(head < ctx->sq_entries)) |
| 6172 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6173 | |
| 6174 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6175 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6176 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6177 | return NULL; |
| 6178 | } |
| 6179 | |
| 6180 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6181 | { |
| 6182 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6183 | } |
| 6184 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6185 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6186 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6187 | IOSQE_BUFFER_SELECT) |
| 6188 | |
| 6189 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6190 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6191 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6192 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6193 | unsigned int sqe_flags; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6194 | int id; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6195 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6196 | /* |
| 6197 | * All io need record the previous position, if LINK vs DARIN, |
| 6198 | * it can be used to mark the position of the first IO in the |
| 6199 | * link list. |
| 6200 | */ |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 6201 | req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6202 | req->opcode = READ_ONCE(sqe->opcode); |
| 6203 | req->user_data = READ_ONCE(sqe->user_data); |
| 6204 | req->io = NULL; |
| 6205 | req->file = NULL; |
| 6206 | req->ctx = ctx; |
| 6207 | req->flags = 0; |
| 6208 | /* one is dropped after submission, the other at completion */ |
| 6209 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6210 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6211 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6212 | |
| 6213 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6214 | return -EINVAL; |
| 6215 | |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6216 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
| 6217 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6218 | |
| 6219 | sqe_flags = READ_ONCE(sqe->flags); |
| 6220 | /* enforce forwards compatibility on users */ |
| 6221 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6222 | return -EINVAL; |
| 6223 | |
| 6224 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6225 | !io_op_defs[req->opcode].buffer_select) |
| 6226 | return -EOPNOTSUPP; |
| 6227 | |
| 6228 | id = READ_ONCE(sqe->personality); |
| 6229 | if (id) { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6230 | io_req_init_async(req); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6231 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 6232 | if (unlikely(!req->work.creds)) |
| 6233 | return -EINVAL; |
| 6234 | get_cred(req->work.creds); |
| 6235 | } |
| 6236 | |
| 6237 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6238 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6239 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6240 | if (!io_op_defs[req->opcode].needs_file) |
| 6241 | return 0; |
| 6242 | |
| 6243 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6244 | } |
| 6245 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6246 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6247 | struct file *ring_file, int ring_fd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6248 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6249 | struct io_submit_state state; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6250 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6251 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6252 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6253 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6254 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 6255 | if (!list_empty(&ctx->cq_overflow_list) && |
| 6256 | !io_cqring_overflow_flush(ctx, false)) |
| 6257 | return -EBUSY; |
| 6258 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6259 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6260 | /* make sure SQ entry isn't read before tail */ |
| 6261 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6262 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6263 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6264 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6265 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6266 | io_submit_state_start(&state, ctx, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6267 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6268 | ctx->ring_fd = ring_fd; |
| 6269 | ctx->ring_file = ring_file; |
| 6270 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6271 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6272 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6273 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6274 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6275 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6276 | sqe = io_get_sqe(ctx); |
| 6277 | if (unlikely(!sqe)) { |
| 6278 | io_consume_sqe(ctx); |
| 6279 | break; |
| 6280 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6281 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6282 | if (unlikely(!req)) { |
| 6283 | if (!submitted) |
| 6284 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6285 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6286 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6287 | |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6288 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6289 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6290 | /* will complete beyond this point, count as submitted */ |
| 6291 | submitted++; |
| 6292 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6293 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6294 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6295 | io_put_req(req); |
| 6296 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6297 | break; |
| 6298 | } |
| 6299 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6300 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6301 | true, io_async_submit(ctx)); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6302 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6303 | if (err) |
| 6304 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6305 | } |
| 6306 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6307 | if (unlikely(submitted != nr)) { |
| 6308 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 6309 | |
| 6310 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 6311 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6312 | if (link) |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6313 | io_queue_link_head(link, &state.comp); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6314 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6315 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6316 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6317 | io_commit_sqring(ctx); |
| 6318 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6319 | return submitted; |
| 6320 | } |
| 6321 | |
| 6322 | static int io_sq_thread(void *data) |
| 6323 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6324 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6325 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6326 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6327 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6328 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6329 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6330 | complete(&ctx->sq_thread_comp); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 6331 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6332 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6333 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6334 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6335 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6336 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6337 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6338 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6339 | unsigned nr_events = 0; |
| 6340 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6341 | mutex_lock(&ctx->uring_lock); |
| 6342 | if (!list_empty(&ctx->poll_list)) |
| 6343 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 6344 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6345 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6346 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6347 | } |
| 6348 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6349 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6350 | |
| 6351 | /* |
| 6352 | * If submit got -EBUSY, flag us as needing the application |
| 6353 | * to enter the kernel to reap and flush events. |
| 6354 | */ |
Xuan Zhuo | b772f07 | 2020-06-23 19:34:06 +0800 | [diff] [blame] | 6355 | if (!to_submit || ret == -EBUSY || need_resched()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6356 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6357 | * Drop cur_mm before scheduling, we can't hold it for |
| 6358 | * long periods (or over schedule()). Do this before |
| 6359 | * adding ourselves to the waitqueue, as the unuse/drop |
| 6360 | * may sleep. |
| 6361 | */ |
Pavel Begunkov | bf9c2f1 | 2020-04-12 02:05:02 +0300 | [diff] [blame] | 6362 | io_sq_thread_drop_mm(ctx); |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6363 | |
| 6364 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6365 | * We're polling. If we're within the defined idle |
| 6366 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6367 | * to sleep. The exception is if we got EBUSY doing |
| 6368 | * more IO, we should wait for the application to |
| 6369 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6370 | */ |
Xuan Zhuo | b772f07 | 2020-06-23 19:34:06 +0800 | [diff] [blame] | 6371 | if (!list_empty(&ctx->poll_list) || need_resched() || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6372 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 6373 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6374 | if (current->task_works) |
| 6375 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 6376 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6377 | continue; |
| 6378 | } |
| 6379 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6380 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 6381 | TASK_INTERRUPTIBLE); |
| 6382 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6383 | /* |
| 6384 | * While doing polled IO, before going to sleep, we need |
| 6385 | * to check if there are new reqs added to poll_list, it |
| 6386 | * is because reqs may have been punted to io worker and |
| 6387 | * will be added to poll_list later, hence check the |
| 6388 | * poll_list again. |
| 6389 | */ |
| 6390 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6391 | !list_empty_careful(&ctx->poll_list)) { |
| 6392 | finish_wait(&ctx->sqo_wait, &wait); |
| 6393 | continue; |
| 6394 | } |
| 6395 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6396 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6397 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 6398 | /* make sure to read SQ tail after writing flags */ |
| 6399 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6400 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6401 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6402 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6403 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6404 | finish_wait(&ctx->sqo_wait, &wait); |
| 6405 | break; |
| 6406 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6407 | if (current->task_works) { |
| 6408 | task_work_run(); |
Hillf Danton | 10bea96 | 2020-04-01 17:19:33 +0800 | [diff] [blame] | 6409 | finish_wait(&ctx->sqo_wait, &wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6410 | continue; |
| 6411 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6412 | if (signal_pending(current)) |
| 6413 | flush_signals(current); |
| 6414 | schedule(); |
| 6415 | finish_wait(&ctx->sqo_wait, &wait); |
| 6416 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6417 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Xiaoguang Wang | d4ae271 | 2020-05-20 21:24:35 +0800 | [diff] [blame] | 6418 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6419 | continue; |
| 6420 | } |
| 6421 | finish_wait(&ctx->sqo_wait, &wait); |
| 6422 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6423 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6424 | } |
| 6425 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6426 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 6b668c9 | 2020-05-20 15:35:03 +0800 | [diff] [blame] | 6427 | if (likely(!percpu_ref_is_dying(&ctx->refs))) |
| 6428 | ret = io_submit_sqes(ctx, to_submit, NULL, -1); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6429 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6430 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6431 | } |
| 6432 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6433 | if (current->task_works) |
| 6434 | task_work_run(); |
| 6435 | |
Pavel Begunkov | bf9c2f1 | 2020-04-12 02:05:02 +0300 | [diff] [blame] | 6436 | io_sq_thread_drop_mm(ctx); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6437 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6438 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6439 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6440 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6441 | return 0; |
| 6442 | } |
| 6443 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6444 | struct io_wait_queue { |
| 6445 | struct wait_queue_entry wq; |
| 6446 | struct io_ring_ctx *ctx; |
| 6447 | unsigned to_wait; |
| 6448 | unsigned nr_timeouts; |
| 6449 | }; |
| 6450 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6451 | 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] | 6452 | { |
| 6453 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6454 | |
| 6455 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6456 | * 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] | 6457 | * started waiting. For timeouts, we always want to return to userspace, |
| 6458 | * regardless of event count. |
| 6459 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6460 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6461 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6462 | } |
| 6463 | |
| 6464 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6465 | int wake_flags, void *key) |
| 6466 | { |
| 6467 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6468 | wq); |
| 6469 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6470 | /* use noflush == true, as we can't safely rely on locking context */ |
| 6471 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6472 | return -1; |
| 6473 | |
| 6474 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6475 | } |
| 6476 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6477 | /* |
| 6478 | * Wait until events become available, if we don't already have some. The |
| 6479 | * application must reap them itself, as they reside on the shared cq ring. |
| 6480 | */ |
| 6481 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 6482 | const sigset_t __user *sig, size_t sigsz) |
| 6483 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6484 | struct io_wait_queue iowq = { |
| 6485 | .wq = { |
| 6486 | .private = current, |
| 6487 | .func = io_wake_function, |
| 6488 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6489 | }, |
| 6490 | .ctx = ctx, |
| 6491 | .to_wait = min_events, |
| 6492 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6493 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6494 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6495 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6496 | do { |
| 6497 | if (io_cqring_events(ctx, false) >= min_events) |
| 6498 | return 0; |
| 6499 | if (!current->task_works) |
| 6500 | break; |
| 6501 | task_work_run(); |
| 6502 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6503 | |
| 6504 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6505 | #ifdef CONFIG_COMPAT |
| 6506 | if (in_compat_syscall()) |
| 6507 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6508 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6509 | else |
| 6510 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6511 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6512 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6513 | if (ret) |
| 6514 | return ret; |
| 6515 | } |
| 6516 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6517 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6518 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6519 | do { |
| 6520 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6521 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6522 | if (current->task_works) |
| 6523 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6524 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6525 | break; |
| 6526 | schedule(); |
| 6527 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6528 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6529 | break; |
| 6530 | } |
| 6531 | } while (1); |
| 6532 | finish_wait(&ctx->wait, &iowq.wq); |
| 6533 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6534 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6535 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6536 | 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] | 6537 | } |
| 6538 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6539 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6540 | { |
| 6541 | #if defined(CONFIG_UNIX) |
| 6542 | if (ctx->ring_sock) { |
| 6543 | struct sock *sock = ctx->ring_sock->sk; |
| 6544 | struct sk_buff *skb; |
| 6545 | |
| 6546 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6547 | kfree_skb(skb); |
| 6548 | } |
| 6549 | #else |
| 6550 | int i; |
| 6551 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6552 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6553 | struct file *file; |
| 6554 | |
| 6555 | file = io_file_from_index(ctx, i); |
| 6556 | if (file) |
| 6557 | fput(file); |
| 6558 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6559 | #endif |
| 6560 | } |
| 6561 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6562 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 6563 | { |
| 6564 | struct fixed_file_data *data; |
| 6565 | |
| 6566 | data = container_of(ref, struct fixed_file_data, refs); |
| 6567 | complete(&data->done); |
| 6568 | } |
| 6569 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6570 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6571 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6572 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6573 | struct fixed_file_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6574 | unsigned nr_tables, i; |
| 6575 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6576 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6577 | return -ENXIO; |
| 6578 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6579 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6580 | if (!list_empty(&data->ref_list)) |
| 6581 | ref_node = list_first_entry(&data->ref_list, |
| 6582 | struct fixed_file_ref_node, node); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6583 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6584 | if (ref_node) |
| 6585 | percpu_ref_kill(&ref_node->refs); |
| 6586 | |
| 6587 | percpu_ref_kill(&data->refs); |
| 6588 | |
| 6589 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6590 | flush_delayed_work(&ctx->file_put_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6591 | wait_for_completion(&data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6592 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6593 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6594 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 6595 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6596 | kfree(data->table[i].files); |
| 6597 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6598 | percpu_ref_exit(&data->refs); |
| 6599 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6600 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6601 | ctx->nr_user_files = 0; |
| 6602 | return 0; |
| 6603 | } |
| 6604 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6605 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 6606 | { |
| 6607 | if (ctx->sqo_thread) { |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6608 | wait_for_completion(&ctx->sq_thread_comp); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6609 | /* |
| 6610 | * The park is a bit of a work-around, without it we get |
| 6611 | * warning spews on shutdown with SQPOLL set and affinity |
| 6612 | * set to a single CPU. |
| 6613 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6614 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6615 | kthread_stop(ctx->sqo_thread); |
| 6616 | ctx->sqo_thread = NULL; |
| 6617 | } |
| 6618 | } |
| 6619 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6620 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 6621 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6622 | io_sq_thread_stop(ctx); |
| 6623 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6624 | if (ctx->io_wq) { |
| 6625 | io_wq_destroy(ctx->io_wq); |
| 6626 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6627 | } |
| 6628 | } |
| 6629 | |
| 6630 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6631 | /* |
| 6632 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 6633 | * the io_uring can be safely unregistered on process exit, even if we have |
| 6634 | * loops in the file referencing. |
| 6635 | */ |
| 6636 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 6637 | { |
| 6638 | struct sock *sk = ctx->ring_sock->sk; |
| 6639 | struct scm_fp_list *fpl; |
| 6640 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6641 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6642 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6643 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 6644 | if (!fpl) |
| 6645 | return -ENOMEM; |
| 6646 | |
| 6647 | skb = alloc_skb(0, GFP_KERNEL); |
| 6648 | if (!skb) { |
| 6649 | kfree(fpl); |
| 6650 | return -ENOMEM; |
| 6651 | } |
| 6652 | |
| 6653 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6654 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6655 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6656 | fpl->user = get_uid(ctx->user); |
| 6657 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6658 | struct file *file = io_file_from_index(ctx, i + offset); |
| 6659 | |
| 6660 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6661 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6662 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6663 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 6664 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6665 | } |
| 6666 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6667 | if (nr_files) { |
| 6668 | fpl->max = SCM_MAX_FD; |
| 6669 | fpl->count = nr_files; |
| 6670 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6671 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6672 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 6673 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6674 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6675 | for (i = 0; i < nr_files; i++) |
| 6676 | fput(fpl->fp[i]); |
| 6677 | } else { |
| 6678 | kfree_skb(skb); |
| 6679 | kfree(fpl); |
| 6680 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6681 | |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
| 6685 | /* |
| 6686 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 6687 | * causes regular reference counting to break down. We rely on the UNIX |
| 6688 | * garbage collection to take care of this problem for us. |
| 6689 | */ |
| 6690 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6691 | { |
| 6692 | unsigned left, total; |
| 6693 | int ret = 0; |
| 6694 | |
| 6695 | total = 0; |
| 6696 | left = ctx->nr_user_files; |
| 6697 | while (left) { |
| 6698 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6699 | |
| 6700 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 6701 | if (ret) |
| 6702 | break; |
| 6703 | left -= this_files; |
| 6704 | total += this_files; |
| 6705 | } |
| 6706 | |
| 6707 | if (!ret) |
| 6708 | return 0; |
| 6709 | |
| 6710 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6711 | struct file *file = io_file_from_index(ctx, total); |
| 6712 | |
| 6713 | if (file) |
| 6714 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6715 | total++; |
| 6716 | } |
| 6717 | |
| 6718 | return ret; |
| 6719 | } |
| 6720 | #else |
| 6721 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6722 | { |
| 6723 | return 0; |
| 6724 | } |
| 6725 | #endif |
| 6726 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6727 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 6728 | unsigned nr_files) |
| 6729 | { |
| 6730 | int i; |
| 6731 | |
| 6732 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6733 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6734 | unsigned this_files; |
| 6735 | |
| 6736 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 6737 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 6738 | GFP_KERNEL); |
| 6739 | if (!table->files) |
| 6740 | break; |
| 6741 | nr_files -= this_files; |
| 6742 | } |
| 6743 | |
| 6744 | if (i == nr_tables) |
| 6745 | return 0; |
| 6746 | |
| 6747 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6748 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6749 | kfree(table->files); |
| 6750 | } |
| 6751 | return 1; |
| 6752 | } |
| 6753 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6754 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6755 | { |
| 6756 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6757 | struct sock *sock = ctx->ring_sock->sk; |
| 6758 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 6759 | struct sk_buff *skb; |
| 6760 | int i; |
| 6761 | |
| 6762 | __skb_queue_head_init(&list); |
| 6763 | |
| 6764 | /* |
| 6765 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 6766 | * remove this entry and rearrange the file array. |
| 6767 | */ |
| 6768 | skb = skb_dequeue(head); |
| 6769 | while (skb) { |
| 6770 | struct scm_fp_list *fp; |
| 6771 | |
| 6772 | fp = UNIXCB(skb).fp; |
| 6773 | for (i = 0; i < fp->count; i++) { |
| 6774 | int left; |
| 6775 | |
| 6776 | if (fp->fp[i] != file) |
| 6777 | continue; |
| 6778 | |
| 6779 | unix_notinflight(fp->user, fp->fp[i]); |
| 6780 | left = fp->count - 1 - i; |
| 6781 | if (left) { |
| 6782 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 6783 | left * sizeof(struct file *)); |
| 6784 | } |
| 6785 | fp->count--; |
| 6786 | if (!fp->count) { |
| 6787 | kfree_skb(skb); |
| 6788 | skb = NULL; |
| 6789 | } else { |
| 6790 | __skb_queue_tail(&list, skb); |
| 6791 | } |
| 6792 | fput(file); |
| 6793 | file = NULL; |
| 6794 | break; |
| 6795 | } |
| 6796 | |
| 6797 | if (!file) |
| 6798 | break; |
| 6799 | |
| 6800 | __skb_queue_tail(&list, skb); |
| 6801 | |
| 6802 | skb = skb_dequeue(head); |
| 6803 | } |
| 6804 | |
| 6805 | if (skb_peek(&list)) { |
| 6806 | spin_lock_irq(&head->lock); |
| 6807 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 6808 | __skb_queue_tail(head, skb); |
| 6809 | spin_unlock_irq(&head->lock); |
| 6810 | } |
| 6811 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6812 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6813 | #endif |
| 6814 | } |
| 6815 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6816 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6817 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6818 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6819 | }; |
| 6820 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6821 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6822 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6823 | struct fixed_file_data *file_data = ref_node->file_data; |
| 6824 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6825 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6826 | |
| 6827 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6828 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6829 | io_ring_file_put(ctx, pfile->file); |
| 6830 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6831 | } |
| 6832 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6833 | spin_lock(&file_data->lock); |
| 6834 | list_del(&ref_node->node); |
| 6835 | spin_unlock(&file_data->lock); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6836 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6837 | percpu_ref_exit(&ref_node->refs); |
| 6838 | kfree(ref_node); |
| 6839 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6840 | } |
| 6841 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6842 | static void io_file_put_work(struct work_struct *work) |
| 6843 | { |
| 6844 | struct io_ring_ctx *ctx; |
| 6845 | struct llist_node *node; |
| 6846 | |
| 6847 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 6848 | node = llist_del_all(&ctx->file_put_llist); |
| 6849 | |
| 6850 | while (node) { |
| 6851 | struct fixed_file_ref_node *ref_node; |
| 6852 | struct llist_node *next = node->next; |
| 6853 | |
| 6854 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 6855 | __io_file_put_work(ref_node); |
| 6856 | node = next; |
| 6857 | } |
| 6858 | } |
| 6859 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6860 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 6861 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6862 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6863 | struct io_ring_ctx *ctx; |
| 6864 | bool first_add; |
| 6865 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6866 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6867 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6868 | ctx = ref_node->file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6869 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6870 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
| 6871 | delay = 0; |
| 6872 | |
| 6873 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 6874 | if (!delay) |
| 6875 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 6876 | else if (first_add) |
| 6877 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 6881 | struct io_ring_ctx *ctx) |
| 6882 | { |
| 6883 | struct fixed_file_ref_node *ref_node; |
| 6884 | |
| 6885 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 6886 | if (!ref_node) |
| 6887 | return ERR_PTR(-ENOMEM); |
| 6888 | |
| 6889 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 6890 | 0, GFP_KERNEL)) { |
| 6891 | kfree(ref_node); |
| 6892 | return ERR_PTR(-ENOMEM); |
| 6893 | } |
| 6894 | INIT_LIST_HEAD(&ref_node->node); |
| 6895 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6896 | ref_node->file_data = ctx->file_data; |
| 6897 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6898 | } |
| 6899 | |
| 6900 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 6901 | { |
| 6902 | percpu_ref_exit(&ref_node->refs); |
| 6903 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6907 | unsigned nr_args) |
| 6908 | { |
| 6909 | __s32 __user *fds = (__s32 __user *) arg; |
| 6910 | unsigned nr_tables; |
| 6911 | struct file *file; |
| 6912 | int fd, ret = 0; |
| 6913 | unsigned i; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6914 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6915 | |
| 6916 | if (ctx->file_data) |
| 6917 | return -EBUSY; |
| 6918 | if (!nr_args) |
| 6919 | return -EINVAL; |
| 6920 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 6921 | return -EMFILE; |
| 6922 | |
| 6923 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 6924 | if (!ctx->file_data) |
| 6925 | return -ENOMEM; |
| 6926 | ctx->file_data->ctx = ctx; |
| 6927 | init_completion(&ctx->file_data->done); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6928 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
Xiaoguang Wang | f7fe934 | 2020-04-07 20:02:31 +0800 | [diff] [blame] | 6929 | spin_lock_init(&ctx->file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6930 | |
| 6931 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 6932 | ctx->file_data->table = kcalloc(nr_tables, |
| 6933 | sizeof(struct fixed_file_table), |
| 6934 | GFP_KERNEL); |
| 6935 | if (!ctx->file_data->table) { |
| 6936 | kfree(ctx->file_data); |
| 6937 | ctx->file_data = NULL; |
| 6938 | return -ENOMEM; |
| 6939 | } |
| 6940 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6941 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6942 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 6943 | kfree(ctx->file_data->table); |
| 6944 | kfree(ctx->file_data); |
| 6945 | ctx->file_data = NULL; |
| 6946 | return -ENOMEM; |
| 6947 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6948 | |
| 6949 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 6950 | percpu_ref_exit(&ctx->file_data->refs); |
| 6951 | kfree(ctx->file_data->table); |
| 6952 | kfree(ctx->file_data); |
| 6953 | ctx->file_data = NULL; |
| 6954 | return -ENOMEM; |
| 6955 | } |
| 6956 | |
| 6957 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 6958 | struct fixed_file_table *table; |
| 6959 | unsigned index; |
| 6960 | |
| 6961 | ret = -EFAULT; |
| 6962 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 6963 | break; |
| 6964 | /* allow sparse sets */ |
| 6965 | if (fd == -1) { |
| 6966 | ret = 0; |
| 6967 | continue; |
| 6968 | } |
| 6969 | |
| 6970 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6971 | index = i & IORING_FILE_TABLE_MASK; |
| 6972 | file = fget(fd); |
| 6973 | |
| 6974 | ret = -EBADF; |
| 6975 | if (!file) |
| 6976 | break; |
| 6977 | |
| 6978 | /* |
| 6979 | * Don't allow io_uring instances to be registered. If UNIX |
| 6980 | * isn't enabled, then this causes a reference cycle and this |
| 6981 | * instance can never get freed. If UNIX is enabled we'll |
| 6982 | * handle it just fine, but there's still no point in allowing |
| 6983 | * a ring fd as it doesn't support regular read/write anyway. |
| 6984 | */ |
| 6985 | if (file->f_op == &io_uring_fops) { |
| 6986 | fput(file); |
| 6987 | break; |
| 6988 | } |
| 6989 | ret = 0; |
| 6990 | table->files[index] = file; |
| 6991 | } |
| 6992 | |
| 6993 | if (ret) { |
| 6994 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6995 | file = io_file_from_index(ctx, i); |
| 6996 | if (file) |
| 6997 | fput(file); |
| 6998 | } |
| 6999 | for (i = 0; i < nr_tables; i++) |
| 7000 | kfree(ctx->file_data->table[i].files); |
| 7001 | |
| 7002 | kfree(ctx->file_data->table); |
| 7003 | kfree(ctx->file_data); |
| 7004 | ctx->file_data = NULL; |
| 7005 | ctx->nr_user_files = 0; |
| 7006 | return ret; |
| 7007 | } |
| 7008 | |
| 7009 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7010 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7011 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7012 | return ret; |
| 7013 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7014 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7015 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7016 | if (IS_ERR(ref_node)) { |
| 7017 | io_sqe_files_unregister(ctx); |
| 7018 | return PTR_ERR(ref_node); |
| 7019 | } |
| 7020 | |
| 7021 | ctx->file_data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7022 | spin_lock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7023 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7024 | spin_unlock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7025 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7026 | return ret; |
| 7027 | } |
| 7028 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7029 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7030 | int index) |
| 7031 | { |
| 7032 | #if defined(CONFIG_UNIX) |
| 7033 | struct sock *sock = ctx->ring_sock->sk; |
| 7034 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7035 | struct sk_buff *skb; |
| 7036 | |
| 7037 | /* |
| 7038 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7039 | * file set. If there's no room, fall back to allocating a new skb |
| 7040 | * and filling it in. |
| 7041 | */ |
| 7042 | spin_lock_irq(&head->lock); |
| 7043 | skb = skb_peek(head); |
| 7044 | if (skb) { |
| 7045 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7046 | |
| 7047 | if (fpl->count < SCM_MAX_FD) { |
| 7048 | __skb_unlink(skb, head); |
| 7049 | spin_unlock_irq(&head->lock); |
| 7050 | fpl->fp[fpl->count] = get_file(file); |
| 7051 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7052 | fpl->count++; |
| 7053 | spin_lock_irq(&head->lock); |
| 7054 | __skb_queue_head(head, skb); |
| 7055 | } else { |
| 7056 | skb = NULL; |
| 7057 | } |
| 7058 | } |
| 7059 | spin_unlock_irq(&head->lock); |
| 7060 | |
| 7061 | if (skb) { |
| 7062 | fput(file); |
| 7063 | return 0; |
| 7064 | } |
| 7065 | |
| 7066 | return __io_sqe_files_scm(ctx, 1, index); |
| 7067 | #else |
| 7068 | return 0; |
| 7069 | #endif |
| 7070 | } |
| 7071 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7072 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7073 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7074 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7075 | struct io_file_put *pfile; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7076 | struct percpu_ref *refs = data->cur_refs; |
| 7077 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7078 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7079 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7080 | if (!pfile) |
| 7081 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7082 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7083 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7084 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7085 | list_add(&pfile->list, &ref_node->file_list); |
| 7086 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7087 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 7091 | struct io_uring_files_update *up, |
| 7092 | unsigned nr_args) |
| 7093 | { |
| 7094 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7095 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7096 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7097 | __s32 __user *fds; |
| 7098 | int fd, i, err; |
| 7099 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7100 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7101 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7102 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7103 | return -EOVERFLOW; |
| 7104 | if (done > ctx->nr_user_files) |
| 7105 | return -EINVAL; |
| 7106 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7107 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7108 | if (IS_ERR(ref_node)) |
| 7109 | return PTR_ERR(ref_node); |
| 7110 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7111 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7112 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7113 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7114 | struct fixed_file_table *table; |
| 7115 | unsigned index; |
| 7116 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7117 | err = 0; |
| 7118 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7119 | err = -EFAULT; |
| 7120 | break; |
| 7121 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7122 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7123 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7124 | index = i & IORING_FILE_TABLE_MASK; |
| 7125 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7126 | file = io_file_from_index(ctx, index); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7127 | err = io_queue_file_removal(data, file); |
| 7128 | if (err) |
| 7129 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7130 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7131 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7132 | } |
| 7133 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7134 | file = fget(fd); |
| 7135 | if (!file) { |
| 7136 | err = -EBADF; |
| 7137 | break; |
| 7138 | } |
| 7139 | /* |
| 7140 | * Don't allow io_uring instances to be registered. If |
| 7141 | * UNIX isn't enabled, then this causes a reference |
| 7142 | * cycle and this instance can never get freed. If UNIX |
| 7143 | * is enabled we'll handle it just fine, but there's |
| 7144 | * still no point in allowing a ring fd as it doesn't |
| 7145 | * support regular read/write anyway. |
| 7146 | */ |
| 7147 | if (file->f_op == &io_uring_fops) { |
| 7148 | fput(file); |
| 7149 | err = -EBADF; |
| 7150 | break; |
| 7151 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7152 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7153 | err = io_sqe_file_register(ctx, file, i); |
| 7154 | if (err) |
| 7155 | break; |
| 7156 | } |
| 7157 | nr_args--; |
| 7158 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7159 | up->offset++; |
| 7160 | } |
| 7161 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7162 | if (needs_switch) { |
| 7163 | percpu_ref_kill(data->cur_refs); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7164 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7165 | list_add(&ref_node->node, &data->ref_list); |
| 7166 | data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7167 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7168 | percpu_ref_get(&ctx->file_data->refs); |
| 7169 | } else |
| 7170 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7171 | |
| 7172 | return done ? done : err; |
| 7173 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7174 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7175 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7176 | unsigned nr_args) |
| 7177 | { |
| 7178 | struct io_uring_files_update up; |
| 7179 | |
| 7180 | if (!ctx->file_data) |
| 7181 | return -ENXIO; |
| 7182 | if (!nr_args) |
| 7183 | return -EINVAL; |
| 7184 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7185 | return -EFAULT; |
| 7186 | if (up.resv) |
| 7187 | return -EINVAL; |
| 7188 | |
| 7189 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7190 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7191 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7192 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7193 | { |
| 7194 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7195 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7196 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7197 | io_put_req(req); |
| 7198 | } |
| 7199 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7200 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7201 | struct io_uring_params *p) |
| 7202 | { |
| 7203 | struct io_wq_data data; |
| 7204 | struct fd f; |
| 7205 | struct io_ring_ctx *ctx_attach; |
| 7206 | unsigned int concurrency; |
| 7207 | int ret = 0; |
| 7208 | |
| 7209 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7210 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7211 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7212 | |
| 7213 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 7214 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7215 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 7216 | |
| 7217 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 7218 | if (IS_ERR(ctx->io_wq)) { |
| 7219 | ret = PTR_ERR(ctx->io_wq); |
| 7220 | ctx->io_wq = NULL; |
| 7221 | } |
| 7222 | return ret; |
| 7223 | } |
| 7224 | |
| 7225 | f = fdget(p->wq_fd); |
| 7226 | if (!f.file) |
| 7227 | return -EBADF; |
| 7228 | |
| 7229 | if (f.file->f_op != &io_uring_fops) { |
| 7230 | ret = -EINVAL; |
| 7231 | goto out_fput; |
| 7232 | } |
| 7233 | |
| 7234 | ctx_attach = f.file->private_data; |
| 7235 | /* @io_wq is protected by holding the fd */ |
| 7236 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 7237 | ret = -EINVAL; |
| 7238 | goto out_fput; |
| 7239 | } |
| 7240 | |
| 7241 | ctx->io_wq = ctx_attach->io_wq; |
| 7242 | out_fput: |
| 7243 | fdput(f); |
| 7244 | return ret; |
| 7245 | } |
| 7246 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7247 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 7248 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7249 | { |
| 7250 | int ret; |
| 7251 | |
| 7252 | mmgrab(current->mm); |
| 7253 | ctx->sqo_mm = current->mm; |
| 7254 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7255 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7256 | ret = -EPERM; |
| 7257 | if (!capable(CAP_SYS_ADMIN)) |
| 7258 | goto err; |
| 7259 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7260 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7261 | if (!ctx->sq_thread_idle) |
| 7262 | ctx->sq_thread_idle = HZ; |
| 7263 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7264 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7265 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7266 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7267 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7268 | if (cpu >= nr_cpu_ids) |
| 7269 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7270 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7271 | goto err; |
| 7272 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7273 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 7274 | ctx, cpu, |
| 7275 | "io_uring-sq"); |
| 7276 | } else { |
| 7277 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 7278 | "io_uring-sq"); |
| 7279 | } |
| 7280 | if (IS_ERR(ctx->sqo_thread)) { |
| 7281 | ret = PTR_ERR(ctx->sqo_thread); |
| 7282 | ctx->sqo_thread = NULL; |
| 7283 | goto err; |
| 7284 | } |
| 7285 | wake_up_process(ctx->sqo_thread); |
| 7286 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7287 | /* Can't have SQ_AFF without SQPOLL */ |
| 7288 | ret = -EINVAL; |
| 7289 | goto err; |
| 7290 | } |
| 7291 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7292 | ret = io_init_wq_offload(ctx, p); |
| 7293 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7294 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7295 | |
| 7296 | return 0; |
| 7297 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 7298 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7299 | mmdrop(ctx->sqo_mm); |
| 7300 | ctx->sqo_mm = NULL; |
| 7301 | return ret; |
| 7302 | } |
| 7303 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7304 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7305 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7306 | { |
| 7307 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7308 | } |
| 7309 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7310 | static inline int __io_account_mem(struct user_struct *user, |
| 7311 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7312 | { |
| 7313 | unsigned long page_limit, cur_pages, new_pages; |
| 7314 | |
| 7315 | /* Don't allow more pages than we can safely lock */ |
| 7316 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7317 | |
| 7318 | do { |
| 7319 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7320 | new_pages = cur_pages + nr_pages; |
| 7321 | if (new_pages > page_limit) |
| 7322 | return -ENOMEM; |
| 7323 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7324 | new_pages) != cur_pages); |
| 7325 | |
| 7326 | return 0; |
| 7327 | } |
| 7328 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7329 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7330 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7331 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 7332 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7333 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7334 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7335 | if (ctx->sqo_mm) { |
| 7336 | if (acct == ACCT_LOCKED) |
| 7337 | ctx->sqo_mm->locked_vm -= nr_pages; |
| 7338 | else if (acct == ACCT_PINNED) |
| 7339 | atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7340 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7341 | } |
| 7342 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7343 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7344 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7345 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7346 | int ret; |
| 7347 | |
| 7348 | if (ctx->limit_mem) { |
| 7349 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7350 | if (ret) |
| 7351 | return ret; |
| 7352 | } |
| 7353 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7354 | if (ctx->sqo_mm) { |
| 7355 | if (acct == ACCT_LOCKED) |
| 7356 | ctx->sqo_mm->locked_vm += nr_pages; |
| 7357 | else if (acct == ACCT_PINNED) |
| 7358 | atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7359 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7360 | |
| 7361 | return 0; |
| 7362 | } |
| 7363 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7364 | static void io_mem_free(void *ptr) |
| 7365 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7366 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7367 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7368 | if (!ptr) |
| 7369 | return; |
| 7370 | |
| 7371 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7372 | if (put_page_testzero(page)) |
| 7373 | free_compound_page(page); |
| 7374 | } |
| 7375 | |
| 7376 | static void *io_mem_alloc(size_t size) |
| 7377 | { |
| 7378 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 7379 | __GFP_NORETRY; |
| 7380 | |
| 7381 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7382 | } |
| 7383 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7384 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7385 | size_t *sq_offset) |
| 7386 | { |
| 7387 | struct io_rings *rings; |
| 7388 | size_t off, sq_array_size; |
| 7389 | |
| 7390 | off = struct_size(rings, cqes, cq_entries); |
| 7391 | if (off == SIZE_MAX) |
| 7392 | return SIZE_MAX; |
| 7393 | |
| 7394 | #ifdef CONFIG_SMP |
| 7395 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 7396 | if (off == 0) |
| 7397 | return SIZE_MAX; |
| 7398 | #endif |
| 7399 | |
| 7400 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 7401 | if (sq_array_size == SIZE_MAX) |
| 7402 | return SIZE_MAX; |
| 7403 | |
| 7404 | if (check_add_overflow(off, sq_array_size, &off)) |
| 7405 | return SIZE_MAX; |
| 7406 | |
| 7407 | if (sq_offset) |
| 7408 | *sq_offset = off; |
| 7409 | |
| 7410 | return off; |
| 7411 | } |
| 7412 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7413 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 7414 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7415 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7416 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7417 | pages = (size_t)1 << get_order( |
| 7418 | rings_size(sq_entries, cq_entries, NULL)); |
| 7419 | pages += (size_t)1 << get_order( |
| 7420 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7421 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7422 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7423 | } |
| 7424 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7425 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 7426 | { |
| 7427 | int i, j; |
| 7428 | |
| 7429 | if (!ctx->user_bufs) |
| 7430 | return -ENXIO; |
| 7431 | |
| 7432 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7433 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7434 | |
| 7435 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7436 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7437 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7438 | io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7439 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7440 | imu->nr_bvecs = 0; |
| 7441 | } |
| 7442 | |
| 7443 | kfree(ctx->user_bufs); |
| 7444 | ctx->user_bufs = NULL; |
| 7445 | ctx->nr_user_bufs = 0; |
| 7446 | return 0; |
| 7447 | } |
| 7448 | |
| 7449 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 7450 | void __user *arg, unsigned index) |
| 7451 | { |
| 7452 | struct iovec __user *src; |
| 7453 | |
| 7454 | #ifdef CONFIG_COMPAT |
| 7455 | if (ctx->compat) { |
| 7456 | struct compat_iovec __user *ciovs; |
| 7457 | struct compat_iovec ciov; |
| 7458 | |
| 7459 | ciovs = (struct compat_iovec __user *) arg; |
| 7460 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 7461 | return -EFAULT; |
| 7462 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 7463 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7464 | dst->iov_len = ciov.iov_len; |
| 7465 | return 0; |
| 7466 | } |
| 7467 | #endif |
| 7468 | src = (struct iovec __user *) arg; |
| 7469 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 7470 | return -EFAULT; |
| 7471 | return 0; |
| 7472 | } |
| 7473 | |
| 7474 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7475 | unsigned nr_args) |
| 7476 | { |
| 7477 | struct vm_area_struct **vmas = NULL; |
| 7478 | struct page **pages = NULL; |
| 7479 | int i, j, got_pages = 0; |
| 7480 | int ret = -EINVAL; |
| 7481 | |
| 7482 | if (ctx->user_bufs) |
| 7483 | return -EBUSY; |
| 7484 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 7485 | return -EINVAL; |
| 7486 | |
| 7487 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 7488 | GFP_KERNEL); |
| 7489 | if (!ctx->user_bufs) |
| 7490 | return -ENOMEM; |
| 7491 | |
| 7492 | for (i = 0; i < nr_args; i++) { |
| 7493 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7494 | unsigned long off, start, end, ubuf; |
| 7495 | int pret, nr_pages; |
| 7496 | struct iovec iov; |
| 7497 | size_t size; |
| 7498 | |
| 7499 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 7500 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 7501 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7502 | |
| 7503 | /* |
| 7504 | * Don't impose further limits on the size and buffer |
| 7505 | * constraints here, we'll -EINVAL later when IO is |
| 7506 | * submitted if they are wrong. |
| 7507 | */ |
| 7508 | ret = -EFAULT; |
| 7509 | if (!iov.iov_base || !iov.iov_len) |
| 7510 | goto err; |
| 7511 | |
| 7512 | /* arbitrary limit, but we need something */ |
| 7513 | if (iov.iov_len > SZ_1G) |
| 7514 | goto err; |
| 7515 | |
| 7516 | ubuf = (unsigned long) iov.iov_base; |
| 7517 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 7518 | start = ubuf >> PAGE_SHIFT; |
| 7519 | nr_pages = end - start; |
| 7520 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7521 | ret = io_account_mem(ctx, nr_pages, ACCT_PINNED); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7522 | if (ret) |
| 7523 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7524 | |
| 7525 | ret = 0; |
| 7526 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 7527 | kvfree(vmas); |
| 7528 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7529 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7530 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7531 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7532 | sizeof(struct vm_area_struct *), |
| 7533 | GFP_KERNEL); |
| 7534 | if (!pages || !vmas) { |
| 7535 | ret = -ENOMEM; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7536 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7537 | goto err; |
| 7538 | } |
| 7539 | got_pages = nr_pages; |
| 7540 | } |
| 7541 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7542 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7543 | GFP_KERNEL); |
| 7544 | ret = -ENOMEM; |
| 7545 | if (!imu->bvec) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7546 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7547 | goto err; |
| 7548 | } |
| 7549 | |
| 7550 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7551 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 7552 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 7553 | FOLL_WRITE | FOLL_LONGTERM, |
| 7554 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7555 | if (pret == nr_pages) { |
| 7556 | /* don't support file backed memory */ |
| 7557 | for (j = 0; j < nr_pages; j++) { |
| 7558 | struct vm_area_struct *vma = vmas[j]; |
| 7559 | |
| 7560 | if (vma->vm_file && |
| 7561 | !is_file_hugepages(vma->vm_file)) { |
| 7562 | ret = -EOPNOTSUPP; |
| 7563 | break; |
| 7564 | } |
| 7565 | } |
| 7566 | } else { |
| 7567 | ret = pret < 0 ? pret : -EFAULT; |
| 7568 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7569 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7570 | if (ret) { |
| 7571 | /* |
| 7572 | * if we did partial map, or found file backed vmas, |
| 7573 | * release any pages we did get |
| 7574 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 7575 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7576 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7577 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7578 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7579 | goto err; |
| 7580 | } |
| 7581 | |
| 7582 | off = ubuf & ~PAGE_MASK; |
| 7583 | size = iov.iov_len; |
| 7584 | for (j = 0; j < nr_pages; j++) { |
| 7585 | size_t vec_len; |
| 7586 | |
| 7587 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 7588 | imu->bvec[j].bv_page = pages[j]; |
| 7589 | imu->bvec[j].bv_len = vec_len; |
| 7590 | imu->bvec[j].bv_offset = off; |
| 7591 | off = 0; |
| 7592 | size -= vec_len; |
| 7593 | } |
| 7594 | /* store original address for later verification */ |
| 7595 | imu->ubuf = ubuf; |
| 7596 | imu->len = iov.iov_len; |
| 7597 | imu->nr_bvecs = nr_pages; |
| 7598 | |
| 7599 | ctx->nr_user_bufs++; |
| 7600 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7601 | kvfree(pages); |
| 7602 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7603 | return 0; |
| 7604 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7605 | kvfree(pages); |
| 7606 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7607 | io_sqe_buffer_unregister(ctx); |
| 7608 | return ret; |
| 7609 | } |
| 7610 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7611 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 7612 | { |
| 7613 | __s32 __user *fds = arg; |
| 7614 | int fd; |
| 7615 | |
| 7616 | if (ctx->cq_ev_fd) |
| 7617 | return -EBUSY; |
| 7618 | |
| 7619 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 7620 | return -EFAULT; |
| 7621 | |
| 7622 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 7623 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 7624 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 7625 | ctx->cq_ev_fd = NULL; |
| 7626 | return ret; |
| 7627 | } |
| 7628 | |
| 7629 | return 0; |
| 7630 | } |
| 7631 | |
| 7632 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 7633 | { |
| 7634 | if (ctx->cq_ev_fd) { |
| 7635 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 7636 | ctx->cq_ev_fd = NULL; |
| 7637 | return 0; |
| 7638 | } |
| 7639 | |
| 7640 | return -ENXIO; |
| 7641 | } |
| 7642 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7643 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 7644 | { |
| 7645 | struct io_ring_ctx *ctx = data; |
| 7646 | struct io_buffer *buf = p; |
| 7647 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 7648 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7649 | return 0; |
| 7650 | } |
| 7651 | |
| 7652 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 7653 | { |
| 7654 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 7655 | idr_destroy(&ctx->io_buffer_idr); |
| 7656 | } |
| 7657 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7658 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 7659 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7660 | io_finish_async(ctx); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7661 | if (ctx->sqo_mm) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7662 | mmdrop(ctx->sqo_mm); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7663 | ctx->sqo_mm = NULL; |
| 7664 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7665 | |
| 7666 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7667 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7668 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7669 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7670 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 7671 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7672 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7673 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7674 | if (ctx->ring_sock) { |
| 7675 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7676 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7677 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7678 | #endif |
| 7679 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7680 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7681 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7682 | |
| 7683 | percpu_ref_exit(&ctx->refs); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7684 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 7685 | ACCT_LOCKED); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7686 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 7687 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 7688 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 7689 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7690 | kfree(ctx); |
| 7691 | } |
| 7692 | |
| 7693 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 7694 | { |
| 7695 | struct io_ring_ctx *ctx = file->private_data; |
| 7696 | __poll_t mask = 0; |
| 7697 | |
| 7698 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 7699 | /* |
| 7700 | * synchronizes with barrier from wq_has_sleeper call in |
| 7701 | * io_commit_cqring |
| 7702 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7703 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7704 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 7705 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7706 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 7707 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7708 | mask |= EPOLLIN | EPOLLRDNORM; |
| 7709 | |
| 7710 | return mask; |
| 7711 | } |
| 7712 | |
| 7713 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 7714 | { |
| 7715 | struct io_ring_ctx *ctx = file->private_data; |
| 7716 | |
| 7717 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 7718 | } |
| 7719 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7720 | static int io_remove_personalities(int id, void *p, void *data) |
| 7721 | { |
| 7722 | struct io_ring_ctx *ctx = data; |
| 7723 | const struct cred *cred; |
| 7724 | |
| 7725 | cred = idr_remove(&ctx->personality_idr, id); |
| 7726 | if (cred) |
| 7727 | put_cred(cred); |
| 7728 | return 0; |
| 7729 | } |
| 7730 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7731 | static void io_ring_exit_work(struct work_struct *work) |
| 7732 | { |
| 7733 | struct io_ring_ctx *ctx; |
| 7734 | |
| 7735 | ctx = container_of(work, struct io_ring_ctx, exit_work); |
| 7736 | if (ctx->rings) |
| 7737 | io_cqring_overflow_flush(ctx, true); |
| 7738 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 7739 | /* |
| 7740 | * If we're doing polled IO and end up having requests being |
| 7741 | * submitted async (out-of-line), then completions can come in while |
| 7742 | * we're waiting for refs to drop. We need to reap these manually, |
| 7743 | * as nobody else will be looking for them. |
| 7744 | */ |
| 7745 | while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) { |
| 7746 | io_iopoll_reap_events(ctx); |
| 7747 | if (ctx->rings) |
| 7748 | io_cqring_overflow_flush(ctx, true); |
| 7749 | } |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7750 | io_ring_ctx_free(ctx); |
| 7751 | } |
| 7752 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7753 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 7754 | { |
| 7755 | mutex_lock(&ctx->uring_lock); |
| 7756 | percpu_ref_kill(&ctx->refs); |
| 7757 | mutex_unlock(&ctx->uring_lock); |
| 7758 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 7759 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 7760 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7761 | |
| 7762 | if (ctx->io_wq) |
| 7763 | io_wq_cancel_all(ctx->io_wq); |
| 7764 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7765 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 7766 | /* if we failed setting up the ctx, we might not have any rings */ |
| 7767 | if (ctx->rings) |
| 7768 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7769 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7770 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
| 7771 | queue_work(system_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7772 | } |
| 7773 | |
| 7774 | static int io_uring_release(struct inode *inode, struct file *file) |
| 7775 | { |
| 7776 | struct io_ring_ctx *ctx = file->private_data; |
| 7777 | |
| 7778 | file->private_data = NULL; |
| 7779 | io_ring_ctx_wait_and_kill(ctx); |
| 7780 | return 0; |
| 7781 | } |
| 7782 | |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7783 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
| 7784 | { |
| 7785 | struct files_struct *files = data; |
| 7786 | |
| 7787 | return work->files == files; |
| 7788 | } |
| 7789 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7790 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 7791 | struct files_struct *files) |
| 7792 | { |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7793 | if (list_empty_careful(&ctx->inflight_list)) |
| 7794 | return; |
| 7795 | |
| 7796 | /* cancel all at once, should be faster than doing it one by one*/ |
| 7797 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); |
| 7798 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7799 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7800 | struct io_kiocb *cancel_req = NULL, *req; |
| 7801 | DEFINE_WAIT(wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7802 | |
| 7803 | spin_lock_irq(&ctx->inflight_lock); |
| 7804 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7805 | if (req->work.files != files) |
| 7806 | continue; |
| 7807 | /* req is being completed, ignore */ |
| 7808 | if (!refcount_inc_not_zero(&req->refs)) |
| 7809 | continue; |
| 7810 | cancel_req = req; |
| 7811 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7812 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7813 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7814 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7815 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7816 | spin_unlock_irq(&ctx->inflight_lock); |
| 7817 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7818 | /* We need to keep going until we don't find a matching req */ |
| 7819 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7820 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7821 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7822 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 7823 | spin_lock_irq(&ctx->completion_lock); |
| 7824 | list_del(&cancel_req->list); |
| 7825 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 7826 | if (list_empty(&ctx->cq_overflow_list)) { |
| 7827 | clear_bit(0, &ctx->sq_check_overflow); |
| 7828 | clear_bit(0, &ctx->cq_check_overflow); |
| 7829 | } |
| 7830 | spin_unlock_irq(&ctx->completion_lock); |
| 7831 | |
| 7832 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 7833 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 7834 | |
| 7835 | /* |
| 7836 | * Put inflight ref and overflow ref. If that's |
| 7837 | * all we had, then we're done with this request. |
| 7838 | */ |
| 7839 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
Pavel Begunkov | 4518a3c | 2020-05-26 20:34:02 +0300 | [diff] [blame] | 7840 | io_free_req(cancel_req); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7841 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7842 | continue; |
| 7843 | } |
Pavel Begunkov | 7b53d59 | 2020-05-30 14:19:15 +0300 | [diff] [blame] | 7844 | } else { |
| 7845 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 7846 | io_put_req(cancel_req); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7847 | } |
| 7848 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7849 | schedule(); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7850 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7851 | } |
| 7852 | } |
| 7853 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7854 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7855 | { |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7856 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7857 | struct task_struct *task = data; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7858 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7859 | return req->task == task; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7860 | } |
| 7861 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7862 | static int io_uring_flush(struct file *file, void *data) |
| 7863 | { |
| 7864 | struct io_ring_ctx *ctx = file->private_data; |
| 7865 | |
| 7866 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7867 | |
| 7868 | /* |
| 7869 | * If the task is going away, cancel work it may have pending |
| 7870 | */ |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7871 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 7872 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7873 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7874 | return 0; |
| 7875 | } |
| 7876 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7877 | static void *io_uring_validate_mmap_request(struct file *file, |
| 7878 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7879 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7880 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7881 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7882 | struct page *page; |
| 7883 | void *ptr; |
| 7884 | |
| 7885 | switch (offset) { |
| 7886 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7887 | case IORING_OFF_CQ_RING: |
| 7888 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7889 | break; |
| 7890 | case IORING_OFF_SQES: |
| 7891 | ptr = ctx->sq_sqes; |
| 7892 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7893 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7894 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7895 | } |
| 7896 | |
| 7897 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 7898 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7899 | return ERR_PTR(-EINVAL); |
| 7900 | |
| 7901 | return ptr; |
| 7902 | } |
| 7903 | |
| 7904 | #ifdef CONFIG_MMU |
| 7905 | |
| 7906 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7907 | { |
| 7908 | size_t sz = vma->vm_end - vma->vm_start; |
| 7909 | unsigned long pfn; |
| 7910 | void *ptr; |
| 7911 | |
| 7912 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 7913 | if (IS_ERR(ptr)) |
| 7914 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7915 | |
| 7916 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 7917 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 7918 | } |
| 7919 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7920 | #else /* !CONFIG_MMU */ |
| 7921 | |
| 7922 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7923 | { |
| 7924 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 7925 | } |
| 7926 | |
| 7927 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 7928 | { |
| 7929 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 7930 | } |
| 7931 | |
| 7932 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 7933 | unsigned long addr, unsigned long len, |
| 7934 | unsigned long pgoff, unsigned long flags) |
| 7935 | { |
| 7936 | void *ptr; |
| 7937 | |
| 7938 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 7939 | if (IS_ERR(ptr)) |
| 7940 | return PTR_ERR(ptr); |
| 7941 | |
| 7942 | return (unsigned long) ptr; |
| 7943 | } |
| 7944 | |
| 7945 | #endif /* !CONFIG_MMU */ |
| 7946 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7947 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 7948 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 7949 | size_t, sigsz) |
| 7950 | { |
| 7951 | struct io_ring_ctx *ctx; |
| 7952 | long ret = -EBADF; |
| 7953 | int submitted = 0; |
| 7954 | struct fd f; |
| 7955 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7956 | if (current->task_works) |
| 7957 | task_work_run(); |
| 7958 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7959 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7960 | return -EINVAL; |
| 7961 | |
| 7962 | f = fdget(fd); |
| 7963 | if (!f.file) |
| 7964 | return -EBADF; |
| 7965 | |
| 7966 | ret = -EOPNOTSUPP; |
| 7967 | if (f.file->f_op != &io_uring_fops) |
| 7968 | goto out_fput; |
| 7969 | |
| 7970 | ret = -ENXIO; |
| 7971 | ctx = f.file->private_data; |
| 7972 | if (!percpu_ref_tryget(&ctx->refs)) |
| 7973 | goto out_fput; |
| 7974 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7975 | /* |
| 7976 | * For SQ polling, the thread will do all submissions and completions. |
| 7977 | * Just return the requested submit count, and wake the thread if |
| 7978 | * we were asked to. |
| 7979 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7980 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7981 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7982 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 7983 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7984 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 7985 | wake_up(&ctx->sqo_wait); |
| 7986 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7987 | } else if (to_submit) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7988 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 7989 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7990 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7991 | |
| 7992 | if (submitted != to_submit) |
| 7993 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7994 | } |
| 7995 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7996 | unsigned nr_events = 0; |
| 7997 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7998 | min_complete = min(min_complete, ctx->cq_entries); |
| 7999 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 8000 | /* |
| 8001 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 8002 | * space applications don't need to do io completion events |
| 8003 | * polling again, they can rely on io_sq_thread to do polling |
| 8004 | * work, which can reduce cpu usage and uring_lock contention. |
| 8005 | */ |
| 8006 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 8007 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8008 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8009 | } else { |
| 8010 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 8011 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8012 | } |
| 8013 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 8014 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 8015 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8016 | out_fput: |
| 8017 | fdput(f); |
| 8018 | return submitted ? submitted : ret; |
| 8019 | } |
| 8020 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8021 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8022 | static int io_uring_show_cred(int id, void *p, void *data) |
| 8023 | { |
| 8024 | const struct cred *cred = p; |
| 8025 | struct seq_file *m = data; |
| 8026 | struct user_namespace *uns = seq_user_ns(m); |
| 8027 | struct group_info *gi; |
| 8028 | kernel_cap_t cap; |
| 8029 | unsigned __capi; |
| 8030 | int g; |
| 8031 | |
| 8032 | seq_printf(m, "%5d\n", id); |
| 8033 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 8034 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 8035 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 8036 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 8037 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 8038 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 8039 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 8040 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 8041 | seq_puts(m, "\n\tGroups:\t"); |
| 8042 | gi = cred->group_info; |
| 8043 | for (g = 0; g < gi->ngroups; g++) { |
| 8044 | seq_put_decimal_ull(m, g ? " " : "", |
| 8045 | from_kgid_munged(uns, gi->gid[g])); |
| 8046 | } |
| 8047 | seq_puts(m, "\n\tCapEff:\t"); |
| 8048 | cap = cred->cap_effective; |
| 8049 | CAP_FOR_EACH_U32(__capi) |
| 8050 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 8051 | seq_putc(m, '\n'); |
| 8052 | return 0; |
| 8053 | } |
| 8054 | |
| 8055 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 8056 | { |
| 8057 | int i; |
| 8058 | |
| 8059 | mutex_lock(&ctx->uring_lock); |
| 8060 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 8061 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8062 | struct fixed_file_table *table; |
| 8063 | struct file *f; |
| 8064 | |
| 8065 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 8066 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 8067 | if (f) |
| 8068 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 8069 | else |
| 8070 | seq_printf(m, "%5u: <none>\n", i); |
| 8071 | } |
| 8072 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 8073 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8074 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 8075 | |
| 8076 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 8077 | (unsigned int) buf->len); |
| 8078 | } |
| 8079 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 8080 | seq_printf(m, "Personalities:\n"); |
| 8081 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 8082 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 8083 | seq_printf(m, "PollList:\n"); |
| 8084 | spin_lock_irq(&ctx->completion_lock); |
| 8085 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 8086 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 8087 | struct io_kiocb *req; |
| 8088 | |
| 8089 | hlist_for_each_entry(req, list, hash_node) |
| 8090 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 8091 | req->task->task_works != NULL); |
| 8092 | } |
| 8093 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8094 | mutex_unlock(&ctx->uring_lock); |
| 8095 | } |
| 8096 | |
| 8097 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 8098 | { |
| 8099 | struct io_ring_ctx *ctx = f->private_data; |
| 8100 | |
| 8101 | if (percpu_ref_tryget(&ctx->refs)) { |
| 8102 | __io_uring_show_fdinfo(ctx, m); |
| 8103 | percpu_ref_put(&ctx->refs); |
| 8104 | } |
| 8105 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8106 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8107 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8108 | static const struct file_operations io_uring_fops = { |
| 8109 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8110 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8111 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8112 | #ifndef CONFIG_MMU |
| 8113 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 8114 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 8115 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8116 | .poll = io_uring_poll, |
| 8117 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8118 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8119 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8120 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8121 | }; |
| 8122 | |
| 8123 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 8124 | struct io_uring_params *p) |
| 8125 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8126 | struct io_rings *rings; |
| 8127 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8128 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8129 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 8130 | if (size == SIZE_MAX) |
| 8131 | return -EOVERFLOW; |
| 8132 | |
| 8133 | rings = io_mem_alloc(size); |
| 8134 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8135 | return -ENOMEM; |
| 8136 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8137 | ctx->rings = rings; |
| 8138 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 8139 | rings->sq_ring_mask = p->sq_entries - 1; |
| 8140 | rings->cq_ring_mask = p->cq_entries - 1; |
| 8141 | rings->sq_ring_entries = p->sq_entries; |
| 8142 | rings->cq_ring_entries = p->cq_entries; |
| 8143 | ctx->sq_mask = rings->sq_ring_mask; |
| 8144 | ctx->cq_mask = rings->cq_ring_mask; |
| 8145 | ctx->sq_entries = rings->sq_ring_entries; |
| 8146 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8147 | |
| 8148 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8149 | if (size == SIZE_MAX) { |
| 8150 | io_mem_free(ctx->rings); |
| 8151 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8152 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8153 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8154 | |
| 8155 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8156 | if (!ctx->sq_sqes) { |
| 8157 | io_mem_free(ctx->rings); |
| 8158 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8159 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8160 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8161 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8162 | return 0; |
| 8163 | } |
| 8164 | |
| 8165 | /* |
| 8166 | * Allocate an anonymous fd, this is what constitutes the application |
| 8167 | * visible backing of an io_uring instance. The application mmaps this |
| 8168 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 8169 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 8170 | */ |
| 8171 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 8172 | { |
| 8173 | struct file *file; |
| 8174 | int ret; |
| 8175 | |
| 8176 | #if defined(CONFIG_UNIX) |
| 8177 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 8178 | &ctx->ring_sock); |
| 8179 | if (ret) |
| 8180 | return ret; |
| 8181 | #endif |
| 8182 | |
| 8183 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 8184 | if (ret < 0) |
| 8185 | goto err; |
| 8186 | |
| 8187 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 8188 | O_RDWR | O_CLOEXEC); |
| 8189 | if (IS_ERR(file)) { |
| 8190 | put_unused_fd(ret); |
| 8191 | ret = PTR_ERR(file); |
| 8192 | goto err; |
| 8193 | } |
| 8194 | |
| 8195 | #if defined(CONFIG_UNIX) |
| 8196 | ctx->ring_sock->file = file; |
| 8197 | #endif |
| 8198 | fd_install(ret, file); |
| 8199 | return ret; |
| 8200 | err: |
| 8201 | #if defined(CONFIG_UNIX) |
| 8202 | sock_release(ctx->ring_sock); |
| 8203 | ctx->ring_sock = NULL; |
| 8204 | #endif |
| 8205 | return ret; |
| 8206 | } |
| 8207 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8208 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 8209 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8210 | { |
| 8211 | struct user_struct *user = NULL; |
| 8212 | struct io_ring_ctx *ctx; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8213 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8214 | int ret; |
| 8215 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8216 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8217 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8218 | if (entries > IORING_MAX_ENTRIES) { |
| 8219 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8220 | return -EINVAL; |
| 8221 | entries = IORING_MAX_ENTRIES; |
| 8222 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8223 | |
| 8224 | /* |
| 8225 | * Use twice as many entries for the CQ ring. It's possible for the |
| 8226 | * application to drive a higher depth than the size of the SQ ring, |
| 8227 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8228 | * some flexibility in overcommitting a bit. If the application has |
| 8229 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 8230 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8231 | */ |
| 8232 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8233 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 8234 | /* |
| 8235 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 8236 | * to a power-of-two, if it isn't already. We do NOT impose |
| 8237 | * any cq vs sq ring sizing. |
| 8238 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8239 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8240 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8241 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 8242 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8243 | return -EINVAL; |
| 8244 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 8245 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8246 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 8247 | } else { |
| 8248 | p->cq_entries = 2 * p->sq_entries; |
| 8249 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8250 | |
| 8251 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8252 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8253 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8254 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8255 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8256 | ring_pages(p->sq_entries, p->cq_entries)); |
| 8257 | if (ret) { |
| 8258 | free_uid(user); |
| 8259 | return ret; |
| 8260 | } |
| 8261 | } |
| 8262 | |
| 8263 | ctx = io_ring_ctx_alloc(p); |
| 8264 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8265 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8266 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8267 | p->cq_entries)); |
| 8268 | free_uid(user); |
| 8269 | return -ENOMEM; |
| 8270 | } |
| 8271 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8272 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 8273 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8274 | |
| 8275 | ret = io_allocate_scq_urings(ctx, p); |
| 8276 | if (ret) |
| 8277 | goto err; |
| 8278 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8279 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8280 | if (ret) |
| 8281 | goto err; |
| 8282 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8283 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8284 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 8285 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 8286 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 8287 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 8288 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 8289 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 8290 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8291 | |
| 8292 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8293 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 8294 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 8295 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 8296 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 8297 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 8298 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 8299 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 8300 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8301 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 8302 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8303 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
| 8304 | IORING_FEAT_POLL_32BITS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8305 | |
| 8306 | if (copy_to_user(params, p, sizeof(*p))) { |
| 8307 | ret = -EFAULT; |
| 8308 | goto err; |
| 8309 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 8310 | /* |
| 8311 | * Install ring fd as the very last thing, so we don't risk someone |
| 8312 | * having closed it before we finish setup |
| 8313 | */ |
| 8314 | ret = io_uring_get_fd(ctx); |
| 8315 | if (ret < 0) |
| 8316 | goto err; |
| 8317 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8318 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8319 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 8320 | ACCT_LOCKED); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8321 | ctx->limit_mem = limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8322 | return ret; |
| 8323 | err: |
| 8324 | io_ring_ctx_wait_and_kill(ctx); |
| 8325 | return ret; |
| 8326 | } |
| 8327 | |
| 8328 | /* |
| 8329 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 8330 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 8331 | * params structure passed in. |
| 8332 | */ |
| 8333 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 8334 | { |
| 8335 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8336 | int i; |
| 8337 | |
| 8338 | if (copy_from_user(&p, params, sizeof(p))) |
| 8339 | return -EFAULT; |
| 8340 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 8341 | if (p.resv[i]) |
| 8342 | return -EINVAL; |
| 8343 | } |
| 8344 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8345 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8346 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8347 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8348 | return -EINVAL; |
| 8349 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8350 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8351 | } |
| 8352 | |
| 8353 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 8354 | struct io_uring_params __user *, params) |
| 8355 | { |
| 8356 | return io_uring_setup(entries, params); |
| 8357 | } |
| 8358 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8359 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 8360 | { |
| 8361 | struct io_uring_probe *p; |
| 8362 | size_t size; |
| 8363 | int i, ret; |
| 8364 | |
| 8365 | size = struct_size(p, ops, nr_args); |
| 8366 | if (size == SIZE_MAX) |
| 8367 | return -EOVERFLOW; |
| 8368 | p = kzalloc(size, GFP_KERNEL); |
| 8369 | if (!p) |
| 8370 | return -ENOMEM; |
| 8371 | |
| 8372 | ret = -EFAULT; |
| 8373 | if (copy_from_user(p, arg, size)) |
| 8374 | goto out; |
| 8375 | ret = -EINVAL; |
| 8376 | if (memchr_inv(p, 0, size)) |
| 8377 | goto out; |
| 8378 | |
| 8379 | p->last_op = IORING_OP_LAST - 1; |
| 8380 | if (nr_args > IORING_OP_LAST) |
| 8381 | nr_args = IORING_OP_LAST; |
| 8382 | |
| 8383 | for (i = 0; i < nr_args; i++) { |
| 8384 | p->ops[i].op = i; |
| 8385 | if (!io_op_defs[i].not_supported) |
| 8386 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 8387 | } |
| 8388 | p->ops_len = i; |
| 8389 | |
| 8390 | ret = 0; |
| 8391 | if (copy_to_user(arg, p, size)) |
| 8392 | ret = -EFAULT; |
| 8393 | out: |
| 8394 | kfree(p); |
| 8395 | return ret; |
| 8396 | } |
| 8397 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8398 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 8399 | { |
| 8400 | const struct cred *creds = get_current_cred(); |
| 8401 | int id; |
| 8402 | |
| 8403 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 8404 | USHRT_MAX, GFP_KERNEL); |
| 8405 | if (id < 0) |
| 8406 | put_cred(creds); |
| 8407 | return id; |
| 8408 | } |
| 8409 | |
| 8410 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 8411 | { |
| 8412 | const struct cred *old_creds; |
| 8413 | |
| 8414 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 8415 | if (old_creds) { |
| 8416 | put_cred(old_creds); |
| 8417 | return 0; |
| 8418 | } |
| 8419 | |
| 8420 | return -EINVAL; |
| 8421 | } |
| 8422 | |
| 8423 | static bool io_register_op_must_quiesce(int op) |
| 8424 | { |
| 8425 | switch (op) { |
| 8426 | case IORING_UNREGISTER_FILES: |
| 8427 | case IORING_REGISTER_FILES_UPDATE: |
| 8428 | case IORING_REGISTER_PROBE: |
| 8429 | case IORING_REGISTER_PERSONALITY: |
| 8430 | case IORING_UNREGISTER_PERSONALITY: |
| 8431 | return false; |
| 8432 | default: |
| 8433 | return true; |
| 8434 | } |
| 8435 | } |
| 8436 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8437 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 8438 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8439 | __releases(ctx->uring_lock) |
| 8440 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8441 | { |
| 8442 | int ret; |
| 8443 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 8444 | /* |
| 8445 | * We're inside the ring mutex, if the ref is already dying, then |
| 8446 | * someone else killed the ctx or is already going through |
| 8447 | * io_uring_register(). |
| 8448 | */ |
| 8449 | if (percpu_ref_is_dying(&ctx->refs)) |
| 8450 | return -ENXIO; |
| 8451 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8452 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8453 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8454 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8455 | /* |
| 8456 | * Drop uring mutex before waiting for references to exit. If |
| 8457 | * another thread is currently inside io_uring_enter() it might |
| 8458 | * need to grab the uring_lock to make progress. If we hold it |
| 8459 | * here across the drain wait, then we can deadlock. It's safe |
| 8460 | * to drop the mutex here, since no new references will come in |
| 8461 | * after we've killed the percpu ref. |
| 8462 | */ |
| 8463 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8464 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8465 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8466 | if (ret) { |
| 8467 | percpu_ref_resurrect(&ctx->refs); |
| 8468 | ret = -EINTR; |
| 8469 | goto out; |
| 8470 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8471 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8472 | |
| 8473 | switch (opcode) { |
| 8474 | case IORING_REGISTER_BUFFERS: |
| 8475 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 8476 | break; |
| 8477 | case IORING_UNREGISTER_BUFFERS: |
| 8478 | ret = -EINVAL; |
| 8479 | if (arg || nr_args) |
| 8480 | break; |
| 8481 | ret = io_sqe_buffer_unregister(ctx); |
| 8482 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8483 | case IORING_REGISTER_FILES: |
| 8484 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 8485 | break; |
| 8486 | case IORING_UNREGISTER_FILES: |
| 8487 | ret = -EINVAL; |
| 8488 | if (arg || nr_args) |
| 8489 | break; |
| 8490 | ret = io_sqe_files_unregister(ctx); |
| 8491 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8492 | case IORING_REGISTER_FILES_UPDATE: |
| 8493 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 8494 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8495 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8496 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8497 | ret = -EINVAL; |
| 8498 | if (nr_args != 1) |
| 8499 | break; |
| 8500 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8501 | if (ret) |
| 8502 | break; |
| 8503 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 8504 | ctx->eventfd_async = 1; |
| 8505 | else |
| 8506 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8507 | break; |
| 8508 | case IORING_UNREGISTER_EVENTFD: |
| 8509 | ret = -EINVAL; |
| 8510 | if (arg || nr_args) |
| 8511 | break; |
| 8512 | ret = io_eventfd_unregister(ctx); |
| 8513 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8514 | case IORING_REGISTER_PROBE: |
| 8515 | ret = -EINVAL; |
| 8516 | if (!arg || nr_args > 256) |
| 8517 | break; |
| 8518 | ret = io_probe(ctx, arg, nr_args); |
| 8519 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8520 | case IORING_REGISTER_PERSONALITY: |
| 8521 | ret = -EINVAL; |
| 8522 | if (arg || nr_args) |
| 8523 | break; |
| 8524 | ret = io_register_personality(ctx); |
| 8525 | break; |
| 8526 | case IORING_UNREGISTER_PERSONALITY: |
| 8527 | ret = -EINVAL; |
| 8528 | if (arg) |
| 8529 | break; |
| 8530 | ret = io_unregister_personality(ctx, nr_args); |
| 8531 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8532 | default: |
| 8533 | ret = -EINVAL; |
| 8534 | break; |
| 8535 | } |
| 8536 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8537 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8538 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8539 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8540 | out: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8541 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8542 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8543 | return ret; |
| 8544 | } |
| 8545 | |
| 8546 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 8547 | void __user *, arg, unsigned int, nr_args) |
| 8548 | { |
| 8549 | struct io_ring_ctx *ctx; |
| 8550 | long ret = -EBADF; |
| 8551 | struct fd f; |
| 8552 | |
| 8553 | f = fdget(fd); |
| 8554 | if (!f.file) |
| 8555 | return -EBADF; |
| 8556 | |
| 8557 | ret = -EOPNOTSUPP; |
| 8558 | if (f.file->f_op != &io_uring_fops) |
| 8559 | goto out_fput; |
| 8560 | |
| 8561 | ctx = f.file->private_data; |
| 8562 | |
| 8563 | mutex_lock(&ctx->uring_lock); |
| 8564 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 8565 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8566 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 8567 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8568 | out_fput: |
| 8569 | fdput(f); |
| 8570 | return ret; |
| 8571 | } |
| 8572 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8573 | static int __init io_uring_init(void) |
| 8574 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8575 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 8576 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 8577 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 8578 | } while (0) |
| 8579 | |
| 8580 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 8581 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 8582 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 8583 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 8584 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 8585 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 8586 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 8587 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 8588 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 8589 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8590 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8591 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 8592 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 8593 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 8594 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 8595 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8596 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 8597 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8598 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 8599 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 8600 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 8601 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 8602 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 8603 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 8604 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 8605 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8606 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8607 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 8608 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 8609 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8610 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8611 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 8612 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 8613 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8614 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 8615 | return 0; |
| 8616 | }; |
| 8617 | __initcall(io_uring_init); |