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 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 323 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 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 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 328 | struct list_head iopoll_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; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 399 | struct list_head list; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 402 | struct io_rw { |
| 403 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 404 | struct kiocb kiocb; |
| 405 | u64 addr; |
| 406 | u64 len; |
| 407 | }; |
| 408 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 409 | struct io_connect { |
| 410 | struct file *file; |
| 411 | struct sockaddr __user *addr; |
| 412 | int addr_len; |
| 413 | }; |
| 414 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 415 | struct io_sr_msg { |
| 416 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 417 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 418 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 419 | void __user *buf; |
| 420 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 421 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 422 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 423 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 424 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 425 | }; |
| 426 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 427 | struct io_open { |
| 428 | struct file *file; |
| 429 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 430 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 431 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 432 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 435 | struct io_files_update { |
| 436 | struct file *file; |
| 437 | u64 arg; |
| 438 | u32 nr_args; |
| 439 | u32 offset; |
| 440 | }; |
| 441 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 442 | struct io_fadvise { |
| 443 | struct file *file; |
| 444 | u64 offset; |
| 445 | u32 len; |
| 446 | u32 advice; |
| 447 | }; |
| 448 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 449 | struct io_madvise { |
| 450 | struct file *file; |
| 451 | u64 addr; |
| 452 | u32 len; |
| 453 | u32 advice; |
| 454 | }; |
| 455 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 456 | struct io_epoll { |
| 457 | struct file *file; |
| 458 | int epfd; |
| 459 | int op; |
| 460 | int fd; |
| 461 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 464 | struct io_splice { |
| 465 | struct file *file_out; |
| 466 | struct file *file_in; |
| 467 | loff_t off_out; |
| 468 | loff_t off_in; |
| 469 | u64 len; |
| 470 | unsigned int flags; |
| 471 | }; |
| 472 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 473 | struct io_provide_buf { |
| 474 | struct file *file; |
| 475 | __u64 addr; |
| 476 | __s32 len; |
| 477 | __u32 bgid; |
| 478 | __u16 nbufs; |
| 479 | __u16 bid; |
| 480 | }; |
| 481 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 482 | struct io_statx { |
| 483 | struct file *file; |
| 484 | int dfd; |
| 485 | unsigned int mask; |
| 486 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 487 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 488 | struct statx __user *buffer; |
| 489 | }; |
| 490 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 491 | struct io_completion { |
| 492 | struct file *file; |
| 493 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 494 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 495 | }; |
| 496 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 497 | struct io_async_connect { |
| 498 | struct sockaddr_storage address; |
| 499 | }; |
| 500 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 501 | struct io_async_msghdr { |
| 502 | struct iovec fast_iov[UIO_FASTIOV]; |
| 503 | struct iovec *iov; |
| 504 | struct sockaddr __user *uaddr; |
| 505 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 506 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 509 | struct io_async_rw { |
| 510 | struct iovec fast_iov[UIO_FASTIOV]; |
| 511 | struct iovec *iov; |
| 512 | ssize_t nr_segs; |
| 513 | ssize_t size; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 514 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 517 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 518 | union { |
| 519 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 520 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 521 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 522 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 523 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 526 | enum { |
| 527 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 528 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 529 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 530 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 531 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 532 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 533 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 534 | REQ_F_LINK_HEAD_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 535 | REQ_F_FAIL_LINK_BIT, |
| 536 | REQ_F_INFLIGHT_BIT, |
| 537 | REQ_F_CUR_POS_BIT, |
| 538 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 539 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 540 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 541 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 542 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 543 | REQ_F_OVERFLOW_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 544 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 545 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 546 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 547 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 548 | REQ_F_TASK_PINNED_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 549 | |
| 550 | /* not a real bit, just to check we're not overflowing the space */ |
| 551 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | enum { |
| 555 | /* ctx owns file */ |
| 556 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 557 | /* drain existing IO first */ |
| 558 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 559 | /* linked sqes */ |
| 560 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 561 | /* doesn't sever on completion < 0 */ |
| 562 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 563 | /* IOSQE_ASYNC */ |
| 564 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 565 | /* IOSQE_BUFFER_SELECT */ |
| 566 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 567 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 568 | /* head of a link */ |
| 569 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 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), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 580 | /* regular file */ |
| 581 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 582 | /* completion under lock */ |
| 583 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 584 | /* needs cleanup */ |
| 585 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 586 | /* in overflow list */ |
| 587 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 588 | /* already went through poll handler */ |
| 589 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 590 | /* buffer already selected */ |
| 591 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 592 | /* doesn't need file table for this request */ |
| 593 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 594 | /* io_wq_work is initialized */ |
| 595 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 596 | /* req->task is refcounted */ |
| 597 | REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 598 | }; |
| 599 | |
| 600 | struct async_poll { |
| 601 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 602 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 603 | }; |
| 604 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 605 | /* |
| 606 | * NOTE! Each of the iocb union members has the file pointer |
| 607 | * as the first entry in their struct definition. So you can |
| 608 | * access the file pointer through any of the sub-structs, |
| 609 | * or directly as just 'ki_filp' in this struct. |
| 610 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 611 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 612 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 613 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 614 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 615 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 616 | struct io_accept accept; |
| 617 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 618 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 619 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 620 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 621 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 622 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 623 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 624 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 625 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 626 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 627 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 628 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 629 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 630 | struct io_statx statx; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 631 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 632 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 633 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 634 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 635 | struct io_async_ctx *io; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 636 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 637 | /* polled IO has completed */ |
| 638 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 639 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 640 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 641 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 642 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 643 | struct io_ring_ctx *ctx; |
| 644 | unsigned int flags; |
| 645 | refcount_t refs; |
| 646 | struct task_struct *task; |
| 647 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 648 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 649 | struct list_head link_list; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 650 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 651 | /* |
| 652 | * 1. used with ctx->iopoll_list with reads/writes |
| 653 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 654 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 655 | struct list_head inflight_entry; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 656 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 657 | struct percpu_ref *fixed_file_refs; |
| 658 | struct callback_head task_work; |
| 659 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 660 | struct hlist_node hash_node; |
| 661 | struct async_poll *apoll; |
| 662 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 663 | }; |
| 664 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 665 | struct io_defer_entry { |
| 666 | struct list_head list; |
| 667 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 668 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 669 | }; |
| 670 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 671 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 672 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 673 | struct io_comp_state { |
| 674 | unsigned int nr; |
| 675 | struct list_head list; |
| 676 | struct io_ring_ctx *ctx; |
| 677 | }; |
| 678 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 679 | struct io_submit_state { |
| 680 | struct blk_plug plug; |
| 681 | |
| 682 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 683 | * io_kiocb alloc cache |
| 684 | */ |
| 685 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 686 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 687 | |
| 688 | /* |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 689 | * Batch completion logic |
| 690 | */ |
| 691 | struct io_comp_state comp; |
| 692 | |
| 693 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 694 | * File reference cache |
| 695 | */ |
| 696 | struct file *file; |
| 697 | unsigned int fd; |
| 698 | unsigned int has_refs; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 699 | unsigned int ios_left; |
| 700 | }; |
| 701 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 702 | struct io_op_def { |
| 703 | /* needs req->io allocated for deferral/async */ |
| 704 | unsigned async_ctx : 1; |
| 705 | /* needs current->mm setup, does mm access */ |
| 706 | unsigned needs_mm : 1; |
| 707 | /* needs req->file assigned */ |
| 708 | unsigned needs_file : 1; |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 709 | /* don't fail if file grab fails */ |
| 710 | unsigned needs_file_no_error : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 711 | /* hash wq insertion if file is a regular file */ |
| 712 | unsigned hash_reg_file : 1; |
| 713 | /* unbound wq insertion if file is a non-regular file */ |
| 714 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 715 | /* opcode is not supported by this kernel */ |
| 716 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 717 | /* needs file table */ |
| 718 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 719 | /* needs ->fs */ |
| 720 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 721 | /* set if opcode supports polled "wait" */ |
| 722 | unsigned pollin : 1; |
| 723 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 724 | /* op supports buffer selection */ |
| 725 | unsigned buffer_select : 1; |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 726 | unsigned needs_fsize : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 730 | [IORING_OP_NOP] = {}, |
| 731 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 732 | .async_ctx = 1, |
| 733 | .needs_mm = 1, |
| 734 | .needs_file = 1, |
| 735 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 736 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 737 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 738 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 739 | [IORING_OP_WRITEV] = { |
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 | .hash_reg_file = 1, |
| 744 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 745 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 746 | .needs_fsize = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 747 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 748 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 749 | .needs_file = 1, |
| 750 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 751 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 752 | .needs_file = 1, |
| 753 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 754 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 755 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 756 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 757 | .needs_file = 1, |
| 758 | .hash_reg_file = 1, |
| 759 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 760 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 761 | .needs_fsize = 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_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 764 | .needs_file = 1, |
| 765 | .unbound_nonreg_file = 1, |
| 766 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 767 | [IORING_OP_POLL_REMOVE] = {}, |
| 768 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 769 | .needs_file = 1, |
| 770 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 771 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 772 | .async_ctx = 1, |
| 773 | .needs_mm = 1, |
| 774 | .needs_file = 1, |
| 775 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 776 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 777 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 778 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 779 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 780 | .async_ctx = 1, |
| 781 | .needs_mm = 1, |
| 782 | .needs_file = 1, |
| 783 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 784 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 785 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 786 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 787 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 788 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 789 | .async_ctx = 1, |
| 790 | .needs_mm = 1, |
| 791 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 792 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 793 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 794 | .needs_mm = 1, |
| 795 | .needs_file = 1, |
| 796 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 797 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 798 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 799 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 800 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 801 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 802 | .async_ctx = 1, |
| 803 | .needs_mm = 1, |
| 804 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 805 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 806 | .async_ctx = 1, |
| 807 | .needs_mm = 1, |
| 808 | .needs_file = 1, |
| 809 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 810 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 811 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 812 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 813 | .needs_file = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 814 | .needs_fsize = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 815 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 816 | [IORING_OP_OPENAT] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 817 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 818 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 819 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 820 | [IORING_OP_CLOSE] = { |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 821 | .needs_file = 1, |
| 822 | .needs_file_no_error = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 823 | .file_table = 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_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 826 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 827 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 828 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 829 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 830 | .needs_mm = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 831 | .needs_fs = 1, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [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_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 835 | .needs_mm = 1, |
| 836 | .needs_file = 1, |
| 837 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 838 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 839 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 840 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 841 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 842 | .needs_mm = 1, |
| 843 | .needs_file = 1, |
| 844 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 845 | .pollout = 1, |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 846 | .needs_fsize = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 847 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 848 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 849 | .needs_file = 1, |
| 850 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 851 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 852 | .needs_mm = 1, |
| 853 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 854 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 855 | .needs_mm = 1, |
| 856 | .needs_file = 1, |
| 857 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 858 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 859 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 860 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 861 | .needs_mm = 1, |
| 862 | .needs_file = 1, |
| 863 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 864 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 865 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 866 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 867 | [IORING_OP_OPENAT2] = { |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 868 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 869 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 870 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 871 | [IORING_OP_EPOLL_CTL] = { |
| 872 | .unbound_nonreg_file = 1, |
| 873 | .file_table = 1, |
| 874 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 875 | [IORING_OP_SPLICE] = { |
| 876 | .needs_file = 1, |
| 877 | .hash_reg_file = 1, |
| 878 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 879 | }, |
| 880 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 881 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 882 | [IORING_OP_TEE] = { |
| 883 | .needs_file = 1, |
| 884 | .hash_reg_file = 1, |
| 885 | .unbound_nonreg_file = 1, |
| 886 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 887 | }; |
| 888 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 889 | enum io_mem_account { |
| 890 | ACCT_LOCKED, |
| 891 | ACCT_PINNED, |
| 892 | }; |
| 893 | |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 894 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 895 | struct io_comp_state *cs); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 896 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 897 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 898 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 899 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 900 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 901 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 902 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 903 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 904 | struct io_uring_files_update *ip, |
| 905 | unsigned nr_args); |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 906 | static int io_prep_work_files(struct io_kiocb *req); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 907 | static void __io_clean_op(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 908 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 909 | int fd, struct file **out_file, bool fixed); |
| 910 | static void __io_queue_sqe(struct io_kiocb *req, |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 911 | const struct io_uring_sqe *sqe, |
| 912 | struct io_comp_state *cs); |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 913 | static void io_file_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 914 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 915 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 916 | struct iovec **iovec, struct iov_iter *iter, |
| 917 | bool needs_lock); |
| 918 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 919 | struct iovec *iovec, struct iovec *fast_iov, |
| 920 | struct iov_iter *iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 921 | |
| 922 | static struct kmem_cache *req_cachep; |
| 923 | |
| 924 | static const struct file_operations io_uring_fops; |
| 925 | |
| 926 | struct sock *io_uring_get_socket(struct file *file) |
| 927 | { |
| 928 | #if defined(CONFIG_UNIX) |
| 929 | if (file->f_op == &io_uring_fops) { |
| 930 | struct io_ring_ctx *ctx = file->private_data; |
| 931 | |
| 932 | return ctx->ring_sock->sk; |
| 933 | } |
| 934 | #endif |
| 935 | return NULL; |
| 936 | } |
| 937 | EXPORT_SYMBOL(io_uring_get_socket); |
| 938 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 939 | static void io_get_req_task(struct io_kiocb *req) |
| 940 | { |
| 941 | if (req->flags & REQ_F_TASK_PINNED) |
| 942 | return; |
| 943 | get_task_struct(req->task); |
| 944 | req->flags |= REQ_F_TASK_PINNED; |
| 945 | } |
| 946 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 947 | static inline void io_clean_op(struct io_kiocb *req) |
| 948 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 949 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 950 | __io_clean_op(req); |
| 951 | } |
| 952 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 953 | /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */ |
| 954 | static void __io_put_req_task(struct io_kiocb *req) |
| 955 | { |
| 956 | if (req->flags & REQ_F_TASK_PINNED) |
| 957 | put_task_struct(req->task); |
| 958 | } |
| 959 | |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 960 | static void io_sq_thread_drop_mm(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 961 | { |
| 962 | struct mm_struct *mm = current->mm; |
| 963 | |
| 964 | if (mm) { |
| 965 | kthread_unuse_mm(mm); |
| 966 | mmput(mm); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 971 | { |
| 972 | if (!current->mm) { |
Pavel Begunkov | cbcf721 | 2020-07-18 11:31:21 +0300 | [diff] [blame] | 973 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) || |
| 974 | !mmget_not_zero(ctx->sqo_mm))) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 975 | return -EFAULT; |
| 976 | kthread_use_mm(ctx->sqo_mm); |
| 977 | } |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, |
| 983 | struct io_kiocb *req) |
| 984 | { |
| 985 | if (!io_op_defs[req->opcode].needs_mm) |
| 986 | return 0; |
| 987 | return __io_sq_thread_acquire_mm(ctx); |
| 988 | } |
| 989 | |
| 990 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 991 | { |
| 992 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 993 | req->flags |= REQ_F_FAIL_LINK; |
| 994 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 995 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 996 | /* |
| 997 | * Note: must call io_req_init_async() for the first time you |
| 998 | * touch any members of io_wq_work. |
| 999 | */ |
| 1000 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1001 | { |
| 1002 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1003 | return; |
| 1004 | |
| 1005 | memset(&req->work, 0, sizeof(req->work)); |
| 1006 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1007 | } |
| 1008 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 1009 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 1010 | { |
| 1011 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 1012 | } |
| 1013 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1014 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1015 | { |
| 1016 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1017 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1018 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1021 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1022 | { |
| 1023 | return !req->timeout.off; |
| 1024 | } |
| 1025 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1026 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1027 | { |
| 1028 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1029 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1030 | |
| 1031 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1032 | if (!ctx) |
| 1033 | return NULL; |
| 1034 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1035 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 1036 | if (!ctx->fallback_req) |
| 1037 | goto err; |
| 1038 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1039 | /* |
| 1040 | * Use 5 bits less than the max cq entries, that should give us around |
| 1041 | * 32 entries per hash list if totally full and uniformly spread. |
| 1042 | */ |
| 1043 | hash_bits = ilog2(p->cq_entries); |
| 1044 | hash_bits -= 5; |
| 1045 | if (hash_bits <= 0) |
| 1046 | hash_bits = 1; |
| 1047 | ctx->cancel_hash_bits = hash_bits; |
| 1048 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1049 | GFP_KERNEL); |
| 1050 | if (!ctx->cancel_hash) |
| 1051 | goto err; |
| 1052 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1053 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1054 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1055 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1056 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1057 | |
| 1058 | ctx->flags = p->flags; |
Jens Axboe | 583863e | 2020-05-17 09:20:00 -0600 | [diff] [blame] | 1059 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1060 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1061 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1062 | init_completion(&ctx->ref_comp); |
| 1063 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1064 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1065 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1066 | mutex_init(&ctx->uring_lock); |
| 1067 | init_waitqueue_head(&ctx->wait); |
| 1068 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1069 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1070 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1071 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1072 | init_waitqueue_head(&ctx->inflight_wait); |
| 1073 | spin_lock_init(&ctx->inflight_lock); |
| 1074 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1075 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1076 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1077 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1078 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1079 | if (ctx->fallback_req) |
| 1080 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1081 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1082 | kfree(ctx); |
| 1083 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1086 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1087 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1088 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1089 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1090 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1091 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 31af27c | 2020-04-15 00:39:50 +0300 | [diff] [blame] | 1092 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1093 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1094 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1095 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1096 | } |
| 1097 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1098 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1099 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1100 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1101 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1102 | /* order cqe stores with ring update */ |
| 1103 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1104 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1105 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 1106 | wake_up_interruptible(&ctx->cq_wait); |
| 1107 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Pavel Begunkov | dca9cf8 | 2020-07-15 12:46:49 +0300 | [diff] [blame] | 1111 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1112 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1113 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1114 | return; |
| 1115 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1116 | if (req->work.mm) { |
| 1117 | mmdrop(req->work.mm); |
| 1118 | req->work.mm = NULL; |
| 1119 | } |
| 1120 | if (req->work.creds) { |
| 1121 | put_cred(req->work.creds); |
| 1122 | req->work.creds = NULL; |
| 1123 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1124 | if (req->work.fs) { |
| 1125 | struct fs_struct *fs = req->work.fs; |
| 1126 | |
| 1127 | spin_lock(&req->work.fs->lock); |
| 1128 | if (--fs->users) |
| 1129 | fs = NULL; |
| 1130 | spin_unlock(&req->work.fs->lock); |
| 1131 | if (fs) |
| 1132 | free_fs_struct(fs); |
Pavel Begunkov | b65e0dd | 2020-07-25 14:41:58 +0300 | [diff] [blame] | 1133 | req->work.fs = NULL; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1134 | } |
Pavel Begunkov | b65e0dd | 2020-07-25 14:41:58 +0300 | [diff] [blame] | 1135 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1136 | } |
| 1137 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1138 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1139 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1140 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1141 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1142 | io_req_init_async(req); |
| 1143 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1144 | if (req->flags & REQ_F_ISREG) { |
| 1145 | if (def->hash_reg_file) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1146 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1147 | } else { |
| 1148 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1149 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1150 | } |
Pavel Begunkov | dca9cf8 | 2020-07-15 12:46:49 +0300 | [diff] [blame] | 1151 | if (!req->work.mm && def->needs_mm) { |
| 1152 | mmgrab(current->mm); |
| 1153 | req->work.mm = current->mm; |
| 1154 | } |
| 1155 | if (!req->work.creds) |
| 1156 | req->work.creds = get_current_cred(); |
| 1157 | if (!req->work.fs && def->needs_fs) { |
| 1158 | spin_lock(¤t->fs->lock); |
| 1159 | if (!current->fs->in_exec) { |
| 1160 | req->work.fs = current->fs; |
| 1161 | req->work.fs->users++; |
| 1162 | } else { |
| 1163 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1164 | } |
| 1165 | spin_unlock(¤t->fs->lock); |
| 1166 | } |
Pavel Begunkov | 57f1a64 | 2020-07-15 12:46:52 +0300 | [diff] [blame] | 1167 | if (def->needs_fsize) |
| 1168 | req->work.fsize = rlimit(RLIMIT_FSIZE); |
| 1169 | else |
| 1170 | req->work.fsize = RLIM_INFINITY; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1171 | } |
| 1172 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1173 | static void io_prep_async_link(struct io_kiocb *req) |
| 1174 | { |
| 1175 | struct io_kiocb *cur; |
| 1176 | |
| 1177 | io_prep_async_work(req); |
| 1178 | if (req->flags & REQ_F_LINK_HEAD) |
| 1179 | list_for_each_entry(cur, &req->link_list, link_list) |
| 1180 | io_prep_async_work(cur); |
| 1181 | } |
| 1182 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1183 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1184 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1185 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1186 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1187 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1188 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1189 | &req->work, req->flags); |
| 1190 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1191 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1192 | } |
| 1193 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1194 | static void io_queue_async_work(struct io_kiocb *req) |
| 1195 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1196 | struct io_kiocb *link; |
| 1197 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1198 | /* init ->work of the whole link before punting */ |
| 1199 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1200 | link = __io_queue_async_work(req); |
| 1201 | |
| 1202 | if (link) |
| 1203 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1204 | } |
| 1205 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1206 | static void io_kill_timeout(struct io_kiocb *req) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1210 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1211 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1212 | atomic_set(&req->ctx->cq_timeouts, |
| 1213 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1214 | list_del_init(&req->timeout.list); |
Pavel Begunkov | f0e20b8 | 2020-03-07 01:15:22 +0300 | [diff] [blame] | 1215 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1216 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1217 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1222 | { |
| 1223 | struct io_kiocb *req, *tmp; |
| 1224 | |
| 1225 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1226 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1227 | io_kill_timeout(req); |
| 1228 | spin_unlock_irq(&ctx->completion_lock); |
| 1229 | } |
| 1230 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1231 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1232 | { |
| 1233 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1234 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1235 | struct io_defer_entry, list); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1236 | struct io_kiocb *link; |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1237 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1238 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1239 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1240 | list_del_init(&de->list); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1241 | /* punt-init is done before queueing for defer */ |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1242 | link = __io_queue_async_work(de->req); |
| 1243 | if (link) { |
| 1244 | __io_queue_linked_timeout(link); |
| 1245 | /* drop submission reference */ |
| 1246 | link->flags |= REQ_F_COMP_LOCKED; |
| 1247 | io_put_req(link); |
| 1248 | } |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1249 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1250 | } while (!list_empty(&ctx->defer_list)); |
| 1251 | } |
| 1252 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1253 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1254 | { |
| 1255 | while (!list_empty(&ctx->timeout_list)) { |
| 1256 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1257 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1258 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1259 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1260 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1261 | if (req->timeout.target_seq != ctx->cached_cq_tail |
| 1262 | - atomic_read(&ctx->cq_timeouts)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1263 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1264 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1265 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1266 | io_kill_timeout(req); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1270 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1271 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1272 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1273 | __io_commit_cqring(ctx); |
| 1274 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1275 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1276 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1277 | } |
| 1278 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1279 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1280 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1281 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1282 | unsigned tail; |
| 1283 | |
| 1284 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1285 | /* |
| 1286 | * writes to the cq entry need to come after reading head; the |
| 1287 | * control dependency is enough as we're using WRITE_ONCE to |
| 1288 | * fill the cq entry |
| 1289 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1290 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1291 | return NULL; |
| 1292 | |
| 1293 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1294 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1297 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1298 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1299 | if (!ctx->cq_ev_fd) |
| 1300 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1301 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1302 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1303 | if (!ctx->eventfd_async) |
| 1304 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1305 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1308 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1309 | { |
| 1310 | if (waitqueue_active(&ctx->wait)) |
| 1311 | wake_up(&ctx->wait); |
| 1312 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1313 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1314 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1315 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1316 | } |
| 1317 | |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1318 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
| 1319 | { |
| 1320 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1321 | clear_bit(0, &ctx->sq_check_overflow); |
| 1322 | clear_bit(0, &ctx->cq_check_overflow); |
| 1323 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1327 | /* Returns true if there are no backlogged entries after the flush */ |
| 1328 | 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] | 1329 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1330 | struct io_rings *rings = ctx->rings; |
| 1331 | struct io_uring_cqe *cqe; |
| 1332 | struct io_kiocb *req; |
| 1333 | unsigned long flags; |
| 1334 | LIST_HEAD(list); |
| 1335 | |
| 1336 | if (!force) { |
| 1337 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1338 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1339 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1340 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1341 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1345 | |
| 1346 | /* if force is set, the ring is going away. always drop after that */ |
| 1347 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1348 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1349 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1350 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1351 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1352 | cqe = io_get_cqring(ctx); |
| 1353 | if (!cqe && !force) |
| 1354 | break; |
| 1355 | |
| 1356 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1357 | compl.list); |
| 1358 | list_move(&req->compl.list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1359 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1360 | if (cqe) { |
| 1361 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1362 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1363 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1364 | } else { |
| 1365 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1366 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | io_commit_cqring(ctx); |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1371 | io_cqring_mark_overflow(ctx); |
| 1372 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1373 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1374 | io_cqring_ev_posted(ctx); |
| 1375 | |
| 1376 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1377 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1378 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1379 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1380 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1381 | |
| 1382 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1385 | 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] | 1386 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1387 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1388 | struct io_uring_cqe *cqe; |
| 1389 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1390 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1391 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * If we can't get a cq entry, userspace overflowed the |
| 1394 | * submission (by quite a lot). Increment the overflow count in |
| 1395 | * the ring. |
| 1396 | */ |
| 1397 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1398 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1399 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1400 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1401 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1402 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1403 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1404 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1405 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1406 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1407 | set_bit(0, &ctx->sq_check_overflow); |
| 1408 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1409 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1410 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1411 | io_clean_op(req); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1412 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1413 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1414 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1415 | refcount_inc(&req->refs); |
| 1416 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1420 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1421 | { |
| 1422 | __io_cqring_fill_event(req, res, 0); |
| 1423 | } |
| 1424 | |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1425 | 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] | 1426 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1427 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1428 | unsigned long flags; |
| 1429 | |
| 1430 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1431 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1432 | io_commit_cqring(ctx); |
| 1433 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1434 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1435 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1438 | static void io_submit_flush_completions(struct io_comp_state *cs) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1439 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1440 | struct io_ring_ctx *ctx = cs->ctx; |
| 1441 | |
| 1442 | spin_lock_irq(&ctx->completion_lock); |
| 1443 | while (!list_empty(&cs->list)) { |
| 1444 | struct io_kiocb *req; |
| 1445 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1446 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
| 1447 | list_del(&req->compl.list); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1448 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1449 | if (!(req->flags & REQ_F_LINK_HEAD)) { |
| 1450 | req->flags |= REQ_F_COMP_LOCKED; |
| 1451 | io_put_req(req); |
| 1452 | } else { |
| 1453 | spin_unlock_irq(&ctx->completion_lock); |
| 1454 | io_put_req(req); |
| 1455 | spin_lock_irq(&ctx->completion_lock); |
| 1456 | } |
| 1457 | } |
| 1458 | io_commit_cqring(ctx); |
| 1459 | spin_unlock_irq(&ctx->completion_lock); |
| 1460 | |
| 1461 | io_cqring_ev_posted(ctx); |
| 1462 | cs->nr = 0; |
| 1463 | } |
| 1464 | |
| 1465 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| 1466 | struct io_comp_state *cs) |
| 1467 | { |
| 1468 | if (!cs) { |
| 1469 | io_cqring_add_event(req, res, cflags); |
| 1470 | io_put_req(req); |
| 1471 | } else { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1472 | io_clean_op(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1473 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1474 | req->compl.cflags = cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1475 | list_add_tail(&req->compl.list, &cs->list); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1476 | if (++cs->nr >= 32) |
| 1477 | io_submit_flush_completions(cs); |
| 1478 | } |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | static void io_req_complete(struct io_kiocb *req, long res) |
| 1482 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1483 | __io_req_complete(req, res, 0, NULL); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1486 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1487 | { |
| 1488 | return req == (struct io_kiocb *) |
| 1489 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1490 | } |
| 1491 | |
| 1492 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1493 | { |
| 1494 | struct io_kiocb *req; |
| 1495 | |
| 1496 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1497 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1498 | return req; |
| 1499 | |
| 1500 | return NULL; |
| 1501 | } |
| 1502 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1503 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1504 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1505 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1506 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1507 | struct io_kiocb *req; |
| 1508 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1509 | if (!state->free_reqs) { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1510 | size_t sz; |
| 1511 | int ret; |
| 1512 | |
| 1513 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1514 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1515 | |
| 1516 | /* |
| 1517 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1518 | * retry single alloc to be on the safe side. |
| 1519 | */ |
| 1520 | if (unlikely(ret <= 0)) { |
| 1521 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1522 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1523 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1524 | ret = 1; |
| 1525 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1526 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1527 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1528 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1529 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1530 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1533 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1534 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1535 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1538 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1539 | bool fixed) |
| 1540 | { |
| 1541 | if (fixed) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 1542 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1543 | else |
| 1544 | fput(file); |
| 1545 | } |
| 1546 | |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1547 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1548 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1549 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1550 | |
Jens Axboe | 5acbbc8 | 2020-07-08 15:15:26 -0600 | [diff] [blame] | 1551 | if (req->io) |
| 1552 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1553 | if (req->file) |
| 1554 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Pavel Begunkov | dca9cf8 | 2020-07-15 12:46:49 +0300 | [diff] [blame] | 1555 | io_req_clean_work(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1556 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1557 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1558 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1559 | unsigned long flags; |
| 1560 | |
| 1561 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1562 | list_del(&req->inflight_entry); |
| 1563 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1564 | wake_up(&ctx->inflight_wait); |
| 1565 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1566 | } |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1567 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1568 | |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1569 | static void __io_free_req(struct io_kiocb *req) |
| 1570 | { |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1571 | struct io_ring_ctx *ctx; |
| 1572 | |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1573 | io_dismantle_req(req); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1574 | __io_put_req_task(req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1575 | ctx = req->ctx; |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 1576 | if (likely(!io_is_fallback_req(req))) |
| 1577 | kmem_cache_free(req_cachep, req); |
| 1578 | else |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1579 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
| 1580 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1581 | } |
| 1582 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1583 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1584 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1585 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1586 | int ret; |
| 1587 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1588 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1589 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1590 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1591 | io_commit_cqring(ctx); |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1592 | req->flags &= ~REQ_F_LINK_HEAD; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1593 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1594 | return true; |
| 1595 | } |
| 1596 | |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1600 | static bool __io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1601 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1602 | struct io_kiocb *link; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1603 | bool wake_ev; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1604 | |
| 1605 | if (list_empty(&req->link_list)) |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1606 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1607 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 1608 | if (link->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1609 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1610 | |
| 1611 | list_del_init(&link->link_list); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame^] | 1612 | link->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1613 | wake_ev = io_link_cancel_timeout(link); |
| 1614 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1615 | return wake_ev; |
| 1616 | } |
| 1617 | |
| 1618 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1619 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1620 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1621 | bool wake_ev; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1622 | |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1623 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
| 1624 | unsigned long flags; |
| 1625 | |
| 1626 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1627 | wake_ev = __io_kill_linked_timeout(req); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1628 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1629 | } else { |
| 1630 | wake_ev = __io_kill_linked_timeout(req); |
| 1631 | } |
| 1632 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1633 | if (wake_ev) |
| 1634 | io_cqring_ev_posted(ctx); |
| 1635 | } |
| 1636 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1637 | static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1638 | { |
| 1639 | struct io_kiocb *nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1640 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1641 | /* |
| 1642 | * The list should never be empty when we are called here. But could |
| 1643 | * potentially happen if the chain is messed up, check to be on the |
| 1644 | * safe side. |
| 1645 | */ |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1646 | if (unlikely(list_empty(&req->link_list))) |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1647 | return NULL; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1648 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1649 | nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| 1650 | list_del_init(&req->link_list); |
| 1651 | if (!list_empty(&nxt->link_list)) |
| 1652 | nxt->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1653 | return nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | /* |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 1657 | * 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] | 1658 | */ |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1659 | static void __io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1660 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1661 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1662 | |
| 1663 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1664 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1665 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1666 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1667 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1668 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1669 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1670 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame^] | 1671 | link->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1672 | __io_double_put_req(link); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1673 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1674 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1675 | |
| 1676 | io_commit_cqring(ctx); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1677 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1678 | } |
| 1679 | |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1680 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1681 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1682 | struct io_ring_ctx *ctx = req->ctx; |
| 1683 | |
| 1684 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
| 1685 | unsigned long flags; |
| 1686 | |
| 1687 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1688 | __io_fail_links(req); |
| 1689 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1690 | } else { |
| 1691 | __io_fail_links(req); |
| 1692 | } |
| 1693 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1694 | io_cqring_ev_posted(ctx); |
| 1695 | } |
| 1696 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1697 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1698 | { |
Pavel Begunkov | 9b0d911 | 2020-06-28 12:52:34 +0300 | [diff] [blame] | 1699 | req->flags &= ~REQ_F_LINK_HEAD; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1700 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 1701 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1702 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1703 | /* |
| 1704 | * If LINK is set, we have dependent requests in this chain. If we |
| 1705 | * didn't fail this request, queue the first one up, moving any other |
| 1706 | * dependencies to the next request. In case of failure, fail the rest |
| 1707 | * of the chain. |
| 1708 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1709 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
| 1710 | return io_req_link_next(req); |
| 1711 | io_fail_links(req); |
| 1712 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1713 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1714 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1715 | static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
| 1716 | { |
| 1717 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) |
| 1718 | return NULL; |
| 1719 | return __io_req_find_next(req); |
| 1720 | } |
| 1721 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1722 | static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb) |
| 1723 | { |
| 1724 | struct task_struct *tsk = req->task; |
| 1725 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1726 | int ret, notify; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1727 | |
| 1728 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1729 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1730 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1731 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1732 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1733 | */ |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1734 | notify = 0; |
| 1735 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1736 | notify = TWA_SIGNAL; |
| 1737 | |
| 1738 | ret = task_work_add(tsk, cb, notify); |
| 1739 | if (!ret) |
| 1740 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1741 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1742 | return ret; |
| 1743 | } |
| 1744 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1745 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1746 | { |
| 1747 | struct io_ring_ctx *ctx = req->ctx; |
| 1748 | |
| 1749 | spin_lock_irq(&ctx->completion_lock); |
| 1750 | io_cqring_fill_event(req, error); |
| 1751 | io_commit_cqring(ctx); |
| 1752 | spin_unlock_irq(&ctx->completion_lock); |
| 1753 | |
| 1754 | io_cqring_ev_posted(ctx); |
| 1755 | req_set_fail_links(req); |
| 1756 | io_double_put_req(req); |
| 1757 | } |
| 1758 | |
| 1759 | static void io_req_task_cancel(struct callback_head *cb) |
| 1760 | { |
| 1761 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1762 | |
| 1763 | __io_req_task_cancel(req, -ECANCELED); |
| 1764 | } |
| 1765 | |
| 1766 | static void __io_req_task_submit(struct io_kiocb *req) |
| 1767 | { |
| 1768 | struct io_ring_ctx *ctx = req->ctx; |
| 1769 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1770 | if (!__io_sq_thread_acquire_mm(ctx)) { |
| 1771 | mutex_lock(&ctx->uring_lock); |
| 1772 | __io_queue_sqe(req, NULL, NULL); |
| 1773 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1774 | } else { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1775 | __io_req_task_cancel(req, -EFAULT); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1776 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1777 | } |
| 1778 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1779 | static void io_req_task_submit(struct callback_head *cb) |
| 1780 | { |
| 1781 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1782 | |
| 1783 | __io_req_task_submit(req); |
| 1784 | } |
| 1785 | |
| 1786 | static void io_req_task_queue(struct io_kiocb *req) |
| 1787 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1788 | int ret; |
| 1789 | |
| 1790 | init_task_work(&req->task_work, io_req_task_submit); |
| 1791 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1792 | ret = io_req_task_work_add(req, &req->task_work); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1793 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1794 | struct task_struct *tsk; |
| 1795 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1796 | init_task_work(&req->task_work, io_req_task_cancel); |
| 1797 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1798 | task_work_add(tsk, &req->task_work, 0); |
| 1799 | wake_up_process(tsk); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1800 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1801 | } |
| 1802 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 1803 | static void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1804 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1805 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1806 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 1807 | if (nxt) |
| 1808 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1811 | static void io_free_req(struct io_kiocb *req) |
| 1812 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 1813 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1814 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1815 | } |
| 1816 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1817 | struct req_batch { |
| 1818 | void *reqs[IO_IOPOLL_BATCH]; |
| 1819 | int to_free; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1820 | |
| 1821 | struct task_struct *task; |
| 1822 | int task_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1823 | }; |
| 1824 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1825 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1826 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1827 | rb->to_free = 0; |
| 1828 | rb->task_refs = 0; |
| 1829 | rb->task = NULL; |
| 1830 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1831 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1832 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
| 1833 | struct req_batch *rb) |
| 1834 | { |
| 1835 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1836 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 1837 | rb->to_free = 0; |
| 1838 | } |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1839 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1840 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 1841 | struct req_batch *rb) |
| 1842 | { |
| 1843 | if (rb->to_free) |
| 1844 | __io_req_free_batch_flush(ctx, rb); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1845 | if (rb->task) { |
| 1846 | put_task_struct_many(rb->task, rb->task_refs); |
| 1847 | rb->task = NULL; |
| 1848 | } |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) |
| 1852 | { |
| 1853 | if (unlikely(io_is_fallback_req(req))) { |
| 1854 | io_free_req(req); |
| 1855 | return; |
| 1856 | } |
| 1857 | if (req->flags & REQ_F_LINK_HEAD) |
| 1858 | io_queue_next(req); |
| 1859 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 1860 | if (req->flags & REQ_F_TASK_PINNED) { |
| 1861 | if (req->task != rb->task) { |
| 1862 | if (rb->task) |
| 1863 | put_task_struct_many(rb->task, rb->task_refs); |
| 1864 | rb->task = req->task; |
| 1865 | rb->task_refs = 0; |
| 1866 | } |
| 1867 | rb->task_refs++; |
| 1868 | req->flags &= ~REQ_F_TASK_PINNED; |
| 1869 | } |
| 1870 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 1871 | io_dismantle_req(req); |
| 1872 | rb->reqs[rb->to_free++] = req; |
| 1873 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1874 | __io_req_free_batch_flush(req->ctx, rb); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1875 | } |
| 1876 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1877 | /* |
| 1878 | * Drop reference to request, return next in chain (if there is one) if this |
| 1879 | * was the last reference to this request. |
| 1880 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1881 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1882 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1883 | struct io_kiocb *nxt = NULL; |
| 1884 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1885 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1886 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1887 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1888 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1889 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1892 | static void io_put_req(struct io_kiocb *req) |
| 1893 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1894 | if (refcount_dec_and_test(&req->refs)) |
| 1895 | io_free_req(req); |
| 1896 | } |
| 1897 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1898 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1899 | { |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 1900 | struct io_kiocb *nxt; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1901 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 1902 | /* |
| 1903 | * A ref is owned by io-wq in which context we're. So, if that's the |
| 1904 | * last one, it's safe to steal next work. False negatives are Ok, |
| 1905 | * it just will be re-punted async in io_put_work() |
| 1906 | */ |
| 1907 | if (refcount_read(&req->refs) != 1) |
| 1908 | return NULL; |
| 1909 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1910 | nxt = io_req_find_next(req); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 1911 | return nxt ? &nxt->work : NULL; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1912 | } |
| 1913 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1914 | /* |
| 1915 | * Must only be used if we don't need to care about links, usually from |
| 1916 | * within the completion handling itself. |
| 1917 | */ |
| 1918 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1919 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1920 | /* drop both submit and complete references */ |
| 1921 | if (refcount_sub_and_test(2, &req->refs)) |
| 1922 | __io_free_req(req); |
| 1923 | } |
| 1924 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1925 | static void io_double_put_req(struct io_kiocb *req) |
| 1926 | { |
| 1927 | /* drop both submit and complete references */ |
| 1928 | if (refcount_sub_and_test(2, &req->refs)) |
| 1929 | io_free_req(req); |
| 1930 | } |
| 1931 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1932 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1933 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1934 | struct io_rings *rings = ctx->rings; |
| 1935 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1936 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1937 | /* |
| 1938 | * noflush == true is from the waitqueue handler, just ensure |
| 1939 | * we wake up the task, and the next invocation will flush the |
| 1940 | * entries. We cannot safely to it from here. |
| 1941 | */ |
| 1942 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1943 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1944 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1945 | io_cqring_overflow_flush(ctx, false); |
| 1946 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1947 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1948 | /* See comment at the top of this file */ |
| 1949 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1950 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1951 | } |
| 1952 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1953 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1954 | { |
| 1955 | struct io_rings *rings = ctx->rings; |
| 1956 | |
| 1957 | /* make sure SQ entry isn't read before tail */ |
| 1958 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1959 | } |
| 1960 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 1961 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1962 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 1963 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1964 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1965 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1966 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 1967 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1968 | kfree(kbuf); |
| 1969 | return cflags; |
| 1970 | } |
| 1971 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 1972 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 1973 | { |
| 1974 | struct io_buffer *kbuf; |
| 1975 | |
| 1976 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 1977 | return io_put_kbuf(req, kbuf); |
| 1978 | } |
| 1979 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 1980 | static inline bool io_run_task_work(void) |
| 1981 | { |
| 1982 | if (current->task_works) { |
| 1983 | __set_current_state(TASK_RUNNING); |
| 1984 | task_work_run(); |
| 1985 | return true; |
| 1986 | } |
| 1987 | |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1991 | static void io_iopoll_queue(struct list_head *again) |
| 1992 | { |
| 1993 | struct io_kiocb *req; |
| 1994 | |
| 1995 | do { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 1996 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
| 1997 | list_del(&req->inflight_entry); |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 1998 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 1999 | } while (!list_empty(again)); |
| 2000 | } |
| 2001 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2002 | /* |
| 2003 | * Find and free completed poll iocbs |
| 2004 | */ |
| 2005 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2006 | struct list_head *done) |
| 2007 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2008 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2009 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2010 | LIST_HEAD(again); |
| 2011 | |
| 2012 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2013 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2014 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2015 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2016 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2017 | int cflags = 0; |
| 2018 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2019 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2020 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2021 | req->iopoll_completed = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2022 | list_move_tail(&req->inflight_entry, &again); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2023 | continue; |
| 2024 | } |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2025 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2026 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2027 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2028 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2029 | |
| 2030 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2031 | (*nr_events)++; |
| 2032 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2033 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2034 | io_req_free_batch(&rb, req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2035 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2036 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2037 | io_commit_cqring(ctx); |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 2038 | if (ctx->flags & IORING_SETUP_SQPOLL) |
| 2039 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2040 | io_req_free_batch_finish(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2041 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2042 | if (!list_empty(&again)) |
| 2043 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2046 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2047 | long min) |
| 2048 | { |
| 2049 | struct io_kiocb *req, *tmp; |
| 2050 | LIST_HEAD(done); |
| 2051 | bool spin; |
| 2052 | int ret; |
| 2053 | |
| 2054 | /* |
| 2055 | * Only spin for completions if we don't have multiple devices hanging |
| 2056 | * off our complete list, and we're under the requested amount. |
| 2057 | */ |
| 2058 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2059 | |
| 2060 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2061 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2062 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2063 | |
| 2064 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2065 | * Move completed and retryable entries to our local lists. |
| 2066 | * If we find a request that requires polling, break out |
| 2067 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2068 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2069 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2070 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2071 | continue; |
| 2072 | } |
| 2073 | if (!list_empty(&done)) |
| 2074 | break; |
| 2075 | |
| 2076 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2077 | if (ret < 0) |
| 2078 | break; |
| 2079 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2080 | /* iopoll may have completed current req */ |
| 2081 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2082 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2083 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2084 | if (ret && spin) |
| 2085 | spin = false; |
| 2086 | ret = 0; |
| 2087 | } |
| 2088 | |
| 2089 | if (!list_empty(&done)) |
| 2090 | io_iopoll_complete(ctx, nr_events, &done); |
| 2091 | |
| 2092 | return ret; |
| 2093 | } |
| 2094 | |
| 2095 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2096 | * 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] | 2097 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2098 | * as a non-spinning completion check. |
| 2099 | */ |
| 2100 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2101 | long min) |
| 2102 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2103 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2104 | int ret; |
| 2105 | |
| 2106 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2107 | if (ret < 0) |
| 2108 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2109 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | return 1; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * We can't just wait for polled events to come to us, we have to actively |
| 2118 | * find and complete them. |
| 2119 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2120 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2121 | { |
| 2122 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2123 | return; |
| 2124 | |
| 2125 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2126 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2127 | unsigned int nr_events = 0; |
| 2128 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2129 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2130 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2131 | /* let it sleep and repeat later if can't complete a request */ |
| 2132 | if (nr_events == 0) |
| 2133 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2134 | /* |
| 2135 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2136 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2137 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2138 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2139 | if (need_resched()) { |
| 2140 | mutex_unlock(&ctx->uring_lock); |
| 2141 | cond_resched(); |
| 2142 | mutex_lock(&ctx->uring_lock); |
| 2143 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2144 | } |
| 2145 | mutex_unlock(&ctx->uring_lock); |
| 2146 | } |
| 2147 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2148 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2149 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2150 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2151 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2152 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2153 | /* |
| 2154 | * We disallow the app entering submit/complete with polling, but we |
| 2155 | * still need to lock the ring to prevent racing with polled issue |
| 2156 | * that got punted to a workqueue. |
| 2157 | */ |
| 2158 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2159 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2160 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2161 | * Don't enter poll loop if we already have events pending. |
| 2162 | * If we do, we can potentially be spinning for commands that |
| 2163 | * already triggered a CQE (eg in error). |
| 2164 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 2165 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2166 | break; |
| 2167 | |
| 2168 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2169 | * If a submit got punted to a workqueue, we can have the |
| 2170 | * application entering polling for a command before it gets |
| 2171 | * issued. That app will hold the uring_lock for the duration |
| 2172 | * of the poll right here, so we need to take a breather every |
| 2173 | * now and then to ensure that the issue has a chance to add |
| 2174 | * the poll to the issued list. Otherwise we can spin here |
| 2175 | * forever, while the workqueue is stuck trying to acquire the |
| 2176 | * very same mutex. |
| 2177 | */ |
| 2178 | if (!(++iters & 7)) { |
| 2179 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2180 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2181 | mutex_lock(&ctx->uring_lock); |
| 2182 | } |
| 2183 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2184 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2185 | if (ret <= 0) |
| 2186 | break; |
| 2187 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2188 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2189 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2190 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2191 | return ret; |
| 2192 | } |
| 2193 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2194 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2195 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2196 | /* |
| 2197 | * Tell lockdep we inherited freeze protection from submission |
| 2198 | * thread. |
| 2199 | */ |
| 2200 | if (req->flags & REQ_F_ISREG) { |
| 2201 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2202 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2203 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2204 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2205 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2208 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 2209 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2210 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2211 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2212 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2213 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2214 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2215 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2216 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2217 | if (res != req->result) |
| 2218 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2219 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2220 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2221 | __io_req_complete(req, res, cflags, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2222 | } |
| 2223 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2224 | #ifdef CONFIG_BLOCK |
| 2225 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2226 | { |
| 2227 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2228 | ssize_t ret = -ECANCELED; |
| 2229 | struct iov_iter iter; |
| 2230 | int rw; |
| 2231 | |
| 2232 | if (error) { |
| 2233 | ret = error; |
| 2234 | goto end_req; |
| 2235 | } |
| 2236 | |
| 2237 | switch (req->opcode) { |
| 2238 | case IORING_OP_READV: |
| 2239 | case IORING_OP_READ_FIXED: |
| 2240 | case IORING_OP_READ: |
| 2241 | rw = READ; |
| 2242 | break; |
| 2243 | case IORING_OP_WRITEV: |
| 2244 | case IORING_OP_WRITE_FIXED: |
| 2245 | case IORING_OP_WRITE: |
| 2246 | rw = WRITE; |
| 2247 | break; |
| 2248 | default: |
| 2249 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2250 | req->opcode); |
| 2251 | goto end_req; |
| 2252 | } |
| 2253 | |
| 2254 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2255 | if (ret < 0) |
| 2256 | goto end_req; |
| 2257 | ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter); |
| 2258 | if (!ret) |
| 2259 | return true; |
| 2260 | kfree(iovec); |
| 2261 | end_req: |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2262 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 2263 | io_req_complete(req, ret); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2264 | return false; |
| 2265 | } |
| 2266 | |
| 2267 | static void io_rw_resubmit(struct callback_head *cb) |
| 2268 | { |
| 2269 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2270 | struct io_ring_ctx *ctx = req->ctx; |
| 2271 | int err; |
| 2272 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2273 | err = io_sq_thread_acquire_mm(ctx, req); |
| 2274 | |
| 2275 | if (io_resubmit_prep(req, err)) { |
| 2276 | refcount_inc(&req->refs); |
| 2277 | io_queue_async_work(req); |
| 2278 | } |
| 2279 | } |
| 2280 | #endif |
| 2281 | |
| 2282 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2283 | { |
| 2284 | #ifdef CONFIG_BLOCK |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2285 | int ret; |
| 2286 | |
| 2287 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2288 | return false; |
| 2289 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2290 | init_task_work(&req->task_work, io_rw_resubmit); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2291 | ret = io_req_task_work_add(req, &req->task_work); |
| 2292 | if (!ret) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2293 | return true; |
| 2294 | #endif |
| 2295 | return false; |
| 2296 | } |
| 2297 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2298 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2299 | struct io_comp_state *cs) |
| 2300 | { |
| 2301 | if (!io_rw_reissue(req, res)) |
| 2302 | io_complete_rw_common(&req->rw.kiocb, res, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2306 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2307 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2308 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2309 | __io_complete_rw(req, res, res2, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2312 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2313 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2314 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2315 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2316 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2317 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2318 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2319 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2320 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2321 | |
| 2322 | WRITE_ONCE(req->result, res); |
| 2323 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2324 | smp_wmb(); |
| 2325 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2330 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2331 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2332 | * accessing the kiocb cookie. |
| 2333 | */ |
| 2334 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 2335 | { |
| 2336 | struct io_ring_ctx *ctx = req->ctx; |
| 2337 | |
| 2338 | /* |
| 2339 | * Track whether we have multiple files in our lists. This will impact |
| 2340 | * how we do polling eventually, not spinning if we're on potentially |
| 2341 | * different devices. |
| 2342 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2343 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2344 | ctx->poll_multi_file = false; |
| 2345 | } else if (!ctx->poll_multi_file) { |
| 2346 | struct io_kiocb *list_req; |
| 2347 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2348 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2349 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2350 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2351 | ctx->poll_multi_file = true; |
| 2352 | } |
| 2353 | |
| 2354 | /* |
| 2355 | * For fast devices, IO may have already completed. If it has, add |
| 2356 | * it to the front so we find it first. |
| 2357 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2358 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2359 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2360 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2361 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2362 | |
| 2363 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2364 | wq_has_sleeper(&ctx->sqo_wait)) |
| 2365 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2368 | static void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2369 | { |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2370 | if (state->has_refs) |
| 2371 | fput_many(state->file, state->has_refs); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2372 | state->file = NULL; |
| 2373 | } |
| 2374 | |
| 2375 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2376 | { |
| 2377 | if (state->file) |
| 2378 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /* |
| 2382 | * Get as many references to a file as we have IOs left in this submission, |
| 2383 | * assuming most submissions are for one file, or at least that each file |
| 2384 | * has more than one submission. |
| 2385 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2386 | 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] | 2387 | { |
| 2388 | if (!state) |
| 2389 | return fget(fd); |
| 2390 | |
| 2391 | if (state->file) { |
| 2392 | if (state->fd == fd) { |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2393 | state->has_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2394 | state->ios_left--; |
| 2395 | return state->file; |
| 2396 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2397 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2398 | } |
| 2399 | state->file = fget_many(fd, state->ios_left); |
| 2400 | if (!state->file) |
| 2401 | return NULL; |
| 2402 | |
| 2403 | state->fd = fd; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2404 | state->ios_left--; |
Pavel Begunkov | 06ef360 | 2020-07-16 23:28:33 +0300 | [diff] [blame] | 2405 | state->has_refs = state->ios_left; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2406 | return state->file; |
| 2407 | } |
| 2408 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2409 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2410 | { |
| 2411 | #ifdef CONFIG_BLOCK |
| 2412 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); |
| 2413 | #else |
| 2414 | return true; |
| 2415 | #endif |
| 2416 | } |
| 2417 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2418 | /* |
| 2419 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2420 | * any file. For now, just ensure that anything potentially problematic is done |
| 2421 | * inline. |
| 2422 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2423 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2424 | { |
| 2425 | umode_t mode = file_inode(file)->i_mode; |
| 2426 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2427 | if (S_ISBLK(mode)) { |
| 2428 | if (io_bdev_nowait(file->f_inode->i_bdev)) |
| 2429 | return true; |
| 2430 | return false; |
| 2431 | } |
| 2432 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2433 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2434 | if (S_ISREG(mode)) { |
| 2435 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
| 2436 | file->f_op != &io_uring_fops) |
| 2437 | return true; |
| 2438 | return false; |
| 2439 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2440 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2441 | /* any ->read/write should understand O_NONBLOCK */ |
| 2442 | if (file->f_flags & O_NONBLOCK) |
| 2443 | return true; |
| 2444 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2445 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2446 | return false; |
| 2447 | |
| 2448 | if (rw == READ) |
| 2449 | return file->f_op->read_iter != NULL; |
| 2450 | |
| 2451 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2454 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2455 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2456 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2457 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2458 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2459 | unsigned ioprio; |
| 2460 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2461 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2462 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2463 | req->flags |= REQ_F_ISREG; |
| 2464 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2465 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2466 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2467 | req->flags |= REQ_F_CUR_POS; |
| 2468 | kiocb->ki_pos = req->file->f_pos; |
| 2469 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2470 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2471 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2472 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2473 | if (unlikely(ret)) |
| 2474 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2475 | |
| 2476 | ioprio = READ_ONCE(sqe->ioprio); |
| 2477 | if (ioprio) { |
| 2478 | ret = ioprio_check_cap(ioprio); |
| 2479 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2480 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2481 | |
| 2482 | kiocb->ki_ioprio = ioprio; |
| 2483 | } else |
| 2484 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2485 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2486 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2487 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2488 | req->flags |= REQ_F_NOWAIT; |
| 2489 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2490 | if (kiocb->ki_flags & IOCB_DIRECT) |
| 2491 | io_get_req_task(req); |
| 2492 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2493 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2494 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2495 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2496 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2497 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2498 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2499 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2500 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2501 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2502 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2503 | req->iopoll_completed = 0; |
Pavel Begunkov | f3a6fa2 | 2020-06-28 12:52:38 +0300 | [diff] [blame] | 2504 | io_get_req_task(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2505 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2506 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2507 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | kiocb->ki_complete = io_complete_rw; |
| 2509 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2510 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2511 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2512 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2513 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2514 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2518 | { |
| 2519 | switch (ret) { |
| 2520 | case -EIOCBQUEUED: |
| 2521 | break; |
| 2522 | case -ERESTARTSYS: |
| 2523 | case -ERESTARTNOINTR: |
| 2524 | case -ERESTARTNOHAND: |
| 2525 | case -ERESTART_RESTARTBLOCK: |
| 2526 | /* |
| 2527 | * We can't just restart the syscall, since previously |
| 2528 | * submitted sqes may already be in progress. Just fail this |
| 2529 | * IO with EINTR. |
| 2530 | */ |
| 2531 | ret = -EINTR; |
| 2532 | /* fall through */ |
| 2533 | default: |
| 2534 | kiocb->ki_complete(kiocb, ret, 0); |
| 2535 | } |
| 2536 | } |
| 2537 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2538 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| 2539 | struct io_comp_state *cs) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2540 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2541 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2542 | |
| 2543 | if (req->flags & REQ_F_CUR_POS) |
| 2544 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2545 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2546 | __io_complete_rw(req, ret, 0, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2547 | else |
| 2548 | io_rw_done(kiocb, ret); |
| 2549 | } |
| 2550 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2551 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2552 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2553 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2554 | struct io_ring_ctx *ctx = req->ctx; |
| 2555 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2556 | struct io_mapped_ubuf *imu; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2557 | u16 index, buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2558 | size_t offset; |
| 2559 | u64 buf_addr; |
| 2560 | |
| 2561 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2562 | if (unlikely(!ctx->user_bufs)) |
| 2563 | return -EFAULT; |
| 2564 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2565 | buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2566 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2567 | return -EFAULT; |
| 2568 | |
| 2569 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2570 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2571 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2572 | |
| 2573 | /* overflow */ |
| 2574 | if (buf_addr + len < buf_addr) |
| 2575 | return -EFAULT; |
| 2576 | /* not inside the mapped region */ |
| 2577 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2578 | return -EFAULT; |
| 2579 | |
| 2580 | /* |
| 2581 | * May not be a start of buffer, set size appropriately |
| 2582 | * and advance us to the beginning. |
| 2583 | */ |
| 2584 | offset = buf_addr - imu->ubuf; |
| 2585 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2586 | |
| 2587 | if (offset) { |
| 2588 | /* |
| 2589 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2590 | * using the latter parts of a big fixed buffer - it iterates |
| 2591 | * over each segment manually. We can cheat a bit here, because |
| 2592 | * we know that: |
| 2593 | * |
| 2594 | * 1) it's a BVEC iter, we set it up |
| 2595 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2596 | * first and last bvec |
| 2597 | * |
| 2598 | * So just find our index, and adjust the iterator afterwards. |
| 2599 | * If the offset is within the first bvec (or the whole first |
| 2600 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2601 | * since we can just skip the first segment, which may not |
| 2602 | * be PAGE_SIZE aligned. |
| 2603 | */ |
| 2604 | const struct bio_vec *bvec = imu->bvec; |
| 2605 | |
| 2606 | if (offset <= bvec->bv_len) { |
| 2607 | iov_iter_advance(iter, offset); |
| 2608 | } else { |
| 2609 | unsigned long seg_skip; |
| 2610 | |
| 2611 | /* skip first vec */ |
| 2612 | offset -= bvec->bv_len; |
| 2613 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2614 | |
| 2615 | iter->bvec = bvec + seg_skip; |
| 2616 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2617 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2618 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2619 | } |
| 2620 | } |
| 2621 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2622 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2625 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2626 | { |
| 2627 | if (needs_lock) |
| 2628 | mutex_unlock(&ctx->uring_lock); |
| 2629 | } |
| 2630 | |
| 2631 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2632 | { |
| 2633 | /* |
| 2634 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2635 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2636 | * The only exception is when we've detached the request and issue it |
| 2637 | * from an async worker thread, grab the lock for that case. |
| 2638 | */ |
| 2639 | if (needs_lock) |
| 2640 | mutex_lock(&ctx->uring_lock); |
| 2641 | } |
| 2642 | |
| 2643 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2644 | int bgid, struct io_buffer *kbuf, |
| 2645 | bool needs_lock) |
| 2646 | { |
| 2647 | struct io_buffer *head; |
| 2648 | |
| 2649 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2650 | return kbuf; |
| 2651 | |
| 2652 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2653 | |
| 2654 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2655 | |
| 2656 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2657 | if (head) { |
| 2658 | if (!list_empty(&head->list)) { |
| 2659 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2660 | list); |
| 2661 | list_del(&kbuf->list); |
| 2662 | } else { |
| 2663 | kbuf = head; |
| 2664 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2665 | } |
| 2666 | if (*len > kbuf->len) |
| 2667 | *len = kbuf->len; |
| 2668 | } else { |
| 2669 | kbuf = ERR_PTR(-ENOBUFS); |
| 2670 | } |
| 2671 | |
| 2672 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2673 | |
| 2674 | return kbuf; |
| 2675 | } |
| 2676 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2677 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2678 | bool needs_lock) |
| 2679 | { |
| 2680 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2681 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2682 | |
| 2683 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2684 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2685 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2686 | if (IS_ERR(kbuf)) |
| 2687 | return kbuf; |
| 2688 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2689 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2690 | return u64_to_user_ptr(kbuf->addr); |
| 2691 | } |
| 2692 | |
| 2693 | #ifdef CONFIG_COMPAT |
| 2694 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2695 | bool needs_lock) |
| 2696 | { |
| 2697 | struct compat_iovec __user *uiov; |
| 2698 | compat_ssize_t clen; |
| 2699 | void __user *buf; |
| 2700 | ssize_t len; |
| 2701 | |
| 2702 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2703 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2704 | return -EFAULT; |
| 2705 | if (__get_user(clen, &uiov->iov_len)) |
| 2706 | return -EFAULT; |
| 2707 | if (clen < 0) |
| 2708 | return -EINVAL; |
| 2709 | |
| 2710 | len = clen; |
| 2711 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2712 | if (IS_ERR(buf)) |
| 2713 | return PTR_ERR(buf); |
| 2714 | iov[0].iov_base = buf; |
| 2715 | iov[0].iov_len = (compat_size_t) len; |
| 2716 | return 0; |
| 2717 | } |
| 2718 | #endif |
| 2719 | |
| 2720 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2721 | bool needs_lock) |
| 2722 | { |
| 2723 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2724 | void __user *buf; |
| 2725 | ssize_t len; |
| 2726 | |
| 2727 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2728 | return -EFAULT; |
| 2729 | |
| 2730 | len = iov[0].iov_len; |
| 2731 | if (len < 0) |
| 2732 | return -EINVAL; |
| 2733 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2734 | if (IS_ERR(buf)) |
| 2735 | return PTR_ERR(buf); |
| 2736 | iov[0].iov_base = buf; |
| 2737 | iov[0].iov_len = len; |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
| 2741 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2742 | bool needs_lock) |
| 2743 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2744 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2745 | struct io_buffer *kbuf; |
| 2746 | |
| 2747 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2748 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2749 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2750 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2751 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2752 | if (!req->rw.len) |
| 2753 | return 0; |
| 2754 | else if (req->rw.len > 1) |
| 2755 | return -EINVAL; |
| 2756 | |
| 2757 | #ifdef CONFIG_COMPAT |
| 2758 | if (req->ctx->compat) |
| 2759 | return io_compat_import(req, iov, needs_lock); |
| 2760 | #endif |
| 2761 | |
| 2762 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2763 | } |
| 2764 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2765 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2766 | struct iovec **iovec, struct iov_iter *iter, |
| 2767 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2768 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2769 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2770 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2771 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2772 | u8 opcode; |
| 2773 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2774 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2775 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2776 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2777 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2778 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2779 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2780 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2781 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2782 | return -EINVAL; |
| 2783 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2784 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2785 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2786 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
| 2787 | if (IS_ERR(buf)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2788 | *iovec = NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2789 | return PTR_ERR(buf); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2790 | } |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2791 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2794 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2795 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2796 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2797 | } |
| 2798 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2799 | if (req->io) { |
| 2800 | struct io_async_rw *iorw = &req->io->rw; |
| 2801 | |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 2802 | iov_iter_init(iter, rw, iorw->iov, iorw->nr_segs, iorw->size); |
| 2803 | *iovec = NULL; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2804 | return iorw->size; |
| 2805 | } |
| 2806 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2807 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2808 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2809 | if (!ret) { |
| 2810 | ret = (*iovec)->iov_len; |
| 2811 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 2812 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2813 | *iovec = NULL; |
| 2814 | return ret; |
| 2815 | } |
| 2816 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2817 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2818 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2819 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2820 | iovec, iter); |
| 2821 | #endif |
| 2822 | |
| 2823 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2824 | } |
| 2825 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2826 | /* |
| 2827 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2828 | * by looping over ->read() or ->write() manually. |
| 2829 | */ |
| 2830 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2831 | struct iov_iter *iter) |
| 2832 | { |
| 2833 | ssize_t ret = 0; |
| 2834 | |
| 2835 | /* |
| 2836 | * Don't support polled IO through this interface, and we can't |
| 2837 | * support non-blocking either. For the latter, this just causes |
| 2838 | * the kiocb to be handled from an async context. |
| 2839 | */ |
| 2840 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2841 | return -EOPNOTSUPP; |
| 2842 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2843 | return -EAGAIN; |
| 2844 | |
| 2845 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2846 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2847 | ssize_t nr; |
| 2848 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2849 | if (!iov_iter_is_bvec(iter)) { |
| 2850 | iovec = iov_iter_iovec(iter); |
| 2851 | } else { |
| 2852 | /* fixed buffers import bvec */ |
| 2853 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2854 | + iter->iov_offset; |
| 2855 | iovec.iov_len = min(iter->count, |
| 2856 | iter->bvec->bv_len - iter->iov_offset); |
| 2857 | } |
| 2858 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2859 | if (rw == READ) { |
| 2860 | nr = file->f_op->read(file, iovec.iov_base, |
| 2861 | iovec.iov_len, &kiocb->ki_pos); |
| 2862 | } else { |
| 2863 | nr = file->f_op->write(file, iovec.iov_base, |
| 2864 | iovec.iov_len, &kiocb->ki_pos); |
| 2865 | } |
| 2866 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2867 | if (iov_iter_is_bvec(iter)) |
| 2868 | kunmap(iter->bvec->bv_page); |
| 2869 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2870 | if (nr < 0) { |
| 2871 | if (!ret) |
| 2872 | ret = nr; |
| 2873 | break; |
| 2874 | } |
| 2875 | ret += nr; |
| 2876 | if (nr != iovec.iov_len) |
| 2877 | break; |
| 2878 | iov_iter_advance(iter, nr); |
| 2879 | } |
| 2880 | |
| 2881 | return ret; |
| 2882 | } |
| 2883 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2884 | 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] | 2885 | struct iovec *iovec, struct iovec *fast_iov, |
| 2886 | struct iov_iter *iter) |
| 2887 | { |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 2888 | struct io_async_rw *rw = &req->io->rw; |
| 2889 | |
| 2890 | rw->nr_segs = iter->nr_segs; |
| 2891 | rw->size = io_size; |
| 2892 | if (!iovec) { |
| 2893 | rw->iov = rw->fast_iov; |
| 2894 | if (rw->iov != fast_iov) |
| 2895 | memcpy(rw->iov, fast_iov, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 2896 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2897 | } else { |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 2898 | rw->iov = iovec; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2899 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2903 | static inline int __io_alloc_async_ctx(struct io_kiocb *req) |
| 2904 | { |
| 2905 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
| 2906 | return req->io == NULL; |
| 2907 | } |
| 2908 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2909 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2910 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2911 | if (!io_op_defs[req->opcode].async_ctx) |
| 2912 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2913 | |
| 2914 | return __io_alloc_async_ctx(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2917 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2918 | struct iovec *iovec, struct iovec *fast_iov, |
| 2919 | struct iov_iter *iter) |
| 2920 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2921 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2922 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2923 | if (!req->io) { |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 2924 | if (__io_alloc_async_ctx(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2925 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2926 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2927 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2928 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2929 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 2932 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw, |
| 2933 | bool force_nonblock) |
| 2934 | { |
| 2935 | struct io_async_ctx *io = req->io; |
| 2936 | struct iov_iter iter; |
| 2937 | ssize_t ret; |
| 2938 | |
| 2939 | io->rw.iov = io->rw.fast_iov; |
| 2940 | req->io = NULL; |
| 2941 | ret = io_import_iovec(rw, req, &io->rw.iov, &iter, !force_nonblock); |
| 2942 | req->io = io; |
| 2943 | if (unlikely(ret < 0)) |
| 2944 | return ret; |
| 2945 | |
| 2946 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2950 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2951 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2952 | { |
| 2953 | ssize_t ret; |
| 2954 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2955 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2956 | if (ret) |
| 2957 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2958 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2959 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2960 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2961 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2962 | /* either don't need iovec imported or already have it */ |
| 2963 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2964 | return 0; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 2965 | return io_rw_prep_async(req, READ, force_nonblock); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 2968 | /* |
| 2969 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 2970 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 2971 | * This gets called when the page is unlocked, and we generally expect that to |
| 2972 | * happen when the page IO is completed and the page is now uptodate. This will |
| 2973 | * queue a task_work based retry of the operation, attempting to copy the data |
| 2974 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 2975 | * do a thread based blocking retry of the operation. That's the unexpected |
| 2976 | * slow path. |
| 2977 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2978 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 2979 | int sync, void *arg) |
| 2980 | { |
| 2981 | struct wait_page_queue *wpq; |
| 2982 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2983 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2984 | int ret; |
| 2985 | |
| 2986 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 2987 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 2988 | if (!wake_page_match(wpq, key)) |
| 2989 | return 0; |
| 2990 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2991 | list_del_init(&wait->entry); |
| 2992 | |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 2993 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2994 | /* submit ref gets dropped, acquire a new one */ |
| 2995 | refcount_inc(&req->refs); |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 2996 | ret = io_req_task_work_add(req, &req->task_work); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 2997 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2998 | struct task_struct *tsk; |
| 2999 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3000 | /* queue just for cancelation */ |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3001 | init_task_work(&req->task_work, io_req_task_cancel); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3002 | tsk = io_wq_get_task(req->ctx->io_wq); |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3003 | task_work_add(tsk, &req->task_work, 0); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3004 | wake_up_process(tsk); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3005 | } |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3006 | return 1; |
| 3007 | } |
| 3008 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3009 | static inline int kiocb_wait_page_queue_init(struct kiocb *kiocb, |
| 3010 | struct wait_page_queue *wait, |
| 3011 | wait_queue_func_t func, |
| 3012 | void *data) |
| 3013 | { |
| 3014 | /* Can't support async wakeup with polled IO */ |
| 3015 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3016 | return -EINVAL; |
| 3017 | if (kiocb->ki_filp->f_mode & FMODE_BUF_RASYNC) { |
| 3018 | wait->wait.func = func; |
| 3019 | wait->wait.private = data; |
| 3020 | wait->wait.flags = 0; |
| 3021 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3022 | kiocb->ki_flags |= IOCB_WAITQ; |
| 3023 | kiocb->ki_waitq = wait; |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
| 3027 | return -EOPNOTSUPP; |
| 3028 | } |
| 3029 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3030 | /* |
| 3031 | * This controls whether a given IO request should be armed for async page |
| 3032 | * based retry. If we return false here, the request is handed to the async |
| 3033 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3034 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3035 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3036 | * will register a callback when the page is unlocked at IO completion. Through |
| 3037 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3038 | * That retry will attempt the buffered read again. The retry will generally |
| 3039 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3040 | * async worker threads for a blocking retry. |
| 3041 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3042 | static bool io_rw_should_retry(struct io_kiocb *req) |
| 3043 | { |
| 3044 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3045 | int ret; |
| 3046 | |
| 3047 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3048 | if (req->flags & REQ_F_NOWAIT) |
| 3049 | return false; |
| 3050 | |
| 3051 | /* already tried, or we're doing O_DIRECT */ |
| 3052 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ)) |
| 3053 | return false; |
| 3054 | /* |
| 3055 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3056 | * support callback based unlocks |
| 3057 | */ |
| 3058 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3059 | return false; |
| 3060 | |
| 3061 | /* |
| 3062 | * If request type doesn't require req->io to defer in general, |
| 3063 | * we need to allocate it here |
| 3064 | */ |
| 3065 | if (!req->io && __io_alloc_async_ctx(req)) |
| 3066 | return false; |
| 3067 | |
| 3068 | ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq, |
| 3069 | io_async_buf_func, req); |
| 3070 | if (!ret) { |
| 3071 | io_get_req_task(req); |
| 3072 | return true; |
| 3073 | } |
| 3074 | |
| 3075 | return false; |
| 3076 | } |
| 3077 | |
| 3078 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3079 | { |
| 3080 | if (req->file->f_op->read_iter) |
| 3081 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3082 | else if (req->file->f_op->read) |
| 3083 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); |
| 3084 | else |
| 3085 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3086 | } |
| 3087 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3088 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
| 3089 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3090 | { |
| 3091 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3092 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3093 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3094 | size_t iov_count; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3095 | ssize_t io_size, ret, ret2; |
| 3096 | unsigned long nr_segs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3097 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3098 | ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3099 | if (ret < 0) |
| 3100 | return ret; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3101 | io_size = ret; |
| 3102 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3103 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3104 | /* Ensure we clear previously set non-block flag */ |
| 3105 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3106 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3107 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3108 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3109 | if (force_nonblock && !io_file_supports_async(req->file, READ)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3110 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3111 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3112 | iov_count = iov_iter_count(&iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3113 | nr_segs = iter.nr_segs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3114 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3115 | if (unlikely(ret)) |
| 3116 | goto out_free; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3117 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3118 | ret2 = io_iter_do_read(req, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3119 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3120 | /* Catch -EAGAIN return for forced non-blocking submission */ |
| 3121 | if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) { |
| 3122 | kiocb_done(kiocb, ret2, cs); |
| 3123 | } else { |
| 3124 | iter.count = iov_count; |
| 3125 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3126 | copy_iov: |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3127 | ret = io_setup_async_rw(req, io_size, iovec, inline_vecs, |
| 3128 | &iter); |
| 3129 | if (ret) |
| 3130 | goto out_free; |
| 3131 | /* it's copied and will be cleaned with ->io */ |
| 3132 | iovec = NULL; |
| 3133 | /* if we can retry, do so with the callbacks armed */ |
| 3134 | if (io_rw_should_retry(req)) { |
| 3135 | ret2 = io_iter_do_read(req, &iter); |
| 3136 | if (ret2 == -EIOCBQUEUED) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3137 | goto out_free; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3138 | } else if (ret2 != -EAGAIN) { |
| 3139 | kiocb_done(kiocb, ret2, cs); |
| 3140 | goto out_free; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3141 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3142 | } |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3143 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3144 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3145 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3146 | out_free: |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3147 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3148 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3149 | return ret; |
| 3150 | } |
| 3151 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3152 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3153 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3154 | { |
| 3155 | ssize_t ret; |
| 3156 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3157 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 3158 | if (ret) |
| 3159 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3160 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3161 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3162 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3163 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3164 | /* either don't need iovec imported or already have it */ |
| 3165 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3166 | return 0; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3167 | return io_rw_prep_async(req, WRITE, force_nonblock); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3170 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
| 3171 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3172 | { |
| 3173 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3174 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3175 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3176 | size_t iov_count; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3177 | ssize_t ret, ret2, io_size; |
| 3178 | unsigned long nr_segs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3179 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3180 | ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3181 | if (ret < 0) |
| 3182 | return ret; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3183 | io_size = ret; |
| 3184 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3185 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3186 | /* Ensure we clear previously set non-block flag */ |
| 3187 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3188 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3189 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3190 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3191 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3192 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3193 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3194 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3195 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3196 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3197 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3198 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3199 | iov_count = iov_iter_count(&iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3200 | nr_segs = iter.nr_segs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3201 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3202 | if (unlikely(ret)) |
| 3203 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3204 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3205 | /* |
| 3206 | * Open-code file_start_write here to grab freeze protection, |
| 3207 | * which will be released by another thread in |
| 3208 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3209 | * released so that it doesn't complain about the held lock when |
| 3210 | * we return to userspace. |
| 3211 | */ |
| 3212 | if (req->flags & REQ_F_ISREG) { |
| 3213 | __sb_start_write(file_inode(req->file)->i_sb, |
| 3214 | SB_FREEZE_WRITE, true); |
| 3215 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3216 | SB_FREEZE_WRITE); |
| 3217 | } |
| 3218 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3219 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3220 | if (req->file->f_op->write_iter) |
| 3221 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3222 | else if (req->file->f_op->write) |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3223 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3224 | else |
| 3225 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3226 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3227 | /* |
| 3228 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3229 | * retry them without IOCB_NOWAIT. |
| 3230 | */ |
| 3231 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3232 | ret2 = -EAGAIN; |
| 3233 | if (!force_nonblock || ret2 != -EAGAIN) { |
| 3234 | kiocb_done(kiocb, ret2, cs); |
| 3235 | } else { |
| 3236 | iter.count = iov_count; |
| 3237 | iter.nr_segs = nr_segs; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3238 | copy_iov: |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3239 | ret = io_setup_async_rw(req, io_size, iovec, inline_vecs, |
| 3240 | &iter); |
| 3241 | if (ret) |
| 3242 | goto out_free; |
| 3243 | /* it's copied and will be cleaned with ->io */ |
| 3244 | iovec = NULL; |
| 3245 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3246 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3247 | out_free: |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3248 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3249 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3250 | return ret; |
| 3251 | } |
| 3252 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3253 | static int __io_splice_prep(struct io_kiocb *req, |
| 3254 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3255 | { |
| 3256 | struct io_splice* sp = &req->splice; |
| 3257 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 3258 | int ret; |
| 3259 | |
| 3260 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3261 | return 0; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3262 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3263 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3264 | |
| 3265 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3266 | sp->len = READ_ONCE(sqe->len); |
| 3267 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3268 | |
| 3269 | if (unlikely(sp->flags & ~valid_flags)) |
| 3270 | return -EINVAL; |
| 3271 | |
| 3272 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 3273 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3274 | if (ret) |
| 3275 | return ret; |
| 3276 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3277 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3278 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3279 | /* |
| 3280 | * Splice operation will be punted aync, and here need to |
| 3281 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3282 | */ |
| 3283 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3284 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3285 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3286 | |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3290 | static int io_tee_prep(struct io_kiocb *req, |
| 3291 | const struct io_uring_sqe *sqe) |
| 3292 | { |
| 3293 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3294 | return -EINVAL; |
| 3295 | return __io_splice_prep(req, sqe); |
| 3296 | } |
| 3297 | |
| 3298 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3299 | { |
| 3300 | struct io_splice *sp = &req->splice; |
| 3301 | struct file *in = sp->file_in; |
| 3302 | struct file *out = sp->file_out; |
| 3303 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3304 | long ret = 0; |
| 3305 | |
| 3306 | if (force_nonblock) |
| 3307 | return -EAGAIN; |
| 3308 | if (sp->len) |
| 3309 | ret = do_tee(in, out, sp->len, flags); |
| 3310 | |
| 3311 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3312 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3313 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3314 | if (ret != sp->len) |
| 3315 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3316 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3317 | return 0; |
| 3318 | } |
| 3319 | |
| 3320 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3321 | { |
| 3322 | struct io_splice* sp = &req->splice; |
| 3323 | |
| 3324 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3325 | sp->off_out = READ_ONCE(sqe->off); |
| 3326 | return __io_splice_prep(req, sqe); |
| 3327 | } |
| 3328 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3329 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3330 | { |
| 3331 | struct io_splice *sp = &req->splice; |
| 3332 | struct file *in = sp->file_in; |
| 3333 | struct file *out = sp->file_out; |
| 3334 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3335 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3336 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3337 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3338 | if (force_nonblock) |
| 3339 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3340 | |
| 3341 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3342 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3343 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3344 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3345 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3346 | |
| 3347 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3348 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3349 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3350 | if (ret != sp->len) |
| 3351 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3352 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3353 | return 0; |
| 3354 | } |
| 3355 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3356 | /* |
| 3357 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3358 | */ |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3359 | 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] | 3360 | { |
| 3361 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3362 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3363 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3364 | return -EINVAL; |
| 3365 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3366 | __io_req_complete(req, 0, 0, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3367 | return 0; |
| 3368 | } |
| 3369 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3370 | 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] | 3371 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3372 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3373 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3374 | if (!req->file) |
| 3375 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3376 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3377 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3378 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3379 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3380 | return -EINVAL; |
| 3381 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3382 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3383 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3384 | return -EINVAL; |
| 3385 | |
| 3386 | req->sync.off = READ_ONCE(sqe->off); |
| 3387 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3388 | return 0; |
| 3389 | } |
| 3390 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3391 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3392 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3393 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3394 | int ret; |
| 3395 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3396 | /* fsync always requires a blocking context */ |
| 3397 | if (force_nonblock) |
| 3398 | return -EAGAIN; |
| 3399 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3400 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3401 | end > 0 ? end : LLONG_MAX, |
| 3402 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3403 | if (ret < 0) |
| 3404 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3405 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3406 | return 0; |
| 3407 | } |
| 3408 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3409 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3410 | const struct io_uring_sqe *sqe) |
| 3411 | { |
| 3412 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3413 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3414 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3415 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3416 | |
| 3417 | req->sync.off = READ_ONCE(sqe->off); |
| 3418 | req->sync.len = READ_ONCE(sqe->addr); |
| 3419 | req->sync.mode = READ_ONCE(sqe->len); |
| 3420 | return 0; |
| 3421 | } |
| 3422 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3423 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3424 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3425 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3426 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3427 | /* fallocate always requiring blocking context */ |
| 3428 | if (force_nonblock) |
| 3429 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3430 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3431 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3432 | if (ret < 0) |
| 3433 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3434 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3435 | return 0; |
| 3436 | } |
| 3437 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3438 | 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] | 3439 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3440 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3441 | int ret; |
| 3442 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3443 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3444 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3445 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3446 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3447 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3448 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3449 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3450 | /* open.how should be already initialised */ |
| 3451 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3452 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3453 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3454 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3455 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3456 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3457 | if (IS_ERR(req->open.filename)) { |
| 3458 | ret = PTR_ERR(req->open.filename); |
| 3459 | req->open.filename = NULL; |
| 3460 | return ret; |
| 3461 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3462 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3463 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3464 | return 0; |
| 3465 | } |
| 3466 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3467 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3468 | { |
| 3469 | u64 flags, mode; |
| 3470 | |
| 3471 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3472 | return 0; |
| 3473 | mode = READ_ONCE(sqe->len); |
| 3474 | flags = READ_ONCE(sqe->open_flags); |
| 3475 | req->open.how = build_open_how(flags, mode); |
| 3476 | return __io_openat_prep(req, sqe); |
| 3477 | } |
| 3478 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3479 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3480 | { |
| 3481 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3482 | size_t len; |
| 3483 | int ret; |
| 3484 | |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3485 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3486 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3487 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3488 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3489 | if (len < OPEN_HOW_SIZE_VER0) |
| 3490 | return -EINVAL; |
| 3491 | |
| 3492 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3493 | len); |
| 3494 | if (ret) |
| 3495 | return ret; |
| 3496 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3497 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3500 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3501 | { |
| 3502 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3503 | struct file *file; |
| 3504 | int ret; |
| 3505 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 3506 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3507 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3508 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3509 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3510 | if (ret) |
| 3511 | goto err; |
| 3512 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3513 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3514 | if (ret < 0) |
| 3515 | goto err; |
| 3516 | |
| 3517 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 3518 | if (IS_ERR(file)) { |
| 3519 | put_unused_fd(ret); |
| 3520 | ret = PTR_ERR(file); |
| 3521 | } else { |
| 3522 | fsnotify_open(file); |
| 3523 | fd_install(ret, file); |
| 3524 | } |
| 3525 | err: |
| 3526 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3527 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3528 | if (ret < 0) |
| 3529 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3530 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3531 | return 0; |
| 3532 | } |
| 3533 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3534 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3535 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3536 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3539 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3540 | const struct io_uring_sqe *sqe) |
| 3541 | { |
| 3542 | struct io_provide_buf *p = &req->pbuf; |
| 3543 | u64 tmp; |
| 3544 | |
| 3545 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3546 | return -EINVAL; |
| 3547 | |
| 3548 | tmp = READ_ONCE(sqe->fd); |
| 3549 | if (!tmp || tmp > USHRT_MAX) |
| 3550 | return -EINVAL; |
| 3551 | |
| 3552 | memset(p, 0, sizeof(*p)); |
| 3553 | p->nbufs = tmp; |
| 3554 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3559 | int bgid, unsigned nbufs) |
| 3560 | { |
| 3561 | unsigned i = 0; |
| 3562 | |
| 3563 | /* shouldn't happen */ |
| 3564 | if (!nbufs) |
| 3565 | return 0; |
| 3566 | |
| 3567 | /* the head kbuf is the list itself */ |
| 3568 | while (!list_empty(&buf->list)) { |
| 3569 | struct io_buffer *nxt; |
| 3570 | |
| 3571 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3572 | list_del(&nxt->list); |
| 3573 | kfree(nxt); |
| 3574 | if (++i == nbufs) |
| 3575 | return i; |
| 3576 | } |
| 3577 | i++; |
| 3578 | kfree(buf); |
| 3579 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3580 | |
| 3581 | return i; |
| 3582 | } |
| 3583 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3584 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3585 | struct io_comp_state *cs) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3586 | { |
| 3587 | struct io_provide_buf *p = &req->pbuf; |
| 3588 | struct io_ring_ctx *ctx = req->ctx; |
| 3589 | struct io_buffer *head; |
| 3590 | int ret = 0; |
| 3591 | |
| 3592 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3593 | |
| 3594 | lockdep_assert_held(&ctx->uring_lock); |
| 3595 | |
| 3596 | ret = -ENOENT; |
| 3597 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3598 | if (head) |
| 3599 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 3600 | |
| 3601 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3602 | if (ret < 0) |
| 3603 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3604 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3605 | return 0; |
| 3606 | } |
| 3607 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3608 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3609 | const struct io_uring_sqe *sqe) |
| 3610 | { |
| 3611 | struct io_provide_buf *p = &req->pbuf; |
| 3612 | u64 tmp; |
| 3613 | |
| 3614 | if (sqe->ioprio || sqe->rw_flags) |
| 3615 | return -EINVAL; |
| 3616 | |
| 3617 | tmp = READ_ONCE(sqe->fd); |
| 3618 | if (!tmp || tmp > USHRT_MAX) |
| 3619 | return -E2BIG; |
| 3620 | p->nbufs = tmp; |
| 3621 | p->addr = READ_ONCE(sqe->addr); |
| 3622 | p->len = READ_ONCE(sqe->len); |
| 3623 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3624 | 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] | 3625 | return -EFAULT; |
| 3626 | |
| 3627 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3628 | tmp = READ_ONCE(sqe->off); |
| 3629 | if (tmp > USHRT_MAX) |
| 3630 | return -E2BIG; |
| 3631 | p->bid = tmp; |
| 3632 | return 0; |
| 3633 | } |
| 3634 | |
| 3635 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3636 | { |
| 3637 | struct io_buffer *buf; |
| 3638 | u64 addr = pbuf->addr; |
| 3639 | int i, bid = pbuf->bid; |
| 3640 | |
| 3641 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3642 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3643 | if (!buf) |
| 3644 | break; |
| 3645 | |
| 3646 | buf->addr = addr; |
| 3647 | buf->len = pbuf->len; |
| 3648 | buf->bid = bid; |
| 3649 | addr += pbuf->len; |
| 3650 | bid++; |
| 3651 | if (!*head) { |
| 3652 | INIT_LIST_HEAD(&buf->list); |
| 3653 | *head = buf; |
| 3654 | } else { |
| 3655 | list_add_tail(&buf->list, &(*head)->list); |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | return i ? i : -ENOMEM; |
| 3660 | } |
| 3661 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3662 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
| 3663 | struct io_comp_state *cs) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3664 | { |
| 3665 | struct io_provide_buf *p = &req->pbuf; |
| 3666 | struct io_ring_ctx *ctx = req->ctx; |
| 3667 | struct io_buffer *head, *list; |
| 3668 | int ret = 0; |
| 3669 | |
| 3670 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3671 | |
| 3672 | lockdep_assert_held(&ctx->uring_lock); |
| 3673 | |
| 3674 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3675 | |
| 3676 | ret = io_add_buffers(p, &head); |
| 3677 | if (ret < 0) |
| 3678 | goto out; |
| 3679 | |
| 3680 | if (!list) { |
| 3681 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3682 | GFP_KERNEL); |
| 3683 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3684 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3685 | goto out; |
| 3686 | } |
| 3687 | } |
| 3688 | out: |
| 3689 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3690 | if (ret < 0) |
| 3691 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3692 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3693 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3696 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3697 | const struct io_uring_sqe *sqe) |
| 3698 | { |
| 3699 | #if defined(CONFIG_EPOLL) |
| 3700 | if (sqe->ioprio || sqe->buf_index) |
| 3701 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3702 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3703 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3704 | |
| 3705 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3706 | req->epoll.op = READ_ONCE(sqe->len); |
| 3707 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3708 | |
| 3709 | if (ep_op_has_event(req->epoll.op)) { |
| 3710 | struct epoll_event __user *ev; |
| 3711 | |
| 3712 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3713 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3714 | return -EFAULT; |
| 3715 | } |
| 3716 | |
| 3717 | return 0; |
| 3718 | #else |
| 3719 | return -EOPNOTSUPP; |
| 3720 | #endif |
| 3721 | } |
| 3722 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3723 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
| 3724 | struct io_comp_state *cs) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3725 | { |
| 3726 | #if defined(CONFIG_EPOLL) |
| 3727 | struct io_epoll *ie = &req->epoll; |
| 3728 | int ret; |
| 3729 | |
| 3730 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3731 | if (force_nonblock && ret == -EAGAIN) |
| 3732 | return -EAGAIN; |
| 3733 | |
| 3734 | if (ret < 0) |
| 3735 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3736 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3737 | return 0; |
| 3738 | #else |
| 3739 | return -EOPNOTSUPP; |
| 3740 | #endif |
| 3741 | } |
| 3742 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3743 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3744 | { |
| 3745 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3746 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3747 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3748 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3749 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3750 | |
| 3751 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3752 | req->madvise.len = READ_ONCE(sqe->len); |
| 3753 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3754 | return 0; |
| 3755 | #else |
| 3756 | return -EOPNOTSUPP; |
| 3757 | #endif |
| 3758 | } |
| 3759 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3760 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3761 | { |
| 3762 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3763 | struct io_madvise *ma = &req->madvise; |
| 3764 | int ret; |
| 3765 | |
| 3766 | if (force_nonblock) |
| 3767 | return -EAGAIN; |
| 3768 | |
| 3769 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3770 | if (ret < 0) |
| 3771 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3772 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3773 | return 0; |
| 3774 | #else |
| 3775 | return -EOPNOTSUPP; |
| 3776 | #endif |
| 3777 | } |
| 3778 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3779 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3780 | { |
| 3781 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3782 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3783 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3784 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3785 | |
| 3786 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3787 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3788 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3789 | return 0; |
| 3790 | } |
| 3791 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3792 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3793 | { |
| 3794 | struct io_fadvise *fa = &req->fadvise; |
| 3795 | int ret; |
| 3796 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3797 | if (force_nonblock) { |
| 3798 | switch (fa->advice) { |
| 3799 | case POSIX_FADV_NORMAL: |
| 3800 | case POSIX_FADV_RANDOM: |
| 3801 | case POSIX_FADV_SEQUENTIAL: |
| 3802 | break; |
| 3803 | default: |
| 3804 | return -EAGAIN; |
| 3805 | } |
| 3806 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3807 | |
| 3808 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3809 | if (ret < 0) |
| 3810 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3811 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3812 | return 0; |
| 3813 | } |
| 3814 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3815 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3816 | { |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3817 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3818 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3819 | if (sqe->ioprio || sqe->buf_index) |
| 3820 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3821 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3822 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3823 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3824 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 3825 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3826 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3827 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3828 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3829 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3830 | return 0; |
| 3831 | } |
| 3832 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3833 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3834 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 3835 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3836 | int ret; |
| 3837 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3838 | if (force_nonblock) { |
| 3839 | /* only need file table for an actual valid fd */ |
| 3840 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 3841 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3842 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 3843 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3844 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 3845 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 3846 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3847 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3848 | if (ret < 0) |
| 3849 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3850 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3851 | return 0; |
| 3852 | } |
| 3853 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3854 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3855 | { |
| 3856 | /* |
| 3857 | * 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] | 3858 | * leave the 'file' in an undeterminate state, and here need to modify |
| 3859 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3860 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3861 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3862 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3863 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3864 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3865 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3866 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 3867 | sqe->rw_flags || sqe->buf_index) |
| 3868 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 3869 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3870 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3871 | |
| 3872 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 3873 | if ((req->file && req->file->f_op == &io_uring_fops) || |
| 3874 | req->close.fd == req->ctx->ring_fd) |
| 3875 | return -EBADF; |
| 3876 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3877 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3878 | return 0; |
| 3879 | } |
| 3880 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3881 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
| 3882 | struct io_comp_state *cs) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3883 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3884 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3885 | int ret; |
| 3886 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3887 | /* might be already done during nonblock submission */ |
| 3888 | if (!close->put_file) { |
| 3889 | ret = __close_fd_get_file(close->fd, &close->put_file); |
| 3890 | if (ret < 0) |
| 3891 | return (ret == -ENOENT) ? -EBADF : ret; |
| 3892 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3893 | |
| 3894 | /* 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] | 3895 | if (close->put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3896 | /* was never set, but play safe */ |
| 3897 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3898 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3899 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 3900 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3901 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3902 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3903 | /* No ->flush() or already async, safely close from here */ |
| 3904 | ret = filp_close(close->put_file, req->work.files); |
| 3905 | if (ret < 0) |
| 3906 | req_set_fail_links(req); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 3907 | fput(close->put_file); |
| 3908 | close->put_file = NULL; |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3909 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3910 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3913 | 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] | 3914 | { |
| 3915 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3916 | |
| 3917 | if (!req->file) |
| 3918 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3919 | |
| 3920 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3921 | return -EINVAL; |
| 3922 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3923 | return -EINVAL; |
| 3924 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3925 | req->sync.off = READ_ONCE(sqe->off); |
| 3926 | req->sync.len = READ_ONCE(sqe->len); |
| 3927 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3928 | return 0; |
| 3929 | } |
| 3930 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3931 | 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] | 3932 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3933 | int ret; |
| 3934 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3935 | /* sync_file_range always requires a blocking context */ |
| 3936 | if (force_nonblock) |
| 3937 | return -EAGAIN; |
| 3938 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3939 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3940 | req->sync.flags); |
| 3941 | if (ret < 0) |
| 3942 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3943 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3944 | return 0; |
| 3945 | } |
| 3946 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 3947 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3948 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3949 | struct io_async_msghdr *kmsg) |
| 3950 | { |
| 3951 | if (req->io) |
| 3952 | return -EAGAIN; |
| 3953 | if (io_alloc_async_ctx(req)) { |
| 3954 | if (kmsg->iov != kmsg->fast_iov) |
| 3955 | kfree(kmsg->iov); |
| 3956 | return -ENOMEM; |
| 3957 | } |
| 3958 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3959 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3960 | return -EAGAIN; |
| 3961 | } |
| 3962 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 3963 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 3964 | struct io_async_msghdr *iomsg) |
| 3965 | { |
| 3966 | iomsg->iov = iomsg->fast_iov; |
| 3967 | iomsg->msg.msg_name = &iomsg->addr; |
| 3968 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
| 3969 | req->sr_msg.msg_flags, &iomsg->iov); |
| 3970 | } |
| 3971 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3972 | 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] | 3973 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3974 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3975 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3976 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3977 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 3978 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3979 | return -EINVAL; |
| 3980 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3981 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 3982 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3983 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3984 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3985 | #ifdef CONFIG_COMPAT |
| 3986 | if (req->ctx->compat) |
| 3987 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3988 | #endif |
| 3989 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3990 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3991 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3992 | /* iovec is already imported */ |
| 3993 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3994 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3995 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 3996 | ret = io_sendmsg_copy_hdr(req, &io->msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3997 | if (!ret) |
| 3998 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3999 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4002 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4003 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4004 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4005 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4006 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4007 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4008 | int ret; |
| 4009 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4010 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4011 | if (unlikely(!sock)) |
| 4012 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4013 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4014 | if (req->io) { |
| 4015 | kmsg = &req->io->msg; |
| 4016 | kmsg->msg.msg_name = &req->io->msg.addr; |
| 4017 | /* if iov is set, it's allocated already */ |
| 4018 | if (!kmsg->iov) |
| 4019 | kmsg->iov = kmsg->fast_iov; |
| 4020 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4021 | } else { |
| 4022 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4023 | if (ret) |
| 4024 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4025 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4026 | } |
| 4027 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4028 | flags = req->sr_msg.msg_flags; |
| 4029 | if (flags & MSG_DONTWAIT) |
| 4030 | req->flags |= REQ_F_NOWAIT; |
| 4031 | else if (force_nonblock) |
| 4032 | flags |= MSG_DONTWAIT; |
| 4033 | |
| 4034 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
| 4035 | if (force_nonblock && ret == -EAGAIN) |
| 4036 | return io_setup_async_msg(req, kmsg); |
| 4037 | if (ret == -ERESTARTSYS) |
| 4038 | ret = -EINTR; |
| 4039 | |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4040 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4041 | kfree(kmsg->iov); |
| 4042 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4043 | if (ret < 0) |
| 4044 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4045 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4046 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4049 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4050 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4051 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4052 | struct io_sr_msg *sr = &req->sr_msg; |
| 4053 | struct msghdr msg; |
| 4054 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4055 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4056 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4057 | int ret; |
| 4058 | |
| 4059 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4060 | if (unlikely(!sock)) |
| 4061 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4062 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4063 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4064 | if (unlikely(ret)) |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4065 | return ret;; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4066 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4067 | msg.msg_name = NULL; |
| 4068 | msg.msg_control = NULL; |
| 4069 | msg.msg_controllen = 0; |
| 4070 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4071 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4072 | flags = req->sr_msg.msg_flags; |
| 4073 | if (flags & MSG_DONTWAIT) |
| 4074 | req->flags |= REQ_F_NOWAIT; |
| 4075 | else if (force_nonblock) |
| 4076 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4077 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4078 | msg.msg_flags = flags; |
| 4079 | ret = sock_sendmsg(sock, &msg); |
| 4080 | if (force_nonblock && ret == -EAGAIN) |
| 4081 | return -EAGAIN; |
| 4082 | if (ret == -ERESTARTSYS) |
| 4083 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4084 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4085 | if (ret < 0) |
| 4086 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4087 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4088 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4089 | } |
| 4090 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4091 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4092 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4093 | { |
| 4094 | struct io_sr_msg *sr = &req->sr_msg; |
| 4095 | struct iovec __user *uiov; |
| 4096 | size_t iov_len; |
| 4097 | int ret; |
| 4098 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4099 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4100 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4101 | if (ret) |
| 4102 | return ret; |
| 4103 | |
| 4104 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4105 | if (iov_len > 1) |
| 4106 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4107 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4108 | return -EFAULT; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4109 | sr->len = iomsg->iov[0].iov_len; |
| 4110 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4111 | sr->len); |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4112 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4113 | } else { |
| 4114 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4115 | &iomsg->iov, &iomsg->msg.msg_iter); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4116 | if (ret > 0) |
| 4117 | ret = 0; |
| 4118 | } |
| 4119 | |
| 4120 | return ret; |
| 4121 | } |
| 4122 | |
| 4123 | #ifdef CONFIG_COMPAT |
| 4124 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4125 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4126 | { |
| 4127 | struct compat_msghdr __user *msg_compat; |
| 4128 | struct io_sr_msg *sr = &req->sr_msg; |
| 4129 | struct compat_iovec __user *uiov; |
| 4130 | compat_uptr_t ptr; |
| 4131 | compat_size_t len; |
| 4132 | int ret; |
| 4133 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4134 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4135 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4136 | &ptr, &len); |
| 4137 | if (ret) |
| 4138 | return ret; |
| 4139 | |
| 4140 | uiov = compat_ptr(ptr); |
| 4141 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4142 | compat_ssize_t clen; |
| 4143 | |
| 4144 | if (len > 1) |
| 4145 | return -EINVAL; |
| 4146 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4147 | return -EFAULT; |
| 4148 | if (__get_user(clen, &uiov->iov_len)) |
| 4149 | return -EFAULT; |
| 4150 | if (clen < 0) |
| 4151 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4152 | sr->len = iomsg->iov[0].iov_len; |
| 4153 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4154 | } else { |
| 4155 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4156 | &iomsg->iov, |
| 4157 | &iomsg->msg.msg_iter); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4158 | if (ret < 0) |
| 4159 | return ret; |
| 4160 | } |
| 4161 | |
| 4162 | return 0; |
| 4163 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4164 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4165 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4166 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4167 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4168 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4169 | iomsg->msg.msg_name = &iomsg->addr; |
| 4170 | iomsg->iov = iomsg->fast_iov; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4171 | |
| 4172 | #ifdef CONFIG_COMPAT |
| 4173 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4174 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4175 | #endif |
| 4176 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4177 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4180 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4181 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4182 | { |
| 4183 | struct io_sr_msg *sr = &req->sr_msg; |
| 4184 | struct io_buffer *kbuf; |
| 4185 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4186 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4187 | if (IS_ERR(kbuf)) |
| 4188 | return kbuf; |
| 4189 | |
| 4190 | sr->kbuf = kbuf; |
| 4191 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4192 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4195 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4196 | { |
| 4197 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4198 | } |
| 4199 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4200 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4201 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4202 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4203 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4204 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4205 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4206 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4207 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4208 | return -EINVAL; |
| 4209 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4210 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4211 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4212 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4213 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4214 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4215 | #ifdef CONFIG_COMPAT |
| 4216 | if (req->ctx->compat) |
| 4217 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4218 | #endif |
| 4219 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4220 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4221 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 4222 | /* iovec is already imported */ |
| 4223 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 4224 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4225 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4226 | ret = io_recvmsg_copy_hdr(req, &io->msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4227 | if (!ret) |
| 4228 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4229 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4232 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4233 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4234 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4235 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4236 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4237 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4238 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4239 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4240 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4241 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4242 | if (unlikely(!sock)) |
| 4243 | return ret; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4244 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4245 | if (req->io) { |
| 4246 | kmsg = &req->io->msg; |
| 4247 | kmsg->msg.msg_name = &req->io->msg.addr; |
| 4248 | /* if iov is set, it's allocated already */ |
| 4249 | if (!kmsg->iov) |
| 4250 | kmsg->iov = kmsg->fast_iov; |
| 4251 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4252 | } else { |
| 4253 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4254 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4255 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4256 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4257 | } |
| 4258 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4259 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4260 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4261 | if (IS_ERR(kbuf)) |
| 4262 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4263 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4264 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4265 | 1, req->sr_msg.len); |
| 4266 | } |
| 4267 | |
| 4268 | flags = req->sr_msg.msg_flags; |
| 4269 | if (flags & MSG_DONTWAIT) |
| 4270 | req->flags |= REQ_F_NOWAIT; |
| 4271 | else if (force_nonblock) |
| 4272 | flags |= MSG_DONTWAIT; |
| 4273 | |
| 4274 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4275 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4276 | if (force_nonblock && ret == -EAGAIN) |
| 4277 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4278 | if (ret == -ERESTARTSYS) |
| 4279 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4280 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4281 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4282 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4283 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4284 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4285 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4286 | if (ret < 0) |
| 4287 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4288 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4289 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4290 | } |
| 4291 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4292 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4293 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4294 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4295 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4296 | struct io_sr_msg *sr = &req->sr_msg; |
| 4297 | struct msghdr msg; |
| 4298 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4299 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4300 | struct iovec iov; |
| 4301 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4302 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4303 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4304 | sock = sock_from_file(req->file, &ret); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4305 | if (unlikely(!sock)) |
| 4306 | return ret; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4307 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4308 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4309 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4310 | if (IS_ERR(kbuf)) |
| 4311 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4312 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4313 | } |
| 4314 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4315 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4316 | if (unlikely(ret)) |
| 4317 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4318 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4319 | msg.msg_name = NULL; |
| 4320 | msg.msg_control = NULL; |
| 4321 | msg.msg_controllen = 0; |
| 4322 | msg.msg_namelen = 0; |
| 4323 | msg.msg_iocb = NULL; |
| 4324 | msg.msg_flags = 0; |
| 4325 | |
| 4326 | flags = req->sr_msg.msg_flags; |
| 4327 | if (flags & MSG_DONTWAIT) |
| 4328 | req->flags |= REQ_F_NOWAIT; |
| 4329 | else if (force_nonblock) |
| 4330 | flags |= MSG_DONTWAIT; |
| 4331 | |
| 4332 | ret = sock_recvmsg(sock, &msg, flags); |
| 4333 | if (force_nonblock && ret == -EAGAIN) |
| 4334 | return -EAGAIN; |
| 4335 | if (ret == -ERESTARTSYS) |
| 4336 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4337 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4338 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4339 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4340 | if (ret < 0) |
| 4341 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4342 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4343 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4344 | } |
| 4345 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4346 | 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] | 4347 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4348 | struct io_accept *accept = &req->accept; |
| 4349 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4350 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4351 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4352 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4353 | return -EINVAL; |
| 4354 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4355 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4356 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4357 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4358 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4359 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4360 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4361 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4362 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4363 | struct io_comp_state *cs) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4364 | { |
| 4365 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4366 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4367 | int ret; |
| 4368 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4369 | if (req->file->f_flags & O_NONBLOCK) |
| 4370 | req->flags |= REQ_F_NOWAIT; |
| 4371 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4372 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4373 | accept->addr_len, accept->flags, |
| 4374 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4375 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4376 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4377 | if (ret < 0) { |
| 4378 | if (ret == -ERESTARTSYS) |
| 4379 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4380 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4381 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4382 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4383 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4386 | 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] | 4387 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4388 | struct io_connect *conn = &req->connect; |
| 4389 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4390 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4391 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 4392 | return -EINVAL; |
| 4393 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4394 | return -EINVAL; |
| 4395 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4396 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4397 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 4398 | |
| 4399 | if (!io) |
| 4400 | return 0; |
| 4401 | |
| 4402 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4403 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4406 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4407 | struct io_comp_state *cs) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4408 | { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4409 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4410 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4411 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4412 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4413 | if (req->io) { |
| 4414 | io = req->io; |
| 4415 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4416 | ret = move_addr_to_kernel(req->connect.addr, |
| 4417 | req->connect.addr_len, |
| 4418 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4419 | if (ret) |
| 4420 | goto out; |
| 4421 | io = &__io; |
| 4422 | } |
| 4423 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4424 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4425 | |
| 4426 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 4427 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4428 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4429 | if (req->io) |
| 4430 | return -EAGAIN; |
| 4431 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4432 | ret = -ENOMEM; |
| 4433 | goto out; |
| 4434 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4435 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4436 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4437 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4438 | if (ret == -ERESTARTSYS) |
| 4439 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4440 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4441 | if (ret < 0) |
| 4442 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4443 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4444 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4445 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4446 | #else /* !CONFIG_NET */ |
| 4447 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4448 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4449 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4452 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4453 | struct io_comp_state *cs) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4454 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4455 | return -EOPNOTSUPP; |
| 4456 | } |
| 4457 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4458 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4459 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4460 | { |
| 4461 | return -EOPNOTSUPP; |
| 4462 | } |
| 4463 | |
| 4464 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4465 | const struct io_uring_sqe *sqe) |
| 4466 | { |
| 4467 | return -EOPNOTSUPP; |
| 4468 | } |
| 4469 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4470 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4471 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4472 | { |
| 4473 | return -EOPNOTSUPP; |
| 4474 | } |
| 4475 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4476 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4477 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4478 | { |
| 4479 | return -EOPNOTSUPP; |
| 4480 | } |
| 4481 | |
| 4482 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4483 | { |
| 4484 | return -EOPNOTSUPP; |
| 4485 | } |
| 4486 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4487 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4488 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4489 | { |
| 4490 | return -EOPNOTSUPP; |
| 4491 | } |
| 4492 | |
| 4493 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4494 | { |
| 4495 | return -EOPNOTSUPP; |
| 4496 | } |
| 4497 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 4498 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 4499 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4500 | { |
| 4501 | return -EOPNOTSUPP; |
| 4502 | } |
| 4503 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4504 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4505 | struct io_poll_table { |
| 4506 | struct poll_table_struct pt; |
| 4507 | struct io_kiocb *req; |
| 4508 | int error; |
| 4509 | }; |
| 4510 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4511 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4512 | __poll_t mask, task_work_func_t func) |
| 4513 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4514 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4515 | |
| 4516 | /* for instances that support it check for an event match first: */ |
| 4517 | if (mask && !(mask & poll->events)) |
| 4518 | return 0; |
| 4519 | |
| 4520 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4521 | |
| 4522 | list_del_init(&poll->wait.entry); |
| 4523 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4524 | req->result = mask; |
| 4525 | init_task_work(&req->task_work, func); |
| 4526 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4527 | * If this fails, then the task is exiting. When a task exits, the |
| 4528 | * work gets canceled, so just cancel this request as well instead |
| 4529 | * of executing it. We can't safely execute it anyway, as we may not |
| 4530 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4531 | */ |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 4532 | ret = io_req_task_work_add(req, &req->task_work); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4533 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 4534 | struct task_struct *tsk; |
| 4535 | |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4536 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4537 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 4538 | task_work_add(tsk, &req->task_work, 0); |
| 4539 | wake_up_process(tsk); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4540 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4541 | return 1; |
| 4542 | } |
| 4543 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4544 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4545 | __acquires(&req->ctx->completion_lock) |
| 4546 | { |
| 4547 | struct io_ring_ctx *ctx = req->ctx; |
| 4548 | |
| 4549 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4550 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4551 | |
| 4552 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4553 | } |
| 4554 | |
| 4555 | spin_lock_irq(&ctx->completion_lock); |
| 4556 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4557 | add_wait_queue(poll->head, &poll->wait); |
| 4558 | return true; |
| 4559 | } |
| 4560 | |
| 4561 | return false; |
| 4562 | } |
| 4563 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4564 | static void io_poll_remove_double(struct io_kiocb *req, void *data) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4565 | { |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4566 | struct io_poll_iocb *poll = data; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4567 | |
| 4568 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4569 | |
| 4570 | if (poll && poll->head) { |
| 4571 | struct wait_queue_head *head = poll->head; |
| 4572 | |
| 4573 | spin_lock(&head->lock); |
| 4574 | list_del_init(&poll->wait.entry); |
| 4575 | if (poll->wait.private) |
| 4576 | refcount_dec(&req->refs); |
| 4577 | poll->head = NULL; |
| 4578 | spin_unlock(&head->lock); |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4583 | { |
| 4584 | struct io_ring_ctx *ctx = req->ctx; |
| 4585 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4586 | io_poll_remove_double(req, req->io); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4587 | req->poll.done = true; |
| 4588 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4589 | io_commit_cqring(ctx); |
| 4590 | } |
| 4591 | |
| 4592 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4593 | { |
| 4594 | struct io_ring_ctx *ctx = req->ctx; |
| 4595 | |
| 4596 | if (io_poll_rewait(req, &req->poll)) { |
| 4597 | spin_unlock_irq(&ctx->completion_lock); |
| 4598 | return; |
| 4599 | } |
| 4600 | |
| 4601 | hash_del(&req->hash_node); |
| 4602 | io_poll_complete(req, req->result, 0); |
| 4603 | req->flags |= REQ_F_COMP_LOCKED; |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 4604 | *nxt = io_put_req_find_next(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4605 | spin_unlock_irq(&ctx->completion_lock); |
| 4606 | |
| 4607 | io_cqring_ev_posted(ctx); |
| 4608 | } |
| 4609 | |
| 4610 | static void io_poll_task_func(struct callback_head *cb) |
| 4611 | { |
| 4612 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4613 | struct io_kiocb *nxt = NULL; |
| 4614 | |
| 4615 | io_poll_task_handler(req, &nxt); |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 4616 | if (nxt) |
| 4617 | __io_req_task_submit(nxt); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4621 | int sync, void *key) |
| 4622 | { |
| 4623 | struct io_kiocb *req = wait->private; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4624 | struct io_poll_iocb *poll = req->apoll->double_poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4625 | __poll_t mask = key_to_poll(key); |
| 4626 | |
| 4627 | /* for instances that support it check for an event match first: */ |
| 4628 | if (mask && !(mask & poll->events)) |
| 4629 | return 0; |
| 4630 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4631 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4632 | bool done; |
| 4633 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4634 | spin_lock(&poll->head->lock); |
| 4635 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4636 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4637 | list_del_init(&poll->wait.entry); |
| 4638 | spin_unlock(&poll->head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4639 | if (!done) |
| 4640 | __io_async_wake(req, poll, mask, io_poll_task_func); |
| 4641 | } |
| 4642 | refcount_dec(&req->refs); |
| 4643 | return 1; |
| 4644 | } |
| 4645 | |
| 4646 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4647 | wait_queue_func_t wake_func) |
| 4648 | { |
| 4649 | poll->head = NULL; |
| 4650 | poll->done = false; |
| 4651 | poll->canceled = false; |
| 4652 | poll->events = events; |
| 4653 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4654 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4655 | } |
| 4656 | |
| 4657 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4658 | struct wait_queue_head *head, |
| 4659 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4660 | { |
| 4661 | struct io_kiocb *req = pt->req; |
| 4662 | |
| 4663 | /* |
| 4664 | * If poll->head is already set, it's because the file being polled |
| 4665 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4666 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4667 | */ |
| 4668 | if (unlikely(poll->head)) { |
| 4669 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4670 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4671 | pt->error = -EINVAL; |
| 4672 | return; |
| 4673 | } |
| 4674 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4675 | if (!poll) { |
| 4676 | pt->error = -ENOMEM; |
| 4677 | return; |
| 4678 | } |
| 4679 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); |
| 4680 | refcount_inc(&req->refs); |
| 4681 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4682 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | pt->error = 0; |
| 4686 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4687 | |
| 4688 | if (poll->events & EPOLLEXCLUSIVE) |
| 4689 | add_wait_queue_exclusive(head, &poll->wait); |
| 4690 | else |
| 4691 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4695 | struct poll_table_struct *p) |
| 4696 | { |
| 4697 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4698 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4699 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4700 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4701 | } |
| 4702 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4703 | static void io_async_task_func(struct callback_head *cb) |
| 4704 | { |
| 4705 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4706 | struct async_poll *apoll = req->apoll; |
| 4707 | struct io_ring_ctx *ctx = req->ctx; |
| 4708 | |
| 4709 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4710 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4711 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4712 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4713 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4714 | } |
| 4715 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4716 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 4717 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4718 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 4719 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4720 | io_poll_remove_double(req, apoll->double_poll); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4721 | spin_unlock_irq(&ctx->completion_lock); |
| 4722 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 4723 | if (!READ_ONCE(apoll->poll.canceled)) |
| 4724 | __io_req_task_submit(req); |
| 4725 | else |
| 4726 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 4727 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4728 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 4729 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4733 | void *key) |
| 4734 | { |
| 4735 | struct io_kiocb *req = wait->private; |
| 4736 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 4737 | |
| 4738 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 4739 | key_to_poll(key)); |
| 4740 | |
| 4741 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 4742 | } |
| 4743 | |
| 4744 | static void io_poll_req_insert(struct io_kiocb *req) |
| 4745 | { |
| 4746 | struct io_ring_ctx *ctx = req->ctx; |
| 4747 | struct hlist_head *list; |
| 4748 | |
| 4749 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 4750 | hlist_add_head(&req->hash_node, list); |
| 4751 | } |
| 4752 | |
| 4753 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 4754 | struct io_poll_iocb *poll, |
| 4755 | struct io_poll_table *ipt, __poll_t mask, |
| 4756 | wait_queue_func_t wake_func) |
| 4757 | __acquires(&ctx->completion_lock) |
| 4758 | { |
| 4759 | struct io_ring_ctx *ctx = req->ctx; |
| 4760 | bool cancel = false; |
| 4761 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4762 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 4763 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4764 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4765 | |
| 4766 | ipt->pt._key = mask; |
| 4767 | ipt->req = req; |
| 4768 | ipt->error = -EINVAL; |
| 4769 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4770 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 4771 | |
| 4772 | spin_lock_irq(&ctx->completion_lock); |
| 4773 | if (likely(poll->head)) { |
| 4774 | spin_lock(&poll->head->lock); |
| 4775 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 4776 | if (ipt->error) |
| 4777 | cancel = true; |
| 4778 | ipt->error = 0; |
| 4779 | mask = 0; |
| 4780 | } |
| 4781 | if (mask || ipt->error) |
| 4782 | list_del_init(&poll->wait.entry); |
| 4783 | else if (cancel) |
| 4784 | WRITE_ONCE(poll->canceled, true); |
| 4785 | else if (!poll->done) /* actually waiting for an event */ |
| 4786 | io_poll_req_insert(req); |
| 4787 | spin_unlock(&poll->head->lock); |
| 4788 | } |
| 4789 | |
| 4790 | return mask; |
| 4791 | } |
| 4792 | |
| 4793 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 4794 | { |
| 4795 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 4796 | struct io_ring_ctx *ctx = req->ctx; |
| 4797 | struct async_poll *apoll; |
| 4798 | struct io_poll_table ipt; |
| 4799 | __poll_t mask, ret; |
| 4800 | |
| 4801 | if (!req->file || !file_can_poll(req->file)) |
| 4802 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4803 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4804 | return false; |
| 4805 | if (!def->pollin && !def->pollout) |
| 4806 | return false; |
| 4807 | |
| 4808 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 4809 | if (unlikely(!apoll)) |
| 4810 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4811 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4812 | |
| 4813 | req->flags |= REQ_F_POLLED; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4814 | io_get_req_task(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4815 | req->apoll = apoll; |
| 4816 | INIT_HLIST_NODE(&req->hash_node); |
| 4817 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4818 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4819 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4820 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4821 | if (def->pollout) |
| 4822 | mask |= POLLOUT | POLLWRNORM; |
| 4823 | mask |= POLLERR | POLLPRI; |
| 4824 | |
| 4825 | ipt.pt._qproc = io_async_queue_proc; |
| 4826 | |
| 4827 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 4828 | io_async_wake); |
| 4829 | if (ret) { |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4830 | io_poll_remove_double(req, apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4831 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4832 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4833 | kfree(apoll); |
| 4834 | return false; |
| 4835 | } |
| 4836 | spin_unlock_irq(&ctx->completion_lock); |
| 4837 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 4838 | apoll->poll.events); |
| 4839 | return true; |
| 4840 | } |
| 4841 | |
| 4842 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 4843 | struct io_poll_iocb *poll) |
| 4844 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4845 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4846 | |
| 4847 | spin_lock(&poll->head->lock); |
| 4848 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 4849 | if (!list_empty(&poll->wait.entry)) { |
| 4850 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4851 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4852 | } |
| 4853 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4854 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4855 | return do_complete; |
| 4856 | } |
| 4857 | |
| 4858 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 4859 | { |
| 4860 | bool do_complete; |
| 4861 | |
| 4862 | if (req->opcode == IORING_OP_POLL_ADD) { |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4863 | io_poll_remove_double(req, req->io); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4864 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 4865 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4866 | struct async_poll *apoll = req->apoll; |
| 4867 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4868 | io_poll_remove_double(req, apoll->double_poll); |
| 4869 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4870 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4871 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 4872 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4873 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4874 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 4875 | kfree(apoll); |
| 4876 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 4877 | } |
| 4878 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4879 | if (do_complete) { |
| 4880 | io_cqring_fill_event(req, -ECANCELED); |
| 4881 | io_commit_cqring(req->ctx); |
| 4882 | req->flags |= REQ_F_COMP_LOCKED; |
| 4883 | io_put_req(req); |
| 4884 | } |
| 4885 | |
| 4886 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 4890 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4891 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4892 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4893 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4894 | |
| 4895 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4896 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 4897 | struct hlist_head *list; |
| 4898 | |
| 4899 | list = &ctx->cancel_hash[i]; |
| 4900 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4901 | posted += io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4902 | } |
| 4903 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4904 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 4905 | if (posted) |
| 4906 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4907 | } |
| 4908 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4909 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 4910 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4911 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4912 | struct io_kiocb *req; |
| 4913 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4914 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 4915 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4916 | if (sqe_addr != req->user_data) |
| 4917 | continue; |
| 4918 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4919 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4920 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | return -ENOENT; |
| 4924 | } |
| 4925 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4926 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 4927 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4928 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4929 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4930 | return -EINVAL; |
| 4931 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 4932 | sqe->poll_events) |
| 4933 | return -EINVAL; |
| 4934 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4935 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4936 | return 0; |
| 4937 | } |
| 4938 | |
| 4939 | /* |
| 4940 | * Find a running poll command that matches one specified in sqe->addr, |
| 4941 | * and remove it if found. |
| 4942 | */ |
| 4943 | static int io_poll_remove(struct io_kiocb *req) |
| 4944 | { |
| 4945 | struct io_ring_ctx *ctx = req->ctx; |
| 4946 | u64 addr; |
| 4947 | int ret; |
| 4948 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4949 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4950 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4951 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4952 | spin_unlock_irq(&ctx->completion_lock); |
| 4953 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4954 | if (ret < 0) |
| 4955 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4956 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4957 | return 0; |
| 4958 | } |
| 4959 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4960 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4961 | void *key) |
| 4962 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 4963 | struct io_kiocb *req = wait->private; |
| 4964 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4965 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4966 | 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] | 4967 | } |
| 4968 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4969 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4970 | struct poll_table_struct *p) |
| 4971 | { |
| 4972 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4973 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4974 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4975 | } |
| 4976 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4977 | 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] | 4978 | { |
| 4979 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4980 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4981 | |
| 4982 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4983 | return -EINVAL; |
| 4984 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 4985 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4986 | if (!poll->file) |
| 4987 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4988 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 4989 | events = READ_ONCE(sqe->poll32_events); |
| 4990 | #ifdef __BIG_ENDIAN |
| 4991 | events = swahw32(events); |
| 4992 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4993 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 4994 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4995 | |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 4996 | io_get_req_task(req); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4997 | return 0; |
| 4998 | } |
| 4999 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5000 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5001 | { |
| 5002 | struct io_poll_iocb *poll = &req->poll; |
| 5003 | struct io_ring_ctx *ctx = req->ctx; |
| 5004 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5005 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5006 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5007 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5008 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5009 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5010 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5011 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5012 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5013 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5014 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5015 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5016 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5017 | spin_unlock_irq(&ctx->completion_lock); |
| 5018 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5019 | if (mask) { |
| 5020 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5021 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5022 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5023 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5024 | } |
| 5025 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5026 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5027 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5028 | struct io_timeout_data *data = container_of(timer, |
| 5029 | struct io_timeout_data, timer); |
| 5030 | struct io_kiocb *req = data->req; |
| 5031 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5032 | unsigned long flags; |
| 5033 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5034 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5035 | atomic_set(&req->ctx->cq_timeouts, |
| 5036 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5037 | |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 5038 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5039 | * We could be racing with timeout deletion. If the list is empty, |
| 5040 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 5041 | */ |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5042 | if (!list_empty(&req->timeout.list)) |
| 5043 | list_del_init(&req->timeout.list); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5044 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5045 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5046 | io_commit_cqring(ctx); |
| 5047 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5048 | |
| 5049 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5050 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5051 | io_put_req(req); |
| 5052 | return HRTIMER_NORESTART; |
| 5053 | } |
| 5054 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5055 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5056 | { |
| 5057 | struct io_kiocb *req; |
| 5058 | int ret = -ENOENT; |
| 5059 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5060 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5061 | if (user_data == req->user_data) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5062 | list_del_init(&req->timeout.list); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5063 | ret = 0; |
| 5064 | break; |
| 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | if (ret == -ENOENT) |
| 5069 | return ret; |
| 5070 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5071 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5072 | if (ret == -1) |
| 5073 | return -EALREADY; |
| 5074 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5075 | req_set_fail_links(req); |
Jens Axboe | 9b7adba | 2020-08-10 10:54:02 -0600 | [diff] [blame^] | 5076 | req->flags |= REQ_F_COMP_LOCKED; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5077 | io_cqring_fill_event(req, -ECANCELED); |
| 5078 | io_put_req(req); |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5082 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5083 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5084 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5085 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5086 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5087 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5088 | return -EINVAL; |
| 5089 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5090 | return -EINVAL; |
| 5091 | |
| 5092 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 5093 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 5094 | if (req->timeout.flags) |
| 5095 | return -EINVAL; |
| 5096 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5097 | return 0; |
| 5098 | } |
| 5099 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5100 | /* |
| 5101 | * Remove or update an existing timeout command |
| 5102 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5103 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5104 | { |
| 5105 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5106 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5107 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5108 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5109 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5110 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5111 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5112 | io_commit_cqring(ctx); |
| 5113 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5114 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5115 | if (ret < 0) |
| 5116 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5117 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5118 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5119 | } |
| 5120 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5121 | 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] | 5122 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5123 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5124 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5125 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5126 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5127 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5128 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5129 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5130 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5131 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5132 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5133 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5134 | flags = READ_ONCE(sqe->timeout_flags); |
| 5135 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5136 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5137 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5138 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5139 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5140 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5141 | return -ENOMEM; |
| 5142 | |
| 5143 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5144 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5145 | |
| 5146 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5147 | return -EFAULT; |
| 5148 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5149 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5150 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5151 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5152 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5153 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5154 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5155 | return 0; |
| 5156 | } |
| 5157 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5158 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5159 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5160 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5161 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5162 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5163 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5164 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5165 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5166 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5167 | /* |
| 5168 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5169 | * timeout event to be satisfied. If it isn't set, then this is |
| 5170 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5171 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5172 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5173 | entry = ctx->timeout_list.prev; |
| 5174 | goto add; |
| 5175 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5176 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5177 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5178 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5179 | |
| 5180 | /* |
| 5181 | * Insertion sort, ensuring the first entry in the list is always |
| 5182 | * the one we need first. |
| 5183 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5184 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5185 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5186 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5187 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5188 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5189 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5190 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5191 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5192 | break; |
| 5193 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5194 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5195 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5196 | data->timer.function = io_timeout_fn; |
| 5197 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5198 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5199 | return 0; |
| 5200 | } |
| 5201 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5202 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5203 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5204 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5205 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5206 | return req->user_data == (unsigned long) data; |
| 5207 | } |
| 5208 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5209 | 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] | 5210 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5211 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5212 | int ret = 0; |
| 5213 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5214 | 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] | 5215 | switch (cancel_ret) { |
| 5216 | case IO_WQ_CANCEL_OK: |
| 5217 | ret = 0; |
| 5218 | break; |
| 5219 | case IO_WQ_CANCEL_RUNNING: |
| 5220 | ret = -EALREADY; |
| 5221 | break; |
| 5222 | case IO_WQ_CANCEL_NOTFOUND: |
| 5223 | ret = -ENOENT; |
| 5224 | break; |
| 5225 | } |
| 5226 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5227 | return ret; |
| 5228 | } |
| 5229 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5230 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5231 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5232 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5233 | { |
| 5234 | unsigned long flags; |
| 5235 | int ret; |
| 5236 | |
| 5237 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5238 | if (ret != -ENOENT) { |
| 5239 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5240 | goto done; |
| 5241 | } |
| 5242 | |
| 5243 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5244 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5245 | if (ret != -ENOENT) |
| 5246 | goto done; |
| 5247 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5248 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5249 | if (!ret) |
| 5250 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5251 | io_cqring_fill_event(req, ret); |
| 5252 | io_commit_cqring(ctx); |
| 5253 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5254 | io_cqring_ev_posted(ctx); |
| 5255 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5256 | if (ret < 0) |
| 5257 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5258 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5261 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5262 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5263 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5264 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5265 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5266 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5267 | return -EINVAL; |
| 5268 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5269 | return -EINVAL; |
| 5270 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5271 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5272 | return 0; |
| 5273 | } |
| 5274 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5275 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5276 | { |
| 5277 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5278 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5279 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5280 | return 0; |
| 5281 | } |
| 5282 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5283 | static int io_files_update_prep(struct io_kiocb *req, |
| 5284 | const struct io_uring_sqe *sqe) |
| 5285 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5286 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5287 | return -EINVAL; |
| 5288 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5289 | return -EINVAL; |
| 5290 | |
| 5291 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5292 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5293 | if (!req->files_update.nr_args) |
| 5294 | return -EINVAL; |
| 5295 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5299 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
| 5300 | struct io_comp_state *cs) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5301 | { |
| 5302 | struct io_ring_ctx *ctx = req->ctx; |
| 5303 | struct io_uring_files_update up; |
| 5304 | int ret; |
| 5305 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5306 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5307 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5308 | |
| 5309 | up.offset = req->files_update.offset; |
| 5310 | up.fds = req->files_update.arg; |
| 5311 | |
| 5312 | mutex_lock(&ctx->uring_lock); |
| 5313 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 5314 | mutex_unlock(&ctx->uring_lock); |
| 5315 | |
| 5316 | if (ret < 0) |
| 5317 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5318 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5319 | return 0; |
| 5320 | } |
| 5321 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5322 | static int io_req_defer_prep(struct io_kiocb *req, |
| 5323 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5324 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5325 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5326 | |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 5327 | if (!sqe) |
| 5328 | return 0; |
| 5329 | |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 5330 | if (io_alloc_async_ctx(req)) |
| 5331 | return -EAGAIN; |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 5332 | ret = io_prep_work_files(req); |
| 5333 | if (unlikely(ret)) |
| 5334 | return ret; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 5335 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5336 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5337 | case IORING_OP_NOP: |
| 5338 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5339 | case IORING_OP_READV: |
| 5340 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5341 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5342 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5343 | break; |
| 5344 | case IORING_OP_WRITEV: |
| 5345 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5346 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5347 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5348 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5349 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5350 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5351 | break; |
| 5352 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5353 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5354 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5355 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5356 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5357 | break; |
| 5358 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5359 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5360 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5361 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5362 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5363 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5364 | break; |
| 5365 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5366 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5367 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5368 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5369 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5370 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5371 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5372 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5373 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5374 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5375 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5376 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5377 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5378 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5379 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5380 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5381 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5382 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5383 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5384 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5385 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5386 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5387 | case IORING_OP_FALLOCATE: |
| 5388 | ret = io_fallocate_prep(req, sqe); |
| 5389 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5390 | case IORING_OP_OPENAT: |
| 5391 | ret = io_openat_prep(req, sqe); |
| 5392 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5393 | case IORING_OP_CLOSE: |
| 5394 | ret = io_close_prep(req, sqe); |
| 5395 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5396 | case IORING_OP_FILES_UPDATE: |
| 5397 | ret = io_files_update_prep(req, sqe); |
| 5398 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5399 | case IORING_OP_STATX: |
| 5400 | ret = io_statx_prep(req, sqe); |
| 5401 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5402 | case IORING_OP_FADVISE: |
| 5403 | ret = io_fadvise_prep(req, sqe); |
| 5404 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5405 | case IORING_OP_MADVISE: |
| 5406 | ret = io_madvise_prep(req, sqe); |
| 5407 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5408 | case IORING_OP_OPENAT2: |
| 5409 | ret = io_openat2_prep(req, sqe); |
| 5410 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5411 | case IORING_OP_EPOLL_CTL: |
| 5412 | ret = io_epoll_ctl_prep(req, sqe); |
| 5413 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5414 | case IORING_OP_SPLICE: |
| 5415 | ret = io_splice_prep(req, sqe); |
| 5416 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5417 | case IORING_OP_PROVIDE_BUFFERS: |
| 5418 | ret = io_provide_buffers_prep(req, sqe); |
| 5419 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5420 | case IORING_OP_REMOVE_BUFFERS: |
| 5421 | ret = io_remove_buffers_prep(req, sqe); |
| 5422 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5423 | case IORING_OP_TEE: |
| 5424 | ret = io_tee_prep(req, sqe); |
| 5425 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5426 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5427 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5428 | req->opcode); |
| 5429 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5430 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5433 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5436 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5437 | { |
| 5438 | struct io_kiocb *pos; |
| 5439 | struct io_ring_ctx *ctx = req->ctx; |
| 5440 | u32 total_submitted, nr_reqs = 1; |
| 5441 | |
| 5442 | if (req->flags & REQ_F_LINK_HEAD) |
| 5443 | list_for_each_entry(pos, &req->link_list, link_list) |
| 5444 | nr_reqs++; |
| 5445 | |
| 5446 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5447 | return total_submitted - nr_reqs; |
| 5448 | } |
| 5449 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5450 | 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] | 5451 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5452 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5453 | struct io_defer_entry *de; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5454 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5455 | u32 seq; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5456 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 5457 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5458 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5459 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5460 | return 0; |
| 5461 | |
| 5462 | seq = io_get_sequence(req); |
| 5463 | /* Still a chance to pass the sequence check */ |
| 5464 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5465 | return 0; |
| 5466 | |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5467 | if (!req->io) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5468 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 5469 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 5470 | return ret; |
| 5471 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5472 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5473 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5474 | if (!de) |
| 5475 | return -ENOMEM; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5476 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5477 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5478 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5479 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5480 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5481 | io_queue_async_work(req); |
| 5482 | return -EIOCBQUEUED; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5483 | } |
| 5484 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 5485 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5486 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5487 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5488 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5489 | spin_unlock_irq(&ctx->completion_lock); |
| 5490 | return -EIOCBQUEUED; |
| 5491 | } |
| 5492 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 5493 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5494 | { |
| 5495 | struct io_async_ctx *io = req->io; |
| 5496 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5497 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5498 | switch (req->opcode) { |
| 5499 | case IORING_OP_READV: |
| 5500 | case IORING_OP_READ_FIXED: |
| 5501 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5502 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5503 | break; |
| 5504 | case IORING_OP_RECVMSG: |
| 5505 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5506 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5507 | break; |
| 5508 | } |
| 5509 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5510 | } |
| 5511 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5512 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 5513 | switch (req->opcode) { |
| 5514 | case IORING_OP_READV: |
| 5515 | case IORING_OP_READ_FIXED: |
| 5516 | case IORING_OP_READ: |
| 5517 | case IORING_OP_WRITEV: |
| 5518 | case IORING_OP_WRITE_FIXED: |
| 5519 | case IORING_OP_WRITE: |
| 5520 | if (io->rw.iov != io->rw.fast_iov) |
| 5521 | kfree(io->rw.iov); |
| 5522 | break; |
| 5523 | case IORING_OP_RECVMSG: |
| 5524 | case IORING_OP_SENDMSG: |
| 5525 | if (io->msg.iov != io->msg.fast_iov) |
| 5526 | kfree(io->msg.iov); |
| 5527 | break; |
| 5528 | case IORING_OP_SPLICE: |
| 5529 | case IORING_OP_TEE: |
| 5530 | io_put_file(req, req->splice.file_in, |
| 5531 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5532 | break; |
| 5533 | } |
| 5534 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5535 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5536 | } |
| 5537 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5538 | 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] | 5539 | bool force_nonblock, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5540 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5541 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5542 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5543 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5544 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5545 | case IORING_OP_NOP: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5546 | ret = io_nop(req, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5547 | break; |
| 5548 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5549 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5550 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5551 | if (sqe) { |
| 5552 | ret = io_read_prep(req, sqe, force_nonblock); |
| 5553 | if (ret < 0) |
| 5554 | break; |
| 5555 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5556 | ret = io_read(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5557 | break; |
| 5558 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5559 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5560 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5561 | if (sqe) { |
| 5562 | ret = io_write_prep(req, sqe, force_nonblock); |
| 5563 | if (ret < 0) |
| 5564 | break; |
| 5565 | } |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 5566 | ret = io_write(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5567 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5568 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5569 | if (sqe) { |
| 5570 | ret = io_prep_fsync(req, sqe); |
| 5571 | if (ret < 0) |
| 5572 | break; |
| 5573 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5574 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5575 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5576 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5577 | if (sqe) { |
| 5578 | ret = io_poll_add_prep(req, sqe); |
| 5579 | if (ret) |
| 5580 | break; |
| 5581 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5582 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5583 | break; |
| 5584 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5585 | if (sqe) { |
| 5586 | ret = io_poll_remove_prep(req, sqe); |
| 5587 | if (ret < 0) |
| 5588 | break; |
| 5589 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5590 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5591 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5592 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5593 | if (sqe) { |
| 5594 | ret = io_prep_sfr(req, sqe); |
| 5595 | if (ret < 0) |
| 5596 | break; |
| 5597 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5598 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5599 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5600 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5601 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5602 | if (sqe) { |
| 5603 | ret = io_sendmsg_prep(req, sqe); |
| 5604 | if (ret < 0) |
| 5605 | break; |
| 5606 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5607 | if (req->opcode == IORING_OP_SENDMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5608 | ret = io_sendmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5609 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5610 | ret = io_send(req, force_nonblock, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5611 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5612 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5613 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5614 | if (sqe) { |
| 5615 | ret = io_recvmsg_prep(req, sqe); |
| 5616 | if (ret) |
| 5617 | break; |
| 5618 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5619 | if (req->opcode == IORING_OP_RECVMSG) |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5620 | ret = io_recvmsg(req, force_nonblock, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5621 | else |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5622 | ret = io_recv(req, force_nonblock, cs); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5623 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5624 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5625 | if (sqe) { |
| 5626 | ret = io_timeout_prep(req, sqe, false); |
| 5627 | if (ret) |
| 5628 | break; |
| 5629 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5630 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5631 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5632 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5633 | if (sqe) { |
| 5634 | ret = io_timeout_remove_prep(req, sqe); |
| 5635 | if (ret) |
| 5636 | break; |
| 5637 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5638 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5639 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5640 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5641 | if (sqe) { |
| 5642 | ret = io_accept_prep(req, sqe); |
| 5643 | if (ret) |
| 5644 | break; |
| 5645 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5646 | ret = io_accept(req, force_nonblock, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5647 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5648 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5649 | if (sqe) { |
| 5650 | ret = io_connect_prep(req, sqe); |
| 5651 | if (ret) |
| 5652 | break; |
| 5653 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5654 | ret = io_connect(req, force_nonblock, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5655 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5656 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5657 | if (sqe) { |
| 5658 | ret = io_async_cancel_prep(req, sqe); |
| 5659 | if (ret) |
| 5660 | break; |
| 5661 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5662 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5663 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5664 | case IORING_OP_FALLOCATE: |
| 5665 | if (sqe) { |
| 5666 | ret = io_fallocate_prep(req, sqe); |
| 5667 | if (ret) |
| 5668 | break; |
| 5669 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5670 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5671 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5672 | case IORING_OP_OPENAT: |
| 5673 | if (sqe) { |
| 5674 | ret = io_openat_prep(req, sqe); |
| 5675 | if (ret) |
| 5676 | break; |
| 5677 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5678 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5679 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5680 | case IORING_OP_CLOSE: |
| 5681 | if (sqe) { |
| 5682 | ret = io_close_prep(req, sqe); |
| 5683 | if (ret) |
| 5684 | break; |
| 5685 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5686 | ret = io_close(req, force_nonblock, cs); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5687 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5688 | case IORING_OP_FILES_UPDATE: |
| 5689 | if (sqe) { |
| 5690 | ret = io_files_update_prep(req, sqe); |
| 5691 | if (ret) |
| 5692 | break; |
| 5693 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5694 | ret = io_files_update(req, force_nonblock, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5695 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5696 | case IORING_OP_STATX: |
| 5697 | if (sqe) { |
| 5698 | ret = io_statx_prep(req, sqe); |
| 5699 | if (ret) |
| 5700 | break; |
| 5701 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5702 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5703 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5704 | case IORING_OP_FADVISE: |
| 5705 | if (sqe) { |
| 5706 | ret = io_fadvise_prep(req, sqe); |
| 5707 | if (ret) |
| 5708 | break; |
| 5709 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5710 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5711 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5712 | case IORING_OP_MADVISE: |
| 5713 | if (sqe) { |
| 5714 | ret = io_madvise_prep(req, sqe); |
| 5715 | if (ret) |
| 5716 | break; |
| 5717 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5718 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5719 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5720 | case IORING_OP_OPENAT2: |
| 5721 | if (sqe) { |
| 5722 | ret = io_openat2_prep(req, sqe); |
| 5723 | if (ret) |
| 5724 | break; |
| 5725 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5726 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5727 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5728 | case IORING_OP_EPOLL_CTL: |
| 5729 | if (sqe) { |
| 5730 | ret = io_epoll_ctl_prep(req, sqe); |
| 5731 | if (ret) |
| 5732 | break; |
| 5733 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5734 | ret = io_epoll_ctl(req, force_nonblock, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5735 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5736 | case IORING_OP_SPLICE: |
| 5737 | if (sqe) { |
| 5738 | ret = io_splice_prep(req, sqe); |
| 5739 | if (ret < 0) |
| 5740 | break; |
| 5741 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5742 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5743 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5744 | case IORING_OP_PROVIDE_BUFFERS: |
| 5745 | if (sqe) { |
| 5746 | ret = io_provide_buffers_prep(req, sqe); |
| 5747 | if (ret) |
| 5748 | break; |
| 5749 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5750 | ret = io_provide_buffers(req, force_nonblock, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5751 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5752 | case IORING_OP_REMOVE_BUFFERS: |
| 5753 | if (sqe) { |
| 5754 | ret = io_remove_buffers_prep(req, sqe); |
| 5755 | if (ret) |
| 5756 | break; |
| 5757 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5758 | ret = io_remove_buffers(req, force_nonblock, cs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5759 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5760 | case IORING_OP_TEE: |
| 5761 | if (sqe) { |
| 5762 | ret = io_tee_prep(req, sqe); |
| 5763 | if (ret < 0) |
| 5764 | break; |
| 5765 | } |
| 5766 | ret = io_tee(req, force_nonblock); |
| 5767 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5768 | default: |
| 5769 | ret = -EINVAL; |
| 5770 | break; |
| 5771 | } |
| 5772 | |
| 5773 | if (ret) |
| 5774 | return ret; |
| 5775 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 5776 | /* If the op doesn't have a file, we're not polling for it */ |
| 5777 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5778 | const bool in_async = io_wq_current_is_worker(); |
| 5779 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5780 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 5781 | if (in_async) |
| 5782 | mutex_lock(&ctx->uring_lock); |
| 5783 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5784 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5785 | |
| 5786 | if (in_async) |
| 5787 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | return 0; |
| 5791 | } |
| 5792 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 5793 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5794 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5795 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 5796 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5797 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5798 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 5799 | timeout = io_prep_linked_timeout(req); |
| 5800 | if (timeout) |
| 5801 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 5802 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5803 | /* if NO_CANCEL is set, we must still run the work */ |
| 5804 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 5805 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5806 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5807 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5808 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5809 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5810 | do { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 5811 | ret = io_issue_sqe(req, NULL, false, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5812 | /* |
| 5813 | * We can get EAGAIN for polled IO even though we're |
| 5814 | * forcing a sync submission from here, since we can't |
| 5815 | * wait for request slots on the block side. |
| 5816 | */ |
| 5817 | if (ret != -EAGAIN) |
| 5818 | break; |
| 5819 | cond_resched(); |
| 5820 | } while (1); |
| 5821 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5822 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5823 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5824 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5825 | io_req_complete(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5826 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5827 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 5828 | return io_steal_work(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5829 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5830 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5831 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 5832 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5833 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5834 | struct fixed_file_table *table; |
| 5835 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5836 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 5837 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5838 | } |
| 5839 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5840 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 5841 | int fd, struct file **out_file, bool fixed) |
| 5842 | { |
| 5843 | struct io_ring_ctx *ctx = req->ctx; |
| 5844 | struct file *file; |
| 5845 | |
| 5846 | if (fixed) { |
| 5847 | if (unlikely(!ctx->file_data || |
| 5848 | (unsigned) fd >= ctx->nr_user_files)) |
| 5849 | return -EBADF; |
| 5850 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 5851 | file = io_file_from_index(ctx, fd); |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5852 | if (file) { |
| 5853 | req->fixed_file_refs = ctx->file_data->cur_refs; |
| 5854 | percpu_ref_get(req->fixed_file_refs); |
| 5855 | } |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5856 | } else { |
| 5857 | trace_io_uring_file_get(ctx, fd); |
| 5858 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5859 | } |
| 5860 | |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 5861 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
| 5862 | *out_file = file; |
| 5863 | return 0; |
| 5864 | } |
| 5865 | return -EBADF; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5866 | } |
| 5867 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5868 | 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] | 5869 | int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5870 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5871 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5872 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 5873 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 5874 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5875 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5876 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5877 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5878 | } |
| 5879 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5880 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5881 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5882 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5883 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5884 | |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 5885 | io_req_init_async(req); |
| 5886 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 5887 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5888 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5889 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5890 | return -EBADF; |
| 5891 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5892 | rcu_read_lock(); |
| 5893 | spin_lock_irq(&ctx->inflight_lock); |
| 5894 | /* |
| 5895 | * We use the f_ops->flush() handler to ensure that we can flush |
| 5896 | * out work accessing these files if the fd is closed. Check if |
| 5897 | * the fd has changed since we started down this path, and disallow |
| 5898 | * this operation if it has. |
| 5899 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5900 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5901 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 5902 | req->flags |= REQ_F_INFLIGHT; |
| 5903 | req->work.files = current->files; |
| 5904 | ret = 0; |
| 5905 | } |
| 5906 | spin_unlock_irq(&ctx->inflight_lock); |
| 5907 | rcu_read_unlock(); |
| 5908 | |
| 5909 | return ret; |
| 5910 | } |
| 5911 | |
Pavel Begunkov | f56040b | 2020-07-23 20:25:21 +0300 | [diff] [blame] | 5912 | static inline int io_prep_work_files(struct io_kiocb *req) |
| 5913 | { |
| 5914 | if (!io_op_defs[req->opcode].file_table) |
| 5915 | return 0; |
| 5916 | return io_grab_files(req); |
| 5917 | } |
| 5918 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5919 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 5920 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5921 | struct io_timeout_data *data = container_of(timer, |
| 5922 | struct io_timeout_data, timer); |
| 5923 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5924 | struct io_ring_ctx *ctx = req->ctx; |
| 5925 | struct io_kiocb *prev = NULL; |
| 5926 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5927 | |
| 5928 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5929 | |
| 5930 | /* |
| 5931 | * We don't expect the list to be empty, that will only happen if we |
| 5932 | * race with the completion of the linked work. |
| 5933 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5934 | if (!list_empty(&req->link_list)) { |
| 5935 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 5936 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5937 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5938 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5939 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 5940 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5941 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
| 5944 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5945 | |
| 5946 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5947 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5948 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5949 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5950 | } else { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5951 | io_req_complete(req, -ETIME); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5952 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5953 | return HRTIMER_NORESTART; |
| 5954 | } |
| 5955 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 5956 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5957 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5958 | /* |
| 5959 | * If the list is now empty, then our linked request finished before |
| 5960 | * we got a chance to setup the timer |
| 5961 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5962 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5963 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5964 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5965 | data->timer.function = io_link_timeout_fn; |
| 5966 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 5967 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5968 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 5972 | { |
| 5973 | struct io_ring_ctx *ctx = req->ctx; |
| 5974 | |
| 5975 | spin_lock_irq(&ctx->completion_lock); |
| 5976 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5977 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5978 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5979 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5980 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5981 | } |
| 5982 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5983 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5984 | { |
| 5985 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5986 | |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 5987 | if (!(req->flags & REQ_F_LINK_HEAD)) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5988 | return NULL; |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 5989 | if (req->flags & REQ_F_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5990 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5991 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5992 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 5993 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5994 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5995 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5996 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5997 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5998 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5999 | } |
| 6000 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6001 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6002 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6003 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6004 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6005 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6006 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6007 | int ret; |
| 6008 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6009 | again: |
| 6010 | linked_timeout = io_prep_linked_timeout(req); |
| 6011 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6012 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
| 6013 | req->work.creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6014 | if (old_creds) |
| 6015 | revert_creds(old_creds); |
| 6016 | if (old_creds == req->work.creds) |
| 6017 | old_creds = NULL; /* restored original creds */ |
| 6018 | else |
| 6019 | old_creds = override_creds(req->work.creds); |
| 6020 | } |
| 6021 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6022 | ret = io_issue_sqe(req, sqe, true, cs); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6023 | |
| 6024 | /* |
| 6025 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6026 | * doesn't support non-blocking read/write attempts |
| 6027 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6028 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6029 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 6030 | punt: |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6031 | ret = io_prep_work_files(req); |
| 6032 | if (unlikely(ret)) |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6033 | goto err; |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6034 | /* |
| 6035 | * Queued up for async execution, worker will release |
| 6036 | * submit reference when the iocb is actually submitted. |
| 6037 | */ |
| 6038 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6039 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6040 | |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6041 | if (linked_timeout) |
| 6042 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6043 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6044 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6045 | |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6046 | if (unlikely(ret)) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6047 | err: |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6048 | /* un-prep timeout, so it'll be killed as any other linked */ |
| 6049 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6050 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6051 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6052 | io_req_complete(req, ret); |
| 6053 | goto exit; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6054 | } |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6055 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6056 | /* drop submission reference */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 6057 | nxt = io_put_req_find_next(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6058 | if (linked_timeout) |
| 6059 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6060 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6061 | if (nxt) { |
| 6062 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 6063 | |
| 6064 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 6065 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6066 | goto again; |
| 6067 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 6068 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6069 | if (old_creds) |
| 6070 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6071 | } |
| 6072 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6073 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6074 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6075 | { |
| 6076 | int ret; |
| 6077 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6078 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6079 | if (ret) { |
| 6080 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6081 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6082 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6083 | io_put_req(req); |
| 6084 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6085 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6086 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6087 | if (!req->io) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6088 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6089 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6090 | goto fail_req; |
| 6091 | } |
| 6092 | |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6093 | /* |
| 6094 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 6095 | * to async execution. |
| 6096 | */ |
Pavel Begunkov | 3e863ea | 2020-07-23 20:17:20 +0300 | [diff] [blame] | 6097 | io_req_init_async(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6098 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 6099 | io_queue_async_work(req); |
| 6100 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6101 | __io_queue_sqe(req, sqe, cs); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6102 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6103 | } |
| 6104 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6105 | static inline void io_queue_link_head(struct io_kiocb *req, |
| 6106 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6107 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6108 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6109 | io_put_req(req); |
| 6110 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6111 | } else |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6112 | io_queue_sqe(req, NULL, cs); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6113 | } |
| 6114 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6115 | 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] | 6116 | struct io_kiocb **link, struct io_comp_state *cs) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6117 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6118 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6119 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6120 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6121 | /* |
| 6122 | * If we already have a head request, queue this one for async |
| 6123 | * submittal once the head completes. If we don't have a head but |
| 6124 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6125 | * submitted sync once the chain is complete. If none of those |
| 6126 | * conditions are true (normal request), then just queue it. |
| 6127 | */ |
| 6128 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6129 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6130 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6131 | /* |
| 6132 | * Taking sequential execution of a link, draining both sides |
| 6133 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6134 | * requests in the link. So, it drains the head and the |
| 6135 | * next after the link request. The last one is done via |
| 6136 | * drain_next flag to persist the effect across calls. |
| 6137 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6138 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6139 | head->flags |= REQ_F_IO_DRAIN; |
| 6140 | ctx->drain_next = 1; |
| 6141 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6142 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6143 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6144 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6145 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6146 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6147 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6148 | trace_io_uring_link(ctx, req, head); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 6149 | io_get_req_task(req); |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6150 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6151 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6152 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6153 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6154 | io_queue_link_head(head, cs); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6155 | *link = NULL; |
| 6156 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6157 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6158 | if (unlikely(ctx->drain_next)) { |
| 6159 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6160 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6161 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6162 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | dea3b49 | 2020-04-12 02:05:04 +0300 | [diff] [blame] | 6163 | req->flags |= REQ_F_LINK_HEAD; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6164 | INIT_LIST_HEAD(&req->link_list); |
Pavel Begunkov | f1d96a8 | 2020-03-13 22:29:14 +0300 | [diff] [blame] | 6165 | |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6166 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6167 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6168 | req->flags |= REQ_F_FAIL_LINK; |
| 6169 | *link = req; |
| 6170 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6171 | io_queue_sqe(req, sqe, cs); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6172 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6173 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6174 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6175 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6176 | } |
| 6177 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6178 | /* |
| 6179 | * Batched submission is done, ensure local IO is flushed out. |
| 6180 | */ |
| 6181 | static void io_submit_state_end(struct io_submit_state *state) |
| 6182 | { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6183 | if (!list_empty(&state->comp.list)) |
| 6184 | io_submit_flush_completions(&state->comp); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6185 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6186 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6187 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6188 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | /* |
| 6192 | * Start submission side cache. |
| 6193 | */ |
| 6194 | static void io_submit_state_start(struct io_submit_state *state, |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6195 | struct io_ring_ctx *ctx, unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6196 | { |
| 6197 | blk_start_plug(&state->plug); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 6198 | #ifdef CONFIG_BLOCK |
| 6199 | state->plug.nowait = true; |
| 6200 | #endif |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6201 | state->comp.nr = 0; |
| 6202 | INIT_LIST_HEAD(&state->comp.list); |
| 6203 | state->comp.ctx = ctx; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6204 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6205 | state->file = NULL; |
| 6206 | state->ios_left = max_ios; |
| 6207 | } |
| 6208 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6209 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6210 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6211 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6212 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6213 | /* |
| 6214 | * Ensure any loads from the SQEs are done at this point, |
| 6215 | * since once we write the new head, the application could |
| 6216 | * write new data to them. |
| 6217 | */ |
| 6218 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6222 | * 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] | 6223 | * that is mapped by userspace. This means that care needs to be taken to |
| 6224 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6225 | * being a good citizen. If members of the sqe are validated and then later |
| 6226 | * used, it's important that those reads are done through READ_ONCE() to |
| 6227 | * prevent a re-load down the line. |
| 6228 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6229 | 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] | 6230 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6231 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6232 | unsigned head; |
| 6233 | |
| 6234 | /* |
| 6235 | * The cached sq head (or cq tail) serves two purposes: |
| 6236 | * |
| 6237 | * 1) allows us to batch the cost of updating the user visible |
| 6238 | * head updates. |
| 6239 | * 2) allows the kernel side to track the head on its own, even |
| 6240 | * though the application is the one updating it. |
| 6241 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6242 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6243 | if (likely(head < ctx->sq_entries)) |
| 6244 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6245 | |
| 6246 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6247 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6248 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6249 | return NULL; |
| 6250 | } |
| 6251 | |
| 6252 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6253 | { |
| 6254 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6255 | } |
| 6256 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6257 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6258 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6259 | IOSQE_BUFFER_SELECT) |
| 6260 | |
| 6261 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6262 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6263 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6264 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6265 | unsigned int sqe_flags; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6266 | int id; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6267 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6268 | req->opcode = READ_ONCE(sqe->opcode); |
| 6269 | req->user_data = READ_ONCE(sqe->user_data); |
| 6270 | req->io = NULL; |
| 6271 | req->file = NULL; |
| 6272 | req->ctx = ctx; |
| 6273 | req->flags = 0; |
| 6274 | /* one is dropped after submission, the other at completion */ |
| 6275 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6276 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6277 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6278 | |
| 6279 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6280 | return -EINVAL; |
| 6281 | |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6282 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
| 6283 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6284 | |
| 6285 | sqe_flags = READ_ONCE(sqe->flags); |
| 6286 | /* enforce forwards compatibility on users */ |
| 6287 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6288 | return -EINVAL; |
| 6289 | |
| 6290 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6291 | !io_op_defs[req->opcode].buffer_select) |
| 6292 | return -EOPNOTSUPP; |
| 6293 | |
| 6294 | id = READ_ONCE(sqe->personality); |
| 6295 | if (id) { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 6296 | io_req_init_async(req); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6297 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 6298 | if (unlikely(!req->work.creds)) |
| 6299 | return -EINVAL; |
| 6300 | get_cred(req->work.creds); |
| 6301 | } |
| 6302 | |
| 6303 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6304 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6305 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6306 | if (!io_op_defs[req->opcode].needs_file) |
| 6307 | return 0; |
| 6308 | |
| 6309 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6310 | } |
| 6311 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6312 | 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] | 6313 | struct file *ring_file, int ring_fd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6314 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6315 | struct io_submit_state state; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6316 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6317 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6318 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6319 | /* 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] | 6320 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 6321 | if (!list_empty(&ctx->cq_overflow_list) && |
| 6322 | !io_cqring_overflow_flush(ctx, false)) |
| 6323 | return -EBUSY; |
| 6324 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6325 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6326 | /* make sure SQ entry isn't read before tail */ |
| 6327 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6328 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6329 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6330 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6331 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6332 | io_submit_state_start(&state, ctx, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6333 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6334 | ctx->ring_fd = ring_fd; |
| 6335 | ctx->ring_file = ring_file; |
| 6336 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6337 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6338 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6339 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6340 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6341 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6342 | sqe = io_get_sqe(ctx); |
| 6343 | if (unlikely(!sqe)) { |
| 6344 | io_consume_sqe(ctx); |
| 6345 | break; |
| 6346 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6347 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6348 | if (unlikely(!req)) { |
| 6349 | if (!submitted) |
| 6350 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6351 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6352 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6353 | |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6354 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6355 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6356 | /* will complete beyond this point, count as submitted */ |
| 6357 | submitted++; |
| 6358 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6359 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6360 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6361 | io_put_req(req); |
| 6362 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6363 | break; |
| 6364 | } |
| 6365 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6366 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6367 | true, io_async_submit(ctx)); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6368 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6369 | if (err) |
| 6370 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6371 | } |
| 6372 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6373 | if (unlikely(submitted != nr)) { |
| 6374 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 6375 | |
| 6376 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 6377 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6378 | if (link) |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6379 | io_queue_link_head(link, &state.comp); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6380 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6381 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6382 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6383 | io_commit_sqring(ctx); |
| 6384 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6385 | return submitted; |
| 6386 | } |
| 6387 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6388 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6389 | { |
| 6390 | /* Tell userspace we may need a wakeup call */ |
| 6391 | spin_lock_irq(&ctx->completion_lock); |
| 6392 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6393 | spin_unlock_irq(&ctx->completion_lock); |
| 6394 | } |
| 6395 | |
| 6396 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6397 | { |
| 6398 | spin_lock_irq(&ctx->completion_lock); |
| 6399 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6400 | spin_unlock_irq(&ctx->completion_lock); |
| 6401 | } |
| 6402 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6403 | static int io_sq_thread(void *data) |
| 6404 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6405 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6406 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6407 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6408 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6409 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6410 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6411 | complete(&ctx->sq_thread_comp); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 6412 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6413 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6414 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6415 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6416 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6417 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6418 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6419 | if (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6420 | unsigned nr_events = 0; |
| 6421 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6422 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6423 | if (!list_empty(&ctx->iopoll_list) && !need_resched()) |
Pavel Begunkov | 9dedd56 | 2020-07-07 16:36:20 +0300 | [diff] [blame] | 6424 | io_do_iopoll(ctx, &nr_events, 0); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6425 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6426 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6427 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6428 | } |
| 6429 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6430 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6431 | |
| 6432 | /* |
| 6433 | * If submit got -EBUSY, flag us as needing the application |
| 6434 | * to enter the kernel to reap and flush events. |
| 6435 | */ |
Xuan Zhuo | b772f07 | 2020-06-23 19:34:06 +0800 | [diff] [blame] | 6436 | if (!to_submit || ret == -EBUSY || need_resched()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6437 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6438 | * Drop cur_mm before scheduling, we can't hold it for |
| 6439 | * long periods (or over schedule()). Do this before |
| 6440 | * adding ourselves to the waitqueue, as the unuse/drop |
| 6441 | * may sleep. |
| 6442 | */ |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 6443 | io_sq_thread_drop_mm(); |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 6444 | |
| 6445 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6446 | * We're polling. If we're within the defined idle |
| 6447 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6448 | * to sleep. The exception is if we got EBUSY doing |
| 6449 | * more IO, we should wait for the application to |
| 6450 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6451 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6452 | if (!list_empty(&ctx->iopoll_list) || need_resched() || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6453 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 6454 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6455 | io_run_task_work(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 6456 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6457 | continue; |
| 6458 | } |
| 6459 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6460 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 6461 | TASK_INTERRUPTIBLE); |
| 6462 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6463 | /* |
| 6464 | * While doing polled IO, before going to sleep, we need |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6465 | * to check if there are new reqs added to iopoll_list, |
| 6466 | * it is because reqs may have been punted to io worker |
| 6467 | * and will be added to iopoll_list later, hence check |
| 6468 | * the iopoll_list again. |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6469 | */ |
| 6470 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 6471 | !list_empty_careful(&ctx->iopoll_list)) { |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6472 | finish_wait(&ctx->sqo_wait, &wait); |
| 6473 | continue; |
| 6474 | } |
| 6475 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6476 | io_ring_set_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6477 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6478 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6479 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6480 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6481 | finish_wait(&ctx->sqo_wait, &wait); |
| 6482 | break; |
| 6483 | } |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6484 | if (io_run_task_work()) { |
Hillf Danton | 10bea96 | 2020-04-01 17:19:33 +0800 | [diff] [blame] | 6485 | finish_wait(&ctx->sqo_wait, &wait); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6486 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6487 | continue; |
| 6488 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6489 | if (signal_pending(current)) |
| 6490 | flush_signals(current); |
| 6491 | schedule(); |
| 6492 | finish_wait(&ctx->sqo_wait, &wait); |
| 6493 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6494 | io_ring_clear_wakeup_flag(ctx); |
Xiaoguang Wang | d4ae271 | 2020-05-20 21:24:35 +0800 | [diff] [blame] | 6495 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6496 | continue; |
| 6497 | } |
| 6498 | finish_wait(&ctx->sqo_wait, &wait); |
| 6499 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6500 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6501 | } |
| 6502 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6503 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 6b668c9 | 2020-05-20 15:35:03 +0800 | [diff] [blame] | 6504 | if (likely(!percpu_ref_is_dying(&ctx->refs))) |
| 6505 | ret = io_submit_sqes(ctx, to_submit, NULL, -1); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 6506 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6507 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6508 | } |
| 6509 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6510 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6511 | |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 6512 | io_sq_thread_drop_mm(); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6513 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6514 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6515 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6516 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6517 | return 0; |
| 6518 | } |
| 6519 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6520 | struct io_wait_queue { |
| 6521 | struct wait_queue_entry wq; |
| 6522 | struct io_ring_ctx *ctx; |
| 6523 | unsigned to_wait; |
| 6524 | unsigned nr_timeouts; |
| 6525 | }; |
| 6526 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6527 | 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] | 6528 | { |
| 6529 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6530 | |
| 6531 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6532 | * 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] | 6533 | * started waiting. For timeouts, we always want to return to userspace, |
| 6534 | * regardless of event count. |
| 6535 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6536 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6537 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6538 | } |
| 6539 | |
| 6540 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6541 | int wake_flags, void *key) |
| 6542 | { |
| 6543 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6544 | wq); |
| 6545 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6546 | /* use noflush == true, as we can't safely rely on locking context */ |
| 6547 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6548 | return -1; |
| 6549 | |
| 6550 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6551 | } |
| 6552 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6553 | /* |
| 6554 | * Wait until events become available, if we don't already have some. The |
| 6555 | * application must reap them itself, as they reside on the shared cq ring. |
| 6556 | */ |
| 6557 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 6558 | const sigset_t __user *sig, size_t sigsz) |
| 6559 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6560 | struct io_wait_queue iowq = { |
| 6561 | .wq = { |
| 6562 | .private = current, |
| 6563 | .func = io_wake_function, |
| 6564 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6565 | }, |
| 6566 | .ctx = ctx, |
| 6567 | .to_wait = min_events, |
| 6568 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6569 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6570 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6571 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6572 | do { |
| 6573 | if (io_cqring_events(ctx, false) >= min_events) |
| 6574 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6575 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6576 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6577 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6578 | |
| 6579 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6580 | #ifdef CONFIG_COMPAT |
| 6581 | if (in_compat_syscall()) |
| 6582 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6583 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6584 | else |
| 6585 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6586 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6587 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6588 | if (ret) |
| 6589 | return ret; |
| 6590 | } |
| 6591 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6592 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6593 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6594 | do { |
| 6595 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6596 | TASK_INTERRUPTIBLE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6597 | /* make sure we run task_work before checking for signals */ |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6598 | if (io_run_task_work()) |
| 6599 | continue; |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6600 | if (signal_pending(current)) { |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6601 | if (current->jobctl & JOBCTL_TASK_WORK) { |
| 6602 | spin_lock_irq(¤t->sighand->siglock); |
| 6603 | current->jobctl &= ~JOBCTL_TASK_WORK; |
| 6604 | recalc_sigpending(); |
| 6605 | spin_unlock_irq(¤t->sighand->siglock); |
| 6606 | continue; |
| 6607 | } |
| 6608 | ret = -EINTR; |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 6609 | break; |
| 6610 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6611 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6612 | break; |
| 6613 | schedule(); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6614 | } while (1); |
| 6615 | finish_wait(&ctx->wait, &iowq.wq); |
| 6616 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6617 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6618 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6619 | 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] | 6620 | } |
| 6621 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6622 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6623 | { |
| 6624 | #if defined(CONFIG_UNIX) |
| 6625 | if (ctx->ring_sock) { |
| 6626 | struct sock *sock = ctx->ring_sock->sk; |
| 6627 | struct sk_buff *skb; |
| 6628 | |
| 6629 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6630 | kfree_skb(skb); |
| 6631 | } |
| 6632 | #else |
| 6633 | int i; |
| 6634 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6635 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6636 | struct file *file; |
| 6637 | |
| 6638 | file = io_file_from_index(ctx, i); |
| 6639 | if (file) |
| 6640 | fput(file); |
| 6641 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6642 | #endif |
| 6643 | } |
| 6644 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6645 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 6646 | { |
| 6647 | struct fixed_file_data *data; |
| 6648 | |
| 6649 | data = container_of(ref, struct fixed_file_data, refs); |
| 6650 | complete(&data->done); |
| 6651 | } |
| 6652 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6653 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6654 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6655 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6656 | struct fixed_file_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6657 | unsigned nr_tables, i; |
| 6658 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6659 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6660 | return -ENXIO; |
| 6661 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6662 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6663 | if (!list_empty(&data->ref_list)) |
| 6664 | ref_node = list_first_entry(&data->ref_list, |
| 6665 | struct fixed_file_ref_node, node); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6666 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6667 | if (ref_node) |
| 6668 | percpu_ref_kill(&ref_node->refs); |
| 6669 | |
| 6670 | percpu_ref_kill(&data->refs); |
| 6671 | |
| 6672 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6673 | flush_delayed_work(&ctx->file_put_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6674 | wait_for_completion(&data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6675 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6676 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6677 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 6678 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6679 | kfree(data->table[i].files); |
| 6680 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6681 | percpu_ref_exit(&data->refs); |
| 6682 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6683 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6684 | ctx->nr_user_files = 0; |
| 6685 | return 0; |
| 6686 | } |
| 6687 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6688 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 6689 | { |
| 6690 | if (ctx->sqo_thread) { |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 6691 | wait_for_completion(&ctx->sq_thread_comp); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6692 | /* |
| 6693 | * The park is a bit of a work-around, without it we get |
| 6694 | * warning spews on shutdown with SQPOLL set and affinity |
| 6695 | * set to a single CPU. |
| 6696 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6697 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6698 | kthread_stop(ctx->sqo_thread); |
| 6699 | ctx->sqo_thread = NULL; |
| 6700 | } |
| 6701 | } |
| 6702 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6703 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 6704 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6705 | io_sq_thread_stop(ctx); |
| 6706 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6707 | if (ctx->io_wq) { |
| 6708 | io_wq_destroy(ctx->io_wq); |
| 6709 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6710 | } |
| 6711 | } |
| 6712 | |
| 6713 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6714 | /* |
| 6715 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 6716 | * the io_uring can be safely unregistered on process exit, even if we have |
| 6717 | * loops in the file referencing. |
| 6718 | */ |
| 6719 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 6720 | { |
| 6721 | struct sock *sk = ctx->ring_sock->sk; |
| 6722 | struct scm_fp_list *fpl; |
| 6723 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6724 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6725 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6726 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 6727 | if (!fpl) |
| 6728 | return -ENOMEM; |
| 6729 | |
| 6730 | skb = alloc_skb(0, GFP_KERNEL); |
| 6731 | if (!skb) { |
| 6732 | kfree(fpl); |
| 6733 | return -ENOMEM; |
| 6734 | } |
| 6735 | |
| 6736 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6737 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6738 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6739 | fpl->user = get_uid(ctx->user); |
| 6740 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6741 | struct file *file = io_file_from_index(ctx, i + offset); |
| 6742 | |
| 6743 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6744 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6745 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6746 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 6747 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6748 | } |
| 6749 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6750 | if (nr_files) { |
| 6751 | fpl->max = SCM_MAX_FD; |
| 6752 | fpl->count = nr_files; |
| 6753 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6754 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6755 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 6756 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6757 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6758 | for (i = 0; i < nr_files; i++) |
| 6759 | fput(fpl->fp[i]); |
| 6760 | } else { |
| 6761 | kfree_skb(skb); |
| 6762 | kfree(fpl); |
| 6763 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6764 | |
| 6765 | return 0; |
| 6766 | } |
| 6767 | |
| 6768 | /* |
| 6769 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 6770 | * causes regular reference counting to break down. We rely on the UNIX |
| 6771 | * garbage collection to take care of this problem for us. |
| 6772 | */ |
| 6773 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6774 | { |
| 6775 | unsigned left, total; |
| 6776 | int ret = 0; |
| 6777 | |
| 6778 | total = 0; |
| 6779 | left = ctx->nr_user_files; |
| 6780 | while (left) { |
| 6781 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6782 | |
| 6783 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 6784 | if (ret) |
| 6785 | break; |
| 6786 | left -= this_files; |
| 6787 | total += this_files; |
| 6788 | } |
| 6789 | |
| 6790 | if (!ret) |
| 6791 | return 0; |
| 6792 | |
| 6793 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6794 | struct file *file = io_file_from_index(ctx, total); |
| 6795 | |
| 6796 | if (file) |
| 6797 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6798 | total++; |
| 6799 | } |
| 6800 | |
| 6801 | return ret; |
| 6802 | } |
| 6803 | #else |
| 6804 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6805 | { |
| 6806 | return 0; |
| 6807 | } |
| 6808 | #endif |
| 6809 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6810 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 6811 | unsigned nr_files) |
| 6812 | { |
| 6813 | int i; |
| 6814 | |
| 6815 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6816 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6817 | unsigned this_files; |
| 6818 | |
| 6819 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 6820 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 6821 | GFP_KERNEL); |
| 6822 | if (!table->files) |
| 6823 | break; |
| 6824 | nr_files -= this_files; |
| 6825 | } |
| 6826 | |
| 6827 | if (i == nr_tables) |
| 6828 | return 0; |
| 6829 | |
| 6830 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6831 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6832 | kfree(table->files); |
| 6833 | } |
| 6834 | return 1; |
| 6835 | } |
| 6836 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6837 | 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] | 6838 | { |
| 6839 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6840 | struct sock *sock = ctx->ring_sock->sk; |
| 6841 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 6842 | struct sk_buff *skb; |
| 6843 | int i; |
| 6844 | |
| 6845 | __skb_queue_head_init(&list); |
| 6846 | |
| 6847 | /* |
| 6848 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 6849 | * remove this entry and rearrange the file array. |
| 6850 | */ |
| 6851 | skb = skb_dequeue(head); |
| 6852 | while (skb) { |
| 6853 | struct scm_fp_list *fp; |
| 6854 | |
| 6855 | fp = UNIXCB(skb).fp; |
| 6856 | for (i = 0; i < fp->count; i++) { |
| 6857 | int left; |
| 6858 | |
| 6859 | if (fp->fp[i] != file) |
| 6860 | continue; |
| 6861 | |
| 6862 | unix_notinflight(fp->user, fp->fp[i]); |
| 6863 | left = fp->count - 1 - i; |
| 6864 | if (left) { |
| 6865 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 6866 | left * sizeof(struct file *)); |
| 6867 | } |
| 6868 | fp->count--; |
| 6869 | if (!fp->count) { |
| 6870 | kfree_skb(skb); |
| 6871 | skb = NULL; |
| 6872 | } else { |
| 6873 | __skb_queue_tail(&list, skb); |
| 6874 | } |
| 6875 | fput(file); |
| 6876 | file = NULL; |
| 6877 | break; |
| 6878 | } |
| 6879 | |
| 6880 | if (!file) |
| 6881 | break; |
| 6882 | |
| 6883 | __skb_queue_tail(&list, skb); |
| 6884 | |
| 6885 | skb = skb_dequeue(head); |
| 6886 | } |
| 6887 | |
| 6888 | if (skb_peek(&list)) { |
| 6889 | spin_lock_irq(&head->lock); |
| 6890 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 6891 | __skb_queue_tail(head, skb); |
| 6892 | spin_unlock_irq(&head->lock); |
| 6893 | } |
| 6894 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6895 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6896 | #endif |
| 6897 | } |
| 6898 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6899 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6900 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6901 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6902 | }; |
| 6903 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6904 | 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] | 6905 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6906 | struct fixed_file_data *file_data = ref_node->file_data; |
| 6907 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6908 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6909 | |
| 6910 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6911 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6912 | io_ring_file_put(ctx, pfile->file); |
| 6913 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6914 | } |
| 6915 | |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 6916 | spin_lock(&file_data->lock); |
| 6917 | list_del(&ref_node->node); |
| 6918 | spin_unlock(&file_data->lock); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6919 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6920 | percpu_ref_exit(&ref_node->refs); |
| 6921 | kfree(ref_node); |
| 6922 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6923 | } |
| 6924 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6925 | static void io_file_put_work(struct work_struct *work) |
| 6926 | { |
| 6927 | struct io_ring_ctx *ctx; |
| 6928 | struct llist_node *node; |
| 6929 | |
| 6930 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 6931 | node = llist_del_all(&ctx->file_put_llist); |
| 6932 | |
| 6933 | while (node) { |
| 6934 | struct fixed_file_ref_node *ref_node; |
| 6935 | struct llist_node *next = node->next; |
| 6936 | |
| 6937 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 6938 | __io_file_put_work(ref_node); |
| 6939 | node = next; |
| 6940 | } |
| 6941 | } |
| 6942 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6943 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 6944 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6945 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6946 | struct io_ring_ctx *ctx; |
| 6947 | bool first_add; |
| 6948 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6949 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6950 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6951 | ctx = ref_node->file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6952 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 6953 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
| 6954 | delay = 0; |
| 6955 | |
| 6956 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 6957 | if (!delay) |
| 6958 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 6959 | else if (first_add) |
| 6960 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 6964 | struct io_ring_ctx *ctx) |
| 6965 | { |
| 6966 | struct fixed_file_ref_node *ref_node; |
| 6967 | |
| 6968 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 6969 | if (!ref_node) |
| 6970 | return ERR_PTR(-ENOMEM); |
| 6971 | |
| 6972 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 6973 | 0, GFP_KERNEL)) { |
| 6974 | kfree(ref_node); |
| 6975 | return ERR_PTR(-ENOMEM); |
| 6976 | } |
| 6977 | INIT_LIST_HEAD(&ref_node->node); |
| 6978 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6979 | ref_node->file_data = ctx->file_data; |
| 6980 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6981 | } |
| 6982 | |
| 6983 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 6984 | { |
| 6985 | percpu_ref_exit(&ref_node->refs); |
| 6986 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6990 | unsigned nr_args) |
| 6991 | { |
| 6992 | __s32 __user *fds = (__s32 __user *) arg; |
| 6993 | unsigned nr_tables; |
| 6994 | struct file *file; |
| 6995 | int fd, ret = 0; |
| 6996 | unsigned i; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6997 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6998 | |
| 6999 | if (ctx->file_data) |
| 7000 | return -EBUSY; |
| 7001 | if (!nr_args) |
| 7002 | return -EINVAL; |
| 7003 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7004 | return -EMFILE; |
| 7005 | |
| 7006 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 7007 | if (!ctx->file_data) |
| 7008 | return -ENOMEM; |
| 7009 | ctx->file_data->ctx = ctx; |
| 7010 | init_completion(&ctx->file_data->done); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7011 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
Xiaoguang Wang | f7fe934 | 2020-04-07 20:02:31 +0800 | [diff] [blame] | 7012 | spin_lock_init(&ctx->file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7013 | |
| 7014 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 7015 | ctx->file_data->table = kcalloc(nr_tables, |
| 7016 | sizeof(struct fixed_file_table), |
| 7017 | GFP_KERNEL); |
| 7018 | if (!ctx->file_data->table) { |
| 7019 | kfree(ctx->file_data); |
| 7020 | ctx->file_data = NULL; |
| 7021 | return -ENOMEM; |
| 7022 | } |
| 7023 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7024 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7025 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7026 | kfree(ctx->file_data->table); |
| 7027 | kfree(ctx->file_data); |
| 7028 | ctx->file_data = NULL; |
| 7029 | return -ENOMEM; |
| 7030 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7031 | |
| 7032 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 7033 | percpu_ref_exit(&ctx->file_data->refs); |
| 7034 | kfree(ctx->file_data->table); |
| 7035 | kfree(ctx->file_data); |
| 7036 | ctx->file_data = NULL; |
| 7037 | return -ENOMEM; |
| 7038 | } |
| 7039 | |
| 7040 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 7041 | struct fixed_file_table *table; |
| 7042 | unsigned index; |
| 7043 | |
| 7044 | ret = -EFAULT; |
| 7045 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 7046 | break; |
| 7047 | /* allow sparse sets */ |
| 7048 | if (fd == -1) { |
| 7049 | ret = 0; |
| 7050 | continue; |
| 7051 | } |
| 7052 | |
| 7053 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7054 | index = i & IORING_FILE_TABLE_MASK; |
| 7055 | file = fget(fd); |
| 7056 | |
| 7057 | ret = -EBADF; |
| 7058 | if (!file) |
| 7059 | break; |
| 7060 | |
| 7061 | /* |
| 7062 | * Don't allow io_uring instances to be registered. If UNIX |
| 7063 | * isn't enabled, then this causes a reference cycle and this |
| 7064 | * instance can never get freed. If UNIX is enabled we'll |
| 7065 | * handle it just fine, but there's still no point in allowing |
| 7066 | * a ring fd as it doesn't support regular read/write anyway. |
| 7067 | */ |
| 7068 | if (file->f_op == &io_uring_fops) { |
| 7069 | fput(file); |
| 7070 | break; |
| 7071 | } |
| 7072 | ret = 0; |
| 7073 | table->files[index] = file; |
| 7074 | } |
| 7075 | |
| 7076 | if (ret) { |
| 7077 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7078 | file = io_file_from_index(ctx, i); |
| 7079 | if (file) |
| 7080 | fput(file); |
| 7081 | } |
| 7082 | for (i = 0; i < nr_tables; i++) |
| 7083 | kfree(ctx->file_data->table[i].files); |
| 7084 | |
Yang Yingliang | 667e57d | 2020-07-10 14:14:20 +0000 | [diff] [blame] | 7085 | percpu_ref_exit(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7086 | kfree(ctx->file_data->table); |
| 7087 | kfree(ctx->file_data); |
| 7088 | ctx->file_data = NULL; |
| 7089 | ctx->nr_user_files = 0; |
| 7090 | return ret; |
| 7091 | } |
| 7092 | |
| 7093 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7094 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7095 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7096 | return ret; |
| 7097 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7098 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7099 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7100 | if (IS_ERR(ref_node)) { |
| 7101 | io_sqe_files_unregister(ctx); |
| 7102 | return PTR_ERR(ref_node); |
| 7103 | } |
| 7104 | |
| 7105 | ctx->file_data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7106 | spin_lock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7107 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7108 | spin_unlock(&ctx->file_data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7109 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7110 | return ret; |
| 7111 | } |
| 7112 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7113 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7114 | int index) |
| 7115 | { |
| 7116 | #if defined(CONFIG_UNIX) |
| 7117 | struct sock *sock = ctx->ring_sock->sk; |
| 7118 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7119 | struct sk_buff *skb; |
| 7120 | |
| 7121 | /* |
| 7122 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7123 | * file set. If there's no room, fall back to allocating a new skb |
| 7124 | * and filling it in. |
| 7125 | */ |
| 7126 | spin_lock_irq(&head->lock); |
| 7127 | skb = skb_peek(head); |
| 7128 | if (skb) { |
| 7129 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7130 | |
| 7131 | if (fpl->count < SCM_MAX_FD) { |
| 7132 | __skb_unlink(skb, head); |
| 7133 | spin_unlock_irq(&head->lock); |
| 7134 | fpl->fp[fpl->count] = get_file(file); |
| 7135 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7136 | fpl->count++; |
| 7137 | spin_lock_irq(&head->lock); |
| 7138 | __skb_queue_head(head, skb); |
| 7139 | } else { |
| 7140 | skb = NULL; |
| 7141 | } |
| 7142 | } |
| 7143 | spin_unlock_irq(&head->lock); |
| 7144 | |
| 7145 | if (skb) { |
| 7146 | fput(file); |
| 7147 | return 0; |
| 7148 | } |
| 7149 | |
| 7150 | return __io_sqe_files_scm(ctx, 1, index); |
| 7151 | #else |
| 7152 | return 0; |
| 7153 | #endif |
| 7154 | } |
| 7155 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7156 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7157 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7158 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7159 | struct io_file_put *pfile; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7160 | struct percpu_ref *refs = data->cur_refs; |
| 7161 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7162 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7163 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7164 | if (!pfile) |
| 7165 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7166 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7167 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7168 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7169 | list_add(&pfile->list, &ref_node->file_list); |
| 7170 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7171 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 7175 | struct io_uring_files_update *up, |
| 7176 | unsigned nr_args) |
| 7177 | { |
| 7178 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7179 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7180 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7181 | __s32 __user *fds; |
| 7182 | int fd, i, err; |
| 7183 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7184 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7185 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7186 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7187 | return -EOVERFLOW; |
| 7188 | if (done > ctx->nr_user_files) |
| 7189 | return -EINVAL; |
| 7190 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7191 | ref_node = alloc_fixed_file_ref_node(ctx); |
| 7192 | if (IS_ERR(ref_node)) |
| 7193 | return PTR_ERR(ref_node); |
| 7194 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7195 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7196 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7197 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7198 | struct fixed_file_table *table; |
| 7199 | unsigned index; |
| 7200 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7201 | err = 0; |
| 7202 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7203 | err = -EFAULT; |
| 7204 | break; |
| 7205 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7206 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7207 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7208 | index = i & IORING_FILE_TABLE_MASK; |
| 7209 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7210 | file = io_file_from_index(ctx, index); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7211 | err = io_queue_file_removal(data, file); |
| 7212 | if (err) |
| 7213 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7214 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7215 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7216 | } |
| 7217 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7218 | file = fget(fd); |
| 7219 | if (!file) { |
| 7220 | err = -EBADF; |
| 7221 | break; |
| 7222 | } |
| 7223 | /* |
| 7224 | * Don't allow io_uring instances to be registered. If |
| 7225 | * UNIX isn't enabled, then this causes a reference |
| 7226 | * cycle and this instance can never get freed. If UNIX |
| 7227 | * is enabled we'll handle it just fine, but there's |
| 7228 | * still no point in allowing a ring fd as it doesn't |
| 7229 | * support regular read/write anyway. |
| 7230 | */ |
| 7231 | if (file->f_op == &io_uring_fops) { |
| 7232 | fput(file); |
| 7233 | err = -EBADF; |
| 7234 | break; |
| 7235 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7236 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7237 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7238 | if (err) { |
| 7239 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7240 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7241 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7242 | } |
| 7243 | nr_args--; |
| 7244 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7245 | up->offset++; |
| 7246 | } |
| 7247 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7248 | if (needs_switch) { |
| 7249 | percpu_ref_kill(data->cur_refs); |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7250 | spin_lock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7251 | list_add(&ref_node->node, &data->ref_list); |
| 7252 | data->cur_refs = &ref_node->refs; |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7253 | spin_unlock(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7254 | percpu_ref_get(&ctx->file_data->refs); |
| 7255 | } else |
| 7256 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7257 | |
| 7258 | return done ? done : err; |
| 7259 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7260 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7261 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7262 | unsigned nr_args) |
| 7263 | { |
| 7264 | struct io_uring_files_update up; |
| 7265 | |
| 7266 | if (!ctx->file_data) |
| 7267 | return -ENXIO; |
| 7268 | if (!nr_args) |
| 7269 | return -EINVAL; |
| 7270 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7271 | return -EFAULT; |
| 7272 | if (up.resv) |
| 7273 | return -EINVAL; |
| 7274 | |
| 7275 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7276 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7277 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7278 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7279 | { |
| 7280 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7281 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7282 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7283 | io_put_req(req); |
| 7284 | } |
| 7285 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7286 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7287 | struct io_uring_params *p) |
| 7288 | { |
| 7289 | struct io_wq_data data; |
| 7290 | struct fd f; |
| 7291 | struct io_ring_ctx *ctx_attach; |
| 7292 | unsigned int concurrency; |
| 7293 | int ret = 0; |
| 7294 | |
| 7295 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7296 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7297 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7298 | |
| 7299 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 7300 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7301 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 7302 | |
| 7303 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 7304 | if (IS_ERR(ctx->io_wq)) { |
| 7305 | ret = PTR_ERR(ctx->io_wq); |
| 7306 | ctx->io_wq = NULL; |
| 7307 | } |
| 7308 | return ret; |
| 7309 | } |
| 7310 | |
| 7311 | f = fdget(p->wq_fd); |
| 7312 | if (!f.file) |
| 7313 | return -EBADF; |
| 7314 | |
| 7315 | if (f.file->f_op != &io_uring_fops) { |
| 7316 | ret = -EINVAL; |
| 7317 | goto out_fput; |
| 7318 | } |
| 7319 | |
| 7320 | ctx_attach = f.file->private_data; |
| 7321 | /* @io_wq is protected by holding the fd */ |
| 7322 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 7323 | ret = -EINVAL; |
| 7324 | goto out_fput; |
| 7325 | } |
| 7326 | |
| 7327 | ctx->io_wq = ctx_attach->io_wq; |
| 7328 | out_fput: |
| 7329 | fdput(f); |
| 7330 | return ret; |
| 7331 | } |
| 7332 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7333 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 7334 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7335 | { |
| 7336 | int ret; |
| 7337 | |
| 7338 | mmgrab(current->mm); |
| 7339 | ctx->sqo_mm = current->mm; |
| 7340 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7341 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7342 | ret = -EPERM; |
| 7343 | if (!capable(CAP_SYS_ADMIN)) |
| 7344 | goto err; |
| 7345 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7346 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7347 | if (!ctx->sq_thread_idle) |
| 7348 | ctx->sq_thread_idle = HZ; |
| 7349 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7350 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7351 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7352 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7353 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7354 | if (cpu >= nr_cpu_ids) |
| 7355 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7356 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7357 | goto err; |
| 7358 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7359 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 7360 | ctx, cpu, |
| 7361 | "io_uring-sq"); |
| 7362 | } else { |
| 7363 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 7364 | "io_uring-sq"); |
| 7365 | } |
| 7366 | if (IS_ERR(ctx->sqo_thread)) { |
| 7367 | ret = PTR_ERR(ctx->sqo_thread); |
| 7368 | ctx->sqo_thread = NULL; |
| 7369 | goto err; |
| 7370 | } |
| 7371 | wake_up_process(ctx->sqo_thread); |
| 7372 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7373 | /* Can't have SQ_AFF without SQPOLL */ |
| 7374 | ret = -EINVAL; |
| 7375 | goto err; |
| 7376 | } |
| 7377 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7378 | ret = io_init_wq_offload(ctx, p); |
| 7379 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7380 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7381 | |
| 7382 | return 0; |
| 7383 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 7384 | io_finish_async(ctx); |
Pavel Begunkov | 8eb06d7 | 2020-06-30 15:20:39 +0300 | [diff] [blame] | 7385 | if (ctx->sqo_mm) { |
| 7386 | mmdrop(ctx->sqo_mm); |
| 7387 | ctx->sqo_mm = NULL; |
| 7388 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7389 | return ret; |
| 7390 | } |
| 7391 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7392 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7393 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7394 | { |
| 7395 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7396 | } |
| 7397 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7398 | static inline int __io_account_mem(struct user_struct *user, |
| 7399 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7400 | { |
| 7401 | unsigned long page_limit, cur_pages, new_pages; |
| 7402 | |
| 7403 | /* Don't allow more pages than we can safely lock */ |
| 7404 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7405 | |
| 7406 | do { |
| 7407 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7408 | new_pages = cur_pages + nr_pages; |
| 7409 | if (new_pages > page_limit) |
| 7410 | return -ENOMEM; |
| 7411 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7412 | new_pages) != cur_pages); |
| 7413 | |
| 7414 | return 0; |
| 7415 | } |
| 7416 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7417 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7418 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7419 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 7420 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7421 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7422 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7423 | if (ctx->sqo_mm) { |
| 7424 | if (acct == ACCT_LOCKED) |
| 7425 | ctx->sqo_mm->locked_vm -= nr_pages; |
| 7426 | else if (acct == ACCT_PINNED) |
| 7427 | atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7428 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7429 | } |
| 7430 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7431 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 7432 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7433 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7434 | int ret; |
| 7435 | |
| 7436 | if (ctx->limit_mem) { |
| 7437 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7438 | if (ret) |
| 7439 | return ret; |
| 7440 | } |
| 7441 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7442 | if (ctx->sqo_mm) { |
| 7443 | if (acct == ACCT_LOCKED) |
| 7444 | ctx->sqo_mm->locked_vm += nr_pages; |
| 7445 | else if (acct == ACCT_PINNED) |
| 7446 | atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm); |
| 7447 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7448 | |
| 7449 | return 0; |
| 7450 | } |
| 7451 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7452 | static void io_mem_free(void *ptr) |
| 7453 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7454 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7455 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7456 | if (!ptr) |
| 7457 | return; |
| 7458 | |
| 7459 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7460 | if (put_page_testzero(page)) |
| 7461 | free_compound_page(page); |
| 7462 | } |
| 7463 | |
| 7464 | static void *io_mem_alloc(size_t size) |
| 7465 | { |
| 7466 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 7467 | __GFP_NORETRY; |
| 7468 | |
| 7469 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7470 | } |
| 7471 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7472 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7473 | size_t *sq_offset) |
| 7474 | { |
| 7475 | struct io_rings *rings; |
| 7476 | size_t off, sq_array_size; |
| 7477 | |
| 7478 | off = struct_size(rings, cqes, cq_entries); |
| 7479 | if (off == SIZE_MAX) |
| 7480 | return SIZE_MAX; |
| 7481 | |
| 7482 | #ifdef CONFIG_SMP |
| 7483 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 7484 | if (off == 0) |
| 7485 | return SIZE_MAX; |
| 7486 | #endif |
| 7487 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 7488 | if (sq_offset) |
| 7489 | *sq_offset = off; |
| 7490 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7491 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 7492 | if (sq_array_size == SIZE_MAX) |
| 7493 | return SIZE_MAX; |
| 7494 | |
| 7495 | if (check_add_overflow(off, sq_array_size, &off)) |
| 7496 | return SIZE_MAX; |
| 7497 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7498 | return off; |
| 7499 | } |
| 7500 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7501 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 7502 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7503 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7504 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7505 | pages = (size_t)1 << get_order( |
| 7506 | rings_size(sq_entries, cq_entries, NULL)); |
| 7507 | pages += (size_t)1 << get_order( |
| 7508 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7509 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7510 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7511 | } |
| 7512 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7513 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 7514 | { |
| 7515 | int i, j; |
| 7516 | |
| 7517 | if (!ctx->user_bufs) |
| 7518 | return -ENXIO; |
| 7519 | |
| 7520 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7521 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7522 | |
| 7523 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7524 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7525 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7526 | io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7527 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7528 | imu->nr_bvecs = 0; |
| 7529 | } |
| 7530 | |
| 7531 | kfree(ctx->user_bufs); |
| 7532 | ctx->user_bufs = NULL; |
| 7533 | ctx->nr_user_bufs = 0; |
| 7534 | return 0; |
| 7535 | } |
| 7536 | |
| 7537 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 7538 | void __user *arg, unsigned index) |
| 7539 | { |
| 7540 | struct iovec __user *src; |
| 7541 | |
| 7542 | #ifdef CONFIG_COMPAT |
| 7543 | if (ctx->compat) { |
| 7544 | struct compat_iovec __user *ciovs; |
| 7545 | struct compat_iovec ciov; |
| 7546 | |
| 7547 | ciovs = (struct compat_iovec __user *) arg; |
| 7548 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 7549 | return -EFAULT; |
| 7550 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 7551 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7552 | dst->iov_len = ciov.iov_len; |
| 7553 | return 0; |
| 7554 | } |
| 7555 | #endif |
| 7556 | src = (struct iovec __user *) arg; |
| 7557 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 7558 | return -EFAULT; |
| 7559 | return 0; |
| 7560 | } |
| 7561 | |
| 7562 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7563 | unsigned nr_args) |
| 7564 | { |
| 7565 | struct vm_area_struct **vmas = NULL; |
| 7566 | struct page **pages = NULL; |
| 7567 | int i, j, got_pages = 0; |
| 7568 | int ret = -EINVAL; |
| 7569 | |
| 7570 | if (ctx->user_bufs) |
| 7571 | return -EBUSY; |
| 7572 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 7573 | return -EINVAL; |
| 7574 | |
| 7575 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 7576 | GFP_KERNEL); |
| 7577 | if (!ctx->user_bufs) |
| 7578 | return -ENOMEM; |
| 7579 | |
| 7580 | for (i = 0; i < nr_args; i++) { |
| 7581 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 7582 | unsigned long off, start, end, ubuf; |
| 7583 | int pret, nr_pages; |
| 7584 | struct iovec iov; |
| 7585 | size_t size; |
| 7586 | |
| 7587 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 7588 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 7589 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7590 | |
| 7591 | /* |
| 7592 | * Don't impose further limits on the size and buffer |
| 7593 | * constraints here, we'll -EINVAL later when IO is |
| 7594 | * submitted if they are wrong. |
| 7595 | */ |
| 7596 | ret = -EFAULT; |
| 7597 | if (!iov.iov_base || !iov.iov_len) |
| 7598 | goto err; |
| 7599 | |
| 7600 | /* arbitrary limit, but we need something */ |
| 7601 | if (iov.iov_len > SZ_1G) |
| 7602 | goto err; |
| 7603 | |
| 7604 | ubuf = (unsigned long) iov.iov_base; |
| 7605 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 7606 | start = ubuf >> PAGE_SHIFT; |
| 7607 | nr_pages = end - start; |
| 7608 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7609 | ret = io_account_mem(ctx, nr_pages, ACCT_PINNED); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7610 | if (ret) |
| 7611 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7612 | |
| 7613 | ret = 0; |
| 7614 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 7615 | kvfree(vmas); |
| 7616 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7617 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7618 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7619 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7620 | sizeof(struct vm_area_struct *), |
| 7621 | GFP_KERNEL); |
| 7622 | if (!pages || !vmas) { |
| 7623 | ret = -ENOMEM; |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7624 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7625 | goto err; |
| 7626 | } |
| 7627 | got_pages = nr_pages; |
| 7628 | } |
| 7629 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7630 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7631 | GFP_KERNEL); |
| 7632 | ret = -ENOMEM; |
| 7633 | if (!imu->bvec) { |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7634 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7635 | goto err; |
| 7636 | } |
| 7637 | |
| 7638 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7639 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 7640 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 7641 | FOLL_WRITE | FOLL_LONGTERM, |
| 7642 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7643 | if (pret == nr_pages) { |
| 7644 | /* don't support file backed memory */ |
| 7645 | for (j = 0; j < nr_pages; j++) { |
| 7646 | struct vm_area_struct *vma = vmas[j]; |
| 7647 | |
| 7648 | if (vma->vm_file && |
| 7649 | !is_file_hugepages(vma->vm_file)) { |
| 7650 | ret = -EOPNOTSUPP; |
| 7651 | break; |
| 7652 | } |
| 7653 | } |
| 7654 | } else { |
| 7655 | ret = pret < 0 ? pret : -EFAULT; |
| 7656 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 7657 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7658 | if (ret) { |
| 7659 | /* |
| 7660 | * if we did partial map, or found file backed vmas, |
| 7661 | * release any pages we did get |
| 7662 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 7663 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7664 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 7665 | io_unaccount_mem(ctx, nr_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7666 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7667 | goto err; |
| 7668 | } |
| 7669 | |
| 7670 | off = ubuf & ~PAGE_MASK; |
| 7671 | size = iov.iov_len; |
| 7672 | for (j = 0; j < nr_pages; j++) { |
| 7673 | size_t vec_len; |
| 7674 | |
| 7675 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 7676 | imu->bvec[j].bv_page = pages[j]; |
| 7677 | imu->bvec[j].bv_len = vec_len; |
| 7678 | imu->bvec[j].bv_offset = off; |
| 7679 | off = 0; |
| 7680 | size -= vec_len; |
| 7681 | } |
| 7682 | /* store original address for later verification */ |
| 7683 | imu->ubuf = ubuf; |
| 7684 | imu->len = iov.iov_len; |
| 7685 | imu->nr_bvecs = nr_pages; |
| 7686 | |
| 7687 | ctx->nr_user_bufs++; |
| 7688 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7689 | kvfree(pages); |
| 7690 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7691 | return 0; |
| 7692 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7693 | kvfree(pages); |
| 7694 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7695 | io_sqe_buffer_unregister(ctx); |
| 7696 | return ret; |
| 7697 | } |
| 7698 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7699 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 7700 | { |
| 7701 | __s32 __user *fds = arg; |
| 7702 | int fd; |
| 7703 | |
| 7704 | if (ctx->cq_ev_fd) |
| 7705 | return -EBUSY; |
| 7706 | |
| 7707 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 7708 | return -EFAULT; |
| 7709 | |
| 7710 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 7711 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 7712 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 7713 | ctx->cq_ev_fd = NULL; |
| 7714 | return ret; |
| 7715 | } |
| 7716 | |
| 7717 | return 0; |
| 7718 | } |
| 7719 | |
| 7720 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 7721 | { |
| 7722 | if (ctx->cq_ev_fd) { |
| 7723 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 7724 | ctx->cq_ev_fd = NULL; |
| 7725 | return 0; |
| 7726 | } |
| 7727 | |
| 7728 | return -ENXIO; |
| 7729 | } |
| 7730 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7731 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 7732 | { |
| 7733 | struct io_ring_ctx *ctx = data; |
| 7734 | struct io_buffer *buf = p; |
| 7735 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 7736 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7737 | return 0; |
| 7738 | } |
| 7739 | |
| 7740 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 7741 | { |
| 7742 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 7743 | idr_destroy(&ctx->io_buffer_idr); |
| 7744 | } |
| 7745 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7746 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 7747 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7748 | io_finish_async(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7749 | io_sqe_buffer_unregister(ctx); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7750 | if (ctx->sqo_mm) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7751 | mmdrop(ctx->sqo_mm); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7752 | ctx->sqo_mm = NULL; |
| 7753 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7754 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7755 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7756 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7757 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 7758 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7759 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7760 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7761 | if (ctx->ring_sock) { |
| 7762 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7763 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7764 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7765 | #endif |
| 7766 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7767 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7768 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7769 | |
| 7770 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7771 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 7772 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 7773 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 7774 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7775 | kfree(ctx); |
| 7776 | } |
| 7777 | |
| 7778 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 7779 | { |
| 7780 | struct io_ring_ctx *ctx = file->private_data; |
| 7781 | __poll_t mask = 0; |
| 7782 | |
| 7783 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 7784 | /* |
| 7785 | * synchronizes with barrier from wq_has_sleeper call in |
| 7786 | * io_commit_cqring |
| 7787 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7788 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7789 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 7790 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7791 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 7792 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7793 | mask |= EPOLLIN | EPOLLRDNORM; |
| 7794 | |
| 7795 | return mask; |
| 7796 | } |
| 7797 | |
| 7798 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 7799 | { |
| 7800 | struct io_ring_ctx *ctx = file->private_data; |
| 7801 | |
| 7802 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 7803 | } |
| 7804 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7805 | static int io_remove_personalities(int id, void *p, void *data) |
| 7806 | { |
| 7807 | struct io_ring_ctx *ctx = data; |
| 7808 | const struct cred *cred; |
| 7809 | |
| 7810 | cred = idr_remove(&ctx->personality_idr, id); |
| 7811 | if (cred) |
| 7812 | put_cred(cred); |
| 7813 | return 0; |
| 7814 | } |
| 7815 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7816 | static void io_ring_exit_work(struct work_struct *work) |
| 7817 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 7818 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 7819 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7820 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 7821 | /* |
| 7822 | * If we're doing polled IO and end up having requests being |
| 7823 | * submitted async (out-of-line), then completions can come in while |
| 7824 | * we're waiting for refs to drop. We need to reap these manually, |
| 7825 | * as nobody else will be looking for them. |
| 7826 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 7827 | do { |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 7828 | if (ctx->rings) |
| 7829 | io_cqring_overflow_flush(ctx, true); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 7830 | io_iopoll_try_reap_events(ctx); |
| 7831 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7832 | io_ring_ctx_free(ctx); |
| 7833 | } |
| 7834 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7835 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 7836 | { |
| 7837 | mutex_lock(&ctx->uring_lock); |
| 7838 | percpu_ref_kill(&ctx->refs); |
| 7839 | mutex_unlock(&ctx->uring_lock); |
| 7840 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 7841 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 7842 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7843 | |
| 7844 | if (ctx->io_wq) |
| 7845 | io_wq_cancel_all(ctx->io_wq); |
| 7846 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 7847 | /* if we failed setting up the ctx, we might not have any rings */ |
| 7848 | if (ctx->rings) |
| 7849 | io_cqring_overflow_flush(ctx, true); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 7850 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7851 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 7852 | |
| 7853 | /* |
| 7854 | * Do this upfront, so we won't have a grace period where the ring |
| 7855 | * is closed but resources aren't reaped yet. This can cause |
| 7856 | * spurious failure in setting up a new ring. |
| 7857 | */ |
Jens Axboe | 760618f | 2020-07-24 12:53:31 -0600 | [diff] [blame] | 7858 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 7859 | ACCT_LOCKED); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 7860 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 7861 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
| 7862 | queue_work(system_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | static int io_uring_release(struct inode *inode, struct file *file) |
| 7866 | { |
| 7867 | struct io_ring_ctx *ctx = file->private_data; |
| 7868 | |
| 7869 | file->private_data = NULL; |
| 7870 | io_ring_ctx_wait_and_kill(ctx); |
| 7871 | return 0; |
| 7872 | } |
| 7873 | |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7874 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
| 7875 | { |
| 7876 | struct files_struct *files = data; |
| 7877 | |
| 7878 | return work->files == files; |
| 7879 | } |
| 7880 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7881 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 7882 | struct files_struct *files) |
| 7883 | { |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 7884 | if (list_empty_careful(&ctx->inflight_list)) |
| 7885 | return; |
| 7886 | |
| 7887 | /* cancel all at once, should be faster than doing it one by one*/ |
| 7888 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); |
| 7889 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7890 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7891 | struct io_kiocb *cancel_req = NULL, *req; |
| 7892 | DEFINE_WAIT(wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7893 | |
| 7894 | spin_lock_irq(&ctx->inflight_lock); |
| 7895 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7896 | if (req->work.files != files) |
| 7897 | continue; |
| 7898 | /* req is being completed, ignore */ |
| 7899 | if (!refcount_inc_not_zero(&req->refs)) |
| 7900 | continue; |
| 7901 | cancel_req = req; |
| 7902 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7903 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7904 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7905 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7906 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7907 | spin_unlock_irq(&ctx->inflight_lock); |
| 7908 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7909 | /* We need to keep going until we don't find a matching req */ |
| 7910 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7911 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7912 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7913 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 7914 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 7915 | list_del(&cancel_req->compl.list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7916 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7917 | |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 7918 | io_cqring_mark_overflow(ctx); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7919 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 7920 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Pavel Begunkov | dd9dfcd | 2020-07-30 18:43:48 +0300 | [diff] [blame] | 7921 | io_commit_cqring(ctx); |
Pavel Begunkov | b2bd1cf | 2020-07-30 18:43:47 +0300 | [diff] [blame] | 7922 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7923 | |
| 7924 | /* |
| 7925 | * Put inflight ref and overflow ref. If that's |
| 7926 | * all we had, then we're done with this request. |
| 7927 | */ |
| 7928 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
Pavel Begunkov | 4518a3c | 2020-05-26 20:34:02 +0300 | [diff] [blame] | 7929 | io_free_req(cancel_req); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7930 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7931 | continue; |
| 7932 | } |
Pavel Begunkov | 7b53d59 | 2020-05-30 14:19:15 +0300 | [diff] [blame] | 7933 | } else { |
| 7934 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 7935 | io_put_req(cancel_req); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7936 | } |
| 7937 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7938 | schedule(); |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 7939 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7940 | } |
| 7941 | } |
| 7942 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7943 | 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] | 7944 | { |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7945 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7946 | struct task_struct *task = data; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7947 | |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7948 | return req->task == task; |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 7949 | } |
| 7950 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7951 | static int io_uring_flush(struct file *file, void *data) |
| 7952 | { |
| 7953 | struct io_ring_ctx *ctx = file->private_data; |
| 7954 | |
| 7955 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7956 | |
| 7957 | /* |
| 7958 | * If the task is going away, cancel work it may have pending |
| 7959 | */ |
Pavel Begunkov | 801dd57 | 2020-06-15 10:33:14 +0300 | [diff] [blame] | 7960 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 7961 | 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] | 7962 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7963 | return 0; |
| 7964 | } |
| 7965 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7966 | static void *io_uring_validate_mmap_request(struct file *file, |
| 7967 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7968 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7969 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7970 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7971 | struct page *page; |
| 7972 | void *ptr; |
| 7973 | |
| 7974 | switch (offset) { |
| 7975 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7976 | case IORING_OFF_CQ_RING: |
| 7977 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7978 | break; |
| 7979 | case IORING_OFF_SQES: |
| 7980 | ptr = ctx->sq_sqes; |
| 7981 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7982 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7983 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 7987 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7988 | return ERR_PTR(-EINVAL); |
| 7989 | |
| 7990 | return ptr; |
| 7991 | } |
| 7992 | |
| 7993 | #ifdef CONFIG_MMU |
| 7994 | |
| 7995 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7996 | { |
| 7997 | size_t sz = vma->vm_end - vma->vm_start; |
| 7998 | unsigned long pfn; |
| 7999 | void *ptr; |
| 8000 | |
| 8001 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 8002 | if (IS_ERR(ptr)) |
| 8003 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8004 | |
| 8005 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 8006 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 8007 | } |
| 8008 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8009 | #else /* !CONFIG_MMU */ |
| 8010 | |
| 8011 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8012 | { |
| 8013 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 8014 | } |
| 8015 | |
| 8016 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 8017 | { |
| 8018 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 8019 | } |
| 8020 | |
| 8021 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 8022 | unsigned long addr, unsigned long len, |
| 8023 | unsigned long pgoff, unsigned long flags) |
| 8024 | { |
| 8025 | void *ptr; |
| 8026 | |
| 8027 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 8028 | if (IS_ERR(ptr)) |
| 8029 | return PTR_ERR(ptr); |
| 8030 | |
| 8031 | return (unsigned long) ptr; |
| 8032 | } |
| 8033 | |
| 8034 | #endif /* !CONFIG_MMU */ |
| 8035 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8036 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 8037 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 8038 | size_t, sigsz) |
| 8039 | { |
| 8040 | struct io_ring_ctx *ctx; |
| 8041 | long ret = -EBADF; |
| 8042 | int submitted = 0; |
| 8043 | struct fd f; |
| 8044 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 8045 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 8046 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8047 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8048 | return -EINVAL; |
| 8049 | |
| 8050 | f = fdget(fd); |
| 8051 | if (!f.file) |
| 8052 | return -EBADF; |
| 8053 | |
| 8054 | ret = -EOPNOTSUPP; |
| 8055 | if (f.file->f_op != &io_uring_fops) |
| 8056 | goto out_fput; |
| 8057 | |
| 8058 | ret = -ENXIO; |
| 8059 | ctx = f.file->private_data; |
| 8060 | if (!percpu_ref_tryget(&ctx->refs)) |
| 8061 | goto out_fput; |
| 8062 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8063 | /* |
| 8064 | * For SQ polling, the thread will do all submissions and completions. |
| 8065 | * Just return the requested submit count, and wake the thread if |
| 8066 | * we were asked to. |
| 8067 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 8068 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8069 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 8070 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 8071 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8072 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 8073 | wake_up(&ctx->sqo_wait); |
| 8074 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 8075 | } else if (to_submit) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8076 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 8077 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8078 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 8079 | |
| 8080 | if (submitted != to_submit) |
| 8081 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8082 | } |
| 8083 | if (flags & IORING_ENTER_GETEVENTS) { |
| 8084 | min_complete = min(min_complete, ctx->cq_entries); |
| 8085 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 8086 | /* |
| 8087 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 8088 | * space applications don't need to do io completion events |
| 8089 | * polling again, they can rely on io_sq_thread to do polling |
| 8090 | * work, which can reduce cpu usage and uring_lock contention. |
| 8091 | */ |
| 8092 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 8093 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 8094 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8095 | } else { |
| 8096 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 8097 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8098 | } |
| 8099 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 8100 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 8101 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8102 | out_fput: |
| 8103 | fdput(f); |
| 8104 | return submitted ? submitted : ret; |
| 8105 | } |
| 8106 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8107 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8108 | static int io_uring_show_cred(int id, void *p, void *data) |
| 8109 | { |
| 8110 | const struct cred *cred = p; |
| 8111 | struct seq_file *m = data; |
| 8112 | struct user_namespace *uns = seq_user_ns(m); |
| 8113 | struct group_info *gi; |
| 8114 | kernel_cap_t cap; |
| 8115 | unsigned __capi; |
| 8116 | int g; |
| 8117 | |
| 8118 | seq_printf(m, "%5d\n", id); |
| 8119 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 8120 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 8121 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 8122 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 8123 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 8124 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 8125 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 8126 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 8127 | seq_puts(m, "\n\tGroups:\t"); |
| 8128 | gi = cred->group_info; |
| 8129 | for (g = 0; g < gi->ngroups; g++) { |
| 8130 | seq_put_decimal_ull(m, g ? " " : "", |
| 8131 | from_kgid_munged(uns, gi->gid[g])); |
| 8132 | } |
| 8133 | seq_puts(m, "\n\tCapEff:\t"); |
| 8134 | cap = cred->cap_effective; |
| 8135 | CAP_FOR_EACH_U32(__capi) |
| 8136 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 8137 | seq_putc(m, '\n'); |
| 8138 | return 0; |
| 8139 | } |
| 8140 | |
| 8141 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 8142 | { |
| 8143 | int i; |
| 8144 | |
| 8145 | mutex_lock(&ctx->uring_lock); |
| 8146 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 8147 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8148 | struct fixed_file_table *table; |
| 8149 | struct file *f; |
| 8150 | |
| 8151 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 8152 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 8153 | if (f) |
| 8154 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 8155 | else |
| 8156 | seq_printf(m, "%5u: <none>\n", i); |
| 8157 | } |
| 8158 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 8159 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8160 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 8161 | |
| 8162 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 8163 | (unsigned int) buf->len); |
| 8164 | } |
| 8165 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 8166 | seq_printf(m, "Personalities:\n"); |
| 8167 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 8168 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 8169 | seq_printf(m, "PollList:\n"); |
| 8170 | spin_lock_irq(&ctx->completion_lock); |
| 8171 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 8172 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 8173 | struct io_kiocb *req; |
| 8174 | |
| 8175 | hlist_for_each_entry(req, list, hash_node) |
| 8176 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 8177 | req->task->task_works != NULL); |
| 8178 | } |
| 8179 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8180 | mutex_unlock(&ctx->uring_lock); |
| 8181 | } |
| 8182 | |
| 8183 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 8184 | { |
| 8185 | struct io_ring_ctx *ctx = f->private_data; |
| 8186 | |
| 8187 | if (percpu_ref_tryget(&ctx->refs)) { |
| 8188 | __io_uring_show_fdinfo(ctx, m); |
| 8189 | percpu_ref_put(&ctx->refs); |
| 8190 | } |
| 8191 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8192 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8193 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8194 | static const struct file_operations io_uring_fops = { |
| 8195 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8196 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8197 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8198 | #ifndef CONFIG_MMU |
| 8199 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 8200 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 8201 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8202 | .poll = io_uring_poll, |
| 8203 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8204 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 8205 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 8206 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8207 | }; |
| 8208 | |
| 8209 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 8210 | struct io_uring_params *p) |
| 8211 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8212 | struct io_rings *rings; |
| 8213 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8214 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 8215 | /* make sure these are sane, as we already accounted them */ |
| 8216 | ctx->sq_entries = p->sq_entries; |
| 8217 | ctx->cq_entries = p->cq_entries; |
| 8218 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8219 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 8220 | if (size == SIZE_MAX) |
| 8221 | return -EOVERFLOW; |
| 8222 | |
| 8223 | rings = io_mem_alloc(size); |
| 8224 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8225 | return -ENOMEM; |
| 8226 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8227 | ctx->rings = rings; |
| 8228 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 8229 | rings->sq_ring_mask = p->sq_entries - 1; |
| 8230 | rings->cq_ring_mask = p->cq_entries - 1; |
| 8231 | rings->sq_ring_entries = p->sq_entries; |
| 8232 | rings->cq_ring_entries = p->cq_entries; |
| 8233 | ctx->sq_mask = rings->sq_ring_mask; |
| 8234 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8235 | |
| 8236 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8237 | if (size == SIZE_MAX) { |
| 8238 | io_mem_free(ctx->rings); |
| 8239 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8240 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8241 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8242 | |
| 8243 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8244 | if (!ctx->sq_sqes) { |
| 8245 | io_mem_free(ctx->rings); |
| 8246 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8247 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 8248 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8249 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8250 | return 0; |
| 8251 | } |
| 8252 | |
| 8253 | /* |
| 8254 | * Allocate an anonymous fd, this is what constitutes the application |
| 8255 | * visible backing of an io_uring instance. The application mmaps this |
| 8256 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 8257 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 8258 | */ |
| 8259 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 8260 | { |
| 8261 | struct file *file; |
| 8262 | int ret; |
| 8263 | |
| 8264 | #if defined(CONFIG_UNIX) |
| 8265 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 8266 | &ctx->ring_sock); |
| 8267 | if (ret) |
| 8268 | return ret; |
| 8269 | #endif |
| 8270 | |
| 8271 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 8272 | if (ret < 0) |
| 8273 | goto err; |
| 8274 | |
| 8275 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 8276 | O_RDWR | O_CLOEXEC); |
| 8277 | if (IS_ERR(file)) { |
| 8278 | put_unused_fd(ret); |
| 8279 | ret = PTR_ERR(file); |
| 8280 | goto err; |
| 8281 | } |
| 8282 | |
| 8283 | #if defined(CONFIG_UNIX) |
| 8284 | ctx->ring_sock->file = file; |
| 8285 | #endif |
| 8286 | fd_install(ret, file); |
| 8287 | return ret; |
| 8288 | err: |
| 8289 | #if defined(CONFIG_UNIX) |
| 8290 | sock_release(ctx->ring_sock); |
| 8291 | ctx->ring_sock = NULL; |
| 8292 | #endif |
| 8293 | return ret; |
| 8294 | } |
| 8295 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8296 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 8297 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8298 | { |
| 8299 | struct user_struct *user = NULL; |
| 8300 | struct io_ring_ctx *ctx; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8301 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8302 | int ret; |
| 8303 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8304 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8305 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8306 | if (entries > IORING_MAX_ENTRIES) { |
| 8307 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8308 | return -EINVAL; |
| 8309 | entries = IORING_MAX_ENTRIES; |
| 8310 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8311 | |
| 8312 | /* |
| 8313 | * Use twice as many entries for the CQ ring. It's possible for the |
| 8314 | * application to drive a higher depth than the size of the SQ ring, |
| 8315 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8316 | * some flexibility in overcommitting a bit. If the application has |
| 8317 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 8318 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8319 | */ |
| 8320 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8321 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 8322 | /* |
| 8323 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 8324 | * to a power-of-two, if it isn't already. We do NOT impose |
| 8325 | * any cq vs sq ring sizing. |
| 8326 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8327 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8328 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8329 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 8330 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 8331 | return -EINVAL; |
| 8332 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 8333 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 8334 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 8335 | } else { |
| 8336 | p->cq_entries = 2 * p->sq_entries; |
| 8337 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8338 | |
| 8339 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8340 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8341 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8342 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8343 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8344 | ring_pages(p->sq_entries, p->cq_entries)); |
| 8345 | if (ret) { |
| 8346 | free_uid(user); |
| 8347 | return ret; |
| 8348 | } |
| 8349 | } |
| 8350 | |
| 8351 | ctx = io_ring_ctx_alloc(p); |
| 8352 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8353 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8354 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8355 | p->cq_entries)); |
| 8356 | free_uid(user); |
| 8357 | return -ENOMEM; |
| 8358 | } |
| 8359 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8360 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 8361 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8362 | |
Jens Axboe | f74441e | 2020-08-05 13:00:44 -0600 | [diff] [blame] | 8363 | /* |
| 8364 | * Account memory _before_ installing the file descriptor. Once |
| 8365 | * the descriptor is installed, it can get closed at any time. Also |
| 8366 | * do this before hitting the general error path, as ring freeing |
| 8367 | * will un-account as well. |
| 8368 | */ |
| 8369 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 8370 | ACCT_LOCKED); |
| 8371 | ctx->limit_mem = limit_mem; |
| 8372 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8373 | ret = io_allocate_scq_urings(ctx, p); |
| 8374 | if (ret) |
| 8375 | goto err; |
| 8376 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8377 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8378 | if (ret) |
| 8379 | goto err; |
| 8380 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8381 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8382 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 8383 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 8384 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 8385 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 8386 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 8387 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 8388 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8389 | |
| 8390 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8391 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 8392 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 8393 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 8394 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 8395 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 8396 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 8397 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 8398 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8399 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 8400 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8401 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
| 8402 | IORING_FEAT_POLL_32BITS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8403 | |
| 8404 | if (copy_to_user(params, p, sizeof(*p))) { |
| 8405 | ret = -EFAULT; |
| 8406 | goto err; |
| 8407 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 8408 | |
| 8409 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 8410 | * Install ring fd as the very last thing, so we don't risk someone |
| 8411 | * having closed it before we finish setup |
| 8412 | */ |
| 8413 | ret = io_uring_get_fd(ctx); |
| 8414 | if (ret < 0) |
| 8415 | goto err; |
| 8416 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8417 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8418 | return ret; |
| 8419 | err: |
| 8420 | io_ring_ctx_wait_and_kill(ctx); |
| 8421 | return ret; |
| 8422 | } |
| 8423 | |
| 8424 | /* |
| 8425 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 8426 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 8427 | * params structure passed in. |
| 8428 | */ |
| 8429 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 8430 | { |
| 8431 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8432 | int i; |
| 8433 | |
| 8434 | if (copy_from_user(&p, params, sizeof(p))) |
| 8435 | return -EFAULT; |
| 8436 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 8437 | if (p.resv[i]) |
| 8438 | return -EINVAL; |
| 8439 | } |
| 8440 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8441 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 8442 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8443 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8444 | return -EINVAL; |
| 8445 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 8446 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8447 | } |
| 8448 | |
| 8449 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 8450 | struct io_uring_params __user *, params) |
| 8451 | { |
| 8452 | return io_uring_setup(entries, params); |
| 8453 | } |
| 8454 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8455 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 8456 | { |
| 8457 | struct io_uring_probe *p; |
| 8458 | size_t size; |
| 8459 | int i, ret; |
| 8460 | |
| 8461 | size = struct_size(p, ops, nr_args); |
| 8462 | if (size == SIZE_MAX) |
| 8463 | return -EOVERFLOW; |
| 8464 | p = kzalloc(size, GFP_KERNEL); |
| 8465 | if (!p) |
| 8466 | return -ENOMEM; |
| 8467 | |
| 8468 | ret = -EFAULT; |
| 8469 | if (copy_from_user(p, arg, size)) |
| 8470 | goto out; |
| 8471 | ret = -EINVAL; |
| 8472 | if (memchr_inv(p, 0, size)) |
| 8473 | goto out; |
| 8474 | |
| 8475 | p->last_op = IORING_OP_LAST - 1; |
| 8476 | if (nr_args > IORING_OP_LAST) |
| 8477 | nr_args = IORING_OP_LAST; |
| 8478 | |
| 8479 | for (i = 0; i < nr_args; i++) { |
| 8480 | p->ops[i].op = i; |
| 8481 | if (!io_op_defs[i].not_supported) |
| 8482 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 8483 | } |
| 8484 | p->ops_len = i; |
| 8485 | |
| 8486 | ret = 0; |
| 8487 | if (copy_to_user(arg, p, size)) |
| 8488 | ret = -EFAULT; |
| 8489 | out: |
| 8490 | kfree(p); |
| 8491 | return ret; |
| 8492 | } |
| 8493 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8494 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 8495 | { |
| 8496 | const struct cred *creds = get_current_cred(); |
| 8497 | int id; |
| 8498 | |
| 8499 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 8500 | USHRT_MAX, GFP_KERNEL); |
| 8501 | if (id < 0) |
| 8502 | put_cred(creds); |
| 8503 | return id; |
| 8504 | } |
| 8505 | |
| 8506 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 8507 | { |
| 8508 | const struct cred *old_creds; |
| 8509 | |
| 8510 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 8511 | if (old_creds) { |
| 8512 | put_cred(old_creds); |
| 8513 | return 0; |
| 8514 | } |
| 8515 | |
| 8516 | return -EINVAL; |
| 8517 | } |
| 8518 | |
| 8519 | static bool io_register_op_must_quiesce(int op) |
| 8520 | { |
| 8521 | switch (op) { |
| 8522 | case IORING_UNREGISTER_FILES: |
| 8523 | case IORING_REGISTER_FILES_UPDATE: |
| 8524 | case IORING_REGISTER_PROBE: |
| 8525 | case IORING_REGISTER_PERSONALITY: |
| 8526 | case IORING_UNREGISTER_PERSONALITY: |
| 8527 | return false; |
| 8528 | default: |
| 8529 | return true; |
| 8530 | } |
| 8531 | } |
| 8532 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8533 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 8534 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8535 | __releases(ctx->uring_lock) |
| 8536 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8537 | { |
| 8538 | int ret; |
| 8539 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 8540 | /* |
| 8541 | * We're inside the ring mutex, if the ref is already dying, then |
| 8542 | * someone else killed the ctx or is already going through |
| 8543 | * io_uring_register(). |
| 8544 | */ |
| 8545 | if (percpu_ref_is_dying(&ctx->refs)) |
| 8546 | return -ENXIO; |
| 8547 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8548 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8549 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 8550 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8551 | /* |
| 8552 | * Drop uring mutex before waiting for references to exit. If |
| 8553 | * another thread is currently inside io_uring_enter() it might |
| 8554 | * need to grab the uring_lock to make progress. If we hold it |
| 8555 | * here across the drain wait, then we can deadlock. It's safe |
| 8556 | * to drop the mutex here, since no new references will come in |
| 8557 | * after we've killed the percpu ref. |
| 8558 | */ |
| 8559 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8560 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8561 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8562 | if (ret) { |
| 8563 | percpu_ref_resurrect(&ctx->refs); |
| 8564 | ret = -EINTR; |
| 8565 | goto out; |
| 8566 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8567 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8568 | |
| 8569 | switch (opcode) { |
| 8570 | case IORING_REGISTER_BUFFERS: |
| 8571 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 8572 | break; |
| 8573 | case IORING_UNREGISTER_BUFFERS: |
| 8574 | ret = -EINVAL; |
| 8575 | if (arg || nr_args) |
| 8576 | break; |
| 8577 | ret = io_sqe_buffer_unregister(ctx); |
| 8578 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8579 | case IORING_REGISTER_FILES: |
| 8580 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 8581 | break; |
| 8582 | case IORING_UNREGISTER_FILES: |
| 8583 | ret = -EINVAL; |
| 8584 | if (arg || nr_args) |
| 8585 | break; |
| 8586 | ret = io_sqe_files_unregister(ctx); |
| 8587 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8588 | case IORING_REGISTER_FILES_UPDATE: |
| 8589 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 8590 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8591 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8592 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8593 | ret = -EINVAL; |
| 8594 | if (nr_args != 1) |
| 8595 | break; |
| 8596 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 8597 | if (ret) |
| 8598 | break; |
| 8599 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 8600 | ctx->eventfd_async = 1; |
| 8601 | else |
| 8602 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8603 | break; |
| 8604 | case IORING_UNREGISTER_EVENTFD: |
| 8605 | ret = -EINVAL; |
| 8606 | if (arg || nr_args) |
| 8607 | break; |
| 8608 | ret = io_eventfd_unregister(ctx); |
| 8609 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 8610 | case IORING_REGISTER_PROBE: |
| 8611 | ret = -EINVAL; |
| 8612 | if (!arg || nr_args > 256) |
| 8613 | break; |
| 8614 | ret = io_probe(ctx, arg, nr_args); |
| 8615 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8616 | case IORING_REGISTER_PERSONALITY: |
| 8617 | ret = -EINVAL; |
| 8618 | if (arg || nr_args) |
| 8619 | break; |
| 8620 | ret = io_register_personality(ctx); |
| 8621 | break; |
| 8622 | case IORING_UNREGISTER_PERSONALITY: |
| 8623 | ret = -EINVAL; |
| 8624 | if (arg) |
| 8625 | break; |
| 8626 | ret = io_unregister_personality(ctx, nr_args); |
| 8627 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8628 | default: |
| 8629 | ret = -EINVAL; |
| 8630 | break; |
| 8631 | } |
| 8632 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8633 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8634 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8635 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 8636 | out: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 8637 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8638 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8639 | return ret; |
| 8640 | } |
| 8641 | |
| 8642 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 8643 | void __user *, arg, unsigned int, nr_args) |
| 8644 | { |
| 8645 | struct io_ring_ctx *ctx; |
| 8646 | long ret = -EBADF; |
| 8647 | struct fd f; |
| 8648 | |
| 8649 | f = fdget(fd); |
| 8650 | if (!f.file) |
| 8651 | return -EBADF; |
| 8652 | |
| 8653 | ret = -EOPNOTSUPP; |
| 8654 | if (f.file->f_op != &io_uring_fops) |
| 8655 | goto out_fput; |
| 8656 | |
| 8657 | ctx = f.file->private_data; |
| 8658 | |
| 8659 | mutex_lock(&ctx->uring_lock); |
| 8660 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 8661 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 8662 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 8663 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8664 | out_fput: |
| 8665 | fdput(f); |
| 8666 | return ret; |
| 8667 | } |
| 8668 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8669 | static int __init io_uring_init(void) |
| 8670 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8671 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 8672 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 8673 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 8674 | } while (0) |
| 8675 | |
| 8676 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 8677 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 8678 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 8679 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 8680 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 8681 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 8682 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 8683 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 8684 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 8685 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8686 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8687 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 8688 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 8689 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 8690 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 8691 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 8692 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 8693 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8694 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 8695 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 8696 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 8697 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 8698 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 8699 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 8700 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 8701 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8702 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8703 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 8704 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 8705 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 8706 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 8707 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 8708 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 8709 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8710 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 8711 | return 0; |
| 8712 | }; |
| 8713 | __initcall(io_uring_init); |