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 |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 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 | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 76 | #include <linux/eventpoll.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 77 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 78 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 79 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 80 | #include <linux/io_uring.h> |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 81 | #include <linux/tracehook.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) |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 93 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 94 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 95 | /* only define max */ |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 96 | #define IORING_MAX_FIXED_FILES (1U << 15) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 97 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 98 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 99 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 100 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 101 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
| 102 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) |
| 103 | |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 104 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
| 105 | |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 106 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
| 107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
| 108 | |
| 109 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN) |
| 110 | |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 111 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 112 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
| 113 | REQ_F_ASYNC_DATA) |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 114 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 115 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
| 116 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 117 | struct io_uring { |
| 118 | u32 head ____cacheline_aligned_in_smp; |
| 119 | u32 tail ____cacheline_aligned_in_smp; |
| 120 | }; |
| 121 | |
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 | * This data is shared with the application through the mmap at offsets |
| 124 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | * |
| 126 | * The offsets to the member fields are published through struct |
| 127 | * io_sqring_offsets when calling io_uring_setup. |
| 128 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 129 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | /* |
| 131 | * Head and tail offsets into the ring; the offsets need to be |
| 132 | * masked to get valid indices. |
| 133 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 134 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 135 | * and the application controls tail of the sq ring and the head of the |
| 136 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 137 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 138 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 139 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 140 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 141 | * ring_entries - 1) |
| 142 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | u32 sq_ring_mask, cq_ring_mask; |
| 144 | /* Ring sizes (constant, power of 2) */ |
| 145 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Number of invalid entries dropped by the kernel due to |
| 148 | * invalid index stored in array |
| 149 | * |
| 150 | * Written by the kernel, shouldn't be modified by the |
| 151 | * application (i.e. get number of "new events" by comparing to |
| 152 | * cached value). |
| 153 | * |
| 154 | * After a new SQ head value was read by the application this |
| 155 | * counter includes all submissions that were dropped reaching |
| 156 | * the new SQ head (and possibly more). |
| 157 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 158 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 159 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 160 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 161 | * |
| 162 | * Written by the kernel, shouldn't be modified by the |
| 163 | * application. |
| 164 | * |
| 165 | * The application needs a full memory barrier before checking |
| 166 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 167 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 168 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 169 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 170 | * Runtime CQ flags |
| 171 | * |
| 172 | * Written by the application, shouldn't be modified by the |
| 173 | * kernel. |
| 174 | */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 175 | u32 cq_flags; |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 176 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 177 | * Number of completion events lost because the queue was full; |
| 178 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 179 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 180 | * the completion queue. |
| 181 | * |
| 182 | * Written by the kernel, shouldn't be modified by the |
| 183 | * application (i.e. get number of "new events" by comparing to |
| 184 | * cached value). |
| 185 | * |
| 186 | * As completion events come in out of order this counter is not |
| 187 | * ordered with any other data. |
| 188 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 189 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Ring buffer of completion events. |
| 192 | * |
| 193 | * The kernel writes completion events fresh every time they are |
| 194 | * produced, so the application is allowed to modify pending |
| 195 | * entries. |
| 196 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 197 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 200 | enum io_uring_cmd_flags { |
| 201 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 202 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 205 | struct io_mapped_ubuf { |
| 206 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 207 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 208 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 209 | unsigned long acct_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 210 | struct bio_vec bvec[]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 213 | struct io_ring_ctx; |
| 214 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 215 | struct io_overflow_cqe { |
| 216 | struct io_uring_cqe cqe; |
| 217 | struct list_head list; |
| 218 | }; |
| 219 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 220 | struct io_fixed_file { |
| 221 | /* file * with additional FFS_* flags */ |
| 222 | unsigned long file_ptr; |
| 223 | }; |
| 224 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 225 | struct io_rsrc_put { |
| 226 | struct list_head list; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 227 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 228 | union { |
| 229 | void *rsrc; |
| 230 | struct file *file; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 231 | struct io_mapped_ubuf *buf; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 232 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 235 | struct io_file_table { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 236 | struct io_fixed_file *files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 239 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 240 | struct percpu_ref refs; |
| 241 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 242 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 243 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 244 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 245 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 246 | }; |
| 247 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 248 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 249 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 250 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 251 | struct io_ring_ctx *ctx; |
| 252 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 253 | u64 **tags; |
| 254 | unsigned int nr; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 255 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 256 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 257 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 258 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 261 | struct io_buffer { |
| 262 | struct list_head list; |
| 263 | __u64 addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 264 | __u32 len; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 265 | __u16 bid; |
| 266 | }; |
| 267 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 268 | struct io_restriction { |
| 269 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 270 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 271 | u8 sqe_flags_allowed; |
| 272 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 273 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 274 | }; |
| 275 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 276 | enum { |
| 277 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 278 | IO_SQ_THREAD_SHOULD_PARK, |
| 279 | }; |
| 280 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 281 | struct io_sq_data { |
| 282 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 283 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 284 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 285 | |
| 286 | /* ctx's that are using this sqd */ |
| 287 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 288 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 289 | struct task_struct *thread; |
| 290 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 291 | |
| 292 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 293 | int sq_cpu; |
| 294 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 295 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 296 | |
| 297 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 298 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 299 | }; |
| 300 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 301 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 302 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 303 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 304 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 305 | struct io_submit_link { |
| 306 | struct io_kiocb *head; |
| 307 | struct io_kiocb *last; |
| 308 | }; |
| 309 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 310 | struct io_submit_state { |
| 311 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 312 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 313 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 314 | bool plug_started; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 315 | bool need_plug; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Batch completion logic |
| 319 | */ |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 320 | struct io_wq_work_list compl_reqs; |
| 321 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 322 | /* inline/task_work completion list, under ->uring_lock */ |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 323 | struct io_wq_work_node free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 326 | struct io_ring_ctx { |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 327 | /* const or read-mostly hot data */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 328 | struct { |
| 329 | struct percpu_ref refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 330 | |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 331 | struct io_rings *rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 332 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 333 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 334 | unsigned int drain_next: 1; |
| 335 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 336 | unsigned int restricted: 1; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 337 | unsigned int off_timeout_used: 1; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 338 | unsigned int drain_active: 1; |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 339 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 340 | |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 341 | /* submission data */ |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 342 | struct { |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 343 | struct mutex uring_lock; |
| 344 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 345 | /* |
| 346 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 347 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 348 | * |
| 349 | * This indirection could e.g. be used to assign fixed |
| 350 | * io_uring_sqe entries to operations and only submit them to |
| 351 | * the queue when needed. |
| 352 | * |
| 353 | * The kernel modifies neither the indices array nor the entries |
| 354 | * array. |
| 355 | */ |
| 356 | u32 *sq_array; |
Pavel Begunkov | c7af47c | 2021-06-14 23:37:20 +0100 | [diff] [blame] | 357 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 358 | unsigned cached_sq_head; |
| 359 | unsigned sq_entries; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 360 | struct list_head defer_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Fixed resources fast path, should be accessed only under |
| 364 | * uring_lock, and updated through io_uring_register(2) |
| 365 | */ |
| 366 | struct io_rsrc_node *rsrc_node; |
| 367 | struct io_file_table file_table; |
| 368 | unsigned nr_user_files; |
| 369 | unsigned nr_user_bufs; |
| 370 | struct io_mapped_ubuf **user_bufs; |
| 371 | |
| 372 | struct io_submit_state submit_state; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 373 | struct list_head timeout_list; |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 374 | struct list_head ltimeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 375 | struct list_head cq_overflow_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 376 | struct xarray io_buffers; |
| 377 | struct xarray personalities; |
| 378 | u32 pers_next; |
| 379 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 380 | } ____cacheline_aligned_in_smp; |
| 381 | |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 382 | /* IRQ completion list, under ->completion_lock */ |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 383 | struct io_wq_work_list locked_free_list; |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 384 | unsigned int locked_free_nr; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 385 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 386 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 387 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 388 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 389 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 390 | struct list_head sqd_list; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 391 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 392 | unsigned long check_cq_overflow; |
| 393 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 394 | struct { |
| 395 | unsigned cached_cq_tail; |
| 396 | unsigned cq_entries; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 397 | struct eventfd_ctx *cq_ev_fd; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 398 | struct wait_queue_head cq_wait; |
| 399 | unsigned cq_extra; |
| 400 | atomic_t cq_timeouts; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 401 | unsigned cq_last_tm_flush; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 402 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 403 | |
| 404 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 405 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 406 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 407 | spinlock_t timeout_lock; |
| 408 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 409 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 410 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 411 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 412 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 413 | * manipulate the list, hence no extra locking is needed there. |
| 414 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 415 | struct io_wq_work_list iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 416 | struct hlist_head *cancel_hash; |
| 417 | unsigned cancel_hash_bits; |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 418 | bool poll_multi_queue; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 419 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 420 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 421 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 422 | |
Pavel Begunkov | b13a891 | 2021-05-16 22:58:07 +0100 | [diff] [blame] | 423 | /* slow path rsrc auxilary data, used by update/register */ |
| 424 | struct { |
| 425 | struct io_rsrc_node *rsrc_backup_node; |
| 426 | struct io_mapped_ubuf *dummy_ubuf; |
| 427 | struct io_rsrc_data *file_data; |
| 428 | struct io_rsrc_data *buf_data; |
| 429 | |
| 430 | struct delayed_work rsrc_put_work; |
| 431 | struct llist_head rsrc_put_llist; |
| 432 | struct list_head rsrc_ref_list; |
| 433 | spinlock_t rsrc_ref_lock; |
| 434 | }; |
| 435 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 436 | /* Keep this last, we don't need it for the fast path */ |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 437 | struct { |
| 438 | #if defined(CONFIG_UNIX) |
| 439 | struct socket *ring_sock; |
| 440 | #endif |
| 441 | /* hashed buffered write serialization */ |
| 442 | struct io_wq_hash *hash_map; |
| 443 | |
| 444 | /* Only used for accounting purposes */ |
| 445 | struct user_struct *user; |
| 446 | struct mm_struct *mm_account; |
| 447 | |
| 448 | /* ctx exit and cancelation */ |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 449 | struct llist_head fallback_llist; |
| 450 | struct delayed_work fallback_work; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 451 | struct work_struct exit_work; |
| 452 | struct list_head tctx_list; |
| 453 | struct completion ref_comp; |
| 454 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 455 | }; |
| 456 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 457 | struct io_uring_task { |
| 458 | /* submission side */ |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 459 | int cached_refs; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 460 | struct xarray xa; |
| 461 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 462 | const struct io_ring_ctx *last; |
| 463 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 464 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 465 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 466 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 467 | |
| 468 | spinlock_t task_lock; |
| 469 | struct io_wq_work_list task_list; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 470 | struct callback_head task_work; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 471 | bool task_running; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 472 | }; |
| 473 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 474 | /* |
| 475 | * First field must be the file pointer in all the |
| 476 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 477 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 478 | struct io_poll_iocb { |
| 479 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 480 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 481 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 482 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 483 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 484 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 487 | struct io_poll_update { |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 488 | struct file *file; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 489 | u64 old_user_data; |
| 490 | u64 new_user_data; |
| 491 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 492 | bool update_events; |
| 493 | bool update_user_data; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 496 | struct io_close { |
| 497 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 498 | int fd; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 499 | u32 file_slot; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 502 | struct io_timeout_data { |
| 503 | struct io_kiocb *req; |
| 504 | struct hrtimer timer; |
| 505 | struct timespec64 ts; |
| 506 | enum hrtimer_mode mode; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 507 | u32 flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 510 | struct io_accept { |
| 511 | struct file *file; |
| 512 | struct sockaddr __user *addr; |
| 513 | int __user *addr_len; |
| 514 | int flags; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 515 | u32 file_slot; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 516 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | struct io_sync { |
| 520 | struct file *file; |
| 521 | loff_t len; |
| 522 | loff_t off; |
| 523 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 524 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 525 | }; |
| 526 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 527 | struct io_cancel { |
| 528 | struct file *file; |
| 529 | u64 addr; |
| 530 | }; |
| 531 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 532 | struct io_timeout { |
| 533 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 534 | u32 off; |
| 535 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 536 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 537 | /* head of the link, used by linked timeouts only */ |
| 538 | struct io_kiocb *head; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 539 | /* for linked completions */ |
| 540 | struct io_kiocb *prev; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 541 | }; |
| 542 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 543 | struct io_timeout_rem { |
| 544 | struct file *file; |
| 545 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 546 | |
| 547 | /* timeout update */ |
| 548 | struct timespec64 ts; |
| 549 | u32 flags; |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 550 | bool ltimeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 551 | }; |
| 552 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 553 | struct io_rw { |
| 554 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 555 | struct kiocb kiocb; |
| 556 | u64 addr; |
| 557 | u64 len; |
| 558 | }; |
| 559 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 560 | struct io_connect { |
| 561 | struct file *file; |
| 562 | struct sockaddr __user *addr; |
| 563 | int addr_len; |
| 564 | }; |
| 565 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 566 | struct io_sr_msg { |
| 567 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 568 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 569 | struct compat_msghdr __user *umsg_compat; |
| 570 | struct user_msghdr __user *umsg; |
| 571 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 572 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 573 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 574 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 575 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 578 | struct io_open { |
| 579 | struct file *file; |
| 580 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 581 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 582 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 583 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 584 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | }; |
| 586 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 587 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 588 | struct file *file; |
| 589 | u64 arg; |
| 590 | u32 nr_args; |
| 591 | u32 offset; |
| 592 | }; |
| 593 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 594 | struct io_fadvise { |
| 595 | struct file *file; |
| 596 | u64 offset; |
| 597 | u32 len; |
| 598 | u32 advice; |
| 599 | }; |
| 600 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 601 | struct io_madvise { |
| 602 | struct file *file; |
| 603 | u64 addr; |
| 604 | u32 len; |
| 605 | u32 advice; |
| 606 | }; |
| 607 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 608 | struct io_epoll { |
| 609 | struct file *file; |
| 610 | int epfd; |
| 611 | int op; |
| 612 | int fd; |
| 613 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 614 | }; |
| 615 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 616 | struct io_splice { |
| 617 | struct file *file_out; |
| 618 | struct file *file_in; |
| 619 | loff_t off_out; |
| 620 | loff_t off_in; |
| 621 | u64 len; |
| 622 | unsigned int flags; |
| 623 | }; |
| 624 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 625 | struct io_provide_buf { |
| 626 | struct file *file; |
| 627 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 628 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 629 | __u32 bgid; |
| 630 | __u16 nbufs; |
| 631 | __u16 bid; |
| 632 | }; |
| 633 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 634 | struct io_statx { |
| 635 | struct file *file; |
| 636 | int dfd; |
| 637 | unsigned int mask; |
| 638 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 639 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 640 | struct statx __user *buffer; |
| 641 | }; |
| 642 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 643 | struct io_shutdown { |
| 644 | struct file *file; |
| 645 | int how; |
| 646 | }; |
| 647 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 648 | struct io_rename { |
| 649 | struct file *file; |
| 650 | int old_dfd; |
| 651 | int new_dfd; |
| 652 | struct filename *oldpath; |
| 653 | struct filename *newpath; |
| 654 | int flags; |
| 655 | }; |
| 656 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 657 | struct io_unlink { |
| 658 | struct file *file; |
| 659 | int dfd; |
| 660 | int flags; |
| 661 | struct filename *filename; |
| 662 | }; |
| 663 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 664 | struct io_mkdir { |
| 665 | struct file *file; |
| 666 | int dfd; |
| 667 | umode_t mode; |
| 668 | struct filename *filename; |
| 669 | }; |
| 670 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 671 | struct io_symlink { |
| 672 | struct file *file; |
| 673 | int new_dfd; |
| 674 | struct filename *oldpath; |
| 675 | struct filename *newpath; |
| 676 | }; |
| 677 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 678 | struct io_hardlink { |
| 679 | struct file *file; |
| 680 | int old_dfd; |
| 681 | int new_dfd; |
| 682 | struct filename *oldpath; |
| 683 | struct filename *newpath; |
| 684 | int flags; |
| 685 | }; |
| 686 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 687 | struct io_async_connect { |
| 688 | struct sockaddr_storage address; |
| 689 | }; |
| 690 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 691 | struct io_async_msghdr { |
| 692 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 693 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 694 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 695 | struct sockaddr __user *uaddr; |
| 696 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 697 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 700 | struct io_async_rw { |
| 701 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 702 | const struct iovec *free_iovec; |
| 703 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 704 | struct iov_iter_state iter_state; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 705 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 706 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 707 | }; |
| 708 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 709 | enum { |
| 710 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 711 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 712 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 713 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 714 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 715 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 716 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 717 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 718 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 719 | REQ_F_INFLIGHT_BIT, |
| 720 | REQ_F_CUR_POS_BIT, |
| 721 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 722 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 723 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 724 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 725 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 726 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 727 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 728 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 729 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 730 | REQ_F_ARM_LTIMEOUT_BIT, |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 731 | REQ_F_ASYNC_DATA_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 732 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 733 | REQ_F_NOWAIT_READ_BIT, |
| 734 | REQ_F_NOWAIT_WRITE_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 735 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 736 | |
| 737 | /* not a real bit, just to check we're not overflowing the space */ |
| 738 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | enum { |
| 742 | /* ctx owns file */ |
| 743 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 744 | /* drain existing IO first */ |
| 745 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 746 | /* linked sqes */ |
| 747 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 748 | /* doesn't sever on completion < 0 */ |
| 749 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 750 | /* IOSQE_ASYNC */ |
| 751 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 752 | /* IOSQE_BUFFER_SELECT */ |
| 753 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 754 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 755 | /* fail rest of links */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 756 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 757 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 758 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 759 | /* read/write uses file position */ |
| 760 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 761 | /* must not punt to workers */ |
| 762 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 763 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 764 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 765 | /* needs cleanup */ |
| 766 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 767 | /* already went through poll handler */ |
| 768 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 769 | /* buffer already selected */ |
| 770 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 771 | /* completion is deferred through io_comp_state */ |
| 772 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 773 | /* caller should reissue async */ |
| 774 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 775 | /* supports async reads */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 776 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 777 | /* supports async writes */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 778 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 779 | /* regular file */ |
| 780 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 781 | /* has creds assigned */ |
| 782 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 783 | /* skip refcounting if not set */ |
| 784 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 785 | /* there is a linked timeout that has to be armed */ |
| 786 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 787 | /* ->async_data allocated */ |
| 788 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 789 | }; |
| 790 | |
| 791 | struct async_poll { |
| 792 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 793 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 794 | }; |
| 795 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 796 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 797 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 798 | struct io_task_work { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 799 | union { |
| 800 | struct io_wq_work_node node; |
| 801 | struct llist_node fallback_node; |
| 802 | }; |
| 803 | io_req_tw_func_t func; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 804 | }; |
| 805 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 806 | enum { |
| 807 | IORING_RSRC_FILE = 0, |
| 808 | IORING_RSRC_BUFFER = 1, |
| 809 | }; |
| 810 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 811 | /* |
| 812 | * NOTE! Each of the iocb union members has the file pointer |
| 813 | * as the first entry in their struct definition. So you can |
| 814 | * access the file pointer through any of the sub-structs, |
| 815 | * or directly as just 'ki_filp' in this struct. |
| 816 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 817 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 818 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 819 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 820 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 821 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 822 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 823 | struct io_accept accept; |
| 824 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 825 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 826 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 827 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 828 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 829 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 830 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 831 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 832 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 833 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 834 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 835 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 836 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 837 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 838 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 839 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 840 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 841 | struct io_unlink unlink; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 842 | struct io_mkdir mkdir; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 843 | struct io_symlink symlink; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 844 | struct io_hardlink hardlink; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 845 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 846 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 847 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 848 | /* polled IO has completed */ |
| 849 | u8 iopoll_completed; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 850 | u16 buf_index; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 851 | unsigned int flags; |
| 852 | |
| 853 | u64 user_data; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 854 | u32 result; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 855 | u32 cflags; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 856 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 857 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 858 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 859 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 860 | struct percpu_ref *fixed_rsrc_refs; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 861 | /* store used ubuf, so we can prevent reloading */ |
| 862 | struct io_mapped_ubuf *imu; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 863 | |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 864 | /* used by request caches, completion batching and iopoll */ |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 865 | struct io_wq_work_node comp_list; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 866 | atomic_t refs; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 867 | struct io_kiocb *link; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 868 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 869 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 870 | struct hlist_node hash_node; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 871 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 872 | struct async_poll *apoll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 873 | /* opcode allocated if it needs to store data for async defer */ |
| 874 | void *async_data; |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 875 | struct io_wq_work work; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 876 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 877 | const struct cred *creds; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 878 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 879 | struct io_buffer *kbuf; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 880 | }; |
| 881 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 882 | struct io_tctx_node { |
| 883 | struct list_head ctx_node; |
| 884 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 885 | struct io_ring_ctx *ctx; |
| 886 | }; |
| 887 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 888 | struct io_defer_entry { |
| 889 | struct list_head list; |
| 890 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 891 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 892 | }; |
| 893 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 894 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | /* needs req->file assigned */ |
| 896 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | /* hash wq insertion if file is a regular file */ |
| 898 | unsigned hash_reg_file : 1; |
| 899 | /* unbound wq insertion if file is a non-regular file */ |
| 900 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 901 | /* opcode is not supported by this kernel */ |
| 902 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 903 | /* set if opcode supports polled "wait" */ |
| 904 | unsigned pollin : 1; |
| 905 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 906 | /* op supports buffer selection */ |
| 907 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 908 | /* do prep async if is going to be punted */ |
| 909 | unsigned needs_async_setup : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 910 | /* should block plug */ |
| 911 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 912 | /* size of async data needed, if any */ |
| 913 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 914 | }; |
| 915 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 916 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 917 | [IORING_OP_NOP] = {}, |
| 918 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 919 | .needs_file = 1, |
| 920 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 921 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 922 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 923 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 924 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 925 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 926 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 927 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 928 | .needs_file = 1, |
| 929 | .hash_reg_file = 1, |
| 930 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 931 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 932 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 933 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 934 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 935 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 936 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 937 | .needs_file = 1, |
| 938 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 939 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 940 | .needs_file = 1, |
| 941 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 942 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 943 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 944 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 945 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 946 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 947 | .needs_file = 1, |
| 948 | .hash_reg_file = 1, |
| 949 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 950 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 951 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 952 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 953 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 954 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 955 | .needs_file = 1, |
| 956 | .unbound_nonreg_file = 1, |
| 957 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 958 | [IORING_OP_POLL_REMOVE] = {}, |
| 959 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 960 | .needs_file = 1, |
| 961 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 962 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 963 | .needs_file = 1, |
| 964 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 965 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 966 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 967 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 968 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 969 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 970 | .needs_file = 1, |
| 971 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 972 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 973 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 974 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 975 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 976 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 977 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 978 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 979 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 980 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 981 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 982 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 983 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 984 | .needs_file = 1, |
| 985 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 986 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 987 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 988 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 989 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 990 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 991 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 992 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 993 | .needs_file = 1, |
| 994 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 995 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 996 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 997 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 998 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 999 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1000 | .needs_file = 1, |
| 1001 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1002 | [IORING_OP_OPENAT] = {}, |
| 1003 | [IORING_OP_CLOSE] = {}, |
| 1004 | [IORING_OP_FILES_UPDATE] = {}, |
| 1005 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1006 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1007 | .needs_file = 1, |
| 1008 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1009 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1010 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1011 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1012 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1013 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1014 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1015 | .needs_file = 1, |
Jens Axboe | 7b3188e | 2021-08-30 19:37:41 -0600 | [diff] [blame] | 1016 | .hash_reg_file = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1017 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1018 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1019 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1020 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1021 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1022 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1023 | .needs_file = 1, |
| 1024 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1025 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1026 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1027 | .needs_file = 1, |
| 1028 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1029 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1030 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1031 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1032 | .needs_file = 1, |
| 1033 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1034 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1035 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1036 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1037 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1038 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1039 | [IORING_OP_EPOLL_CTL] = { |
| 1040 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1041 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1042 | [IORING_OP_SPLICE] = { |
| 1043 | .needs_file = 1, |
| 1044 | .hash_reg_file = 1, |
| 1045 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1046 | }, |
| 1047 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1048 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1049 | [IORING_OP_TEE] = { |
| 1050 | .needs_file = 1, |
| 1051 | .hash_reg_file = 1, |
| 1052 | .unbound_nonreg_file = 1, |
| 1053 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1054 | [IORING_OP_SHUTDOWN] = { |
| 1055 | .needs_file = 1, |
| 1056 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1057 | [IORING_OP_RENAMEAT] = {}, |
| 1058 | [IORING_OP_UNLINKAT] = {}, |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 1059 | [IORING_OP_MKDIRAT] = {}, |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 1060 | [IORING_OP_SYMLINKAT] = {}, |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 1061 | [IORING_OP_LINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1062 | }; |
| 1063 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1064 | /* requests with any of those set should undergo io_disarm_next() */ |
| 1065 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) |
| 1066 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1067 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 1068 | static void io_uring_del_tctx_node(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1069 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1070 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1071 | bool cancel_all); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 1072 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1073 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1074 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1075 | long res, unsigned int cflags); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1076 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1077 | static void io_put_req_deferred(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1078 | static void io_dismantle_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1079 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 1080 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 1081 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 1082 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1083 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 1084 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1085 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1086 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1087 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1088 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1089 | static void io_req_task_queue(struct io_kiocb *req); |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1090 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1091 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1092 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1093 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 1094 | unsigned int issue_flags, u32 slot_index); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 1095 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1096 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 1097 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1098 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1099 | static struct kmem_cache *req_cachep; |
| 1100 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1101 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1102 | |
| 1103 | struct sock *io_uring_get_socket(struct file *file) |
| 1104 | { |
| 1105 | #if defined(CONFIG_UNIX) |
| 1106 | if (file->f_op == &io_uring_fops) { |
| 1107 | struct io_ring_ctx *ctx = file->private_data; |
| 1108 | |
| 1109 | return ctx->ring_sock->sk; |
| 1110 | } |
| 1111 | #endif |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1115 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1116 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
| 1117 | { |
| 1118 | if (!*locked) { |
| 1119 | mutex_lock(&ctx->uring_lock); |
| 1120 | *locked = true; |
| 1121 | } |
| 1122 | } |
| 1123 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1124 | #define io_for_each_link(pos, head) \ |
| 1125 | for (pos = (head); pos; pos = pos->link) |
| 1126 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1127 | /* |
| 1128 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1129 | * see commit f958d7b528b1 for details. |
| 1130 | */ |
| 1131 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1132 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1133 | |
| 1134 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1135 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1136 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1137 | return atomic_inc_not_zero(&req->refs); |
| 1138 | } |
| 1139 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1140 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1141 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1142 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
| 1143 | return true; |
| 1144 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1145 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1146 | return atomic_dec_and_test(&req->refs); |
| 1147 | } |
| 1148 | |
| 1149 | static inline void req_ref_put(struct io_kiocb *req) |
| 1150 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1151 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1152 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
| 1153 | } |
| 1154 | |
| 1155 | static inline void req_ref_get(struct io_kiocb *req) |
| 1156 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1157 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1158 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1159 | atomic_inc(&req->refs); |
| 1160 | } |
| 1161 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1162 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
| 1163 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1164 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1165 | __io_submit_flush_completions(ctx); |
| 1166 | } |
| 1167 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1168 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1169 | { |
| 1170 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1171 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1172 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1176 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1177 | { |
| 1178 | __io_req_set_refcount(req, 1); |
| 1179 | } |
| 1180 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1181 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1182 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1183 | struct io_ring_ctx *ctx = req->ctx; |
| 1184 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1185 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1186 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1187 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1191 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1192 | { |
| 1193 | bool got = percpu_ref_tryget(ref); |
| 1194 | |
| 1195 | /* already at zero, wait for ->release() */ |
| 1196 | if (!got) |
| 1197 | wait_for_completion(compl); |
| 1198 | percpu_ref_resurrect(ref); |
| 1199 | if (got) |
| 1200 | percpu_ref_put(ref); |
| 1201 | } |
| 1202 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1203 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1204 | bool cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1205 | { |
| 1206 | struct io_kiocb *req; |
| 1207 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1208 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1209 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1210 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1211 | return true; |
| 1212 | |
| 1213 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1214 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1215 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1216 | } |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 1220 | static inline bool req_has_async_data(struct io_kiocb *req) |
| 1221 | { |
| 1222 | return req->flags & REQ_F_ASYNC_DATA; |
| 1223 | } |
| 1224 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1225 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1226 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1227 | req->flags |= REQ_F_FAIL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1228 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1229 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1230 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1231 | { |
| 1232 | req_set_fail(req); |
| 1233 | req->result = res; |
| 1234 | } |
| 1235 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1236 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1237 | { |
| 1238 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1239 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1240 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1243 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1244 | { |
| 1245 | return !req->timeout.off; |
| 1246 | } |
| 1247 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1248 | static __cold void io_fallback_req_func(struct work_struct *work) |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1249 | { |
| 1250 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1251 | fallback_work.work); |
| 1252 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1253 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1254 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1255 | |
| 1256 | percpu_ref_get(&ctx->refs); |
| 1257 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1258 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1259 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1260 | if (locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1261 | io_submit_flush_completions(ctx); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1262 | mutex_unlock(&ctx->uring_lock); |
| 1263 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1264 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1265 | |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1268 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1269 | { |
| 1270 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1271 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1272 | |
| 1273 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1274 | if (!ctx) |
| 1275 | return NULL; |
| 1276 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * Use 5 bits less than the max cq entries, that should give us around |
| 1279 | * 32 entries per hash list if totally full and uniformly spread. |
| 1280 | */ |
| 1281 | hash_bits = ilog2(p->cq_entries); |
| 1282 | hash_bits -= 5; |
| 1283 | if (hash_bits <= 0) |
| 1284 | hash_bits = 1; |
| 1285 | ctx->cancel_hash_bits = hash_bits; |
| 1286 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1287 | GFP_KERNEL); |
| 1288 | if (!ctx->cancel_hash) |
| 1289 | goto err; |
| 1290 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1291 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1292 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1293 | if (!ctx->dummy_ubuf) |
| 1294 | goto err; |
| 1295 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1296 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1297 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1298 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1299 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1300 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1301 | |
| 1302 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1303 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1304 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1305 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1306 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1307 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1308 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1309 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1310 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1311 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1312 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 1313 | INIT_WQ_LIST(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1314 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1315 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1316 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1317 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1318 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1319 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1320 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1321 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1322 | ctx->submit_state.free_list.next = NULL; |
| 1323 | INIT_WQ_LIST(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1324 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1325 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1326 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1327 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1328 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1329 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1330 | kfree(ctx); |
| 1331 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1334 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1335 | { |
| 1336 | struct io_rings *r = ctx->rings; |
| 1337 | |
| 1338 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1339 | ctx->cq_extra--; |
| 1340 | } |
| 1341 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1342 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1343 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1344 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1345 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1346 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1347 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1348 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1349 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1350 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1351 | } |
| 1352 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1353 | #define FFS_ASYNC_READ 0x1UL |
| 1354 | #define FFS_ASYNC_WRITE 0x2UL |
| 1355 | #ifdef CONFIG_64BIT |
| 1356 | #define FFS_ISREG 0x4UL |
| 1357 | #else |
| 1358 | #define FFS_ISREG 0x0UL |
| 1359 | #endif |
| 1360 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) |
| 1361 | |
| 1362 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1363 | { |
| 1364 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); |
| 1365 | } |
| 1366 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1367 | static inline void io_req_track_inflight(struct io_kiocb *req) |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1368 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1369 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1370 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1371 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1375 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
| 1376 | { |
| 1377 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1378 | } |
| 1379 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1380 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1381 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1382 | if (WARN_ON_ONCE(!req->link)) |
| 1383 | return NULL; |
| 1384 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1385 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1386 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1389 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1390 | __io_req_set_refcount(req->link, 2); |
| 1391 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1395 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1396 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1397 | return NULL; |
| 1398 | return __io_prep_linked_timeout(req); |
| 1399 | } |
| 1400 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1401 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1402 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1403 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1404 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1405 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1406 | if (!(req->flags & REQ_F_CREDS)) { |
| 1407 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1408 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1409 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1410 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1411 | req->work.list.next = NULL; |
| 1412 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1413 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1414 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1415 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1416 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1417 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1418 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1419 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1420 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1421 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1422 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1423 | |
| 1424 | switch (req->opcode) { |
| 1425 | case IORING_OP_SPLICE: |
| 1426 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1427 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1428 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1429 | break; |
| 1430 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1431 | } |
| 1432 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1433 | static void io_prep_async_link(struct io_kiocb *req) |
| 1434 | { |
| 1435 | struct io_kiocb *cur; |
| 1436 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1437 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1438 | struct io_ring_ctx *ctx = req->ctx; |
| 1439 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1440 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1441 | io_for_each_link(cur, req) |
| 1442 | io_prep_async_work(cur); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1443 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1444 | } else { |
| 1445 | io_for_each_link(cur, req) |
| 1446 | io_prep_async_work(cur); |
| 1447 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1448 | } |
| 1449 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1450 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
| 1451 | { |
| 1452 | struct io_submit_state *state = &req->ctx->submit_state; |
| 1453 | |
| 1454 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
| 1455 | } |
| 1456 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1457 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1458 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1459 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1460 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1461 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1462 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1463 | /* must not take the lock, NULL it as a precaution */ |
| 1464 | locked = NULL; |
| 1465 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1466 | BUG_ON(!tctx); |
| 1467 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1468 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1469 | /* init ->work of the whole link before punting */ |
| 1470 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1471 | |
| 1472 | /* |
| 1473 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1474 | * happen, catch it here and ensure the request is marked as |
| 1475 | * canceled. That will make io-wq go through the usual work cancel |
| 1476 | * procedure rather than attempt to run this request (or create a new |
| 1477 | * worker for it). |
| 1478 | */ |
| 1479 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1480 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1481 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1482 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1483 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1484 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1485 | if (link) |
| 1486 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1487 | } |
| 1488 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1489 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1490 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1491 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1492 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1493 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1494 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1495 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1496 | if (status) |
| 1497 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1498 | atomic_set(&req->ctx->cq_timeouts, |
| 1499 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1500 | list_del_init(&req->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1501 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1502 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1506 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1507 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1508 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1509 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1510 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1511 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1512 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1513 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1514 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1515 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1516 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1517 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1518 | } |
| 1519 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1520 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1521 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1522 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1523 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1524 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1525 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1526 | while (!list_empty(&ctx->timeout_list)) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1527 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1528 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1529 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1530 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1531 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1532 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1533 | |
| 1534 | /* |
| 1535 | * Since seq can easily wrap around over time, subtract |
| 1536 | * the last seq at which timeouts were flushed before comparing. |
| 1537 | * Assuming not more than 2^31-1 events have happened since, |
| 1538 | * these subtractions won't have wrapped, so we can check if |
| 1539 | * target is in [last_seq, current_seq] by comparing the two. |
| 1540 | */ |
| 1541 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1542 | events_got = seq - ctx->cq_last_tm_flush; |
| 1543 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1544 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1545 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1546 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1547 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1548 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1549 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1550 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1551 | } |
| 1552 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1553 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1554 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1555 | if (ctx->off_timeout_used) |
| 1556 | io_flush_timeouts(ctx); |
| 1557 | if (ctx->drain_active) |
| 1558 | io_queue_deferred(ctx); |
| 1559 | } |
| 1560 | |
| 1561 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1562 | { |
| 1563 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1564 | __io_commit_cqring_flush(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1565 | /* order cqe stores with ring update */ |
| 1566 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1567 | } |
| 1568 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1569 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1570 | { |
| 1571 | struct io_rings *r = ctx->rings; |
| 1572 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1573 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1574 | } |
| 1575 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1576 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1577 | { |
| 1578 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1579 | } |
| 1580 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1581 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1582 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1583 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1584 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1585 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1586 | /* |
| 1587 | * writes to the cq entry need to come after reading head; the |
| 1588 | * control dependency is enough as we're using WRITE_ONCE to |
| 1589 | * fill the cq entry |
| 1590 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1591 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1592 | return NULL; |
| 1593 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1594 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1595 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1598 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1599 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1600 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1601 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1602 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1603 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1604 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1607 | /* |
| 1608 | * This should only get called when at least one event has been posted. |
| 1609 | * Some applications rely on the eventfd notification count only changing |
| 1610 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1611 | * 1:1 relationship between how many times this function is called (and |
| 1612 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1613 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1614 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1615 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1616 | /* |
| 1617 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1618 | * io_should_wake() handle the termination of the loop and only |
| 1619 | * wake as many waiters as we need to. |
| 1620 | */ |
| 1621 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1622 | wake_up_all(&ctx->cq_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1623 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1624 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1625 | } |
| 1626 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1627 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1628 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1629 | /* see waitqueue_active() comment */ |
| 1630 | smp_mb(); |
| 1631 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1632 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1633 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1634 | wake_up_all(&ctx->cq_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1635 | } |
| 1636 | if (io_should_trigger_evfd(ctx)) |
| 1637 | eventfd_signal(ctx->cq_ev_fd, 1); |
| 1638 | } |
| 1639 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1640 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1641 | 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] | 1642 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1643 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1644 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1645 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1646 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1647 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1648 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1649 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1650 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1651 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1652 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1653 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1654 | if (!cqe && !force) |
| 1655 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1656 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1657 | struct io_overflow_cqe, list); |
| 1658 | if (cqe) |
| 1659 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1660 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1661 | io_account_cq_overflow(ctx); |
| 1662 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1663 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1664 | list_del(&ocqe->list); |
| 1665 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1668 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1669 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1670 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1671 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1672 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1673 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1674 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1675 | if (posted) |
| 1676 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1677 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1678 | if (posted) |
| 1679 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1680 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1683 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1684 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1685 | bool ret = true; |
| 1686 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1687 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1688 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1689 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1690 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1691 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1692 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1693 | mutex_unlock(&ctx->uring_lock); |
| 1694 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1695 | |
| 1696 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1699 | /* must to be called somewhat shortly after putting a request */ |
| 1700 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1701 | { |
| 1702 | struct io_uring_task *tctx = task->io_uring; |
| 1703 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1704 | if (likely(task == current)) { |
| 1705 | tctx->cached_refs += nr; |
| 1706 | } else { |
| 1707 | percpu_counter_sub(&tctx->inflight, nr); |
| 1708 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1709 | wake_up(&tctx->wait); |
| 1710 | put_task_struct_many(task, nr); |
| 1711 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1712 | } |
| 1713 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1714 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1715 | { |
| 1716 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1717 | |
| 1718 | percpu_counter_add(&tctx->inflight, refill); |
| 1719 | refcount_add(refill, ¤t->usage); |
| 1720 | tctx->cached_refs += refill; |
| 1721 | } |
| 1722 | |
| 1723 | static inline void io_get_task_refs(int nr) |
| 1724 | { |
| 1725 | struct io_uring_task *tctx = current->io_uring; |
| 1726 | |
| 1727 | tctx->cached_refs -= nr; |
| 1728 | if (unlikely(tctx->cached_refs < 0)) |
| 1729 | io_task_refs_refill(tctx); |
| 1730 | } |
| 1731 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1732 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
| 1733 | long res, unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1734 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1735 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1736 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1737 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1738 | if (!ocqe) { |
| 1739 | /* |
| 1740 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1741 | * or cannot allocate an overflow entry, then we need to drop it |
| 1742 | * on the floor. |
| 1743 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1744 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1745 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1746 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1747 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1748 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1749 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1750 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1751 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1752 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1753 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1754 | ocqe->cqe.res = res; |
| 1755 | ocqe->cqe.flags = cflags; |
| 1756 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1757 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1760 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1761 | long res, unsigned int cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1762 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1763 | struct io_uring_cqe *cqe; |
| 1764 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1765 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* |
| 1768 | * If we can't get a cq entry, userspace overflowed the |
| 1769 | * submission (by quite a lot). Increment the overflow count in |
| 1770 | * the ring. |
| 1771 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1772 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1773 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1774 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1775 | WRITE_ONCE(cqe->res, res); |
| 1776 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1777 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1778 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1779 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1782 | /* not as hot to bloat with inlining */ |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1783 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1784 | long res, unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1785 | { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1786 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1789 | static void io_req_complete_post(struct io_kiocb *req, long res, |
| 1790 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1791 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1792 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1793 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1794 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1795 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1796 | /* |
| 1797 | * If we're the last reference to this request, add to our locked |
| 1798 | * free_list cache. |
| 1799 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1800 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1801 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1802 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1803 | io_disarm_next(req); |
| 1804 | if (req->link) { |
| 1805 | io_req_task_queue(req->link); |
| 1806 | req->link = NULL; |
| 1807 | } |
| 1808 | } |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1809 | io_dismantle_req(req); |
| 1810 | io_put_task(req->task, 1); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1811 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1812 | ctx->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1813 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1814 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1815 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1816 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1819 | static inline void io_req_complete_state(struct io_kiocb *req, long res, |
| 1820 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1821 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1822 | req->result = res; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 1823 | req->cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1824 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1825 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1826 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1827 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1828 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1829 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1830 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1831 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1832 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1833 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1834 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1835 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1836 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1837 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1838 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1841 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
| 1842 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1843 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1844 | io_req_complete_post(req, res, 0); |
| 1845 | } |
| 1846 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 1847 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 1848 | { |
| 1849 | /* |
| 1850 | * We don't submit, fail them all, for that replace hardlinks with |
| 1851 | * normal links. Extra REQ_F_LINK is tolerated. |
| 1852 | */ |
| 1853 | req->flags &= ~REQ_F_HARDLINK; |
| 1854 | req->flags |= REQ_F_LINK; |
| 1855 | io_req_complete_failed(req, req->result); |
| 1856 | } |
| 1857 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1858 | /* |
| 1859 | * Don't initialise the fields below on every allocation, but do that in |
| 1860 | * advance and keep them valid across allocations. |
| 1861 | */ |
| 1862 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1863 | { |
| 1864 | req->ctx = ctx; |
| 1865 | req->link = NULL; |
| 1866 | req->async_data = NULL; |
| 1867 | /* not necessary, but safer to zero */ |
| 1868 | req->result = 0; |
| 1869 | } |
| 1870 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1871 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1872 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1873 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1874 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1875 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1876 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1877 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1880 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1881 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1882 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1883 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1884 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1885 | /* |
| 1886 | * If we have more than a batch's worth of requests in our IRQ side |
| 1887 | * locked cache, grab the lock and move them over to our submission |
| 1888 | * side cache. |
| 1889 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1890 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1891 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1892 | return !!state->free_list.next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1895 | /* |
| 1896 | * A request might get retired back into the request caches even before opcode |
| 1897 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 1898 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 1899 | * and with extra caution to not get a request that is still worked on. |
| 1900 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1901 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1902 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1903 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1904 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1905 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1906 | void *reqs[IO_REQ_ALLOC_BATCH]; |
| 1907 | struct io_kiocb *req; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1908 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1909 | |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1910 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 1911 | return true; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1912 | |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1913 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1914 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1915 | /* |
| 1916 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1917 | * retry single alloc to be on the safe side. |
| 1918 | */ |
| 1919 | if (unlikely(ret <= 0)) { |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1920 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1921 | if (!reqs[0]) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 1922 | return false; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1923 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1924 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1925 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 1926 | percpu_ref_get_many(&ctx->refs, ret); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1927 | for (i = 0; i < ret; i++) { |
| 1928 | req = reqs[i]; |
| 1929 | |
| 1930 | io_preinit_req(req, ctx); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1931 | wq_stack_add_head(&req->comp_list, &state->free_list); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1932 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 1933 | return true; |
| 1934 | } |
| 1935 | |
| 1936 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) |
| 1937 | { |
| 1938 | if (unlikely(!ctx->submit_state.free_list.next)) |
| 1939 | return __io_alloc_req_refill(ctx); |
| 1940 | return true; |
| 1941 | } |
| 1942 | |
| 1943 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
| 1944 | { |
| 1945 | struct io_wq_work_node *node; |
| 1946 | |
| 1947 | node = wq_stack_extract(&ctx->submit_state.free_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1948 | return container_of(node, struct io_kiocb, comp_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1951 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1952 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1953 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1954 | fput(file); |
| 1955 | } |
| 1956 | |
Pavel Begunkov | 6b63952 | 2021-09-08 16:40:50 +0100 | [diff] [blame] | 1957 | static inline void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1958 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1959 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1960 | |
Pavel Begunkov | 867f8fa | 2021-10-04 20:02:58 +0100 | [diff] [blame^] | 1961 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 1962 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1963 | if (!(flags & REQ_F_FIXED_FILE)) |
| 1964 | io_put_file(req->file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1965 | if (req->fixed_rsrc_refs) |
| 1966 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1967 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1968 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1969 | static __cold void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1970 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1971 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1972 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1973 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1974 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1975 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1976 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1977 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 1978 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1979 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1980 | } |
| 1981 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1982 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1983 | { |
| 1984 | struct io_kiocb *nxt = req->link; |
| 1985 | |
| 1986 | req->link = nxt->link; |
| 1987 | nxt->link = NULL; |
| 1988 | } |
| 1989 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1990 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 1991 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 1992 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1993 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1994 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1995 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 1996 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1997 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1998 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1999 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2000 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2001 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2002 | list_del(&link->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2003 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2004 | -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2005 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2006 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2007 | } |
| 2008 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2009 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2010 | } |
| 2011 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2012 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2013 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2014 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2015 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2016 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2017 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2018 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2019 | long res = -ECANCELED; |
| 2020 | |
| 2021 | if (link->flags & REQ_F_FAIL) |
| 2022 | res = link->result; |
| 2023 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2024 | nxt = link->link; |
| 2025 | link->link = NULL; |
| 2026 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2027 | trace_io_uring_fail_link(req, link); |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2028 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2029 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2030 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2031 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2032 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2033 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2034 | static bool io_disarm_next(struct io_kiocb *req) |
| 2035 | __must_hold(&req->ctx->completion_lock) |
| 2036 | { |
| 2037 | bool posted = false; |
| 2038 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2039 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2040 | struct io_kiocb *link = req->link; |
| 2041 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2042 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2043 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2044 | io_remove_next_linked(req); |
| 2045 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2046 | -ECANCELED, 0); |
| 2047 | io_put_req_deferred(link); |
| 2048 | posted = true; |
| 2049 | } |
| 2050 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2051 | struct io_ring_ctx *ctx = req->ctx; |
| 2052 | |
| 2053 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2054 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2055 | spin_unlock_irq(&ctx->timeout_lock); |
| 2056 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2057 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2058 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2059 | posted |= (req->link != NULL); |
| 2060 | io_fail_links(req); |
| 2061 | } |
| 2062 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2063 | } |
| 2064 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2065 | static void __io_req_find_next_prep(struct io_kiocb *req) |
| 2066 | { |
| 2067 | struct io_ring_ctx *ctx = req->ctx; |
| 2068 | bool posted; |
| 2069 | |
| 2070 | spin_lock(&ctx->completion_lock); |
| 2071 | posted = io_disarm_next(req); |
| 2072 | if (posted) |
| 2073 | io_commit_cqring(req->ctx); |
| 2074 | spin_unlock(&ctx->completion_lock); |
| 2075 | if (posted) |
| 2076 | io_cqring_ev_posted(ctx); |
| 2077 | } |
| 2078 | |
| 2079 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2080 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2081 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2082 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2083 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
| 2084 | return NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2085 | /* |
| 2086 | * If LINK is set, we have dependent requests in this chain. If we |
| 2087 | * didn't fail this request, queue the first one up, moving any other |
| 2088 | * dependencies to the next request. In case of failure, fail the rest |
| 2089 | * of the chain. |
| 2090 | */ |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2091 | if (unlikely(req->flags & IO_DISARM_MASK)) |
| 2092 | __io_req_find_next_prep(req); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2093 | nxt = req->link; |
| 2094 | req->link = NULL; |
| 2095 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2096 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2097 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2098 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2099 | { |
| 2100 | if (!ctx) |
| 2101 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2102 | if (*locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2103 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2104 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2105 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2106 | } |
| 2107 | percpu_ref_put(&ctx->refs); |
| 2108 | } |
| 2109 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2110 | static void tctx_task_work(struct callback_head *cb) |
| 2111 | { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2112 | bool locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2113 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2114 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2115 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2116 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2117 | while (1) { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2118 | struct io_wq_work_node *node; |
| 2119 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2120 | if (!tctx->task_list.first && locked) |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2121 | io_submit_flush_completions(ctx); |
| 2122 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2123 | spin_lock_irq(&tctx->task_lock); |
Pavel Begunkov | c6538be | 2021-06-17 18:14:08 +0100 | [diff] [blame] | 2124 | node = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2125 | INIT_WQ_LIST(&tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2126 | if (!node) |
| 2127 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2128 | spin_unlock_irq(&tctx->task_lock); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2129 | if (!node) |
| 2130 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2131 | |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2132 | do { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2133 | struct io_wq_work_node *next = node->next; |
| 2134 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2135 | io_task_work.node); |
| 2136 | |
| 2137 | if (req->ctx != ctx) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2138 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2139 | ctx = req->ctx; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2140 | /* if not contended, grab and improve batching */ |
| 2141 | locked = mutex_trylock(&ctx->uring_lock); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2142 | percpu_ref_get(&ctx->refs); |
| 2143 | } |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2144 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2145 | node = next; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2146 | } while (node); |
| 2147 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2148 | cond_resched(); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2149 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2150 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2151 | ctx_flush_and_put(ctx, &locked); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2154 | static void io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2155 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2156 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2157 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2158 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2159 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2160 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2161 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2162 | |
| 2163 | WARN_ON_ONCE(!tctx); |
| 2164 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2165 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2166 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2167 | running = tctx->task_running; |
| 2168 | if (!running) |
| 2169 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2170 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2171 | |
| 2172 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2173 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2174 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2175 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2176 | /* |
| 2177 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2178 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2179 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2180 | * will do the job. |
| 2181 | */ |
| 2182 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | d97ec62 | 2021-09-08 16:40:53 +0100 | [diff] [blame] | 2183 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
| 2184 | if (notify == TWA_NONE) |
| 2185 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2186 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2187 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2188 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2189 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2190 | tctx->task_running = false; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2191 | node = tctx->task_list.first; |
| 2192 | INIT_WQ_LIST(&tctx->task_list); |
| 2193 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2194 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2195 | while (node) { |
| 2196 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2197 | node = node->next; |
| 2198 | if (llist_add(&req->io_task_work.fallback_node, |
| 2199 | &req->ctx->fallback_llist)) |
| 2200 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2201 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2204 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2205 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2206 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2207 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2208 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2209 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2210 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2211 | } |
| 2212 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2213 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2214 | { |
| 2215 | struct io_ring_ctx *ctx = req->ctx; |
| 2216 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2217 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2218 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2219 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2220 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2221 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2222 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2223 | } |
| 2224 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2225 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2226 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2227 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2228 | req->io_task_work.func = io_req_task_cancel; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2229 | io_req_task_work_add(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2232 | static void io_req_task_queue(struct io_kiocb *req) |
| 2233 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2234 | req->io_task_work.func = io_req_task_submit; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2235 | io_req_task_work_add(req); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2238 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2239 | { |
| 2240 | req->io_task_work.func = io_queue_async_work; |
| 2241 | io_req_task_work_add(req); |
| 2242 | } |
| 2243 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2244 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2245 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2246 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2247 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2248 | if (nxt) |
| 2249 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2250 | } |
| 2251 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2252 | static void io_free_req(struct io_kiocb *req) |
| 2253 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2254 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2255 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2256 | } |
| 2257 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2258 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2259 | { |
| 2260 | io_free_req(req); |
| 2261 | } |
| 2262 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2263 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2264 | struct io_wq_work_node *node) |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2265 | __must_hold(&ctx->uring_lock) |
| 2266 | { |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2267 | struct task_struct *task = NULL; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2268 | int task_refs = 0; |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2269 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2270 | do { |
| 2271 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2272 | comp_list); |
| 2273 | |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2274 | if (!req_ref_put_and_test(req)) { |
| 2275 | node = req->comp_list.next; |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2276 | continue; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2277 | } |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2278 | |
| 2279 | io_queue_next(req); |
| 2280 | io_dismantle_req(req); |
| 2281 | |
| 2282 | if (req->task != task) { |
| 2283 | if (task) |
| 2284 | io_put_task(task, task_refs); |
| 2285 | task = req->task; |
| 2286 | task_refs = 0; |
| 2287 | } |
| 2288 | task_refs++; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2289 | node = req->comp_list.next; |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2290 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2291 | } while (node); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2292 | |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2293 | if (task) |
| 2294 | io_put_task(task, task_refs); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2297 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2298 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2299 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2300 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2301 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2302 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2303 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2304 | wq_list_for_each(node, prev, &state->compl_reqs) { |
| 2305 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2306 | comp_list); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2307 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2308 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 2309 | req->cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2310 | } |
| 2311 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2312 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2313 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2314 | |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2315 | io_free_batch_list(ctx, state->compl_reqs.first); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2316 | INIT_WQ_LIST(&state->compl_reqs); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2317 | } |
| 2318 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2319 | /* |
| 2320 | * Drop reference to request, return next in chain (if there is one) if this |
| 2321 | * was the last reference to this request. |
| 2322 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2323 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2324 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2325 | struct io_kiocb *nxt = NULL; |
| 2326 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2327 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2328 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2329 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2330 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2331 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2334 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2335 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2336 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2337 | io_free_req(req); |
| 2338 | } |
| 2339 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2340 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2341 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2342 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2343 | req->io_task_work.func = io_free_req_work; |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2344 | io_req_task_work_add(req); |
| 2345 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2348 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2349 | { |
| 2350 | /* See comment at the top of this file */ |
| 2351 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2352 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2353 | } |
| 2354 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2355 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2356 | { |
| 2357 | struct io_rings *rings = ctx->rings; |
| 2358 | |
| 2359 | /* make sure SQ entry isn't read before tail */ |
| 2360 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2361 | } |
| 2362 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2363 | 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] | 2364 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2365 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2366 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2367 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2368 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2369 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2370 | kfree(kbuf); |
| 2371 | return cflags; |
| 2372 | } |
| 2373 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2374 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2375 | { |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2376 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 2377 | return 0; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 2378 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2379 | } |
| 2380 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2381 | static inline bool io_run_task_work(void) |
| 2382 | { |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2383 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2384 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2385 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2386 | return true; |
| 2387 | } |
| 2388 | |
| 2389 | return false; |
| 2390 | } |
| 2391 | |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2392 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2393 | { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2394 | struct io_wq_work_node *pos, *start, *prev; |
Christoph Hellwig | d729cf9 | 2021-10-12 13:12:20 +0200 | [diff] [blame] | 2395 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2396 | DEFINE_IO_COMP_BATCH(iob); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2397 | int nr_events = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2398 | |
| 2399 | /* |
| 2400 | * Only spin for completions if we don't have multiple devices hanging |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2401 | * off our complete list. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2402 | */ |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2403 | if (ctx->poll_multi_queue || force_nonspin) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2404 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2405 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2406 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
| 2407 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2408 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2409 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2410 | |
| 2411 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2412 | * Move completed and retryable entries to our local lists. |
| 2413 | * If we find a request that requires polling, break out |
| 2414 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2415 | */ |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2416 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2417 | break; |
| 2418 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2419 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2420 | if (unlikely(ret < 0)) |
| 2421 | return ret; |
| 2422 | else if (ret) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2423 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2424 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2425 | /* iopoll may have completed current req */ |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2426 | if (!rq_list_empty(iob.req_list) || |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2427 | READ_ONCE(req->iopoll_completed)) |
| 2428 | break; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2431 | if (!rq_list_empty(iob.req_list)) |
| 2432 | iob.complete(&iob); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2433 | else if (!pos) |
| 2434 | return 0; |
| 2435 | |
| 2436 | prev = start; |
| 2437 | wq_list_for_each_resume(pos, prev) { |
| 2438 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
| 2439 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2440 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
| 2441 | if (!smp_load_acquire(&req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2442 | break; |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2443 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2444 | io_put_rw_kbuf(req)); |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2445 | nr_events++; |
| 2446 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2447 | |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2448 | if (unlikely(!nr_events)) |
| 2449 | return 0; |
| 2450 | |
| 2451 | io_commit_cqring(ctx); |
| 2452 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2453 | pos = start ? start->next : ctx->iopoll_list.first; |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2454 | wq_list_cut(&ctx->iopoll_list, prev, start); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2455 | io_free_batch_list(ctx, pos); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2456 | return nr_events; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2460 | * We can't just wait for polled events to come to us, we have to actively |
| 2461 | * find and complete them. |
| 2462 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2463 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2464 | { |
| 2465 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2466 | return; |
| 2467 | |
| 2468 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2469 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2470 | /* let it sleep and repeat later if can't complete a request */ |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2471 | if (io_do_iopoll(ctx, true) == 0) |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2472 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2473 | /* |
| 2474 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2475 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2476 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2477 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2478 | if (need_resched()) { |
| 2479 | mutex_unlock(&ctx->uring_lock); |
| 2480 | cond_resched(); |
| 2481 | mutex_lock(&ctx->uring_lock); |
| 2482 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2483 | } |
| 2484 | mutex_unlock(&ctx->uring_lock); |
| 2485 | } |
| 2486 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2487 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2488 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2489 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2490 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2491 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2492 | /* |
| 2493 | * We disallow the app entering submit/complete with polling, but we |
| 2494 | * still need to lock the ring to prevent racing with polled issue |
| 2495 | * that got punted to a workqueue. |
| 2496 | */ |
| 2497 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2498 | /* |
| 2499 | * Don't enter poll loop if we already have events pending. |
| 2500 | * If we do, we can potentially be spinning for commands that |
| 2501 | * already triggered a CQE (eg in error). |
| 2502 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2503 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2504 | __io_cqring_overflow_flush(ctx, false); |
| 2505 | if (io_cqring_events(ctx)) |
| 2506 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2507 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2508 | /* |
| 2509 | * If a submit got punted to a workqueue, we can have the |
| 2510 | * application entering polling for a command before it gets |
| 2511 | * issued. That app will hold the uring_lock for the duration |
| 2512 | * of the poll right here, so we need to take a breather every |
| 2513 | * now and then to ensure that the issue has a chance to add |
| 2514 | * the poll to the issued list. Otherwise we can spin here |
| 2515 | * forever, while the workqueue is stuck trying to acquire the |
| 2516 | * very same mutex. |
| 2517 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2518 | if (wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2519 | u32 tail = ctx->cached_cq_tail; |
| 2520 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2521 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2522 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2523 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2524 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2525 | /* some requests don't go through iopoll_list */ |
| 2526 | if (tail != ctx->cached_cq_tail || |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2527 | wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2528 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2529 | } |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2530 | ret = io_do_iopoll(ctx, !min); |
| 2531 | if (ret < 0) |
| 2532 | break; |
| 2533 | nr_events += ret; |
| 2534 | ret = 0; |
| 2535 | } while (nr_events < min && !need_resched()); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2536 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2537 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2538 | return ret; |
| 2539 | } |
| 2540 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2541 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2542 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2543 | /* |
| 2544 | * Tell lockdep we inherited freeze protection from submission |
| 2545 | * thread. |
| 2546 | */ |
| 2547 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2548 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2549 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2550 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2551 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2555 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2556 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2557 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2558 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2559 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2560 | if (!req_has_async_data(req)) |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2561 | return !io_req_prep_async(req); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 2562 | iov_iter_restore(&rw->iter, &rw->iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2563 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2564 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2565 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2566 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2567 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2568 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2569 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2570 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2571 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2572 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2573 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2574 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2575 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2576 | /* |
| 2577 | * If ref is dying, we might be running poll reap from the exit work. |
| 2578 | * Don't attempt to reissue from that path, just let it fail with |
| 2579 | * -EAGAIN. |
| 2580 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2581 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2582 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2583 | /* |
| 2584 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2585 | * not in the original thread group (or in task context). |
| 2586 | */ |
| 2587 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2588 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2589 | return true; |
| 2590 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2591 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2592 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2593 | { |
| 2594 | return false; |
| 2595 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2596 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2597 | { |
| 2598 | return false; |
| 2599 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2600 | #endif |
| 2601 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2602 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2603 | { |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2604 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2605 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2606 | if (res != req->result) { |
| 2607 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2608 | io_rw_should_reissue(req)) { |
| 2609 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2610 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2611 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2612 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2613 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2614 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2615 | return false; |
| 2616 | } |
| 2617 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2618 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2619 | { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2620 | unsigned int cflags = io_put_rw_kbuf(req); |
| 2621 | long res = req->result; |
| 2622 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2623 | if (*locked) { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2624 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2625 | io_req_add_compl_list(req); |
| 2626 | } else { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2627 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2628 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2632 | unsigned int issue_flags) |
| 2633 | { |
| 2634 | if (__io_complete_rw_common(req, res)) |
| 2635 | return; |
Pavel Begunkov | 6363785 | 2021-09-02 00:38:22 +0100 | [diff] [blame] | 2636 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2640 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2641 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2642 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2643 | if (__io_complete_rw_common(req, res)) |
| 2644 | return; |
| 2645 | req->result = res; |
| 2646 | req->io_task_work.func = io_req_task_complete; |
| 2647 | io_req_task_work_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2650 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2651 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2652 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2653 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2654 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2655 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2656 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2657 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2658 | req->flags |= REQ_F_REISSUE; |
| 2659 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2660 | } |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2661 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2662 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2663 | req->result = res; |
| 2664 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
| 2665 | smp_store_release(&req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | /* |
| 2669 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2670 | * Adding the kiocb to the list AFTER submission ensures that we don't |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2671 | * find it from a io_do_iopoll() thread before the issuer is done |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2672 | * accessing the kiocb cookie. |
| 2673 | */ |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2674 | static void io_iopoll_req_issued(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2675 | { |
| 2676 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2677 | const bool in_async = io_wq_current_is_worker(); |
| 2678 | |
| 2679 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 2680 | if (unlikely(in_async)) |
| 2681 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2682 | |
| 2683 | /* |
| 2684 | * Track whether we have multiple files in our lists. This will impact |
| 2685 | * how we do polling eventually, not spinning if we're on potentially |
| 2686 | * different devices. |
| 2687 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2688 | if (wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2689 | ctx->poll_multi_queue = false; |
| 2690 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2691 | struct io_kiocb *list_req; |
| 2692 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2693 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
| 2694 | comp_list); |
Christoph Hellwig | 30da1b4 | 2021-10-12 13:12:14 +0200 | [diff] [blame] | 2695 | if (list_req->file != req->file) |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2696 | ctx->poll_multi_queue = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * For fast devices, IO may have already completed. If it has, add |
| 2701 | * it to the front so we find it first. |
| 2702 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2703 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2704 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2705 | else |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2706 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2707 | |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2708 | if (unlikely(in_async)) { |
| 2709 | /* |
| 2710 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2711 | * in sq thread task context or in io worker task context. If |
| 2712 | * current task context is sq thread, we don't need to check |
| 2713 | * whether should wake up sq thread. |
| 2714 | */ |
| 2715 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2716 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2717 | wake_up(&ctx->sq_data->wait); |
| 2718 | |
| 2719 | mutex_unlock(&ctx->uring_lock); |
| 2720 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2723 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2724 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2725 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2726 | } |
| 2727 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2728 | /* |
| 2729 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2730 | * any file. For now, just ensure that anything potentially problematic is done |
| 2731 | * inline. |
| 2732 | */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2733 | static bool __io_file_supports_nowait(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2734 | { |
| 2735 | umode_t mode = file_inode(file)->i_mode; |
| 2736 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2737 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2738 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2739 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2740 | return true; |
| 2741 | return false; |
| 2742 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2743 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2744 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2745 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2746 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2747 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2748 | file->f_op != &io_uring_fops) |
| 2749 | return true; |
| 2750 | return false; |
| 2751 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2752 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2753 | /* any ->read/write should understand O_NONBLOCK */ |
| 2754 | if (file->f_flags & O_NONBLOCK) |
| 2755 | return true; |
| 2756 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2757 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2758 | return false; |
| 2759 | |
| 2760 | if (rw == READ) |
| 2761 | return file->f_op->read_iter != NULL; |
| 2762 | |
| 2763 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2766 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2767 | { |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2768 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2769 | return true; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2770 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2771 | return true; |
| 2772 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2773 | return __io_file_supports_nowait(req->file, rw); |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2776 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2777 | int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2778 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2779 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2780 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2781 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2782 | unsigned ioprio; |
| 2783 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2784 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 2785 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2786 | req->flags |= REQ_F_ISREG; |
| 2787 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2788 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2789 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2790 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2791 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2792 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2793 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2794 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2795 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2796 | if (unlikely(ret)) |
| 2797 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2798 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2799 | /* |
| 2800 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 2801 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 2802 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 2803 | */ |
| 2804 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 2805 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2806 | req->flags |= REQ_F_NOWAIT; |
| 2807 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2808 | ioprio = READ_ONCE(sqe->ioprio); |
| 2809 | if (ioprio) { |
| 2810 | ret = ioprio_check_cap(ioprio); |
| 2811 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2812 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2813 | |
| 2814 | kiocb->ki_ioprio = ioprio; |
| 2815 | } else |
| 2816 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2817 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2818 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2819 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2820 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2821 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2822 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 2823 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2824 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2825 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2826 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2827 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2828 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2829 | kiocb->ki_complete = io_complete_rw; |
| 2830 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2831 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2832 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2833 | req->opcode == IORING_OP_WRITE_FIXED) { |
| 2834 | req->imu = NULL; |
| 2835 | io_req_set_rsrc_node(req); |
| 2836 | } |
| 2837 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2838 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2839 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2840 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2841 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2845 | { |
| 2846 | switch (ret) { |
| 2847 | case -EIOCBQUEUED: |
| 2848 | break; |
| 2849 | case -ERESTARTSYS: |
| 2850 | case -ERESTARTNOINTR: |
| 2851 | case -ERESTARTNOHAND: |
| 2852 | case -ERESTART_RESTARTBLOCK: |
| 2853 | /* |
| 2854 | * We can't just restart the syscall, since previously |
| 2855 | * submitted sqes may already be in progress. Just fail this |
| 2856 | * IO with EINTR. |
| 2857 | */ |
| 2858 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2859 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2860 | default: |
| 2861 | kiocb->ki_complete(kiocb, ret, 0); |
| 2862 | } |
| 2863 | } |
| 2864 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2865 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2866 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2867 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2868 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2869 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2870 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2871 | /* add previously done IO, if any */ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2872 | if (req_has_async_data(req) && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2873 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2874 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2875 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2876 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2877 | } |
| 2878 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2879 | if (req->flags & REQ_F_CUR_POS) |
| 2880 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2881 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2882 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2883 | else |
| 2884 | io_rw_done(kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2885 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2886 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2887 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 2888 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2889 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2890 | } else { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2891 | unsigned int cflags = io_put_rw_kbuf(req); |
| 2892 | struct io_ring_ctx *ctx = req->ctx; |
| 2893 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2894 | req_set_fail(req); |
Hao Xu | 14cfbb7 | 2021-10-14 22:04:00 +0800 | [diff] [blame] | 2895 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2896 | mutex_lock(&ctx->uring_lock); |
| 2897 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2898 | mutex_unlock(&ctx->uring_lock); |
| 2899 | } else { |
| 2900 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2901 | } |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2902 | } |
| 2903 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2904 | } |
| 2905 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2906 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 2907 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2908 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2909 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2910 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2911 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2912 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2913 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2914 | return -EFAULT; |
| 2915 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 2916 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2917 | return -EFAULT; |
| 2918 | |
| 2919 | /* |
| 2920 | * May not be a start of buffer, set size appropriately |
| 2921 | * and advance us to the beginning. |
| 2922 | */ |
| 2923 | offset = buf_addr - imu->ubuf; |
| 2924 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2925 | |
| 2926 | if (offset) { |
| 2927 | /* |
| 2928 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2929 | * using the latter parts of a big fixed buffer - it iterates |
| 2930 | * over each segment manually. We can cheat a bit here, because |
| 2931 | * we know that: |
| 2932 | * |
| 2933 | * 1) it's a BVEC iter, we set it up |
| 2934 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2935 | * first and last bvec |
| 2936 | * |
| 2937 | * So just find our index, and adjust the iterator afterwards. |
| 2938 | * If the offset is within the first bvec (or the whole first |
| 2939 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2940 | * since we can just skip the first segment, which may not |
| 2941 | * be PAGE_SIZE aligned. |
| 2942 | */ |
| 2943 | const struct bio_vec *bvec = imu->bvec; |
| 2944 | |
| 2945 | if (offset <= bvec->bv_len) { |
| 2946 | iov_iter_advance(iter, offset); |
| 2947 | } else { |
| 2948 | unsigned long seg_skip; |
| 2949 | |
| 2950 | /* skip first vec */ |
| 2951 | offset -= bvec->bv_len; |
| 2952 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2953 | |
| 2954 | iter->bvec = bvec + seg_skip; |
| 2955 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2956 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2957 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2961 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2964 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 2965 | { |
| 2966 | struct io_ring_ctx *ctx = req->ctx; |
| 2967 | struct io_mapped_ubuf *imu = req->imu; |
| 2968 | u16 index, buf_index = req->buf_index; |
| 2969 | |
| 2970 | if (likely(!imu)) { |
| 2971 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2972 | return -EFAULT; |
| 2973 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2974 | imu = READ_ONCE(ctx->user_bufs[index]); |
| 2975 | req->imu = imu; |
| 2976 | } |
| 2977 | return __io_import_fixed(req, rw, iter, imu); |
| 2978 | } |
| 2979 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2980 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2981 | { |
| 2982 | if (needs_lock) |
| 2983 | mutex_unlock(&ctx->uring_lock); |
| 2984 | } |
| 2985 | |
| 2986 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2987 | { |
| 2988 | /* |
| 2989 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2990 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2991 | * The only exception is when we've detached the request and issue it |
| 2992 | * from an async worker thread, grab the lock for that case. |
| 2993 | */ |
| 2994 | if (needs_lock) |
| 2995 | mutex_lock(&ctx->uring_lock); |
| 2996 | } |
| 2997 | |
| 2998 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 2999 | int bgid, bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3000 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3001 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3002 | struct io_buffer *head; |
| 3003 | |
| 3004 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3005 | return kbuf; |
| 3006 | |
| 3007 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3008 | |
| 3009 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3010 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3011 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3012 | if (head) { |
| 3013 | if (!list_empty(&head->list)) { |
| 3014 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3015 | list); |
| 3016 | list_del(&kbuf->list); |
| 3017 | } else { |
| 3018 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3019 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3020 | } |
| 3021 | if (*len > kbuf->len) |
| 3022 | *len = kbuf->len; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3023 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3024 | req->kbuf = kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3025 | } else { |
| 3026 | kbuf = ERR_PTR(-ENOBUFS); |
| 3027 | } |
| 3028 | |
| 3029 | io_ring_submit_unlock(req->ctx, needs_lock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3030 | return kbuf; |
| 3031 | } |
| 3032 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3033 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3034 | bool needs_lock) |
| 3035 | { |
| 3036 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3037 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3038 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3039 | bgid = req->buf_index; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3040 | kbuf = io_buffer_select(req, len, bgid, needs_lock); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3041 | if (IS_ERR(kbuf)) |
| 3042 | return kbuf; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3043 | return u64_to_user_ptr(kbuf->addr); |
| 3044 | } |
| 3045 | |
| 3046 | #ifdef CONFIG_COMPAT |
| 3047 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3048 | bool needs_lock) |
| 3049 | { |
| 3050 | struct compat_iovec __user *uiov; |
| 3051 | compat_ssize_t clen; |
| 3052 | void __user *buf; |
| 3053 | ssize_t len; |
| 3054 | |
| 3055 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3056 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3057 | return -EFAULT; |
| 3058 | if (__get_user(clen, &uiov->iov_len)) |
| 3059 | return -EFAULT; |
| 3060 | if (clen < 0) |
| 3061 | return -EINVAL; |
| 3062 | |
| 3063 | len = clen; |
| 3064 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3065 | if (IS_ERR(buf)) |
| 3066 | return PTR_ERR(buf); |
| 3067 | iov[0].iov_base = buf; |
| 3068 | iov[0].iov_len = (compat_size_t) len; |
| 3069 | return 0; |
| 3070 | } |
| 3071 | #endif |
| 3072 | |
| 3073 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3074 | bool needs_lock) |
| 3075 | { |
| 3076 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3077 | void __user *buf; |
| 3078 | ssize_t len; |
| 3079 | |
| 3080 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3081 | return -EFAULT; |
| 3082 | |
| 3083 | len = iov[0].iov_len; |
| 3084 | if (len < 0) |
| 3085 | return -EINVAL; |
| 3086 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3087 | if (IS_ERR(buf)) |
| 3088 | return PTR_ERR(buf); |
| 3089 | iov[0].iov_base = buf; |
| 3090 | iov[0].iov_len = len; |
| 3091 | return 0; |
| 3092 | } |
| 3093 | |
| 3094 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3095 | bool needs_lock) |
| 3096 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3097 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3098 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3099 | |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3100 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3101 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3102 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3103 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3104 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3105 | return -EINVAL; |
| 3106 | |
| 3107 | #ifdef CONFIG_COMPAT |
| 3108 | if (req->ctx->compat) |
| 3109 | return io_compat_import(req, iov, needs_lock); |
| 3110 | #endif |
| 3111 | |
| 3112 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3113 | } |
| 3114 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3115 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3116 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3117 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3118 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3119 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3120 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3121 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3122 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3123 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3124 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3125 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3126 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3127 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3128 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3129 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3130 | return -EINVAL; |
| 3131 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3132 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3133 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3134 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3135 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3136 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3137 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3140 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3141 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3142 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3145 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3146 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3147 | if (!ret) |
| 3148 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3149 | *iovec = NULL; |
| 3150 | return ret; |
| 3151 | } |
| 3152 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3153 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3154 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3157 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3158 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3159 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3160 | } |
| 3161 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3162 | /* |
| 3163 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3164 | * by looping over ->read() or ->write() manually. |
| 3165 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3166 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3167 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3168 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3169 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3170 | ssize_t ret = 0; |
| 3171 | |
| 3172 | /* |
| 3173 | * Don't support polled IO through this interface, and we can't |
| 3174 | * support non-blocking either. For the latter, this just causes |
| 3175 | * the kiocb to be handled from an async context. |
| 3176 | */ |
| 3177 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3178 | return -EOPNOTSUPP; |
| 3179 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3180 | return -EAGAIN; |
| 3181 | |
| 3182 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3183 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3184 | ssize_t nr; |
| 3185 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3186 | if (!iov_iter_is_bvec(iter)) { |
| 3187 | iovec = iov_iter_iovec(iter); |
| 3188 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3189 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3190 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3191 | } |
| 3192 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3193 | if (rw == READ) { |
| 3194 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3195 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3196 | } else { |
| 3197 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3198 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | if (nr < 0) { |
| 3202 | if (!ret) |
| 3203 | ret = nr; |
| 3204 | break; |
| 3205 | } |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3206 | if (!iov_iter_is_bvec(iter)) { |
| 3207 | iov_iter_advance(iter, nr); |
| 3208 | } else { |
| 3209 | req->rw.len -= nr; |
| 3210 | req->rw.addr += nr; |
| 3211 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3212 | ret += nr; |
| 3213 | if (nr != iovec.iov_len) |
| 3214 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | return ret; |
| 3218 | } |
| 3219 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3220 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3221 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3222 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3223 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3224 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3225 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3226 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3227 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3228 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3229 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3230 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3231 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3232 | unsigned iov_off = 0; |
| 3233 | |
| 3234 | rw->iter.iov = rw->fast_iov; |
| 3235 | if (iter->iov != fast_iov) { |
| 3236 | iov_off = iter->iov - fast_iov; |
| 3237 | rw->iter.iov += iov_off; |
| 3238 | } |
| 3239 | if (rw->fast_iov != fast_iov) |
| 3240 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3241 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3242 | } else { |
| 3243 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | |
Hao Xu | 8d4af68 | 2021-09-22 18:15:22 +0800 | [diff] [blame] | 3247 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3248 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3249 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3250 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3251 | if (req->async_data) { |
| 3252 | req->flags |= REQ_F_ASYNC_DATA; |
| 3253 | return false; |
| 3254 | } |
| 3255 | return true; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3258 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3259 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3260 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3261 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3262 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3263 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3264 | if (!req_has_async_data(req)) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3265 | struct io_async_rw *iorw; |
| 3266 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3267 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3268 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3269 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3270 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3271 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3272 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3273 | iorw = req->async_data; |
| 3274 | /* we've copied and mapped the iter, ensure state is saved */ |
| 3275 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3276 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3277 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3278 | } |
| 3279 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3280 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3281 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3282 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3283 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3284 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3285 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3286 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3287 | if (unlikely(ret < 0)) |
| 3288 | return ret; |
| 3289 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3290 | iorw->bytes_done = 0; |
| 3291 | iorw->free_iovec = iov; |
| 3292 | if (iov) |
| 3293 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3294 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3295 | return 0; |
| 3296 | } |
| 3297 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3298 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3299 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3300 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3301 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3302 | return io_prep_rw(req, sqe, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3305 | /* |
| 3306 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3307 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3308 | * This gets called when the page is unlocked, and we generally expect that to |
| 3309 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3310 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3311 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3312 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3313 | * slow path. |
| 3314 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3315 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3316 | int sync, void *arg) |
| 3317 | { |
| 3318 | struct wait_page_queue *wpq; |
| 3319 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3320 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3321 | |
| 3322 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3323 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3324 | if (!wake_page_match(wpq, key)) |
| 3325 | return 0; |
| 3326 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3327 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3328 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3329 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3330 | return 1; |
| 3331 | } |
| 3332 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3333 | /* |
| 3334 | * This controls whether a given IO request should be armed for async page |
| 3335 | * based retry. If we return false here, the request is handed to the async |
| 3336 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3337 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3338 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3339 | * will register a callback when the page is unlocked at IO completion. Through |
| 3340 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3341 | * That retry will attempt the buffered read again. The retry will generally |
| 3342 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3343 | * async worker threads for a blocking retry. |
| 3344 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3345 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3346 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3347 | struct io_async_rw *rw = req->async_data; |
| 3348 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3349 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3350 | |
| 3351 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3352 | if (req->flags & REQ_F_NOWAIT) |
| 3353 | return false; |
| 3354 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3355 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3356 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3357 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3358 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3359 | /* |
| 3360 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3361 | * support callback based unlocks |
| 3362 | */ |
| 3363 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3364 | return false; |
| 3365 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3366 | wait->wait.func = io_async_buf_func; |
| 3367 | wait->wait.private = req; |
| 3368 | wait->wait.flags = 0; |
| 3369 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3370 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3371 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3372 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3373 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3374 | } |
| 3375 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3376 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3377 | { |
| 3378 | if (req->file->f_op->read_iter) |
| 3379 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3380 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3381 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3382 | else |
| 3383 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3384 | } |
| 3385 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3386 | static bool need_read_all(struct io_kiocb *req) |
| 3387 | { |
| 3388 | return req->flags & REQ_F_ISREG || |
| 3389 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3390 | } |
| 3391 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3392 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3393 | { |
| 3394 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3395 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3396 | struct iov_iter __iter, *iter = &__iter; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3397 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3398 | struct iov_iter_state __state, *state; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3399 | struct io_async_rw *rw; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3400 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3401 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3402 | if (req_has_async_data(req)) { |
| 3403 | rw = req->async_data; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3404 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3405 | state = &rw->iter_state; |
| 3406 | /* |
| 3407 | * We come here from an earlier attempt, restore our state to |
| 3408 | * match in case it doesn't. It's cheap enough that we don't |
| 3409 | * need to make this conditional. |
| 3410 | */ |
| 3411 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3412 | iovec = NULL; |
| 3413 | } else { |
| 3414 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3415 | if (ret < 0) |
| 3416 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3417 | state = &__state; |
| 3418 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3419 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3420 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3421 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3422 | /* Ensure we clear previously set non-block flag */ |
| 3423 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3424 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3425 | else |
| 3426 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3427 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3428 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3429 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3430 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3431 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3432 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3433 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3434 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3435 | if (unlikely(ret)) { |
| 3436 | kfree(iovec); |
| 3437 | return ret; |
| 3438 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3439 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3440 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3441 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3442 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3443 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3444 | /* IOPOLL retry should happen for io-wq threads */ |
| 3445 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3446 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3447 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3448 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3449 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3450 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3451 | } else if (ret == -EIOCBQUEUED) { |
| 3452 | goto out_free; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3453 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3454 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3455 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3456 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3457 | } |
| 3458 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3459 | /* |
| 3460 | * Don't depend on the iter state matching what was consumed, or being |
| 3461 | * untouched in case of error. Restore it and we'll advance it |
| 3462 | * manually if we need to. |
| 3463 | */ |
| 3464 | iov_iter_restore(iter, state); |
| 3465 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3466 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3467 | if (ret2) |
| 3468 | return ret2; |
| 3469 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3470 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3471 | rw = req->async_data; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3472 | /* |
| 3473 | * Now use our persistent iterator and state, if we aren't already. |
| 3474 | * We've restored and mapped the iter to match. |
| 3475 | */ |
| 3476 | if (iter != &rw->iter) { |
| 3477 | iter = &rw->iter; |
| 3478 | state = &rw->iter_state; |
| 3479 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3480 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3481 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3482 | /* |
| 3483 | * We end up here because of a partial read, either from |
| 3484 | * above or inside this loop. Advance the iter by the bytes |
| 3485 | * that were consumed. |
| 3486 | */ |
| 3487 | iov_iter_advance(iter, ret); |
| 3488 | if (!iov_iter_count(iter)) |
| 3489 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3490 | rw->bytes_done += ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3491 | iov_iter_save_state(iter, state); |
| 3492 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3493 | /* if we can retry, do so with the callbacks armed */ |
| 3494 | if (!io_rw_should_retry(req)) { |
| 3495 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3496 | return -EAGAIN; |
| 3497 | } |
| 3498 | |
| 3499 | /* |
| 3500 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3501 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3502 | * desired page gets unlocked. We can also get a partial read |
| 3503 | * here, and if we do, then just retry at the new offset. |
| 3504 | */ |
| 3505 | ret = io_iter_do_read(req, iter); |
| 3506 | if (ret == -EIOCBQUEUED) |
| 3507 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3508 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3509 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3510 | iov_iter_restore(iter, state); |
| 3511 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3512 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3513 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3514 | out_free: |
| 3515 | /* it's faster to check here then delegate to kfree */ |
| 3516 | if (iovec) |
| 3517 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3518 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3521 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3522 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3523 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3524 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3525 | return io_prep_rw(req, sqe, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3528 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3529 | { |
| 3530 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3531 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3532 | struct iov_iter __iter, *iter = &__iter; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3533 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3534 | struct iov_iter_state __state, *state; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3535 | struct io_async_rw *rw; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3536 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3537 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3538 | if (req_has_async_data(req)) { |
| 3539 | rw = req->async_data; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3540 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3541 | state = &rw->iter_state; |
| 3542 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3543 | iovec = NULL; |
| 3544 | } else { |
| 3545 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3546 | if (ret < 0) |
| 3547 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3548 | state = &__state; |
| 3549 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3550 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3551 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3552 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3553 | /* Ensure we clear previously set non-block flag */ |
| 3554 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3555 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3556 | else |
| 3557 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3558 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3559 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3560 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3561 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3562 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3563 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3564 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3565 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3566 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3567 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3568 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3569 | if (unlikely(ret)) |
| 3570 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3571 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3572 | /* |
| 3573 | * Open-code file_start_write here to grab freeze protection, |
| 3574 | * which will be released by another thread in |
| 3575 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3576 | * released so that it doesn't complain about the held lock when |
| 3577 | * we return to userspace. |
| 3578 | */ |
| 3579 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3580 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3581 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3582 | SB_FREEZE_WRITE); |
| 3583 | } |
| 3584 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3585 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3586 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3587 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3588 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3589 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3590 | else |
| 3591 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3592 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3593 | if (req->flags & REQ_F_REISSUE) { |
| 3594 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3595 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3596 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3597 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3598 | /* |
| 3599 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3600 | * retry them without IOCB_NOWAIT. |
| 3601 | */ |
| 3602 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3603 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3604 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3605 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3606 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3607 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3608 | /* IOPOLL retry should happen for io-wq threads */ |
| 3609 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3610 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3611 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3612 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3613 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3614 | copy_iov: |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3615 | iov_iter_restore(iter, state); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3616 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3617 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3618 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3619 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3620 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3621 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3622 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3623 | return ret; |
| 3624 | } |
| 3625 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3626 | static int io_renameat_prep(struct io_kiocb *req, |
| 3627 | const struct io_uring_sqe *sqe) |
| 3628 | { |
| 3629 | struct io_rename *ren = &req->rename; |
| 3630 | const char __user *oldf, *newf; |
| 3631 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3632 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3633 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3634 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3635 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3636 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3637 | return -EBADF; |
| 3638 | |
| 3639 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3640 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3641 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3642 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3643 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3644 | |
| 3645 | ren->oldpath = getname(oldf); |
| 3646 | if (IS_ERR(ren->oldpath)) |
| 3647 | return PTR_ERR(ren->oldpath); |
| 3648 | |
| 3649 | ren->newpath = getname(newf); |
| 3650 | if (IS_ERR(ren->newpath)) { |
| 3651 | putname(ren->oldpath); |
| 3652 | return PTR_ERR(ren->newpath); |
| 3653 | } |
| 3654 | |
| 3655 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3656 | return 0; |
| 3657 | } |
| 3658 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3659 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3660 | { |
| 3661 | struct io_rename *ren = &req->rename; |
| 3662 | int ret; |
| 3663 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3664 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3665 | return -EAGAIN; |
| 3666 | |
| 3667 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3668 | ren->newpath, ren->flags); |
| 3669 | |
| 3670 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3671 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3672 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3673 | io_req_complete(req, ret); |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3677 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3678 | const struct io_uring_sqe *sqe) |
| 3679 | { |
| 3680 | struct io_unlink *un = &req->unlink; |
| 3681 | const char __user *fname; |
| 3682 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3683 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3684 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3685 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3686 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3687 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3688 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3689 | return -EBADF; |
| 3690 | |
| 3691 | un->dfd = READ_ONCE(sqe->fd); |
| 3692 | |
| 3693 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3694 | if (un->flags & ~AT_REMOVEDIR) |
| 3695 | return -EINVAL; |
| 3696 | |
| 3697 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3698 | un->filename = getname(fname); |
| 3699 | if (IS_ERR(un->filename)) |
| 3700 | return PTR_ERR(un->filename); |
| 3701 | |
| 3702 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3703 | return 0; |
| 3704 | } |
| 3705 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3706 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3707 | { |
| 3708 | struct io_unlink *un = &req->unlink; |
| 3709 | int ret; |
| 3710 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3711 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3712 | return -EAGAIN; |
| 3713 | |
| 3714 | if (un->flags & AT_REMOVEDIR) |
| 3715 | ret = do_rmdir(un->dfd, un->filename); |
| 3716 | else |
| 3717 | ret = do_unlinkat(un->dfd, un->filename); |
| 3718 | |
| 3719 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3720 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3721 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3722 | io_req_complete(req, ret); |
| 3723 | return 0; |
| 3724 | } |
| 3725 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3726 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3727 | const struct io_uring_sqe *sqe) |
| 3728 | { |
| 3729 | struct io_mkdir *mkd = &req->mkdir; |
| 3730 | const char __user *fname; |
| 3731 | |
| 3732 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3733 | return -EINVAL; |
| 3734 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3735 | sqe->splice_fd_in) |
| 3736 | return -EINVAL; |
| 3737 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3738 | return -EBADF; |
| 3739 | |
| 3740 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3741 | mkd->mode = READ_ONCE(sqe->len); |
| 3742 | |
| 3743 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3744 | mkd->filename = getname(fname); |
| 3745 | if (IS_ERR(mkd->filename)) |
| 3746 | return PTR_ERR(mkd->filename); |
| 3747 | |
| 3748 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
| 3752 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) |
| 3753 | { |
| 3754 | struct io_mkdir *mkd = &req->mkdir; |
| 3755 | int ret; |
| 3756 | |
| 3757 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3758 | return -EAGAIN; |
| 3759 | |
| 3760 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3761 | |
| 3762 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3763 | if (ret < 0) |
| 3764 | req_set_fail(req); |
| 3765 | io_req_complete(req, ret); |
| 3766 | return 0; |
| 3767 | } |
| 3768 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3769 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3770 | const struct io_uring_sqe *sqe) |
| 3771 | { |
| 3772 | struct io_symlink *sl = &req->symlink; |
| 3773 | const char __user *oldpath, *newpath; |
| 3774 | |
| 3775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3776 | return -EINVAL; |
| 3777 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 3778 | sqe->splice_fd_in) |
| 3779 | return -EINVAL; |
| 3780 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3781 | return -EBADF; |
| 3782 | |
| 3783 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 3784 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3785 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3786 | |
| 3787 | sl->oldpath = getname(oldpath); |
| 3788 | if (IS_ERR(sl->oldpath)) |
| 3789 | return PTR_ERR(sl->oldpath); |
| 3790 | |
| 3791 | sl->newpath = getname(newpath); |
| 3792 | if (IS_ERR(sl->newpath)) { |
| 3793 | putname(sl->oldpath); |
| 3794 | return PTR_ERR(sl->newpath); |
| 3795 | } |
| 3796 | |
| 3797 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3798 | return 0; |
| 3799 | } |
| 3800 | |
| 3801 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) |
| 3802 | { |
| 3803 | struct io_symlink *sl = &req->symlink; |
| 3804 | int ret; |
| 3805 | |
| 3806 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3807 | return -EAGAIN; |
| 3808 | |
| 3809 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 3810 | |
| 3811 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3812 | if (ret < 0) |
| 3813 | req_set_fail(req); |
| 3814 | io_req_complete(req, ret); |
| 3815 | return 0; |
| 3816 | } |
| 3817 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3818 | static int io_linkat_prep(struct io_kiocb *req, |
| 3819 | const struct io_uring_sqe *sqe) |
| 3820 | { |
| 3821 | struct io_hardlink *lnk = &req->hardlink; |
| 3822 | const char __user *oldf, *newf; |
| 3823 | |
| 3824 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3825 | return -EINVAL; |
| 3826 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 3827 | return -EINVAL; |
| 3828 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3829 | return -EBADF; |
| 3830 | |
| 3831 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 3832 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 3833 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3834 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3835 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 3836 | |
| 3837 | lnk->oldpath = getname(oldf); |
| 3838 | if (IS_ERR(lnk->oldpath)) |
| 3839 | return PTR_ERR(lnk->oldpath); |
| 3840 | |
| 3841 | lnk->newpath = getname(newf); |
| 3842 | if (IS_ERR(lnk->newpath)) { |
| 3843 | putname(lnk->oldpath); |
| 3844 | return PTR_ERR(lnk->newpath); |
| 3845 | } |
| 3846 | |
| 3847 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3848 | return 0; |
| 3849 | } |
| 3850 | |
| 3851 | static int io_linkat(struct io_kiocb *req, int issue_flags) |
| 3852 | { |
| 3853 | struct io_hardlink *lnk = &req->hardlink; |
| 3854 | int ret; |
| 3855 | |
| 3856 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3857 | return -EAGAIN; |
| 3858 | |
| 3859 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 3860 | lnk->newpath, lnk->flags); |
| 3861 | |
| 3862 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3863 | if (ret < 0) |
| 3864 | req_set_fail(req); |
| 3865 | io_req_complete(req, ret); |
| 3866 | return 0; |
| 3867 | } |
| 3868 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3869 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3870 | const struct io_uring_sqe *sqe) |
| 3871 | { |
| 3872 | #if defined(CONFIG_NET) |
| 3873 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3874 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3875 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3876 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3877 | return -EINVAL; |
| 3878 | |
| 3879 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3880 | return 0; |
| 3881 | #else |
| 3882 | return -EOPNOTSUPP; |
| 3883 | #endif |
| 3884 | } |
| 3885 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3886 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3887 | { |
| 3888 | #if defined(CONFIG_NET) |
| 3889 | struct socket *sock; |
| 3890 | int ret; |
| 3891 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3892 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3893 | return -EAGAIN; |
| 3894 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3895 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3896 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3897 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3898 | |
| 3899 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3900 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3901 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3902 | io_req_complete(req, ret); |
| 3903 | return 0; |
| 3904 | #else |
| 3905 | return -EOPNOTSUPP; |
| 3906 | #endif |
| 3907 | } |
| 3908 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3909 | static int __io_splice_prep(struct io_kiocb *req, |
| 3910 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3911 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 3912 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3913 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3914 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3915 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3916 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3917 | |
| 3918 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3919 | sp->len = READ_ONCE(sqe->len); |
| 3920 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3921 | |
| 3922 | if (unlikely(sp->flags & ~valid_flags)) |
| 3923 | return -EINVAL; |
| 3924 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 3925 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3926 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3927 | if (!sp->file_in) |
| 3928 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3929 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3930 | return 0; |
| 3931 | } |
| 3932 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3933 | static int io_tee_prep(struct io_kiocb *req, |
| 3934 | const struct io_uring_sqe *sqe) |
| 3935 | { |
| 3936 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3937 | return -EINVAL; |
| 3938 | return __io_splice_prep(req, sqe); |
| 3939 | } |
| 3940 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3941 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3942 | { |
| 3943 | struct io_splice *sp = &req->splice; |
| 3944 | struct file *in = sp->file_in; |
| 3945 | struct file *out = sp->file_out; |
| 3946 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3947 | long ret = 0; |
| 3948 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3949 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3950 | return -EAGAIN; |
| 3951 | if (sp->len) |
| 3952 | ret = do_tee(in, out, sp->len, flags); |
| 3953 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3954 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3955 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3956 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3957 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3958 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3959 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3960 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3961 | return 0; |
| 3962 | } |
| 3963 | |
| 3964 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3965 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 3966 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3967 | |
| 3968 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3969 | sp->off_out = READ_ONCE(sqe->off); |
| 3970 | return __io_splice_prep(req, sqe); |
| 3971 | } |
| 3972 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3973 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3974 | { |
| 3975 | struct io_splice *sp = &req->splice; |
| 3976 | struct file *in = sp->file_in; |
| 3977 | struct file *out = sp->file_out; |
| 3978 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3979 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3980 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3981 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3982 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3983 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3984 | |
| 3985 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3986 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3987 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3988 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3989 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3990 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3991 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3992 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3993 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3994 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3995 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3996 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3997 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3998 | return 0; |
| 3999 | } |
| 4000 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4001 | /* |
| 4002 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4003 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4004 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4005 | { |
| 4006 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4007 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4008 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4009 | return -EINVAL; |
| 4010 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4011 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4012 | return 0; |
| 4013 | } |
| 4014 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4015 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4016 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4017 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4018 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4019 | if (!req->file) |
| 4020 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4021 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4022 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4023 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4024 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4025 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4026 | return -EINVAL; |
| 4027 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4028 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4029 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4030 | return -EINVAL; |
| 4031 | |
| 4032 | req->sync.off = READ_ONCE(sqe->off); |
| 4033 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4034 | return 0; |
| 4035 | } |
| 4036 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4037 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4038 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4039 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4040 | int ret; |
| 4041 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4042 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4043 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4044 | return -EAGAIN; |
| 4045 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4046 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4047 | end > 0 ? end : LLONG_MAX, |
| 4048 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4049 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4050 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4051 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4052 | return 0; |
| 4053 | } |
| 4054 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4055 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4056 | const struct io_uring_sqe *sqe) |
| 4057 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4058 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4059 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4060 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4061 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4062 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4063 | |
| 4064 | req->sync.off = READ_ONCE(sqe->off); |
| 4065 | req->sync.len = READ_ONCE(sqe->addr); |
| 4066 | req->sync.mode = READ_ONCE(sqe->len); |
| 4067 | return 0; |
| 4068 | } |
| 4069 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4070 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4071 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4072 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4073 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4074 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4075 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4076 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4077 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4078 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4079 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4080 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4081 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4082 | return 0; |
| 4083 | } |
| 4084 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4085 | 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] | 4086 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4087 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4088 | int ret; |
| 4089 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4090 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4091 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4092 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4093 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4094 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4095 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4096 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4097 | /* open.how should be already initialised */ |
| 4098 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4099 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4100 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4101 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4102 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4103 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4104 | if (IS_ERR(req->open.filename)) { |
| 4105 | ret = PTR_ERR(req->open.filename); |
| 4106 | req->open.filename = NULL; |
| 4107 | return ret; |
| 4108 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4109 | |
| 4110 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4111 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4112 | return -EINVAL; |
| 4113 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4114 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4115 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4116 | return 0; |
| 4117 | } |
| 4118 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4119 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4120 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4121 | u64 mode = READ_ONCE(sqe->len); |
| 4122 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4123 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4124 | req->open.how = build_open_how(flags, mode); |
| 4125 | return __io_openat_prep(req, sqe); |
| 4126 | } |
| 4127 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4128 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4129 | { |
| 4130 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4131 | size_t len; |
| 4132 | int ret; |
| 4133 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4134 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4135 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4136 | if (len < OPEN_HOW_SIZE_VER0) |
| 4137 | return -EINVAL; |
| 4138 | |
| 4139 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4140 | len); |
| 4141 | if (ret) |
| 4142 | return ret; |
| 4143 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4144 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4147 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4148 | { |
| 4149 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4150 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4151 | bool resolve_nonblock, nonblock_set; |
| 4152 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4153 | int ret; |
| 4154 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4155 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4156 | if (ret) |
| 4157 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4158 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4159 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4160 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4161 | /* |
| 4162 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4163 | * it'll always -EAGAIN |
| 4164 | */ |
| 4165 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4166 | return -EAGAIN; |
| 4167 | op.lookup_flags |= LOOKUP_CACHED; |
| 4168 | op.open_flag |= O_NONBLOCK; |
| 4169 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4170 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4171 | if (!fixed) { |
| 4172 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4173 | if (ret < 0) |
| 4174 | goto err; |
| 4175 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4176 | |
| 4177 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4178 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4179 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4180 | * We could hang on to this 'fd' on retrying, but seems like |
| 4181 | * marginal gain for something that is now known to be a slower |
| 4182 | * path. So just put it, and we'll get a new one when we retry. |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4183 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4184 | if (!fixed) |
| 4185 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4186 | |
| 4187 | ret = PTR_ERR(file); |
| 4188 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4189 | if (ret == -EAGAIN && |
| 4190 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4191 | return -EAGAIN; |
| 4192 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4195 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4196 | file->f_flags &= ~O_NONBLOCK; |
| 4197 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4198 | |
| 4199 | if (!fixed) |
| 4200 | fd_install(ret, file); |
| 4201 | else |
| 4202 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4203 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4204 | err: |
| 4205 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4206 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4207 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4208 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4209 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4210 | return 0; |
| 4211 | } |
| 4212 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4213 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4214 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4215 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4218 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4219 | const struct io_uring_sqe *sqe) |
| 4220 | { |
| 4221 | struct io_provide_buf *p = &req->pbuf; |
| 4222 | u64 tmp; |
| 4223 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4224 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4225 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4226 | return -EINVAL; |
| 4227 | |
| 4228 | tmp = READ_ONCE(sqe->fd); |
| 4229 | if (!tmp || tmp > USHRT_MAX) |
| 4230 | return -EINVAL; |
| 4231 | |
| 4232 | memset(p, 0, sizeof(*p)); |
| 4233 | p->nbufs = tmp; |
| 4234 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4235 | return 0; |
| 4236 | } |
| 4237 | |
| 4238 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4239 | int bgid, unsigned nbufs) |
| 4240 | { |
| 4241 | unsigned i = 0; |
| 4242 | |
| 4243 | /* shouldn't happen */ |
| 4244 | if (!nbufs) |
| 4245 | return 0; |
| 4246 | |
| 4247 | /* the head kbuf is the list itself */ |
| 4248 | while (!list_empty(&buf->list)) { |
| 4249 | struct io_buffer *nxt; |
| 4250 | |
| 4251 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4252 | list_del(&nxt->list); |
| 4253 | kfree(nxt); |
| 4254 | if (++i == nbufs) |
| 4255 | return i; |
| 4256 | } |
| 4257 | i++; |
| 4258 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4259 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4260 | |
| 4261 | return i; |
| 4262 | } |
| 4263 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4264 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4265 | { |
| 4266 | struct io_provide_buf *p = &req->pbuf; |
| 4267 | struct io_ring_ctx *ctx = req->ctx; |
| 4268 | struct io_buffer *head; |
| 4269 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4270 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4271 | |
| 4272 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4273 | |
| 4274 | lockdep_assert_held(&ctx->uring_lock); |
| 4275 | |
| 4276 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4277 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4278 | if (head) |
| 4279 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4280 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4281 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4282 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4283 | /* complete before unlock, IOPOLL may need the lock */ |
| 4284 | __io_req_complete(req, issue_flags, ret, 0); |
| 4285 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4286 | return 0; |
| 4287 | } |
| 4288 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4289 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4290 | const struct io_uring_sqe *sqe) |
| 4291 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4292 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4293 | struct io_provide_buf *p = &req->pbuf; |
| 4294 | u64 tmp; |
| 4295 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4296 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4297 | return -EINVAL; |
| 4298 | |
| 4299 | tmp = READ_ONCE(sqe->fd); |
| 4300 | if (!tmp || tmp > USHRT_MAX) |
| 4301 | return -E2BIG; |
| 4302 | p->nbufs = tmp; |
| 4303 | p->addr = READ_ONCE(sqe->addr); |
| 4304 | p->len = READ_ONCE(sqe->len); |
| 4305 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4306 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4307 | &size)) |
| 4308 | return -EOVERFLOW; |
| 4309 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4310 | return -EOVERFLOW; |
| 4311 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4312 | size = (unsigned long)p->len * p->nbufs; |
| 4313 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4314 | return -EFAULT; |
| 4315 | |
| 4316 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4317 | tmp = READ_ONCE(sqe->off); |
| 4318 | if (tmp > USHRT_MAX) |
| 4319 | return -E2BIG; |
| 4320 | p->bid = tmp; |
| 4321 | return 0; |
| 4322 | } |
| 4323 | |
| 4324 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4325 | { |
| 4326 | struct io_buffer *buf; |
| 4327 | u64 addr = pbuf->addr; |
| 4328 | int i, bid = pbuf->bid; |
| 4329 | |
| 4330 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4331 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4332 | if (!buf) |
| 4333 | break; |
| 4334 | |
| 4335 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4336 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4337 | buf->bid = bid; |
| 4338 | addr += pbuf->len; |
| 4339 | bid++; |
| 4340 | if (!*head) { |
| 4341 | INIT_LIST_HEAD(&buf->list); |
| 4342 | *head = buf; |
| 4343 | } else { |
| 4344 | list_add_tail(&buf->list, &(*head)->list); |
| 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | return i ? i : -ENOMEM; |
| 4349 | } |
| 4350 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4351 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4352 | { |
| 4353 | struct io_provide_buf *p = &req->pbuf; |
| 4354 | struct io_ring_ctx *ctx = req->ctx; |
| 4355 | struct io_buffer *head, *list; |
| 4356 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4357 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4358 | |
| 4359 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4360 | |
| 4361 | lockdep_assert_held(&ctx->uring_lock); |
| 4362 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4363 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4364 | |
| 4365 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4366 | if (ret >= 0 && !list) { |
| 4367 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4368 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4369 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4370 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4371 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4372 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4373 | /* complete before unlock, IOPOLL may need the lock */ |
| 4374 | __io_req_complete(req, issue_flags, ret, 0); |
| 4375 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4376 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4379 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4380 | const struct io_uring_sqe *sqe) |
| 4381 | { |
| 4382 | #if defined(CONFIG_EPOLL) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4383 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4384 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4385 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4386 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4387 | |
| 4388 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4389 | req->epoll.op = READ_ONCE(sqe->len); |
| 4390 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4391 | |
| 4392 | if (ep_op_has_event(req->epoll.op)) { |
| 4393 | struct epoll_event __user *ev; |
| 4394 | |
| 4395 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4396 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4397 | return -EFAULT; |
| 4398 | } |
| 4399 | |
| 4400 | return 0; |
| 4401 | #else |
| 4402 | return -EOPNOTSUPP; |
| 4403 | #endif |
| 4404 | } |
| 4405 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4406 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4407 | { |
| 4408 | #if defined(CONFIG_EPOLL) |
| 4409 | struct io_epoll *ie = &req->epoll; |
| 4410 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4411 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4412 | |
| 4413 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4414 | if (force_nonblock && ret == -EAGAIN) |
| 4415 | return -EAGAIN; |
| 4416 | |
| 4417 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4418 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4419 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4420 | return 0; |
| 4421 | #else |
| 4422 | return -EOPNOTSUPP; |
| 4423 | #endif |
| 4424 | } |
| 4425 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4426 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4427 | { |
| 4428 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4429 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4430 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4431 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4432 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4433 | |
| 4434 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4435 | req->madvise.len = READ_ONCE(sqe->len); |
| 4436 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4437 | return 0; |
| 4438 | #else |
| 4439 | return -EOPNOTSUPP; |
| 4440 | #endif |
| 4441 | } |
| 4442 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4443 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4444 | { |
| 4445 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4446 | struct io_madvise *ma = &req->madvise; |
| 4447 | int ret; |
| 4448 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4449 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4450 | return -EAGAIN; |
| 4451 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4452 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4453 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4454 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4455 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4456 | return 0; |
| 4457 | #else |
| 4458 | return -EOPNOTSUPP; |
| 4459 | #endif |
| 4460 | } |
| 4461 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4462 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4463 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4464 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4465 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4466 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4467 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4468 | |
| 4469 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4470 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4471 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4472 | return 0; |
| 4473 | } |
| 4474 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4475 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4476 | { |
| 4477 | struct io_fadvise *fa = &req->fadvise; |
| 4478 | int ret; |
| 4479 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4480 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4481 | switch (fa->advice) { |
| 4482 | case POSIX_FADV_NORMAL: |
| 4483 | case POSIX_FADV_RANDOM: |
| 4484 | case POSIX_FADV_SEQUENTIAL: |
| 4485 | break; |
| 4486 | default: |
| 4487 | return -EAGAIN; |
| 4488 | } |
| 4489 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4490 | |
| 4491 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4492 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4493 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4494 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4495 | return 0; |
| 4496 | } |
| 4497 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4498 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4499 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4500 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4501 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4502 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4503 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4504 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4505 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4506 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4507 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4508 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4509 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4510 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4511 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4512 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4513 | return 0; |
| 4514 | } |
| 4515 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4516 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4517 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4518 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4519 | int ret; |
| 4520 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4521 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4522 | return -EAGAIN; |
| 4523 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4524 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4525 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4526 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4527 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4528 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4529 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4530 | return 0; |
| 4531 | } |
| 4532 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4533 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4534 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4535 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4536 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4537 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4538 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4539 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4540 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4541 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4542 | |
| 4543 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4544 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4545 | if (req->close.file_slot && req->close.fd) |
| 4546 | return -EINVAL; |
| 4547 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
| 4550 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4551 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4552 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4553 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4554 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4555 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4556 | struct file *file = NULL; |
| 4557 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4558 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4559 | if (req->close.file_slot) { |
| 4560 | ret = io_close_fixed(req, issue_flags); |
| 4561 | goto err; |
| 4562 | } |
| 4563 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4564 | spin_lock(&files->file_lock); |
| 4565 | fdt = files_fdtable(files); |
| 4566 | if (close->fd >= fdt->max_fds) { |
| 4567 | spin_unlock(&files->file_lock); |
| 4568 | goto err; |
| 4569 | } |
| 4570 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4571 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4572 | spin_unlock(&files->file_lock); |
| 4573 | file = NULL; |
| 4574 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4575 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4576 | |
| 4577 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4578 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4579 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4580 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4581 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4582 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4583 | ret = __close_fd_get_file(close->fd, &file); |
| 4584 | spin_unlock(&files->file_lock); |
| 4585 | if (ret < 0) { |
| 4586 | if (ret == -ENOENT) |
| 4587 | ret = -EBADF; |
| 4588 | goto err; |
| 4589 | } |
| 4590 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4591 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4592 | ret = filp_close(file, current->files); |
| 4593 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4594 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4595 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4596 | if (file) |
| 4597 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4598 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4599 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4600 | } |
| 4601 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4602 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4603 | { |
| 4604 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4605 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4606 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4607 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4608 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4609 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4610 | return -EINVAL; |
| 4611 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4612 | req->sync.off = READ_ONCE(sqe->off); |
| 4613 | req->sync.len = READ_ONCE(sqe->len); |
| 4614 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4615 | return 0; |
| 4616 | } |
| 4617 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4618 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4619 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4620 | int ret; |
| 4621 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4622 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4623 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4624 | return -EAGAIN; |
| 4625 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4626 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4627 | req->sync.flags); |
| 4628 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4629 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4630 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4631 | return 0; |
| 4632 | } |
| 4633 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4634 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4635 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4636 | struct io_async_msghdr *kmsg) |
| 4637 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4638 | struct io_async_msghdr *async_msg = req->async_data; |
| 4639 | |
| 4640 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4641 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4642 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4643 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4644 | return -ENOMEM; |
| 4645 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4646 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4647 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4648 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4649 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4650 | /* if were using fast_iov, set it to the new one */ |
| 4651 | if (!async_msg->free_iov) |
| 4652 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4653 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4654 | return -EAGAIN; |
| 4655 | } |
| 4656 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4657 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4658 | struct io_async_msghdr *iomsg) |
| 4659 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4660 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4661 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4662 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4663 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4664 | } |
| 4665 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4666 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4667 | { |
| 4668 | int ret; |
| 4669 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4670 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4671 | if (!ret) |
| 4672 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4673 | return ret; |
| 4674 | } |
| 4675 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4676 | 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] | 4677 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4678 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4679 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4680 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4681 | return -EINVAL; |
| 4682 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4683 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4684 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4685 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4686 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4687 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4688 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4689 | #ifdef CONFIG_COMPAT |
| 4690 | if (req->ctx->compat) |
| 4691 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4692 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4693 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4696 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4697 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4698 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4699 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4700 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4701 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4702 | int ret; |
| 4703 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4704 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4705 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4706 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4707 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 4708 | if (req_has_async_data(req)) { |
| 4709 | kmsg = req->async_data; |
| 4710 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4711 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4712 | if (ret) |
| 4713 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4714 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4717 | flags = req->sr_msg.msg_flags; |
| 4718 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4719 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4720 | if (flags & MSG_WAITALL) |
| 4721 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4722 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4723 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4724 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4725 | return io_setup_async_msg(req, kmsg); |
| 4726 | if (ret == -ERESTARTSYS) |
| 4727 | ret = -EINTR; |
| 4728 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4729 | /* fast path, check for non-NULL to avoid function call */ |
| 4730 | if (kmsg->free_iov) |
| 4731 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4732 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4733 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4734 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4735 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4736 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4739 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4740 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4741 | struct io_sr_msg *sr = &req->sr_msg; |
| 4742 | struct msghdr msg; |
| 4743 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4744 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4745 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4746 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4747 | int ret; |
| 4748 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4749 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4750 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4751 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4752 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4753 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4754 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4755 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4756 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4757 | msg.msg_name = NULL; |
| 4758 | msg.msg_control = NULL; |
| 4759 | msg.msg_controllen = 0; |
| 4760 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4761 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4762 | flags = req->sr_msg.msg_flags; |
| 4763 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4764 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4765 | if (flags & MSG_WAITALL) |
| 4766 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4767 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4768 | msg.msg_flags = flags; |
| 4769 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4770 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4771 | return -EAGAIN; |
| 4772 | if (ret == -ERESTARTSYS) |
| 4773 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4774 | |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4775 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4776 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4777 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4778 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4779 | } |
| 4780 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4781 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4782 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4783 | { |
| 4784 | struct io_sr_msg *sr = &req->sr_msg; |
| 4785 | struct iovec __user *uiov; |
| 4786 | size_t iov_len; |
| 4787 | int ret; |
| 4788 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4789 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4790 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4791 | if (ret) |
| 4792 | return ret; |
| 4793 | |
| 4794 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4795 | if (iov_len > 1) |
| 4796 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4797 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4798 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4799 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4800 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4801 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4802 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4803 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4804 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4805 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4806 | if (ret > 0) |
| 4807 | ret = 0; |
| 4808 | } |
| 4809 | |
| 4810 | return ret; |
| 4811 | } |
| 4812 | |
| 4813 | #ifdef CONFIG_COMPAT |
| 4814 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4815 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4816 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4817 | struct io_sr_msg *sr = &req->sr_msg; |
| 4818 | struct compat_iovec __user *uiov; |
| 4819 | compat_uptr_t ptr; |
| 4820 | compat_size_t len; |
| 4821 | int ret; |
| 4822 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4823 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4824 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4825 | if (ret) |
| 4826 | return ret; |
| 4827 | |
| 4828 | uiov = compat_ptr(ptr); |
| 4829 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4830 | compat_ssize_t clen; |
| 4831 | |
| 4832 | if (len > 1) |
| 4833 | return -EINVAL; |
| 4834 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4835 | return -EFAULT; |
| 4836 | if (__get_user(clen, &uiov->iov_len)) |
| 4837 | return -EFAULT; |
| 4838 | if (clen < 0) |
| 4839 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4840 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4841 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4842 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4843 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4844 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4845 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4846 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4847 | if (ret < 0) |
| 4848 | return ret; |
| 4849 | } |
| 4850 | |
| 4851 | return 0; |
| 4852 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4853 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4854 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4855 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4856 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4857 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4858 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4859 | |
| 4860 | #ifdef CONFIG_COMPAT |
| 4861 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4862 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4863 | #endif |
| 4864 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4865 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4866 | } |
| 4867 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4868 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4869 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4870 | { |
| 4871 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4872 | |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 4873 | return io_buffer_select(req, &sr->len, sr->bgid, needs_lock); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4876 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4877 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 4878 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4879 | } |
| 4880 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4881 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4882 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4883 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4884 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4885 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4886 | if (!ret) |
| 4887 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4888 | return ret; |
| 4889 | } |
| 4890 | |
| 4891 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4892 | { |
| 4893 | struct io_sr_msg *sr = &req->sr_msg; |
| 4894 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4895 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4896 | return -EINVAL; |
| 4897 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4898 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4899 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4900 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4901 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4902 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4903 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4904 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4905 | #ifdef CONFIG_COMPAT |
| 4906 | if (req->ctx->compat) |
| 4907 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4908 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4909 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4912 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4913 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4914 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4915 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4916 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4917 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4918 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4919 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4920 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4921 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4922 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4923 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4924 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4925 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 4926 | if (req_has_async_data(req)) { |
| 4927 | kmsg = req->async_data; |
| 4928 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4929 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4930 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4931 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4932 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4933 | } |
| 4934 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4935 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4936 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4937 | if (IS_ERR(kbuf)) |
| 4938 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4939 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4940 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4941 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4942 | 1, req->sr_msg.len); |
| 4943 | } |
| 4944 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4945 | flags = req->sr_msg.msg_flags; |
| 4946 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4947 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4948 | if (flags & MSG_WAITALL) |
| 4949 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4950 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4951 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4952 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4953 | if (force_nonblock && ret == -EAGAIN) |
| 4954 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4955 | if (ret == -ERESTARTSYS) |
| 4956 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4957 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4958 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4959 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4960 | /* fast path, check for non-NULL to avoid function call */ |
| 4961 | if (kmsg->free_iov) |
| 4962 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4963 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4964 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4965 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4966 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4967 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4968 | } |
| 4969 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4970 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4971 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4972 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4973 | struct io_sr_msg *sr = &req->sr_msg; |
| 4974 | struct msghdr msg; |
| 4975 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4976 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4977 | struct iovec iov; |
| 4978 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4979 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4980 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4981 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4982 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4983 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4984 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4985 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4986 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4987 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4988 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4989 | if (IS_ERR(kbuf)) |
| 4990 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4991 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4992 | } |
| 4993 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4994 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4995 | if (unlikely(ret)) |
| 4996 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4997 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4998 | msg.msg_name = NULL; |
| 4999 | msg.msg_control = NULL; |
| 5000 | msg.msg_controllen = 0; |
| 5001 | msg.msg_namelen = 0; |
| 5002 | msg.msg_iocb = NULL; |
| 5003 | msg.msg_flags = 0; |
| 5004 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5005 | flags = req->sr_msg.msg_flags; |
| 5006 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5007 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5008 | if (flags & MSG_WAITALL) |
| 5009 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5010 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5011 | ret = sock_recvmsg(sock, &msg, flags); |
| 5012 | if (force_nonblock && ret == -EAGAIN) |
| 5013 | return -EAGAIN; |
| 5014 | if (ret == -ERESTARTSYS) |
| 5015 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5016 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5017 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5018 | cflags = io_put_recv_kbuf(req); |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5019 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5020 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5021 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5022 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5023 | } |
| 5024 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5025 | 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] | 5026 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5027 | struct io_accept *accept = &req->accept; |
| 5028 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5029 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5030 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5031 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5032 | return -EINVAL; |
| 5033 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5034 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5035 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5036 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5037 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5038 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5039 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5040 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5041 | (accept->flags & SOCK_CLOEXEC))) |
| 5042 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5043 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5044 | return -EINVAL; |
| 5045 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5046 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5047 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5048 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5049 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5050 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5051 | { |
| 5052 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5053 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5054 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5055 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5056 | struct file *file; |
| 5057 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5058 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5059 | if (req->file->f_flags & O_NONBLOCK) |
| 5060 | req->flags |= REQ_F_NOWAIT; |
| 5061 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5062 | if (!fixed) { |
| 5063 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5064 | if (unlikely(fd < 0)) |
| 5065 | return fd; |
| 5066 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5067 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5068 | accept->flags); |
| 5069 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5070 | if (!fixed) |
| 5071 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5072 | ret = PTR_ERR(file); |
| 5073 | if (ret == -EAGAIN && force_nonblock) |
| 5074 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5075 | if (ret == -ERESTARTSYS) |
| 5076 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5077 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5078 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5079 | fd_install(fd, file); |
| 5080 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5081 | } else { |
| 5082 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5083 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5084 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5085 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5086 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5087 | } |
| 5088 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5089 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5090 | { |
| 5091 | struct io_async_connect *io = req->async_data; |
| 5092 | struct io_connect *conn = &req->connect; |
| 5093 | |
| 5094 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5095 | } |
| 5096 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5097 | 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] | 5098 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5099 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5100 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5102 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5103 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5104 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5105 | return -EINVAL; |
| 5106 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5107 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5108 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5109 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5110 | } |
| 5111 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5112 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5113 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5114 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5115 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5116 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5117 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5118 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5119 | if (req_has_async_data(req)) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5120 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5121 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5122 | ret = move_addr_to_kernel(req->connect.addr, |
| 5123 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5124 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5125 | if (ret) |
| 5126 | goto out; |
| 5127 | io = &__io; |
| 5128 | } |
| 5129 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5130 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5131 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5132 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5133 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5134 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5135 | if (req_has_async_data(req)) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5136 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5137 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5138 | ret = -ENOMEM; |
| 5139 | goto out; |
| 5140 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5141 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5142 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5143 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5144 | if (ret == -ERESTARTSYS) |
| 5145 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5146 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5147 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5148 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5149 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5150 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5151 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5152 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5153 | #define IO_NETOP_FN(op) \ |
| 5154 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5155 | { \ |
| 5156 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5159 | #define IO_NETOP_PREP(op) \ |
| 5160 | IO_NETOP_FN(op) \ |
| 5161 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5162 | { \ |
| 5163 | return -EOPNOTSUPP; \ |
| 5164 | } \ |
| 5165 | |
| 5166 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5167 | IO_NETOP_PREP(op) \ |
| 5168 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5169 | { \ |
| 5170 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5171 | } |
| 5172 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5173 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5174 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5175 | IO_NETOP_PREP_ASYNC(connect); |
| 5176 | IO_NETOP_PREP(accept); |
| 5177 | IO_NETOP_FN(send); |
| 5178 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5179 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5180 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5181 | struct io_poll_table { |
| 5182 | struct poll_table_struct pt; |
| 5183 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5184 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5185 | int error; |
| 5186 | }; |
| 5187 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5188 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5189 | __poll_t mask, io_req_tw_func_t func) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5190 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5191 | /* for instances that support it check for an event match first: */ |
| 5192 | if (mask && !(mask & poll->events)) |
| 5193 | return 0; |
| 5194 | |
| 5195 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5196 | |
| 5197 | list_del_init(&poll->wait.entry); |
| 5198 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5199 | req->result = mask; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5200 | req->io_task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5201 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5202 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5203 | * If this fails, then the task is exiting. When a task exits, the |
| 5204 | * work gets canceled, so just cancel this request as well instead |
| 5205 | * of executing it. We can't safely execute it anyway, as we may not |
| 5206 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5207 | */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5208 | io_req_task_work_add(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5209 | return 1; |
| 5210 | } |
| 5211 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5212 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5213 | __acquires(&req->ctx->completion_lock) |
| 5214 | { |
| 5215 | struct io_ring_ctx *ctx = req->ctx; |
| 5216 | |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 5217 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5218 | if (unlikely(req->task->flags & PF_EXITING)) |
| 5219 | WRITE_ONCE(poll->canceled, true); |
| 5220 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5221 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5222 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5223 | |
| 5224 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5225 | } |
| 5226 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5227 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5228 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5229 | add_wait_queue(poll->head, &poll->wait); |
| 5230 | return true; |
| 5231 | } |
| 5232 | |
| 5233 | return false; |
| 5234 | } |
| 5235 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5236 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5237 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5238 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5239 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5240 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5241 | return req->apoll->double_poll; |
| 5242 | } |
| 5243 | |
| 5244 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5245 | { |
| 5246 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5247 | return &req->poll; |
| 5248 | return &req->apoll->poll; |
| 5249 | } |
| 5250 | |
| 5251 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5252 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5253 | { |
| 5254 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5255 | |
| 5256 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5257 | |
| 5258 | if (poll && poll->head) { |
| 5259 | struct wait_queue_head *head = poll->head; |
| 5260 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5261 | spin_lock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5262 | list_del_init(&poll->wait.entry); |
| 5263 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5264 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5265 | poll->head = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5266 | spin_unlock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5270 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5271 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5272 | { |
| 5273 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5274 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5275 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5276 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5277 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 5278 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5279 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5280 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5281 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5282 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5283 | if (req->poll.events & EPOLLONESHOT) |
| 5284 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5285 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
| 5286 | req->poll.events |= EPOLLONESHOT; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5287 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5288 | } |
Hao Xu | 7b289c3 | 2021-04-13 15:20:39 +0800 | [diff] [blame] | 5289 | if (flags & IORING_CQE_F_MORE) |
| 5290 | ctx->cq_extra++; |
| 5291 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5292 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5293 | } |
| 5294 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5295 | static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
| 5296 | __must_hold(&req->ctx->completion_lock) |
| 5297 | { |
| 5298 | bool done; |
| 5299 | |
| 5300 | done = __io_poll_complete(req, mask); |
| 5301 | io_commit_cqring(req->ctx); |
| 5302 | return done; |
| 5303 | } |
| 5304 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5305 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5306 | { |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5307 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5308 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5309 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5310 | if (io_poll_rewait(req, &req->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5311 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5312 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5313 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5314 | |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5315 | if (req->poll.done) { |
| 5316 | spin_unlock(&ctx->completion_lock); |
| 5317 | return; |
| 5318 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5319 | done = __io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5320 | if (done) { |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5321 | io_poll_remove_double(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5322 | hash_del(&req->hash_node); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5323 | req->poll.done = true; |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5324 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5325 | req->result = 0; |
| 5326 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 5327 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5328 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5329 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5330 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5331 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5332 | if (done) { |
| 5333 | nxt = io_put_req_find_next(req); |
| 5334 | if (nxt) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5335 | io_req_task_submit(nxt, locked); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5336 | } |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 5337 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5341 | int sync, void *key) |
| 5342 | { |
| 5343 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5344 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5345 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5346 | unsigned long flags; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5347 | |
| 5348 | /* for instances that support it check for an event match first: */ |
| 5349 | if (mask && !(mask & poll->events)) |
| 5350 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5351 | if (!(poll->events & EPOLLONESHOT)) |
| 5352 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5353 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5354 | list_del_init(&wait->entry); |
| 5355 | |
Jens Axboe | 9ce85ef | 2021-07-09 08:20:28 -0600 | [diff] [blame] | 5356 | if (poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5357 | bool done; |
| 5358 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5359 | spin_lock_irqsave(&poll->head->lock, flags); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5360 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5361 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5362 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5363 | /* make sure double remove sees this as being gone */ |
| 5364 | wait->private = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5365 | spin_unlock_irqrestore(&poll->head->lock, flags); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5366 | if (!done) { |
| 5367 | /* use wait func handler, so it matches the rq type */ |
| 5368 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5369 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5370 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5371 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5372 | return 1; |
| 5373 | } |
| 5374 | |
| 5375 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5376 | wait_queue_func_t wake_func) |
| 5377 | { |
| 5378 | poll->head = NULL; |
| 5379 | poll->done = false; |
| 5380 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 5381 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5382 | /* mask in events that we always want/need */ |
| 5383 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5384 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5385 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5386 | } |
| 5387 | |
| 5388 | 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] | 5389 | struct wait_queue_head *head, |
| 5390 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5391 | { |
| 5392 | struct io_kiocb *req = pt->req; |
| 5393 | |
| 5394 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5395 | * The file being polled uses multiple waitqueues for poll handling |
| 5396 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5397 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5398 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5399 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5400 | struct io_poll_iocb *poll_one = poll; |
| 5401 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5402 | /* double add on the same waitqueue head, ignore */ |
| 5403 | if (poll_one->head == head) |
| 5404 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5405 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5406 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5407 | if ((*poll_ptr)->head == head) |
| 5408 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5409 | pt->error = -EINVAL; |
| 5410 | return; |
| 5411 | } |
Jens Axboe | ea6a693d | 2021-04-15 09:47:13 -0600 | [diff] [blame] | 5412 | /* |
| 5413 | * Can't handle multishot for double wait for now, turn it |
| 5414 | * into one-shot mode. |
| 5415 | */ |
Pavel Begunkov | 7a27472 | 2021-05-17 12:43:34 +0100 | [diff] [blame] | 5416 | if (!(poll_one->events & EPOLLONESHOT)) |
| 5417 | poll_one->events |= EPOLLONESHOT; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5418 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5419 | if (!poll) { |
| 5420 | pt->error = -ENOMEM; |
| 5421 | return; |
| 5422 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5423 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5424 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5425 | poll->wait.private = req; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5426 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5427 | *poll_ptr = poll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5428 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5429 | req->flags |= REQ_F_ASYNC_DATA; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5430 | } |
| 5431 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5432 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5433 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5434 | |
| 5435 | if (poll->events & EPOLLEXCLUSIVE) |
| 5436 | add_wait_queue_exclusive(head, &poll->wait); |
| 5437 | else |
| 5438 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5442 | struct poll_table_struct *p) |
| 5443 | { |
| 5444 | 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] | 5445 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5446 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5447 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5448 | } |
| 5449 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5450 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5451 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5452 | struct async_poll *apoll = req->apoll; |
| 5453 | struct io_ring_ctx *ctx = req->ctx; |
| 5454 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5455 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5456 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5457 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5458 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5459 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5460 | } |
| 5461 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5462 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5463 | io_poll_remove_double(req); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5464 | apoll->poll.done = true; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5465 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5466 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5467 | if (!READ_ONCE(apoll->poll.canceled)) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5468 | io_req_task_submit(req, locked); |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5469 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5470 | io_req_complete_failed(req, -ECANCELED); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5471 | } |
| 5472 | |
| 5473 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5474 | void *key) |
| 5475 | { |
| 5476 | struct io_kiocb *req = wait->private; |
| 5477 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5478 | |
| 5479 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5480 | key_to_poll(key)); |
| 5481 | |
| 5482 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5483 | } |
| 5484 | |
| 5485 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5486 | { |
| 5487 | struct io_ring_ctx *ctx = req->ctx; |
| 5488 | struct hlist_head *list; |
| 5489 | |
| 5490 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5491 | hlist_add_head(&req->hash_node, list); |
| 5492 | } |
| 5493 | |
| 5494 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5495 | struct io_poll_iocb *poll, |
| 5496 | struct io_poll_table *ipt, __poll_t mask, |
| 5497 | wait_queue_func_t wake_func) |
| 5498 | __acquires(&ctx->completion_lock) |
| 5499 | { |
| 5500 | struct io_ring_ctx *ctx = req->ctx; |
| 5501 | bool cancel = false; |
| 5502 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5503 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5504 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5505 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5506 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5507 | |
| 5508 | ipt->pt._key = mask; |
| 5509 | ipt->req = req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5510 | ipt->error = 0; |
| 5511 | ipt->nr_entries = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5512 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5513 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5514 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
| 5515 | ipt->error = -EINVAL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5516 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5517 | spin_lock(&ctx->completion_lock); |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5518 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
Pavel Begunkov | 46fee9a | 2021-07-20 10:50:44 +0100 | [diff] [blame] | 5519 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5520 | if (likely(poll->head)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5521 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5522 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5523 | if (ipt->error) |
| 5524 | cancel = true; |
| 5525 | ipt->error = 0; |
| 5526 | mask = 0; |
| 5527 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5528 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5529 | list_del_init(&poll->wait.entry); |
| 5530 | else if (cancel) |
| 5531 | WRITE_ONCE(poll->canceled, true); |
| 5532 | else if (!poll->done) /* actually waiting for an event */ |
| 5533 | io_poll_req_insert(req); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5534 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | return mask; |
| 5538 | } |
| 5539 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5540 | enum { |
| 5541 | IO_APOLL_OK, |
| 5542 | IO_APOLL_ABORTED, |
| 5543 | IO_APOLL_READY |
| 5544 | }; |
| 5545 | |
| 5546 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5547 | { |
| 5548 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5549 | struct io_ring_ctx *ctx = req->ctx; |
| 5550 | struct async_poll *apoll; |
| 5551 | struct io_poll_table ipt; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5552 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5553 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5554 | |
| 5555 | if (!req->file || !file_can_poll(req->file)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5556 | return IO_APOLL_ABORTED; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5557 | if (req->flags & REQ_F_POLLED) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5558 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5559 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5560 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5561 | |
| 5562 | if (def->pollin) { |
| 5563 | rw = READ; |
| 5564 | mask |= POLLIN | POLLRDNORM; |
| 5565 | |
| 5566 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5567 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5568 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5569 | mask &= ~POLLIN; |
| 5570 | } else { |
| 5571 | rw = WRITE; |
| 5572 | mask |= POLLOUT | POLLWRNORM; |
| 5573 | } |
| 5574 | |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5575 | /* if we can't nonblock try, then no point in arming a poll handler */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 5576 | if (!io_file_supports_nowait(req, rw)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5577 | return IO_APOLL_ABORTED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5578 | |
| 5579 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5580 | if (unlikely(!apoll)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5581 | return IO_APOLL_ABORTED; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5582 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5583 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5584 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5585 | ipt.pt._qproc = io_async_queue_proc; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5586 | io_req_set_refcount(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5587 | |
| 5588 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5589 | io_async_wake); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5590 | spin_unlock(&ctx->completion_lock); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5591 | if (ret || ipt.error) |
| 5592 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5593 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5594 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5595 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5596 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5600 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5601 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5602 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5603 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5604 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5605 | if (!poll->head) |
| 5606 | return false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5607 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5608 | if (do_cancel) |
| 5609 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5610 | if (!list_empty(&poll->wait.entry)) { |
| 5611 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5612 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5613 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5614 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5615 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5616 | return do_complete; |
| 5617 | } |
| 5618 | |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5619 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5620 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5621 | { |
| 5622 | bool do_complete; |
| 5623 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5624 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5625 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5626 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5627 | if (do_complete) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5628 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5629 | io_commit_cqring(req->ctx); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5630 | req_set_fail(req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5631 | io_put_req_deferred(req); |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5632 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5633 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5634 | } |
| 5635 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5636 | /* |
| 5637 | * Returns true if we found and killed one or more poll requests |
| 5638 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 5639 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
| 5640 | struct task_struct *tsk, bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5641 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5642 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5643 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5644 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5645 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5646 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5647 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5648 | struct hlist_head *list; |
| 5649 | |
| 5650 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5651 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 5652 | if (io_match_task(req, tsk, cancel_all)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5653 | posted += io_poll_remove_one(req); |
| 5654 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5655 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5656 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5657 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5658 | if (posted) |
| 5659 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5660 | |
| 5661 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5662 | } |
| 5663 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5664 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5665 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5666 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5667 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5668 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5669 | struct io_kiocb *req; |
| 5670 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5671 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5672 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5673 | if (sqe_addr != req->user_data) |
| 5674 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5675 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5676 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5677 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5678 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5679 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5680 | } |
| 5681 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5682 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5683 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5684 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5685 | { |
| 5686 | struct io_kiocb *req; |
| 5687 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5688 | req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5689 | if (!req) |
| 5690 | return -ENOENT; |
| 5691 | if (io_poll_remove_one(req)) |
| 5692 | return 0; |
| 5693 | |
| 5694 | return -EALREADY; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5695 | } |
| 5696 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5697 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5698 | unsigned int flags) |
| 5699 | { |
| 5700 | u32 events; |
| 5701 | |
| 5702 | events = READ_ONCE(sqe->poll32_events); |
| 5703 | #ifdef __BIG_ENDIAN |
| 5704 | events = swahw32(events); |
| 5705 | #endif |
| 5706 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5707 | events |= EPOLLONESHOT; |
| 5708 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5709 | } |
| 5710 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5711 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5712 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5713 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5714 | struct io_poll_update *upd = &req->poll_update; |
| 5715 | u32 flags; |
| 5716 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5717 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5718 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5719 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5720 | return -EINVAL; |
| 5721 | flags = READ_ONCE(sqe->len); |
| 5722 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5723 | IORING_POLL_ADD_MULTI)) |
| 5724 | return -EINVAL; |
| 5725 | /* meaningless without update */ |
| 5726 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5727 | return -EINVAL; |
| 5728 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5729 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5730 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5731 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5732 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5733 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5734 | if (!upd->update_user_data && upd->new_user_data) |
| 5735 | return -EINVAL; |
| 5736 | if (upd->update_events) |
| 5737 | upd->events = io_poll_parse_events(sqe, flags); |
| 5738 | else if (sqe->poll32_events) |
| 5739 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5740 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5741 | return 0; |
| 5742 | } |
| 5743 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5744 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5745 | void *key) |
| 5746 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5747 | struct io_kiocb *req = wait->private; |
| 5748 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5749 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5750 | 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] | 5751 | } |
| 5752 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5753 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5754 | struct poll_table_struct *p) |
| 5755 | { |
| 5756 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5757 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5758 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5759 | } |
| 5760 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5761 | 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] | 5762 | { |
| 5763 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5764 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5765 | |
| 5766 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5767 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5768 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5769 | return -EINVAL; |
| 5770 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5771 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5772 | return -EINVAL; |
| 5773 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5774 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5775 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5776 | return 0; |
| 5777 | } |
| 5778 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5779 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5780 | { |
| 5781 | struct io_poll_iocb *poll = &req->poll; |
| 5782 | struct io_ring_ctx *ctx = req->ctx; |
| 5783 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5784 | __poll_t mask; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5785 | bool done; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5786 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5787 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5788 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5789 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5790 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5791 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5792 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5793 | ipt.error = 0; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5794 | done = io_poll_complete(req, mask); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5795 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5796 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5797 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5798 | if (mask) { |
| 5799 | io_cqring_ev_posted(ctx); |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5800 | if (done) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5801 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5802 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5803 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5804 | } |
| 5805 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5806 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5807 | { |
| 5808 | struct io_ring_ctx *ctx = req->ctx; |
| 5809 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5810 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5811 | int ret; |
| 5812 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5813 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5814 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5815 | if (!preq) { |
| 5816 | ret = -ENOENT; |
| 5817 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5818 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5819 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5820 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
| 5821 | completing = true; |
| 5822 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; |
| 5823 | goto err; |
| 5824 | } |
| 5825 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5826 | /* |
| 5827 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5828 | * update those. For multishot, if we're racing with completion, just |
| 5829 | * let completion re-add it. |
| 5830 | */ |
| 5831 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5832 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5833 | ret = -EALREADY; |
| 5834 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5835 | } |
| 5836 | /* we now have a detached poll request. reissue. */ |
| 5837 | ret = 0; |
| 5838 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5839 | if (ret < 0) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5840 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5841 | req_set_fail(req); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5842 | io_req_complete(req, ret); |
| 5843 | return 0; |
| 5844 | } |
| 5845 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5846 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5847 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5848 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5849 | preq->poll.events |= IO_POLL_UNMASK; |
| 5850 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5851 | if (req->poll_update.update_user_data) |
| 5852 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5853 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5854 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5855 | /* complete update request, we're done with it */ |
| 5856 | io_req_complete(req, ret); |
| 5857 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5858 | if (!completing) { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5859 | ret = io_poll_add(preq, issue_flags); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5860 | if (ret < 0) { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5861 | req_set_fail(preq); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5862 | io_req_complete(preq, ret); |
| 5863 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5864 | } |
| 5865 | return 0; |
| 5866 | } |
| 5867 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5868 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5869 | { |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 5870 | struct io_timeout_data *data = req->async_data; |
| 5871 | |
| 5872 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
| 5873 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 5874 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5875 | } |
| 5876 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5877 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5878 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5879 | struct io_timeout_data *data = container_of(timer, |
| 5880 | struct io_timeout_data, timer); |
| 5881 | struct io_kiocb *req = data->req; |
| 5882 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5883 | unsigned long flags; |
| 5884 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5885 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5886 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5887 | atomic_set(&req->ctx->cq_timeouts, |
| 5888 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5889 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5890 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5891 | req->io_task_work.func = io_req_task_timeout; |
| 5892 | io_req_task_work_add(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5893 | return HRTIMER_NORESTART; |
| 5894 | } |
| 5895 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5896 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5897 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5898 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5899 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5900 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5901 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5902 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5903 | |
| 5904 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5905 | found = user_data == req->user_data; |
| 5906 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5907 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5908 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5909 | if (!found) |
| 5910 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5911 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5912 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5913 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5914 | return ERR_PTR(-EALREADY); |
| 5915 | list_del_init(&req->timeout.list); |
| 5916 | return req; |
| 5917 | } |
| 5918 | |
| 5919 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 5920 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5921 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5922 | { |
| 5923 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5924 | |
| 5925 | if (IS_ERR(req)) |
| 5926 | return PTR_ERR(req); |
| 5927 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5928 | req_set_fail(req); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5929 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5930 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5931 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5932 | } |
| 5933 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 5934 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 5935 | { |
| 5936 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 5937 | case IORING_TIMEOUT_BOOTTIME: |
| 5938 | return CLOCK_BOOTTIME; |
| 5939 | case IORING_TIMEOUT_REALTIME: |
| 5940 | return CLOCK_REALTIME; |
| 5941 | default: |
| 5942 | /* can't happen, vetted at prep time */ |
| 5943 | WARN_ON_ONCE(1); |
| 5944 | fallthrough; |
| 5945 | case 0: |
| 5946 | return CLOCK_MONOTONIC; |
| 5947 | } |
| 5948 | } |
| 5949 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 5950 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5951 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5952 | __must_hold(&ctx->timeout_lock) |
| 5953 | { |
| 5954 | struct io_timeout_data *io; |
| 5955 | struct io_kiocb *req; |
| 5956 | bool found = false; |
| 5957 | |
| 5958 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 5959 | found = user_data == req->user_data; |
| 5960 | if (found) |
| 5961 | break; |
| 5962 | } |
| 5963 | if (!found) |
| 5964 | return -ENOENT; |
| 5965 | |
| 5966 | io = req->async_data; |
| 5967 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 5968 | return -EALREADY; |
| 5969 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 5970 | io->timer.function = io_link_timeout_fn; |
| 5971 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5975 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5976 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5977 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5978 | { |
| 5979 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5980 | struct io_timeout_data *data; |
| 5981 | |
| 5982 | if (IS_ERR(req)) |
| 5983 | return PTR_ERR(req); |
| 5984 | |
| 5985 | req->timeout.off = 0; /* noseq */ |
| 5986 | data = req->async_data; |
| 5987 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 5988 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5989 | data->timer.function = io_timeout_fn; |
| 5990 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5991 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5992 | } |
| 5993 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5994 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5995 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5996 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5997 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5998 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5999 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6000 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6001 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6002 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6003 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6004 | return -EINVAL; |
| 6005 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6006 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6007 | tr->addr = READ_ONCE(sqe->addr); |
| 6008 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6009 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6010 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6011 | return -EINVAL; |
| 6012 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6013 | tr->ltimeout = true; |
| 6014 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6015 | return -EINVAL; |
| 6016 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6017 | return -EFAULT; |
| 6018 | } else if (tr->flags) { |
| 6019 | /* timeout removal doesn't support flags */ |
| 6020 | return -EINVAL; |
| 6021 | } |
| 6022 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6023 | return 0; |
| 6024 | } |
| 6025 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6026 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6027 | { |
| 6028 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6029 | : HRTIMER_MODE_REL; |
| 6030 | } |
| 6031 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6032 | /* |
| 6033 | * Remove or update an existing timeout command |
| 6034 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6035 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6036 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6037 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6038 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6039 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6040 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6041 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6042 | spin_lock(&ctx->completion_lock); |
| 6043 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6044 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6045 | spin_unlock_irq(&ctx->timeout_lock); |
| 6046 | spin_unlock(&ctx->completion_lock); |
| 6047 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6048 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6049 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6050 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6051 | if (tr->ltimeout) |
| 6052 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6053 | else |
| 6054 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6055 | spin_unlock_irq(&ctx->timeout_lock); |
| 6056 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6057 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6058 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6059 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6060 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6061 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6062 | } |
| 6063 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6064 | 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] | 6065 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6066 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6067 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6068 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6069 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6070 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6071 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6072 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6073 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6074 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6075 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6076 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6077 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6078 | flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 6079 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
| 6080 | IORING_TIMEOUT_ETIME_SUCCESS)) |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6081 | return -EINVAL; |
| 6082 | /* more than one clock specified is invalid, obviously */ |
| 6083 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6084 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6085 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6086 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6087 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6088 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6089 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6090 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6091 | if (!req_has_async_data(req) && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6092 | return -ENOMEM; |
| 6093 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6094 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6095 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6096 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6097 | |
| 6098 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6099 | return -EFAULT; |
| 6100 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6101 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6102 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6103 | |
| 6104 | if (is_timeout_link) { |
| 6105 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6106 | |
| 6107 | if (!link->head) |
| 6108 | return -EINVAL; |
| 6109 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6110 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6111 | req->timeout.head = link->last; |
| 6112 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6113 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6114 | return 0; |
| 6115 | } |
| 6116 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6117 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6118 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6119 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6120 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6121 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6122 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6123 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6124 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6125 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6126 | /* |
| 6127 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6128 | * timeout event to be satisfied. If it isn't set, then this is |
| 6129 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6130 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6131 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6132 | entry = ctx->timeout_list.prev; |
| 6133 | goto add; |
| 6134 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6135 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6136 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6137 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6138 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6139 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6140 | * This is safe because ->completion_lock is held, and submissions |
| 6141 | * and completions are never mixed in the same ->completion_lock section. |
| 6142 | */ |
| 6143 | ctx->cq_last_tm_flush = tail; |
| 6144 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6145 | /* |
| 6146 | * Insertion sort, ensuring the first entry in the list is always |
| 6147 | * the one we need first. |
| 6148 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6149 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6150 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6151 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6152 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6153 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6154 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6155 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6156 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6157 | break; |
| 6158 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6159 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6160 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6161 | data->timer.function = io_timeout_fn; |
| 6162 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6163 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6164 | return 0; |
| 6165 | } |
| 6166 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6167 | struct io_cancel_data { |
| 6168 | struct io_ring_ctx *ctx; |
| 6169 | u64 user_data; |
| 6170 | }; |
| 6171 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6172 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6173 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6174 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6175 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6176 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6177 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6178 | } |
| 6179 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6180 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6181 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6182 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6183 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6184 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6185 | int ret = 0; |
| 6186 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6187 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6188 | return -ENOENT; |
| 6189 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6190 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6191 | switch (cancel_ret) { |
| 6192 | case IO_WQ_CANCEL_OK: |
| 6193 | ret = 0; |
| 6194 | break; |
| 6195 | case IO_WQ_CANCEL_RUNNING: |
| 6196 | ret = -EALREADY; |
| 6197 | break; |
| 6198 | case IO_WQ_CANCEL_NOTFOUND: |
| 6199 | ret = -ENOENT; |
| 6200 | break; |
| 6201 | } |
| 6202 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6203 | return ret; |
| 6204 | } |
| 6205 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6206 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6207 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6208 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6209 | int ret; |
| 6210 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6211 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6212 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6213 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6214 | if (ret != -ENOENT) |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6215 | return ret; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6216 | |
| 6217 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6218 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6219 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6220 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6221 | if (ret != -ENOENT) |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6222 | goto out; |
| 6223 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6224 | out: |
| 6225 | spin_unlock(&ctx->completion_lock); |
| 6226 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6227 | } |
| 6228 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6229 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6230 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6231 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6232 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6233 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6234 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6235 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6236 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6237 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6238 | return -EINVAL; |
| 6239 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6240 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6241 | return 0; |
| 6242 | } |
| 6243 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6244 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6245 | { |
| 6246 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6247 | u64 sqe_addr = req->cancel.addr; |
| 6248 | struct io_tctx_node *node; |
| 6249 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6250 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6251 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6252 | if (ret != -ENOENT) |
| 6253 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6254 | |
| 6255 | /* slow path, try all io-wq's */ |
| 6256 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 6257 | ret = -ENOENT; |
| 6258 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6259 | struct io_uring_task *tctx = node->task->io_uring; |
| 6260 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6261 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6262 | if (ret != -ENOENT) |
| 6263 | break; |
| 6264 | } |
| 6265 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6266 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6267 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6268 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6269 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6270 | return 0; |
| 6271 | } |
| 6272 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6273 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6274 | const struct io_uring_sqe *sqe) |
| 6275 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6276 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6277 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6278 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6279 | return -EINVAL; |
| 6280 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6281 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6282 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6283 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6284 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6285 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6286 | return 0; |
| 6287 | } |
| 6288 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6289 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6290 | { |
| 6291 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6292 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6293 | int ret; |
| 6294 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6295 | up.offset = req->rsrc_update.offset; |
| 6296 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6297 | up.nr = 0; |
| 6298 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6299 | up.resv = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6300 | |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6301 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6302 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6303 | &up, req->rsrc_update.nr_args); |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6304 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6305 | |
| 6306 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6307 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6308 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6309 | return 0; |
| 6310 | } |
| 6311 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6312 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6313 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6314 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6315 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6316 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6317 | case IORING_OP_READV: |
| 6318 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6319 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6320 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6321 | case IORING_OP_WRITEV: |
| 6322 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6323 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6324 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6325 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6326 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6327 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6328 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6329 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6330 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6331 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6332 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6333 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6334 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6335 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6336 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6337 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6338 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6339 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6340 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6341 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6342 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6343 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6344 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6345 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6346 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6347 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6348 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6349 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6350 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6351 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6352 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6353 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6354 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6355 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6356 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6357 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6358 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6359 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6360 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6361 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6362 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6363 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6364 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6365 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6366 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6367 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6368 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6369 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6370 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6371 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6372 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6373 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6374 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6375 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6376 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6377 | case IORING_OP_SHUTDOWN: |
| 6378 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6379 | case IORING_OP_RENAMEAT: |
| 6380 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6381 | case IORING_OP_UNLINKAT: |
| 6382 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6383 | case IORING_OP_MKDIRAT: |
| 6384 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6385 | case IORING_OP_SYMLINKAT: |
| 6386 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6387 | case IORING_OP_LINKAT: |
| 6388 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6389 | } |
| 6390 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6391 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6392 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6393 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6394 | } |
| 6395 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6396 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6397 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6398 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6399 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6400 | if (WARN_ON_ONCE(req_has_async_data(req))) |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6401 | return -EFAULT; |
| 6402 | if (io_alloc_async_data(req)) |
| 6403 | return -EAGAIN; |
| 6404 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6405 | switch (req->opcode) { |
| 6406 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6407 | return io_rw_prep_async(req, READ); |
| 6408 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6409 | return io_rw_prep_async(req, WRITE); |
| 6410 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6411 | return io_sendmsg_prep_async(req); |
| 6412 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6413 | return io_recvmsg_prep_async(req); |
| 6414 | case IORING_OP_CONNECT: |
| 6415 | return io_connect_prep_async(req); |
| 6416 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6417 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6418 | req->opcode); |
| 6419 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6420 | } |
| 6421 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6422 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6423 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6424 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6425 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6426 | /* need original cached_sq_head, but it was increased for each req */ |
| 6427 | io_for_each_link(req, req) |
| 6428 | seq--; |
| 6429 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6430 | } |
| 6431 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 6432 | static __cold void io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6433 | { |
| 6434 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6435 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6436 | int ret; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6437 | u32 seq = io_get_sequence(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6438 | |
| 6439 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6440 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6441 | queue: |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6442 | ctx->drain_active = false; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6443 | io_req_task_queue(req); |
| 6444 | return; |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6445 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6446 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6447 | ret = io_req_prep_async(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6448 | if (ret) { |
| 6449 | fail: |
| 6450 | io_req_complete_failed(req, ret); |
| 6451 | return; |
| 6452 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6453 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6454 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6455 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6456 | ret = -ENOMEM; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6457 | goto fail; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6458 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6459 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6460 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6461 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6462 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6463 | kfree(de); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6464 | goto queue; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
| 6467 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6468 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6469 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6470 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6471 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6472 | } |
| 6473 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6474 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6475 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6476 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 6477 | kfree(req->kbuf); |
| 6478 | req->kbuf = NULL; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6479 | } |
| 6480 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6481 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6482 | switch (req->opcode) { |
| 6483 | case IORING_OP_READV: |
| 6484 | case IORING_OP_READ_FIXED: |
| 6485 | case IORING_OP_READ: |
| 6486 | case IORING_OP_WRITEV: |
| 6487 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6488 | case IORING_OP_WRITE: { |
| 6489 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6490 | |
| 6491 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6492 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6493 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6494 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6495 | case IORING_OP_SENDMSG: { |
| 6496 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6497 | |
| 6498 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6499 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6500 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6501 | case IORING_OP_SPLICE: |
| 6502 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6503 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6504 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6505 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6506 | case IORING_OP_OPENAT: |
| 6507 | case IORING_OP_OPENAT2: |
| 6508 | if (req->open.filename) |
| 6509 | putname(req->open.filename); |
| 6510 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6511 | case IORING_OP_RENAMEAT: |
| 6512 | putname(req->rename.oldpath); |
| 6513 | putname(req->rename.newpath); |
| 6514 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6515 | case IORING_OP_UNLINKAT: |
| 6516 | putname(req->unlink.filename); |
| 6517 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6518 | case IORING_OP_MKDIRAT: |
| 6519 | putname(req->mkdir.filename); |
| 6520 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6521 | case IORING_OP_SYMLINKAT: |
| 6522 | putname(req->symlink.oldpath); |
| 6523 | putname(req->symlink.newpath); |
| 6524 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6525 | case IORING_OP_LINKAT: |
| 6526 | putname(req->hardlink.oldpath); |
| 6527 | putname(req->hardlink.newpath); |
| 6528 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6529 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6530 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6531 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6532 | kfree(req->apoll->double_poll); |
| 6533 | kfree(req->apoll); |
| 6534 | req->apoll = NULL; |
| 6535 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6536 | if (req->flags & REQ_F_INFLIGHT) { |
| 6537 | struct io_uring_task *tctx = req->task->io_uring; |
| 6538 | |
| 6539 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6540 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6541 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6542 | put_cred(req->creds); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6543 | if (req->flags & REQ_F_ASYNC_DATA) { |
| 6544 | kfree(req->async_data); |
| 6545 | req->async_data = NULL; |
| 6546 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6547 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6548 | } |
| 6549 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6550 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6551 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6552 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6553 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6554 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6555 | |
Pavel Begunkov | 6878b40 | 2021-09-24 21:59:41 +0100 | [diff] [blame] | 6556 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6557 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6558 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6559 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6560 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6561 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6562 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6563 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6564 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6565 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6566 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6567 | break; |
| 6568 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6569 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6570 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6571 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6572 | break; |
| 6573 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6574 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6575 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6576 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6577 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6578 | break; |
| 6579 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6580 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6581 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6582 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6583 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6584 | break; |
| 6585 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6586 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6587 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6588 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6589 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6590 | break; |
| 6591 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6592 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6593 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6594 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6595 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6596 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6597 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6598 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6599 | break; |
| 6600 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6601 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6602 | break; |
| 6603 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6604 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6605 | break; |
| 6606 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6607 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6608 | break; |
| 6609 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6610 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6611 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6612 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6613 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6614 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6615 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6616 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6617 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6618 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6619 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6620 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6621 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6622 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6623 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6624 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6625 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6626 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6627 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6628 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6629 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6630 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6631 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6632 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6633 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6634 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6635 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6636 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6637 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6638 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6639 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6640 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6641 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6642 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6643 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6644 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6645 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6646 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6647 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6648 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6649 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6650 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6651 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6652 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6653 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6654 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6655 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6656 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6657 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6658 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6659 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6660 | case IORING_OP_MKDIRAT: |
| 6661 | ret = io_mkdirat(req, issue_flags); |
| 6662 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6663 | case IORING_OP_SYMLINKAT: |
| 6664 | ret = io_symlinkat(req, issue_flags); |
| 6665 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6666 | case IORING_OP_LINKAT: |
| 6667 | ret = io_linkat(req, issue_flags); |
| 6668 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6669 | default: |
| 6670 | ret = -EINVAL; |
| 6671 | break; |
| 6672 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6673 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6674 | if (creds) |
| 6675 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6676 | if (ret) |
| 6677 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6678 | /* If the op doesn't have a file, we're not polling for it */ |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 6679 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
| 6680 | io_iopoll_req_issued(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6681 | |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 6685 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 6686 | { |
| 6687 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6688 | |
| 6689 | req = io_put_req_find_next(req); |
| 6690 | return req ? &req->work : NULL; |
| 6691 | } |
| 6692 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6693 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6694 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6695 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6696 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6697 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6698 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6699 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 6700 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 6701 | __io_req_set_refcount(req, 2); |
| 6702 | else |
| 6703 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6704 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6705 | timeout = io_prep_linked_timeout(req); |
| 6706 | if (timeout) |
| 6707 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6708 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6709 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6710 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6711 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6712 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6713 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6714 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6715 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6716 | /* |
| 6717 | * We can get EAGAIN for polled IO even though we're |
| 6718 | * forcing a sync submission from here, since we can't |
| 6719 | * wait for request slots on the block side. |
| 6720 | */ |
| 6721 | if (ret != -EAGAIN) |
| 6722 | break; |
| 6723 | cond_resched(); |
| 6724 | } while (1); |
| 6725 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6726 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6727 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6728 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6729 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6730 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6731 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6732 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6733 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6734 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6735 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6738 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6739 | int index) |
| 6740 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6741 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6742 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6743 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6744 | } |
| 6745 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6746 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6747 | { |
| 6748 | unsigned long file_ptr = (unsigned long) file; |
| 6749 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6750 | if (__io_file_supports_nowait(file, READ)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6751 | file_ptr |= FFS_ASYNC_READ; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6752 | if (__io_file_supports_nowait(file, WRITE)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6753 | file_ptr |= FFS_ASYNC_WRITE; |
| 6754 | if (S_ISREG(file_inode(file)->i_mode)) |
| 6755 | file_ptr |= FFS_ISREG; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6756 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6757 | } |
| 6758 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6759 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
| 6760 | struct io_kiocb *req, int fd) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6761 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6762 | struct file *file; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6763 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6764 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6765 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
| 6766 | return NULL; |
| 6767 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6768 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 6769 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6770 | file_ptr &= ~FFS_MASK; |
| 6771 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6772 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6773 | io_req_set_rsrc_node(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6774 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6775 | } |
| 6776 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6777 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6778 | struct io_kiocb *req, int fd) |
| 6779 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6780 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6781 | |
| 6782 | trace_io_uring_file_get(ctx, fd); |
| 6783 | |
| 6784 | /* we don't allow fixed io_uring files */ |
| 6785 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6786 | io_req_track_inflight(req); |
| 6787 | return file; |
| 6788 | } |
| 6789 | |
| 6790 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6791 | struct io_kiocb *req, int fd, bool fixed) |
| 6792 | { |
| 6793 | if (fixed) |
| 6794 | return io_file_get_fixed(ctx, req, fd); |
| 6795 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6796 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6797 | } |
| 6798 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6799 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6800 | { |
| 6801 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6802 | int ret; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6803 | |
| 6804 | if (prev) { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6805 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6806 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6807 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6808 | } else { |
| 6809 | io_req_complete_post(req, -ETIME, 0); |
| 6810 | } |
| 6811 | } |
| 6812 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6813 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6814 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6815 | struct io_timeout_data *data = container_of(timer, |
| 6816 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6817 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6818 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6819 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6820 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6821 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6822 | prev = req->timeout.head; |
| 6823 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6824 | |
| 6825 | /* |
| 6826 | * We don't expect the list to be empty, that will only happen if we |
| 6827 | * race with the completion of the linked work. |
| 6828 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6829 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6830 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6831 | if (!req_ref_inc_not_zero(prev)) |
| 6832 | prev = NULL; |
| 6833 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6834 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6835 | req->timeout.prev = prev; |
| 6836 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6837 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6838 | req->io_task_work.func = io_req_task_link_timeout; |
| 6839 | io_req_task_work_add(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6840 | return HRTIMER_NORESTART; |
| 6841 | } |
| 6842 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6843 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6844 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6845 | struct io_ring_ctx *ctx = req->ctx; |
| 6846 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6847 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6848 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6849 | * If the back reference is NULL, then our linked request finished |
| 6850 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6851 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6852 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6853 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6854 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6855 | data->timer.function = io_link_timeout_fn; |
| 6856 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6857 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6858 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6859 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6860 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6861 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6862 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6863 | } |
| 6864 | |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6865 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
| 6866 | __must_hold(&req->ctx->uring_lock) |
| 6867 | { |
| 6868 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 6869 | |
| 6870 | switch (io_arm_poll_handler(req)) { |
| 6871 | case IO_APOLL_READY: |
| 6872 | if (linked_timeout) { |
| 6873 | io_unprep_linked_timeout(req); |
| 6874 | linked_timeout = NULL; |
| 6875 | } |
| 6876 | io_req_task_queue(req); |
| 6877 | break; |
| 6878 | case IO_APOLL_ABORTED: |
| 6879 | /* |
| 6880 | * Queued up for async execution, worker will release |
| 6881 | * submit reference when the iocb is actually submitted. |
| 6882 | */ |
| 6883 | io_queue_async_work(req, NULL); |
| 6884 | break; |
| 6885 | } |
| 6886 | |
| 6887 | if (linked_timeout) |
| 6888 | io_queue_linked_timeout(linked_timeout); |
| 6889 | } |
| 6890 | |
| 6891 | static inline void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 6892 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6893 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6894 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 6895 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6896 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6897 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6898 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 6899 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
| 6900 | io_req_add_compl_list(req); |
Pavel Begunkov | d9f9d28 | 2021-09-24 22:00:00 +0100 | [diff] [blame] | 6901 | return; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 6902 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6903 | /* |
| 6904 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6905 | * doesn't support non-blocking read/write attempts |
| 6906 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6907 | if (likely(!ret)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6908 | linked_timeout = io_prep_linked_timeout(req); |
| 6909 | if (linked_timeout) |
| 6910 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6911 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6912 | io_queue_sqe_arm_apoll(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6913 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6914 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6915 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6916 | } |
| 6917 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 6918 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 6919 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6920 | { |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 6921 | if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 6922 | io_req_complete_fail_submit(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6923 | } else if (unlikely(req->ctx->drain_active)) { |
| 6924 | io_drain_req(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6925 | } else { |
| 6926 | int ret = io_req_prep_async(req); |
| 6927 | |
| 6928 | if (unlikely(ret)) |
| 6929 | io_req_complete_failed(req, ret); |
| 6930 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6931 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6932 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6933 | } |
| 6934 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 6935 | static inline void io_queue_sqe(struct io_kiocb *req) |
| 6936 | __must_hold(&req->ctx->uring_lock) |
| 6937 | { |
| 6938 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) |
| 6939 | __io_queue_sqe(req); |
| 6940 | else |
| 6941 | io_queue_sqe_fallback(req); |
| 6942 | } |
| 6943 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6944 | /* |
| 6945 | * Check SQE restrictions (opcode and flags). |
| 6946 | * |
| 6947 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6948 | */ |
| 6949 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6950 | struct io_kiocb *req, |
| 6951 | unsigned int sqe_flags) |
| 6952 | { |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6953 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6954 | return false; |
| 6955 | |
| 6956 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6957 | ctx->restrictions.sqe_flags_required) |
| 6958 | return false; |
| 6959 | |
| 6960 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6961 | ctx->restrictions.sqe_flags_required)) |
| 6962 | return false; |
| 6963 | |
| 6964 | return true; |
| 6965 | } |
| 6966 | |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 6967 | static void io_init_req_drain(struct io_kiocb *req) |
| 6968 | { |
| 6969 | struct io_ring_ctx *ctx = req->ctx; |
| 6970 | struct io_kiocb *head = ctx->submit_state.link.head; |
| 6971 | |
| 6972 | ctx->drain_active = true; |
| 6973 | if (head) { |
| 6974 | /* |
| 6975 | * If we need to drain a request in the middle of a link, drain |
| 6976 | * the head request and the next request/link after the current |
| 6977 | * link. Considering sequential execution of links, |
| 6978 | * IOSQE_IO_DRAIN will be maintained for every request of our |
| 6979 | * link. |
| 6980 | */ |
| 6981 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
| 6982 | ctx->drain_next = true; |
| 6983 | } |
| 6984 | } |
| 6985 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6986 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6987 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 6988 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6989 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6990 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6991 | unsigned int sqe_flags; |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 6992 | int personality; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6993 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 6994 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6995 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6996 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6997 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6998 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6999 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7000 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7001 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7002 | |
| 7003 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 7004 | return -EINVAL; |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7005 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
| 7006 | /* enforce forwards compatibility on users */ |
| 7007 | if (sqe_flags & ~SQE_VALID_FLAGS) |
| 7008 | return -EINVAL; |
| 7009 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 7010 | !io_op_defs[req->opcode].buffer_select) |
| 7011 | return -EOPNOTSUPP; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7012 | if (sqe_flags & IOSQE_IO_DRAIN) |
| 7013 | io_init_req_drain(req); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7014 | } |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7015 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { |
| 7016 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) |
| 7017 | return -EACCES; |
| 7018 | /* knock it to the slow queue path, will be drained there */ |
| 7019 | if (ctx->drain_active) |
| 7020 | req->flags |= REQ_F_FORCE_ASYNC; |
| 7021 | /* if there is no link, we're at "next" request and need to drain */ |
| 7022 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { |
| 7023 | ctx->drain_next = false; |
| 7024 | ctx->drain_active = true; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7025 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7026 | } |
| 7027 | } |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7028 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7029 | personality = READ_ONCE(sqe->personality); |
| 7030 | if (personality) { |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7031 | req->creds = xa_load(&ctx->personalities, personality); |
| 7032 | if (!req->creds) |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7033 | return -EINVAL; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7034 | get_cred(req->creds); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 7035 | req->flags |= REQ_F_CREDS; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7036 | } |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7037 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7038 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7039 | /* |
| 7040 | * Plug now if we have more than 1 IO left after this, and the target |
| 7041 | * is potentially a read/write to block based storage. |
| 7042 | */ |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7043 | if (state->need_plug && io_op_defs[req->opcode].plug) { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7044 | blk_start_plug(&state->plug); |
| 7045 | state->plug_started = true; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7046 | state->need_plug = false; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7047 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7048 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7049 | if (io_op_defs[req->opcode].needs_file) { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7050 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7051 | (sqe_flags & IOSQE_FIXED_FILE)); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7052 | if (unlikely(!req->file)) |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7053 | return -EBADF; |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7054 | } |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7055 | |
| 7056 | return io_req_prep(req, sqe); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7057 | } |
| 7058 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7059 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7060 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7061 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7062 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7063 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7064 | int ret; |
| 7065 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7066 | ret = io_init_req(ctx, req, sqe); |
| 7067 | if (unlikely(ret)) { |
Jens Axboe | a87acfd | 2021-09-11 16:04:50 -0600 | [diff] [blame] | 7068 | trace_io_uring_req_failed(sqe, ret); |
| 7069 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7070 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7071 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7072 | /* |
| 7073 | * we can judge a link req is failed or cancelled by if |
| 7074 | * REQ_F_FAIL is set, but the head is an exception since |
| 7075 | * it may be set REQ_F_FAIL because of other req's failure |
| 7076 | * so let's leverage req->result to distinguish if a head |
| 7077 | * is set REQ_F_FAIL because of its failure or other req's |
| 7078 | * failure so that we can set the correct ret code for it. |
| 7079 | * init result here to avoid affecting the normal path. |
| 7080 | */ |
| 7081 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7082 | req_fail_link_node(link->head, -ECANCELED); |
| 7083 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7084 | /* |
| 7085 | * the current req is a normal req, we should return |
| 7086 | * error and thus break the submittion loop. |
| 7087 | */ |
| 7088 | io_req_complete_failed(req, ret); |
| 7089 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7090 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7091 | req_fail_link_node(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7092 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7093 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7094 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7095 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7096 | req->flags, true, |
| 7097 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7098 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7099 | /* |
| 7100 | * If we already have a head request, queue this one for async |
| 7101 | * submittal once the head completes. If we don't have a head but |
| 7102 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7103 | * submitted sync once the chain is complete. If none of those |
| 7104 | * conditions are true (normal request), then just queue it. |
| 7105 | */ |
| 7106 | if (link->head) { |
| 7107 | struct io_kiocb *head = link->head; |
| 7108 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7109 | if (!(req->flags & REQ_F_FAIL)) { |
| 7110 | ret = io_req_prep_async(req); |
| 7111 | if (unlikely(ret)) { |
| 7112 | req_fail_link_node(req, ret); |
| 7113 | if (!(head->flags & REQ_F_FAIL)) |
| 7114 | req_fail_link_node(head, -ECANCELED); |
| 7115 | } |
| 7116 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7117 | trace_io_uring_link(ctx, req, head); |
| 7118 | link->last->link = req; |
| 7119 | link->last = req; |
| 7120 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7121 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
| 7122 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7123 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7124 | link->head = NULL; |
| 7125 | req = head; |
| 7126 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 7127 | link->head = req; |
| 7128 | link->last = req; |
| 7129 | return 0; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7130 | } |
| 7131 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7132 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7133 | return 0; |
| 7134 | } |
| 7135 | |
| 7136 | /* |
| 7137 | * Batched submission is done, ensure local IO is flushed out. |
| 7138 | */ |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7139 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7140 | { |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7141 | struct io_submit_state *state = &ctx->submit_state; |
| 7142 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7143 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7144 | io_queue_sqe(state->link.head); |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7145 | /* flush only after queuing links as they can generate completions */ |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 7146 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7147 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7148 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7149 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7150 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7151 | /* |
| 7152 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7153 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7154 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7155 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7156 | { |
| 7157 | state->plug_started = false; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7158 | state->need_plug = max_ios > 2; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7159 | /* set only head, no need to init link_last in advance */ |
| 7160 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7161 | } |
| 7162 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7163 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7164 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7165 | struct io_rings *rings = ctx->rings; |
| 7166 | |
| 7167 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7168 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7169 | * since once we write the new head, the application could |
| 7170 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7171 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7172 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7173 | } |
| 7174 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7175 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7176 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7177 | * that is mapped by userspace. This means that care needs to be taken to |
| 7178 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7179 | * being a good citizen. If members of the sqe are validated and then later |
| 7180 | * used, it's important that those reads are done through READ_ONCE() to |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 7181 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7182 | */ |
| 7183 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7184 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7185 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7186 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7187 | |
| 7188 | /* |
| 7189 | * The cached sq head (or cq tail) serves two purposes: |
| 7190 | * |
| 7191 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7192 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7193 | * 2) allows the kernel side to track the head on its own, even |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7194 | * though the application is the one updating it. |
| 7195 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7196 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7197 | if (likely(head < ctx->sq_entries)) |
| 7198 | return &ctx->sq_sqes[head]; |
| 7199 | |
| 7200 | /* drop invalid entries */ |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7201 | ctx->cq_extra--; |
| 7202 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7203 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7204 | return NULL; |
| 7205 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7206 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7207 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7208 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7209 | { |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7210 | unsigned int entries = io_sqring_entries(ctx); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7211 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7212 | |
Pavel Begunkov | 51d48da | 2021-10-04 20:02:47 +0100 | [diff] [blame] | 7213 | if (unlikely(!entries)) |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7214 | return 0; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7215 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7216 | nr = min3(nr, ctx->sq_entries, entries); |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7217 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7218 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7219 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7220 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7221 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7222 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7223 | |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7224 | if (unlikely(!io_alloc_req_refill(ctx))) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7225 | if (!submitted) |
| 7226 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7227 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7228 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7229 | req = io_alloc_req(ctx); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7230 | sqe = io_get_sqe(ctx); |
| 7231 | if (unlikely(!sqe)) { |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 7232 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7233 | break; |
| 7234 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7235 | /* will complete beyond this point, count as submitted */ |
| 7236 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7237 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7238 | break; |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7239 | } while (submitted < nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7240 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7241 | if (unlikely(submitted != nr)) { |
| 7242 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7243 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7244 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7245 | current->io_uring->cached_refs += unused; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7246 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7247 | |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7248 | io_submit_state_end(ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7249 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7250 | io_commit_sqring(ctx); |
| 7251 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7252 | return submitted; |
| 7253 | } |
| 7254 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7255 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7256 | { |
| 7257 | return READ_ONCE(sqd->state); |
| 7258 | } |
| 7259 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7260 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7261 | { |
| 7262 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7263 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7264 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7265 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7266 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7270 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7271 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7272 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7273 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7274 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7275 | } |
| 7276 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7277 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7278 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7279 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7280 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7281 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7282 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7283 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7284 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7285 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7286 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7287 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7288 | const struct cred *creds = NULL; |
| 7289 | |
| 7290 | if (ctx->sq_creds != current_cred()) |
| 7291 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7292 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7293 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7294 | if (!wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 7295 | io_do_iopoll(ctx, true); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7296 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7297 | /* |
| 7298 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7299 | * but also it is relied upon by io_ring_exit_work() |
| 7300 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7301 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7302 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7303 | ret = io_submit_sqes(ctx, to_submit); |
| 7304 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7305 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7306 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7307 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7308 | if (creds) |
| 7309 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7310 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7311 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7312 | return ret; |
| 7313 | } |
| 7314 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7315 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7316 | { |
| 7317 | struct io_ring_ctx *ctx; |
| 7318 | unsigned sq_thread_idle = 0; |
| 7319 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7320 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7321 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7322 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7323 | } |
| 7324 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7325 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7326 | { |
| 7327 | bool did_sig = false; |
| 7328 | struct ksignal ksig; |
| 7329 | |
| 7330 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7331 | signal_pending(current)) { |
| 7332 | mutex_unlock(&sqd->lock); |
| 7333 | if (signal_pending(current)) |
| 7334 | did_sig = get_signal(&ksig); |
| 7335 | cond_resched(); |
| 7336 | mutex_lock(&sqd->lock); |
| 7337 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7338 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7339 | } |
| 7340 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7341 | static int io_sq_thread(void *data) |
| 7342 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7343 | struct io_sq_data *sqd = data; |
| 7344 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7345 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7346 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7347 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7348 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7349 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7350 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7351 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7352 | if (sqd->sq_cpu != -1) |
| 7353 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7354 | else |
| 7355 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7356 | current->flags |= PF_NO_SETAFFINITY; |
| 7357 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7358 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7359 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7360 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7361 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7362 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7363 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7364 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7365 | timeout = jiffies + sqd->sq_thread_idle; |
| 7366 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7367 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7368 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7369 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7370 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7371 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7372 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7373 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7374 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7375 | if (io_run_task_work()) |
| 7376 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7377 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7378 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7379 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7380 | if (sqt_spin) |
| 7381 | timeout = jiffies + sqd->sq_thread_idle; |
| 7382 | continue; |
| 7383 | } |
| 7384 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7385 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7386 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7387 | bool needs_sched = true; |
| 7388 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7389 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7390 | io_ring_set_wakeup_flag(ctx); |
| 7391 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7392 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7393 | !wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7394 | needs_sched = false; |
| 7395 | break; |
| 7396 | } |
| 7397 | if (io_sqring_entries(ctx)) { |
| 7398 | needs_sched = false; |
| 7399 | break; |
| 7400 | } |
| 7401 | } |
| 7402 | |
| 7403 | if (needs_sched) { |
| 7404 | mutex_unlock(&sqd->lock); |
| 7405 | schedule(); |
| 7406 | mutex_lock(&sqd->lock); |
| 7407 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7408 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7409 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7410 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7411 | |
| 7412 | finish_wait(&sqd->wait, &wait); |
| 7413 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7414 | } |
| 7415 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7416 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7417 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7418 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7419 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7420 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7421 | mutex_unlock(&sqd->lock); |
| 7422 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7423 | complete(&sqd->exited); |
| 7424 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7425 | } |
| 7426 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7427 | struct io_wait_queue { |
| 7428 | struct wait_queue_entry wq; |
| 7429 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7430 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7431 | unsigned nr_timeouts; |
| 7432 | }; |
| 7433 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7434 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7435 | { |
| 7436 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7437 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7438 | |
| 7439 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7440 | * 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] | 7441 | * started waiting. For timeouts, we always want to return to userspace, |
| 7442 | * regardless of event count. |
| 7443 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7444 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7445 | } |
| 7446 | |
| 7447 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7448 | int wake_flags, void *key) |
| 7449 | { |
| 7450 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7451 | wq); |
| 7452 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7453 | /* |
| 7454 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7455 | * the task, and the next invocation will do it. |
| 7456 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7457 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7458 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7459 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7460 | } |
| 7461 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7462 | static int io_run_task_work_sig(void) |
| 7463 | { |
| 7464 | if (io_run_task_work()) |
| 7465 | return 1; |
| 7466 | if (!signal_pending(current)) |
| 7467 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7468 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7469 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7470 | return -EINTR; |
| 7471 | } |
| 7472 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7473 | /* when returns >0, the caller should retry */ |
| 7474 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7475 | struct io_wait_queue *iowq, |
| 7476 | signed long *timeout) |
| 7477 | { |
| 7478 | int ret; |
| 7479 | |
| 7480 | /* make sure we run task_work before checking for signals */ |
| 7481 | ret = io_run_task_work_sig(); |
| 7482 | if (ret || io_should_wake(iowq)) |
| 7483 | return ret; |
| 7484 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7485 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7486 | return 1; |
| 7487 | |
| 7488 | *timeout = schedule_timeout(*timeout); |
| 7489 | return !*timeout ? -ETIME : 1; |
| 7490 | } |
| 7491 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7492 | /* |
| 7493 | * Wait until events become available, if we don't already have some. The |
| 7494 | * application must reap them itself, as they reside on the shared cq ring. |
| 7495 | */ |
| 7496 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7497 | const sigset_t __user *sig, size_t sigsz, |
| 7498 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7499 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7500 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7501 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7502 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7503 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7504 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7505 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7506 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7507 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7508 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7509 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7510 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7511 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7512 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7513 | if (uts) { |
| 7514 | struct timespec64 ts; |
| 7515 | |
| 7516 | if (get_timespec64(&ts, uts)) |
| 7517 | return -EFAULT; |
| 7518 | timeout = timespec64_to_jiffies(&ts); |
| 7519 | } |
| 7520 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7521 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7522 | #ifdef CONFIG_COMPAT |
| 7523 | if (in_compat_syscall()) |
| 7524 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7525 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7526 | else |
| 7527 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7528 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7529 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7530 | if (ret) |
| 7531 | return ret; |
| 7532 | } |
| 7533 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7534 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7535 | iowq.wq.private = current; |
| 7536 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7537 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7538 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7539 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7540 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7541 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7542 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7543 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7544 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7545 | ret = -EBUSY; |
| 7546 | break; |
| 7547 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7548 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7549 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7550 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7551 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7552 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7553 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7554 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7555 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7556 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7557 | 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] | 7558 | } |
| 7559 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7560 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7561 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7562 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7563 | |
| 7564 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7565 | kfree(table[i]); |
| 7566 | kfree(table); |
| 7567 | } |
| 7568 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7569 | static __cold void **io_alloc_page_table(size_t size) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7570 | { |
| 7571 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7572 | size_t init_size = size; |
| 7573 | void **table; |
| 7574 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7575 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7576 | if (!table) |
| 7577 | return NULL; |
| 7578 | |
| 7579 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7580 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7581 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7582 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7583 | if (!table[i]) { |
| 7584 | io_free_page_table(table, init_size); |
| 7585 | return NULL; |
| 7586 | } |
| 7587 | size -= this_size; |
| 7588 | } |
| 7589 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7590 | } |
| 7591 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7592 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7593 | { |
| 7594 | percpu_ref_exit(&ref_node->refs); |
| 7595 | kfree(ref_node); |
| 7596 | } |
| 7597 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7598 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7599 | { |
| 7600 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7601 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7602 | unsigned long flags; |
| 7603 | bool first_add = false; |
| 7604 | |
| 7605 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7606 | node->done = true; |
| 7607 | |
| 7608 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7609 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7610 | struct io_rsrc_node, node); |
| 7611 | /* recycle ref nodes in order */ |
| 7612 | if (!node->done) |
| 7613 | break; |
| 7614 | list_del(&node->node); |
| 7615 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7616 | } |
| 7617 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7618 | |
| 7619 | if (first_add) |
| 7620 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
| 7621 | } |
| 7622 | |
| 7623 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
| 7624 | { |
| 7625 | struct io_rsrc_node *ref_node; |
| 7626 | |
| 7627 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7628 | if (!ref_node) |
| 7629 | return NULL; |
| 7630 | |
| 7631 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 7632 | 0, GFP_KERNEL)) { |
| 7633 | kfree(ref_node); |
| 7634 | return NULL; |
| 7635 | } |
| 7636 | INIT_LIST_HEAD(&ref_node->node); |
| 7637 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 7638 | ref_node->done = false; |
| 7639 | return ref_node; |
| 7640 | } |
| 7641 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7642 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7643 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7644 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7645 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7646 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7647 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7648 | if (data_to_kill) { |
| 7649 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7650 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7651 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7652 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7653 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7654 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7655 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7656 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7657 | percpu_ref_kill(&rsrc_node->refs); |
| 7658 | ctx->rsrc_node = NULL; |
| 7659 | } |
| 7660 | |
| 7661 | if (!ctx->rsrc_node) { |
| 7662 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7663 | ctx->rsrc_backup_node = NULL; |
| 7664 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7667 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7668 | { |
| 7669 | if (ctx->rsrc_backup_node) |
| 7670 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7671 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7672 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7673 | } |
| 7674 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7675 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
| 7676 | struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7677 | { |
| 7678 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7679 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7680 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7681 | if (data->quiesce) |
| 7682 | return -ENXIO; |
| 7683 | |
| 7684 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7685 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7686 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7687 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7688 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7689 | io_rsrc_node_switch(ctx, data); |
| 7690 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7691 | /* kill initial ref, already quiesced if zero */ |
| 7692 | if (atomic_dec_and_test(&data->refs)) |
| 7693 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7694 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7695 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7696 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7697 | if (!ret) { |
| 7698 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7699 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7700 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7701 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7702 | atomic_inc(&data->refs); |
| 7703 | /* wait for all works potentially completing data->done */ |
| 7704 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7705 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7706 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7707 | ret = io_run_task_work_sig(); |
| 7708 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7709 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7710 | data->quiesce = false; |
| 7711 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7712 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7715 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 7716 | { |
| 7717 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 7718 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 7719 | |
| 7720 | return &data->tags[table_idx][off]; |
| 7721 | } |
| 7722 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7723 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7724 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7725 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 7726 | |
| 7727 | if (data->tags) |
| 7728 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7729 | kfree(data); |
| 7730 | } |
| 7731 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7732 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 7733 | u64 __user *utags, unsigned nr, |
| 7734 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7735 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7736 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7737 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7738 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7739 | |
| 7740 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7741 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7742 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7743 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7744 | if (!data->tags) { |
| 7745 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7746 | return -ENOMEM; |
| 7747 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7748 | |
| 7749 | data->nr = nr; |
| 7750 | data->ctx = ctx; |
| 7751 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7752 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7753 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7754 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 7755 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 7756 | |
| 7757 | if (copy_from_user(tag_slot, &utags[i], |
| 7758 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7759 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7760 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7761 | } |
| 7762 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7763 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7764 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7765 | *pdata = data; |
| 7766 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7767 | fail: |
| 7768 | io_rsrc_data_free(data); |
| 7769 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7772 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 7773 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7774 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 7775 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7776 | return !!table->files; |
| 7777 | } |
| 7778 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7779 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7780 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7781 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7782 | table->files = NULL; |
| 7783 | } |
| 7784 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7785 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7786 | { |
| 7787 | #if defined(CONFIG_UNIX) |
| 7788 | if (ctx->ring_sock) { |
| 7789 | struct sock *sock = ctx->ring_sock->sk; |
| 7790 | struct sk_buff *skb; |
| 7791 | |
| 7792 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7793 | kfree_skb(skb); |
| 7794 | } |
| 7795 | #else |
| 7796 | int i; |
| 7797 | |
| 7798 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7799 | struct file *file; |
| 7800 | |
| 7801 | file = io_file_from_index(ctx, i); |
| 7802 | if (file) |
| 7803 | fput(file); |
| 7804 | } |
| 7805 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7806 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7807 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7808 | ctx->file_data = NULL; |
| 7809 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7812 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7813 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7814 | int ret; |
| 7815 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7816 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7817 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7818 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7819 | if (!ret) |
| 7820 | __io_sqe_files_unregister(ctx); |
| 7821 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7822 | } |
| 7823 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7824 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7825 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7826 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7827 | WARN_ON_ONCE(sqd->thread == current); |
| 7828 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7829 | /* |
| 7830 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7831 | * other threads incrementing park_pending and setting the bit. |
| 7832 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7833 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7834 | if (atomic_dec_return(&sqd->park_pending)) |
| 7835 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7836 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7837 | } |
| 7838 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7839 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7840 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7841 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7842 | WARN_ON_ONCE(sqd->thread == current); |
| 7843 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7844 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7845 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7846 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7847 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7848 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7849 | } |
| 7850 | |
| 7851 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7852 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7853 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7854 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7855 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7856 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7857 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7858 | if (sqd->thread) |
| 7859 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7860 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7861 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7862 | } |
| 7863 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7864 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7865 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7866 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7867 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 7868 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7869 | io_sq_thread_stop(sqd); |
| 7870 | kfree(sqd); |
| 7871 | } |
| 7872 | } |
| 7873 | |
| 7874 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7875 | { |
| 7876 | struct io_sq_data *sqd = ctx->sq_data; |
| 7877 | |
| 7878 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7879 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7880 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7881 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7882 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7883 | |
| 7884 | io_put_sq_data(sqd); |
| 7885 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7886 | } |
| 7887 | } |
| 7888 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7889 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7890 | { |
| 7891 | struct io_ring_ctx *ctx_attach; |
| 7892 | struct io_sq_data *sqd; |
| 7893 | struct fd f; |
| 7894 | |
| 7895 | f = fdget(p->wq_fd); |
| 7896 | if (!f.file) |
| 7897 | return ERR_PTR(-ENXIO); |
| 7898 | if (f.file->f_op != &io_uring_fops) { |
| 7899 | fdput(f); |
| 7900 | return ERR_PTR(-EINVAL); |
| 7901 | } |
| 7902 | |
| 7903 | ctx_attach = f.file->private_data; |
| 7904 | sqd = ctx_attach->sq_data; |
| 7905 | if (!sqd) { |
| 7906 | fdput(f); |
| 7907 | return ERR_PTR(-EINVAL); |
| 7908 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7909 | if (sqd->task_tgid != current->tgid) { |
| 7910 | fdput(f); |
| 7911 | return ERR_PTR(-EPERM); |
| 7912 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7913 | |
| 7914 | refcount_inc(&sqd->refs); |
| 7915 | fdput(f); |
| 7916 | return sqd; |
| 7917 | } |
| 7918 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7919 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 7920 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7921 | { |
| 7922 | struct io_sq_data *sqd; |
| 7923 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7924 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7925 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 7926 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7927 | if (!IS_ERR(sqd)) { |
| 7928 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7929 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7930 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7931 | /* fall through for EPERM case, setup new sqd/task */ |
| 7932 | if (PTR_ERR(sqd) != -EPERM) |
| 7933 | return sqd; |
| 7934 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7935 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7936 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7937 | if (!sqd) |
| 7938 | return ERR_PTR(-ENOMEM); |
| 7939 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7940 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7941 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7942 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7943 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7944 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7945 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7946 | return sqd; |
| 7947 | } |
| 7948 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7949 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7950 | /* |
| 7951 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7952 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7953 | * loops in the file referencing. |
| 7954 | */ |
| 7955 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7956 | { |
| 7957 | struct sock *sk = ctx->ring_sock->sk; |
| 7958 | struct scm_fp_list *fpl; |
| 7959 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7960 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7961 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7962 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7963 | if (!fpl) |
| 7964 | return -ENOMEM; |
| 7965 | |
| 7966 | skb = alloc_skb(0, GFP_KERNEL); |
| 7967 | if (!skb) { |
| 7968 | kfree(fpl); |
| 7969 | return -ENOMEM; |
| 7970 | } |
| 7971 | |
| 7972 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7973 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7974 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7975 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7976 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7977 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7978 | |
| 7979 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7980 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7981 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7982 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7983 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7984 | } |
| 7985 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7986 | if (nr_files) { |
| 7987 | fpl->max = SCM_MAX_FD; |
| 7988 | fpl->count = nr_files; |
| 7989 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7990 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7991 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7992 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7993 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7994 | for (i = 0; i < nr_files; i++) |
| 7995 | fput(fpl->fp[i]); |
| 7996 | } else { |
| 7997 | kfree_skb(skb); |
| 7998 | kfree(fpl); |
| 7999 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8000 | |
| 8001 | return 0; |
| 8002 | } |
| 8003 | |
| 8004 | /* |
| 8005 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8006 | * causes regular reference counting to break down. We rely on the UNIX |
| 8007 | * garbage collection to take care of this problem for us. |
| 8008 | */ |
| 8009 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8010 | { |
| 8011 | unsigned left, total; |
| 8012 | int ret = 0; |
| 8013 | |
| 8014 | total = 0; |
| 8015 | left = ctx->nr_user_files; |
| 8016 | while (left) { |
| 8017 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8018 | |
| 8019 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8020 | if (ret) |
| 8021 | break; |
| 8022 | left -= this_files; |
| 8023 | total += this_files; |
| 8024 | } |
| 8025 | |
| 8026 | if (!ret) |
| 8027 | return 0; |
| 8028 | |
| 8029 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8030 | struct file *file = io_file_from_index(ctx, total); |
| 8031 | |
| 8032 | if (file) |
| 8033 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8034 | total++; |
| 8035 | } |
| 8036 | |
| 8037 | return ret; |
| 8038 | } |
| 8039 | #else |
| 8040 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8041 | { |
| 8042 | return 0; |
| 8043 | } |
| 8044 | #endif |
| 8045 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8046 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8047 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8048 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8049 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8050 | struct sock *sock = ctx->ring_sock->sk; |
| 8051 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8052 | struct sk_buff *skb; |
| 8053 | int i; |
| 8054 | |
| 8055 | __skb_queue_head_init(&list); |
| 8056 | |
| 8057 | /* |
| 8058 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8059 | * remove this entry and rearrange the file array. |
| 8060 | */ |
| 8061 | skb = skb_dequeue(head); |
| 8062 | while (skb) { |
| 8063 | struct scm_fp_list *fp; |
| 8064 | |
| 8065 | fp = UNIXCB(skb).fp; |
| 8066 | for (i = 0; i < fp->count; i++) { |
| 8067 | int left; |
| 8068 | |
| 8069 | if (fp->fp[i] != file) |
| 8070 | continue; |
| 8071 | |
| 8072 | unix_notinflight(fp->user, fp->fp[i]); |
| 8073 | left = fp->count - 1 - i; |
| 8074 | if (left) { |
| 8075 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8076 | left * sizeof(struct file *)); |
| 8077 | } |
| 8078 | fp->count--; |
| 8079 | if (!fp->count) { |
| 8080 | kfree_skb(skb); |
| 8081 | skb = NULL; |
| 8082 | } else { |
| 8083 | __skb_queue_tail(&list, skb); |
| 8084 | } |
| 8085 | fput(file); |
| 8086 | file = NULL; |
| 8087 | break; |
| 8088 | } |
| 8089 | |
| 8090 | if (!file) |
| 8091 | break; |
| 8092 | |
| 8093 | __skb_queue_tail(&list, skb); |
| 8094 | |
| 8095 | skb = skb_dequeue(head); |
| 8096 | } |
| 8097 | |
| 8098 | if (skb_peek(&list)) { |
| 8099 | spin_lock_irq(&head->lock); |
| 8100 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8101 | __skb_queue_tail(head, skb); |
| 8102 | spin_unlock_irq(&head->lock); |
| 8103 | } |
| 8104 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8105 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8106 | #endif |
| 8107 | } |
| 8108 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8109 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8110 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8111 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8112 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8113 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8114 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8115 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8116 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8117 | |
| 8118 | if (prsrc->tag) { |
| 8119 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8120 | |
| 8121 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8122 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8123 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | 2840f71 | 2021-04-27 16:13:51 +0100 | [diff] [blame] | 8124 | ctx->cq_extra++; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8125 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8126 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8127 | io_cqring_ev_posted(ctx); |
| 8128 | io_ring_submit_unlock(ctx, lock_ring); |
| 8129 | } |
| 8130 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8131 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8132 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8133 | } |
| 8134 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8135 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8136 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8137 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8138 | } |
| 8139 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8140 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8141 | { |
| 8142 | struct io_ring_ctx *ctx; |
| 8143 | struct llist_node *node; |
| 8144 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8145 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8146 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8147 | |
| 8148 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8149 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8150 | struct llist_node *next = node->next; |
| 8151 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8152 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8153 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8154 | node = next; |
| 8155 | } |
| 8156 | } |
| 8157 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8158 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8159 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8160 | { |
| 8161 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8162 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8163 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8164 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8165 | |
| 8166 | if (ctx->file_data) |
| 8167 | return -EBUSY; |
| 8168 | if (!nr_args) |
| 8169 | return -EINVAL; |
| 8170 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8171 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8172 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8173 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8174 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8175 | if (ret) |
| 8176 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8177 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8178 | &ctx->file_data); |
| 8179 | if (ret) |
| 8180 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8181 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8182 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8183 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8184 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8185 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8186 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8187 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8188 | ret = -EFAULT; |
| 8189 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8190 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8191 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8192 | if (fd == -1) { |
| 8193 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8194 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8195 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8196 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8197 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8198 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8199 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8200 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8201 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8202 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8203 | |
| 8204 | /* |
| 8205 | * Don't allow io_uring instances to be registered. If UNIX |
| 8206 | * isn't enabled, then this causes a reference cycle and this |
| 8207 | * instance can never get freed. If UNIX is enabled we'll |
| 8208 | * handle it just fine, but there's still no point in allowing |
| 8209 | * a ring fd as it doesn't support regular read/write anyway. |
| 8210 | */ |
| 8211 | if (file->f_op == &io_uring_fops) { |
| 8212 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8213 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8214 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8215 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8216 | } |
| 8217 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8218 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8219 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8220 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8221 | return ret; |
| 8222 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8223 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8224 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8225 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8226 | out_fput: |
| 8227 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8228 | file = io_file_from_index(ctx, i); |
| 8229 | if (file) |
| 8230 | fput(file); |
| 8231 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8232 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8233 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8234 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8235 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8236 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8237 | return ret; |
| 8238 | } |
| 8239 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8240 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8241 | int index) |
| 8242 | { |
| 8243 | #if defined(CONFIG_UNIX) |
| 8244 | struct sock *sock = ctx->ring_sock->sk; |
| 8245 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8246 | struct sk_buff *skb; |
| 8247 | |
| 8248 | /* |
| 8249 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8250 | * file set. If there's no room, fall back to allocating a new skb |
| 8251 | * and filling it in. |
| 8252 | */ |
| 8253 | spin_lock_irq(&head->lock); |
| 8254 | skb = skb_peek(head); |
| 8255 | if (skb) { |
| 8256 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8257 | |
| 8258 | if (fpl->count < SCM_MAX_FD) { |
| 8259 | __skb_unlink(skb, head); |
| 8260 | spin_unlock_irq(&head->lock); |
| 8261 | fpl->fp[fpl->count] = get_file(file); |
| 8262 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8263 | fpl->count++; |
| 8264 | spin_lock_irq(&head->lock); |
| 8265 | __skb_queue_head(head, skb); |
| 8266 | } else { |
| 8267 | skb = NULL; |
| 8268 | } |
| 8269 | } |
| 8270 | spin_unlock_irq(&head->lock); |
| 8271 | |
| 8272 | if (skb) { |
| 8273 | fput(file); |
| 8274 | return 0; |
| 8275 | } |
| 8276 | |
| 8277 | return __io_sqe_files_scm(ctx, 1, index); |
| 8278 | #else |
| 8279 | return 0; |
| 8280 | #endif |
| 8281 | } |
| 8282 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8283 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8284 | struct io_rsrc_node *node, void *rsrc) |
| 8285 | { |
| 8286 | struct io_rsrc_put *prsrc; |
| 8287 | |
| 8288 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8289 | if (!prsrc) |
| 8290 | return -ENOMEM; |
| 8291 | |
| 8292 | prsrc->tag = *io_get_tag_slot(data, idx); |
| 8293 | prsrc->rsrc = rsrc; |
| 8294 | list_add(&prsrc->list, &node->rsrc_list); |
| 8295 | return 0; |
| 8296 | } |
| 8297 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8298 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8299 | unsigned int issue_flags, u32 slot_index) |
| 8300 | { |
| 8301 | struct io_ring_ctx *ctx = req->ctx; |
| 8302 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8303 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8304 | struct io_fixed_file *file_slot; |
| 8305 | int ret = -EBADF; |
| 8306 | |
| 8307 | io_ring_submit_lock(ctx, !force_nonblock); |
| 8308 | if (file->f_op == &io_uring_fops) |
| 8309 | goto err; |
| 8310 | ret = -ENXIO; |
| 8311 | if (!ctx->file_data) |
| 8312 | goto err; |
| 8313 | ret = -EINVAL; |
| 8314 | if (slot_index >= ctx->nr_user_files) |
| 8315 | goto err; |
| 8316 | |
| 8317 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8318 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8319 | |
| 8320 | if (file_slot->file_ptr) { |
| 8321 | struct file *old_file; |
| 8322 | |
| 8323 | ret = io_rsrc_node_switch_start(ctx); |
| 8324 | if (ret) |
| 8325 | goto err; |
| 8326 | |
| 8327 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8328 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8329 | ctx->rsrc_node, old_file); |
| 8330 | if (ret) |
| 8331 | goto err; |
| 8332 | file_slot->file_ptr = 0; |
| 8333 | needs_switch = true; |
| 8334 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8335 | |
| 8336 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8337 | io_fixed_file_set(file_slot, file); |
| 8338 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8339 | if (ret) { |
| 8340 | file_slot->file_ptr = 0; |
| 8341 | goto err; |
| 8342 | } |
| 8343 | |
| 8344 | ret = 0; |
| 8345 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8346 | if (needs_switch) |
| 8347 | io_rsrc_node_switch(ctx, ctx->file_data); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8348 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 8349 | if (ret) |
| 8350 | fput(file); |
| 8351 | return ret; |
| 8352 | } |
| 8353 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8354 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8355 | { |
| 8356 | unsigned int offset = req->close.file_slot - 1; |
| 8357 | struct io_ring_ctx *ctx = req->ctx; |
| 8358 | struct io_fixed_file *file_slot; |
| 8359 | struct file *file; |
| 8360 | int ret, i; |
| 8361 | |
| 8362 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8363 | ret = -ENXIO; |
| 8364 | if (unlikely(!ctx->file_data)) |
| 8365 | goto out; |
| 8366 | ret = -EINVAL; |
| 8367 | if (offset >= ctx->nr_user_files) |
| 8368 | goto out; |
| 8369 | ret = io_rsrc_node_switch_start(ctx); |
| 8370 | if (ret) |
| 8371 | goto out; |
| 8372 | |
| 8373 | i = array_index_nospec(offset, ctx->nr_user_files); |
| 8374 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
| 8375 | ret = -EBADF; |
| 8376 | if (!file_slot->file_ptr) |
| 8377 | goto out; |
| 8378 | |
| 8379 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8380 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8381 | if (ret) |
| 8382 | goto out; |
| 8383 | |
| 8384 | file_slot->file_ptr = 0; |
| 8385 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8386 | ret = 0; |
| 8387 | out: |
| 8388 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8389 | return ret; |
| 8390 | } |
| 8391 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8392 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8393 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8394 | unsigned nr_args) |
| 8395 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8396 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8397 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8398 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8399 | struct io_fixed_file *file_slot; |
| 8400 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8401 | int fd, i, err = 0; |
| 8402 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8403 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8404 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8405 | if (!ctx->file_data) |
| 8406 | return -ENXIO; |
| 8407 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8408 | return -EINVAL; |
| 8409 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8410 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8411 | u64 tag = 0; |
| 8412 | |
| 8413 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8414 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8415 | err = -EFAULT; |
| 8416 | break; |
| 8417 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8418 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8419 | err = -EINVAL; |
| 8420 | break; |
| 8421 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8422 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8423 | continue; |
| 8424 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8425 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8426 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8427 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8428 | if (file_slot->file_ptr) { |
| 8429 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8430 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 8431 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8432 | if (err) |
| 8433 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8434 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8435 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8436 | } |
| 8437 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8438 | file = fget(fd); |
| 8439 | if (!file) { |
| 8440 | err = -EBADF; |
| 8441 | break; |
| 8442 | } |
| 8443 | /* |
| 8444 | * Don't allow io_uring instances to be registered. If |
| 8445 | * UNIX isn't enabled, then this causes a reference |
| 8446 | * cycle and this instance can never get freed. If UNIX |
| 8447 | * is enabled we'll handle it just fine, but there's |
| 8448 | * still no point in allowing a ring fd as it doesn't |
| 8449 | * support regular read/write anyway. |
| 8450 | */ |
| 8451 | if (file->f_op == &io_uring_fops) { |
| 8452 | fput(file); |
| 8453 | err = -EBADF; |
| 8454 | break; |
| 8455 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8456 | *io_get_tag_slot(data, up->offset + done) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8457 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8458 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8459 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8460 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8461 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8462 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8463 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8464 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8465 | } |
| 8466 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8467 | if (needs_switch) |
| 8468 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8469 | return done ? done : err; |
| 8470 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8471 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8472 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8473 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8474 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8475 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8476 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8477 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8478 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8479 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8480 | hash = ctx->hash_map; |
| 8481 | if (!hash) { |
| 8482 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8483 | if (!hash) { |
| 8484 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8485 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8486 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8487 | refcount_set(&hash->refs, 1); |
| 8488 | init_waitqueue_head(&hash->wait); |
| 8489 | ctx->hash_map = hash; |
| 8490 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8491 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8492 | |
| 8493 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8494 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8495 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8496 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8497 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8498 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8499 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8500 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8501 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8502 | } |
| 8503 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8504 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
| 8505 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8506 | { |
| 8507 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8508 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8509 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8510 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8511 | if (unlikely(!tctx)) |
| 8512 | return -ENOMEM; |
| 8513 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8514 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8515 | if (unlikely(ret)) { |
| 8516 | kfree(tctx); |
| 8517 | return ret; |
| 8518 | } |
| 8519 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8520 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8521 | if (IS_ERR(tctx->io_wq)) { |
| 8522 | ret = PTR_ERR(tctx->io_wq); |
| 8523 | percpu_counter_destroy(&tctx->inflight); |
| 8524 | kfree(tctx); |
| 8525 | return ret; |
| 8526 | } |
| 8527 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8528 | xa_init(&tctx->xa); |
| 8529 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8530 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8531 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8532 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8533 | spin_lock_init(&tctx->task_lock); |
| 8534 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8535 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8536 | return 0; |
| 8537 | } |
| 8538 | |
| 8539 | void __io_uring_free(struct task_struct *tsk) |
| 8540 | { |
| 8541 | struct io_uring_task *tctx = tsk->io_uring; |
| 8542 | |
| 8543 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8544 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8545 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8546 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8547 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8548 | kfree(tctx); |
| 8549 | tsk->io_uring = NULL; |
| 8550 | } |
| 8551 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8552 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8553 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8554 | { |
| 8555 | int ret; |
| 8556 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8557 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8558 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8559 | IORING_SETUP_ATTACH_WQ) { |
| 8560 | struct fd f; |
| 8561 | |
| 8562 | f = fdget(p->wq_fd); |
| 8563 | if (!f.file) |
| 8564 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8565 | if (f.file->f_op != &io_uring_fops) { |
| 8566 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8567 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8568 | } |
| 8569 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8570 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8571 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8572 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8573 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8574 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8575 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8576 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8577 | if (IS_ERR(sqd)) { |
| 8578 | ret = PTR_ERR(sqd); |
| 8579 | goto err; |
| 8580 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8581 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8582 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8583 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8584 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8585 | if (!ctx->sq_thread_idle) |
| 8586 | ctx->sq_thread_idle = HZ; |
| 8587 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8588 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8589 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8590 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8591 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8592 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8593 | io_sq_thread_unpark(sqd); |
| 8594 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8595 | if (ret < 0) |
| 8596 | goto err; |
| 8597 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8598 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8599 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8600 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8601 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8602 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8603 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8604 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8605 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8606 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8607 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8608 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8609 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8610 | |
| 8611 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8612 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8613 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 8614 | if (IS_ERR(tsk)) { |
| 8615 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8616 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8617 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8618 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8619 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8620 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8621 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8622 | if (ret) |
| 8623 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8624 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8625 | /* Can't have SQ_AFF without SQPOLL */ |
| 8626 | ret = -EINVAL; |
| 8627 | goto err; |
| 8628 | } |
| 8629 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8630 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8631 | err_sqpoll: |
| 8632 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8633 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8634 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8635 | return ret; |
| 8636 | } |
| 8637 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8638 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8639 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8640 | { |
| 8641 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8642 | } |
| 8643 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8644 | static inline int __io_account_mem(struct user_struct *user, |
| 8645 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8646 | { |
| 8647 | unsigned long page_limit, cur_pages, new_pages; |
| 8648 | |
| 8649 | /* Don't allow more pages than we can safely lock */ |
| 8650 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8651 | |
| 8652 | do { |
| 8653 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8654 | new_pages = cur_pages + nr_pages; |
| 8655 | if (new_pages > page_limit) |
| 8656 | return -ENOMEM; |
| 8657 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8658 | new_pages) != cur_pages); |
| 8659 | |
| 8660 | return 0; |
| 8661 | } |
| 8662 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8663 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8664 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8665 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8666 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8667 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8668 | if (ctx->mm_account) |
| 8669 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8670 | } |
| 8671 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8672 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8673 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8674 | int ret; |
| 8675 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8676 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8677 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8678 | if (ret) |
| 8679 | return ret; |
| 8680 | } |
| 8681 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8682 | if (ctx->mm_account) |
| 8683 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8684 | |
| 8685 | return 0; |
| 8686 | } |
| 8687 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8688 | static void io_mem_free(void *ptr) |
| 8689 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8690 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8691 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8692 | if (!ptr) |
| 8693 | return; |
| 8694 | |
| 8695 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8696 | if (put_page_testzero(page)) |
| 8697 | free_compound_page(page); |
| 8698 | } |
| 8699 | |
| 8700 | static void *io_mem_alloc(size_t size) |
| 8701 | { |
| 8702 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8703 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8704 | |
| 8705 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8706 | } |
| 8707 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8708 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8709 | size_t *sq_offset) |
| 8710 | { |
| 8711 | struct io_rings *rings; |
| 8712 | size_t off, sq_array_size; |
| 8713 | |
| 8714 | off = struct_size(rings, cqes, cq_entries); |
| 8715 | if (off == SIZE_MAX) |
| 8716 | return SIZE_MAX; |
| 8717 | |
| 8718 | #ifdef CONFIG_SMP |
| 8719 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8720 | if (off == 0) |
| 8721 | return SIZE_MAX; |
| 8722 | #endif |
| 8723 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8724 | if (sq_offset) |
| 8725 | *sq_offset = off; |
| 8726 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8727 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8728 | if (sq_array_size == SIZE_MAX) |
| 8729 | return SIZE_MAX; |
| 8730 | |
| 8731 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8732 | return SIZE_MAX; |
| 8733 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8734 | return off; |
| 8735 | } |
| 8736 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8737 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8738 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8739 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8740 | unsigned int i; |
| 8741 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8742 | if (imu != ctx->dummy_ubuf) { |
| 8743 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8744 | unpin_user_page(imu->bvec[i].bv_page); |
| 8745 | if (imu->acct_pages) |
| 8746 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8747 | kvfree(imu); |
| 8748 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8749 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8750 | } |
| 8751 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8752 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 8753 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 8754 | io_buffer_unmap(ctx, &prsrc->buf); |
| 8755 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8759 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8760 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8761 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8762 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8763 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8764 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 8765 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8766 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8767 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8768 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8769 | } |
| 8770 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8771 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 8772 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8773 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8774 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8775 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8776 | return -ENXIO; |
| 8777 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8778 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
| 8779 | if (!ret) |
| 8780 | __io_sqe_buffers_unregister(ctx); |
| 8781 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8782 | } |
| 8783 | |
| 8784 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8785 | void __user *arg, unsigned index) |
| 8786 | { |
| 8787 | struct iovec __user *src; |
| 8788 | |
| 8789 | #ifdef CONFIG_COMPAT |
| 8790 | if (ctx->compat) { |
| 8791 | struct compat_iovec __user *ciovs; |
| 8792 | struct compat_iovec ciov; |
| 8793 | |
| 8794 | ciovs = (struct compat_iovec __user *) arg; |
| 8795 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8796 | return -EFAULT; |
| 8797 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8798 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8799 | dst->iov_len = ciov.iov_len; |
| 8800 | return 0; |
| 8801 | } |
| 8802 | #endif |
| 8803 | src = (struct iovec __user *) arg; |
| 8804 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8805 | return -EFAULT; |
| 8806 | return 0; |
| 8807 | } |
| 8808 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8809 | /* |
| 8810 | * Not super efficient, but this is just a registration time. And we do cache |
| 8811 | * the last compound head, so generally we'll only do a full search if we don't |
| 8812 | * match that one. |
| 8813 | * |
| 8814 | * We check if the given compound head page has already been accounted, to |
| 8815 | * avoid double accounting it. This allows us to account the full size of the |
| 8816 | * page, not just the constituent pages of a huge page. |
| 8817 | */ |
| 8818 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8819 | int nr_pages, struct page *hpage) |
| 8820 | { |
| 8821 | int i, j; |
| 8822 | |
| 8823 | /* check current page array */ |
| 8824 | for (i = 0; i < nr_pages; i++) { |
| 8825 | if (!PageCompound(pages[i])) |
| 8826 | continue; |
| 8827 | if (compound_head(pages[i]) == hpage) |
| 8828 | return true; |
| 8829 | } |
| 8830 | |
| 8831 | /* check previously registered pages */ |
| 8832 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8833 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8834 | |
| 8835 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8836 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8837 | continue; |
| 8838 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8839 | return true; |
| 8840 | } |
| 8841 | } |
| 8842 | |
| 8843 | return false; |
| 8844 | } |
| 8845 | |
| 8846 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8847 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8848 | struct page **last_hpage) |
| 8849 | { |
| 8850 | int i, ret; |
| 8851 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 8852 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8853 | for (i = 0; i < nr_pages; i++) { |
| 8854 | if (!PageCompound(pages[i])) { |
| 8855 | imu->acct_pages++; |
| 8856 | } else { |
| 8857 | struct page *hpage; |
| 8858 | |
| 8859 | hpage = compound_head(pages[i]); |
| 8860 | if (hpage == *last_hpage) |
| 8861 | continue; |
| 8862 | *last_hpage = hpage; |
| 8863 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8864 | continue; |
| 8865 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8866 | } |
| 8867 | } |
| 8868 | |
| 8869 | if (!imu->acct_pages) |
| 8870 | return 0; |
| 8871 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8872 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8873 | if (ret) |
| 8874 | imu->acct_pages = 0; |
| 8875 | return ret; |
| 8876 | } |
| 8877 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8878 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8879 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8880 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8881 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8882 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8883 | struct vm_area_struct **vmas = NULL; |
| 8884 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8885 | unsigned long off, start, end, ubuf; |
| 8886 | size_t size; |
| 8887 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8888 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8889 | if (!iov->iov_base) { |
| 8890 | *pimu = ctx->dummy_ubuf; |
| 8891 | return 0; |
| 8892 | } |
| 8893 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8894 | ubuf = (unsigned long) iov->iov_base; |
| 8895 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8896 | start = ubuf >> PAGE_SHIFT; |
| 8897 | nr_pages = end - start; |
| 8898 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8899 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8900 | ret = -ENOMEM; |
| 8901 | |
| 8902 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8903 | if (!pages) |
| 8904 | goto done; |
| 8905 | |
| 8906 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8907 | GFP_KERNEL); |
| 8908 | if (!vmas) |
| 8909 | goto done; |
| 8910 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8911 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 8912 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8913 | goto done; |
| 8914 | |
| 8915 | ret = 0; |
| 8916 | mmap_read_lock(current->mm); |
| 8917 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8918 | pages, vmas); |
| 8919 | if (pret == nr_pages) { |
| 8920 | /* don't support file backed memory */ |
| 8921 | for (i = 0; i < nr_pages; i++) { |
| 8922 | struct vm_area_struct *vma = vmas[i]; |
| 8923 | |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 8924 | if (vma_is_shmem(vma)) |
| 8925 | continue; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8926 | if (vma->vm_file && |
| 8927 | !is_file_hugepages(vma->vm_file)) { |
| 8928 | ret = -EOPNOTSUPP; |
| 8929 | break; |
| 8930 | } |
| 8931 | } |
| 8932 | } else { |
| 8933 | ret = pret < 0 ? pret : -EFAULT; |
| 8934 | } |
| 8935 | mmap_read_unlock(current->mm); |
| 8936 | if (ret) { |
| 8937 | /* |
| 8938 | * if we did partial map, or found file backed vmas, |
| 8939 | * release any pages we did get |
| 8940 | */ |
| 8941 | if (pret > 0) |
| 8942 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8943 | goto done; |
| 8944 | } |
| 8945 | |
| 8946 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8947 | if (ret) { |
| 8948 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8949 | goto done; |
| 8950 | } |
| 8951 | |
| 8952 | off = ubuf & ~PAGE_MASK; |
| 8953 | size = iov->iov_len; |
| 8954 | for (i = 0; i < nr_pages; i++) { |
| 8955 | size_t vec_len; |
| 8956 | |
| 8957 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8958 | imu->bvec[i].bv_page = pages[i]; |
| 8959 | imu->bvec[i].bv_len = vec_len; |
| 8960 | imu->bvec[i].bv_offset = off; |
| 8961 | off = 0; |
| 8962 | size -= vec_len; |
| 8963 | } |
| 8964 | /* store original address for later verification */ |
| 8965 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 8966 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8967 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8968 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8969 | ret = 0; |
| 8970 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8971 | if (ret) |
| 8972 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8973 | kvfree(pages); |
| 8974 | kvfree(vmas); |
| 8975 | return ret; |
| 8976 | } |
| 8977 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8978 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8979 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8980 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 8981 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8982 | } |
| 8983 | |
| 8984 | static int io_buffer_validate(struct iovec *iov) |
| 8985 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 8986 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 8987 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8988 | /* |
| 8989 | * Don't impose further limits on the size and buffer |
| 8990 | * constraints here, we'll -EINVAL later when IO is |
| 8991 | * submitted if they are wrong. |
| 8992 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8993 | if (!iov->iov_base) |
| 8994 | return iov->iov_len ? -EFAULT : 0; |
| 8995 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8996 | return -EFAULT; |
| 8997 | |
| 8998 | /* arbitrary limit, but we need something */ |
| 8999 | if (iov->iov_len > SZ_1G) |
| 9000 | return -EFAULT; |
| 9001 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9002 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9003 | return -EOVERFLOW; |
| 9004 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9005 | return 0; |
| 9006 | } |
| 9007 | |
| 9008 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9009 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9010 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9011 | struct page *last_hpage = NULL; |
| 9012 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9013 | int i, ret; |
| 9014 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9015 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9016 | if (ctx->user_bufs) |
| 9017 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9018 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9019 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9020 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9021 | if (ret) |
| 9022 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9023 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9024 | if (ret) |
| 9025 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9026 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9027 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9028 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9029 | return ret; |
| 9030 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9031 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9032 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9033 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9034 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9035 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9036 | ret = io_buffer_validate(&iov); |
| 9037 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9038 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9039 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9040 | ret = -EINVAL; |
| 9041 | break; |
| 9042 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9043 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9044 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9045 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9046 | if (ret) |
| 9047 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9048 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9049 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9050 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9051 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9052 | ctx->buf_data = data; |
| 9053 | if (ret) |
| 9054 | __io_sqe_buffers_unregister(ctx); |
| 9055 | else |
| 9056 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9057 | return ret; |
| 9058 | } |
| 9059 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9060 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9061 | struct io_uring_rsrc_update2 *up, |
| 9062 | unsigned int nr_args) |
| 9063 | { |
| 9064 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9065 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9066 | struct page *last_hpage = NULL; |
| 9067 | bool needs_switch = false; |
| 9068 | __u32 done; |
| 9069 | int i, err; |
| 9070 | |
| 9071 | if (!ctx->buf_data) |
| 9072 | return -ENXIO; |
| 9073 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9074 | return -EINVAL; |
| 9075 | |
| 9076 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9077 | struct io_mapped_ubuf *imu; |
| 9078 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9079 | u64 tag = 0; |
| 9080 | |
| 9081 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9082 | if (err) |
| 9083 | break; |
| 9084 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9085 | err = -EFAULT; |
| 9086 | break; |
| 9087 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9088 | err = io_buffer_validate(&iov); |
| 9089 | if (err) |
| 9090 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9091 | if (!iov.iov_base && tag) { |
| 9092 | err = -EINVAL; |
| 9093 | break; |
| 9094 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9095 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9096 | if (err) |
| 9097 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9098 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9099 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9100 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9101 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
| 9102 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9103 | if (unlikely(err)) { |
| 9104 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9105 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9106 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9107 | ctx->user_bufs[i] = NULL; |
| 9108 | needs_switch = true; |
| 9109 | } |
| 9110 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9111 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9112 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9113 | } |
| 9114 | |
| 9115 | if (needs_switch) |
| 9116 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9117 | return done ? done : err; |
| 9118 | } |
| 9119 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9120 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9121 | { |
| 9122 | __s32 __user *fds = arg; |
| 9123 | int fd; |
| 9124 | |
| 9125 | if (ctx->cq_ev_fd) |
| 9126 | return -EBUSY; |
| 9127 | |
| 9128 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9129 | return -EFAULT; |
| 9130 | |
| 9131 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9132 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9133 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9134 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9135 | ctx->cq_ev_fd = NULL; |
| 9136 | return ret; |
| 9137 | } |
| 9138 | |
| 9139 | return 0; |
| 9140 | } |
| 9141 | |
| 9142 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9143 | { |
| 9144 | if (ctx->cq_ev_fd) { |
| 9145 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9146 | ctx->cq_ev_fd = NULL; |
| 9147 | return 0; |
| 9148 | } |
| 9149 | |
| 9150 | return -ENXIO; |
| 9151 | } |
| 9152 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9153 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9154 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9155 | struct io_buffer *buf; |
| 9156 | unsigned long index; |
| 9157 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9158 | xa_for_each(&ctx->io_buffers, index, buf) { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9159 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9160 | cond_resched(); |
| 9161 | } |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9162 | } |
| 9163 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9164 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9165 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9166 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9167 | int nr = 0; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9168 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9169 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9170 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9171 | |
| 9172 | while (state->free_list.next) { |
| 9173 | struct io_wq_work_node *node; |
| 9174 | struct io_kiocb *req; |
| 9175 | |
| 9176 | node = wq_stack_extract(&state->free_list); |
| 9177 | req = container_of(node, struct io_kiocb, comp_list); |
| 9178 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9179 | nr++; |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9180 | } |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9181 | if (nr) |
| 9182 | percpu_ref_put_many(&ctx->refs, nr); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9183 | mutex_unlock(&ctx->uring_lock); |
| 9184 | } |
| 9185 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9186 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9187 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9188 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9189 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9190 | } |
| 9191 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9192 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9193 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9194 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9195 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9196 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9197 | mmdrop(ctx->mm_account); |
| 9198 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9199 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9200 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9201 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9202 | io_wait_rsrc_data(ctx->buf_data); |
| 9203 | io_wait_rsrc_data(ctx->file_data); |
| 9204 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9205 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9206 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9207 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9208 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9209 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9210 | if (ctx->rings) |
| 9211 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9212 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9213 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9214 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9215 | if (ctx->sq_creds) |
| 9216 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9217 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9218 | /* there are no registered resources left, nobody uses it */ |
| 9219 | if (ctx->rsrc_node) |
| 9220 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9221 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9222 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9223 | flush_delayed_work(&ctx->rsrc_put_work); |
| 9224 | |
| 9225 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9226 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9227 | |
| 9228 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9229 | if (ctx->ring_sock) { |
| 9230 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9231 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9232 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9233 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9234 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9235 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9236 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9237 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9238 | |
| 9239 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9240 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9241 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9242 | if (ctx->hash_map) |
| 9243 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9244 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9245 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9246 | kfree(ctx); |
| 9247 | } |
| 9248 | |
| 9249 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9250 | { |
| 9251 | struct io_ring_ctx *ctx = file->private_data; |
| 9252 | __poll_t mask = 0; |
| 9253 | |
Pavel Begunkov | d60aa65 | 2021-10-04 20:02:52 +0100 | [diff] [blame] | 9254 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9255 | /* |
| 9256 | * synchronizes with barrier from wq_has_sleeper call in |
| 9257 | * io_commit_cqring |
| 9258 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9259 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9260 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9261 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9262 | |
| 9263 | /* |
| 9264 | * Don't flush cqring overflow list here, just do a simple check. |
| 9265 | * Otherwise there could possible be ABBA deadlock: |
| 9266 | * CPU0 CPU1 |
| 9267 | * ---- ---- |
| 9268 | * lock(&ctx->uring_lock); |
| 9269 | * lock(&ep->mtx); |
| 9270 | * lock(&ctx->uring_lock); |
| 9271 | * lock(&ep->mtx); |
| 9272 | * |
| 9273 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9274 | * pushs them to do the flush. |
| 9275 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9276 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9277 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9278 | |
| 9279 | return mask; |
| 9280 | } |
| 9281 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9282 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9283 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9284 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9285 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9286 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9287 | if (creds) { |
| 9288 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9289 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9290 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9291 | |
| 9292 | return -EINVAL; |
| 9293 | } |
| 9294 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9295 | struct io_tctx_exit { |
| 9296 | struct callback_head task_work; |
| 9297 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9298 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9299 | }; |
| 9300 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9301 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9302 | { |
| 9303 | struct io_uring_task *tctx = current->io_uring; |
| 9304 | struct io_tctx_exit *work; |
| 9305 | |
| 9306 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9307 | /* |
| 9308 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9309 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 9310 | */ |
| 9311 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9312 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9313 | complete(&work->completion); |
| 9314 | } |
| 9315 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9316 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9317 | { |
| 9318 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9319 | |
| 9320 | return req->ctx == data; |
| 9321 | } |
| 9322 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9323 | static __cold void io_ring_exit_work(struct work_struct *work) |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9324 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9325 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9326 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9327 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9328 | struct io_tctx_exit exit; |
| 9329 | struct io_tctx_node *node; |
| 9330 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9331 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9332 | /* |
| 9333 | * If we're doing polled IO and end up having requests being |
| 9334 | * submitted async (out-of-line), then completions can come in while |
| 9335 | * we're waiting for refs to drop. We need to reap these manually, |
| 9336 | * as nobody else will be looking for them. |
| 9337 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9338 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9339 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9340 | if (ctx->sq_data) { |
| 9341 | struct io_sq_data *sqd = ctx->sq_data; |
| 9342 | struct task_struct *tsk; |
| 9343 | |
| 9344 | io_sq_thread_park(sqd); |
| 9345 | tsk = sqd->thread; |
| 9346 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9347 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9348 | io_cancel_ctx_cb, ctx, true); |
| 9349 | io_sq_thread_unpark(sqd); |
| 9350 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9351 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9352 | io_req_caches_free(ctx); |
| 9353 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9354 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9355 | /* there is little hope left, don't run it too often */ |
| 9356 | interval = HZ * 60; |
| 9357 | } |
| 9358 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9359 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9360 | init_completion(&exit.completion); |
| 9361 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9362 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9363 | /* |
| 9364 | * Some may use context even when all refs and requests have been put, |
| 9365 | * and they are free to do so while still holding uring_lock or |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 9366 | * completion_lock, see io_req_task_submit(). Apart from other work, |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9367 | * this lock/unlock section also waits them to finish. |
| 9368 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9369 | mutex_lock(&ctx->uring_lock); |
| 9370 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9371 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9372 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9373 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9374 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9375 | /* don't spin on a single task if cancellation failed */ |
| 9376 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9377 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9378 | if (WARN_ON_ONCE(ret)) |
| 9379 | continue; |
| 9380 | wake_up_process(node->task); |
| 9381 | |
| 9382 | mutex_unlock(&ctx->uring_lock); |
| 9383 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9384 | mutex_lock(&ctx->uring_lock); |
| 9385 | } |
| 9386 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9387 | spin_lock(&ctx->completion_lock); |
| 9388 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9389 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9390 | io_ring_ctx_free(ctx); |
| 9391 | } |
| 9392 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9393 | /* Returns true if we found and killed one or more timeouts */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9394 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
| 9395 | struct task_struct *tsk, bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9396 | { |
| 9397 | struct io_kiocb *req, *tmp; |
| 9398 | int canceled = 0; |
| 9399 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9400 | spin_lock(&ctx->completion_lock); |
| 9401 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9402 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9403 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9404 | io_kill_timeout(req, -ECANCELED); |
| 9405 | canceled++; |
| 9406 | } |
| 9407 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9408 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9409 | if (canceled != 0) |
| 9410 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9411 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9412 | if (canceled != 0) |
| 9413 | io_cqring_ev_posted(ctx); |
| 9414 | return canceled != 0; |
| 9415 | } |
| 9416 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9417 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9418 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9419 | unsigned long index; |
| 9420 | struct creds *creds; |
| 9421 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9422 | mutex_lock(&ctx->uring_lock); |
| 9423 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9424 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9425 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9426 | xa_for_each(&ctx->personalities, index, creds) |
| 9427 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9428 | mutex_unlock(&ctx->uring_lock); |
| 9429 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9430 | io_kill_timeouts(ctx, NULL, true); |
| 9431 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9432 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9433 | /* if we failed setting up the ctx, we might not have any rings */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9434 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9435 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9436 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9437 | /* |
| 9438 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9439 | * if we're exiting a ton of rings at the same time. It just adds |
| 9440 | * noise and overhead, there's no discernable change in runtime |
| 9441 | * over using system_wq. |
| 9442 | */ |
| 9443 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9447 | { |
| 9448 | struct io_ring_ctx *ctx = file->private_data; |
| 9449 | |
| 9450 | file->private_data = NULL; |
| 9451 | io_ring_ctx_wait_and_kill(ctx); |
| 9452 | return 0; |
| 9453 | } |
| 9454 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9455 | struct io_task_cancel { |
| 9456 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9457 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9458 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9459 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9460 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9461 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9462 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9463 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9464 | bool ret; |
| 9465 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9466 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9467 | struct io_ring_ctx *ctx = req->ctx; |
| 9468 | |
| 9469 | /* protect against races with linked timeouts */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9470 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9471 | ret = io_match_task(req, cancel->task, cancel->all); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9472 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9473 | } else { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9474 | ret = io_match_task(req, cancel->task, cancel->all); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9475 | } |
| 9476 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9477 | } |
| 9478 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9479 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
| 9480 | struct task_struct *task, |
| 9481 | bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9482 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9483 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9484 | LIST_HEAD(list); |
| 9485 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9486 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9487 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9488 | if (io_match_task(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9489 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9490 | break; |
| 9491 | } |
| 9492 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9493 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9494 | if (list_empty(&list)) |
| 9495 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9496 | |
| 9497 | while (!list_empty(&list)) { |
| 9498 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9499 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9500 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9501 | kfree(de); |
| 9502 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9503 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9504 | } |
| 9505 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9506 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9507 | { |
| 9508 | struct io_tctx_node *node; |
| 9509 | enum io_wq_cancel cret; |
| 9510 | bool ret = false; |
| 9511 | |
| 9512 | mutex_lock(&ctx->uring_lock); |
| 9513 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9514 | struct io_uring_task *tctx = node->task->io_uring; |
| 9515 | |
| 9516 | /* |
| 9517 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9518 | * killed after ctx nodes, which requires to take the lock. |
| 9519 | */ |
| 9520 | if (!tctx || !tctx->io_wq) |
| 9521 | continue; |
| 9522 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9523 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9524 | } |
| 9525 | mutex_unlock(&ctx->uring_lock); |
| 9526 | |
| 9527 | return ret; |
| 9528 | } |
| 9529 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9530 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9531 | struct task_struct *task, |
| 9532 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9533 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9534 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9535 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9536 | |
| 9537 | while (1) { |
| 9538 | enum io_wq_cancel cret; |
| 9539 | bool ret = false; |
| 9540 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9541 | if (!task) { |
| 9542 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9543 | } else if (tctx && tctx->io_wq) { |
| 9544 | /* |
| 9545 | * Cancels requests of all rings, not only @ctx, but |
| 9546 | * it's fine as the task is in exit/exec. |
| 9547 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9548 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9549 | &cancel, true); |
| 9550 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9551 | } |
| 9552 | |
| 9553 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9554 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9555 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 9556 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9557 | io_iopoll_try_reap_events(ctx); |
| 9558 | ret = true; |
| 9559 | } |
| 9560 | } |
| 9561 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9562 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9563 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9564 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9565 | if (task) |
| 9566 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9567 | if (!ret) |
| 9568 | break; |
| 9569 | cond_resched(); |
| 9570 | } |
| 9571 | } |
| 9572 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9573 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9574 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9575 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9576 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9577 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9578 | |
| 9579 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9580 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9581 | if (unlikely(ret)) |
| 9582 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9583 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9584 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9585 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 9586 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 9587 | if (!node) |
| 9588 | return -ENOMEM; |
| 9589 | node->ctx = ctx; |
| 9590 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9591 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9592 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 9593 | node, GFP_KERNEL)); |
| 9594 | if (ret) { |
| 9595 | kfree(node); |
| 9596 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9597 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9598 | |
| 9599 | mutex_lock(&ctx->uring_lock); |
| 9600 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 9601 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9602 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9603 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9604 | return 0; |
| 9605 | } |
| 9606 | |
| 9607 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9608 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9609 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9610 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9611 | { |
| 9612 | struct io_uring_task *tctx = current->io_uring; |
| 9613 | |
| 9614 | if (likely(tctx && tctx->last == ctx)) |
| 9615 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9616 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9617 | } |
| 9618 | |
| 9619 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9620 | * Remove this io_uring_file -> task mapping. |
| 9621 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9622 | static __cold void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9623 | { |
| 9624 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9625 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9626 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 9627 | if (!tctx) |
| 9628 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9629 | node = xa_erase(&tctx->xa, index); |
| 9630 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9631 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9632 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9633 | WARN_ON_ONCE(current != node->task); |
| 9634 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 9635 | |
| 9636 | mutex_lock(&node->ctx->uring_lock); |
| 9637 | list_del(&node->ctx_node); |
| 9638 | mutex_unlock(&node->ctx->uring_lock); |
| 9639 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9640 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9641 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9642 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9643 | } |
| 9644 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9645 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9646 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9647 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9648 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9649 | unsigned long index; |
| 9650 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9651 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9652 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9653 | cond_resched(); |
| 9654 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9655 | if (wq) { |
| 9656 | /* |
| 9657 | * Must be after io_uring_del_task_file() (removes nodes under |
| 9658 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 9659 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9660 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 9661 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9662 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9665 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9666 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9667 | if (tracked) |
| 9668 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9669 | return percpu_counter_sum(&tctx->inflight); |
| 9670 | } |
| 9671 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9672 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9673 | { |
| 9674 | struct io_uring_task *tctx = task->io_uring; |
| 9675 | unsigned int refs = tctx->cached_refs; |
| 9676 | |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9677 | if (refs) { |
| 9678 | tctx->cached_refs = 0; |
| 9679 | percpu_counter_sub(&tctx->inflight, refs); |
| 9680 | put_task_struct_many(task, refs); |
| 9681 | } |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9682 | } |
| 9683 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9684 | /* |
| 9685 | * Find any io_uring ctx that this task has registered or done IO on, and cancel |
| 9686 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. |
| 9687 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9688 | static __cold void io_uring_cancel_generic(bool cancel_all, |
| 9689 | struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9690 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9691 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 9692 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9693 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9694 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9695 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9696 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 9697 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 9698 | if (!current->io_uring) |
| 9699 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 9700 | if (tctx->io_wq) |
| 9701 | io_wq_exit_start(tctx->io_wq); |
| 9702 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9703 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9704 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9705 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9706 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9707 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9708 | if (!inflight) |
| 9709 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9710 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9711 | if (!sqd) { |
| 9712 | struct io_tctx_node *node; |
| 9713 | unsigned long index; |
| 9714 | |
| 9715 | xa_for_each(&tctx->xa, index, node) { |
| 9716 | /* sqpoll task will cancel all its requests */ |
| 9717 | if (node->ctx->sq_data) |
| 9718 | continue; |
| 9719 | io_uring_try_cancel_requests(node->ctx, current, |
| 9720 | cancel_all); |
| 9721 | } |
| 9722 | } else { |
| 9723 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 9724 | io_uring_try_cancel_requests(ctx, current, |
| 9725 | cancel_all); |
| 9726 | } |
| 9727 | |
| 9728 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9729 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9730 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9731 | * If we've seen completions, retry without waiting. This |
| 9732 | * avoids a race where a completion comes in before we did |
| 9733 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9734 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9735 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9736 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9737 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9738 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9739 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9740 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9741 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9742 | if (cancel_all) { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9743 | /* for exec all current's requests should be gone, kill tctx */ |
| 9744 | __io_uring_free(current); |
| 9745 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9746 | } |
| 9747 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9748 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9749 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9750 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9751 | } |
| 9752 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9753 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9754 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9755 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9756 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9757 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9758 | struct page *page; |
| 9759 | void *ptr; |
| 9760 | |
| 9761 | switch (offset) { |
| 9762 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9763 | case IORING_OFF_CQ_RING: |
| 9764 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9765 | break; |
| 9766 | case IORING_OFF_SQES: |
| 9767 | ptr = ctx->sq_sqes; |
| 9768 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9769 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9770 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9771 | } |
| 9772 | |
| 9773 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9774 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9775 | return ERR_PTR(-EINVAL); |
| 9776 | |
| 9777 | return ptr; |
| 9778 | } |
| 9779 | |
| 9780 | #ifdef CONFIG_MMU |
| 9781 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9782 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9783 | { |
| 9784 | size_t sz = vma->vm_end - vma->vm_start; |
| 9785 | unsigned long pfn; |
| 9786 | void *ptr; |
| 9787 | |
| 9788 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9789 | if (IS_ERR(ptr)) |
| 9790 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9791 | |
| 9792 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9793 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9794 | } |
| 9795 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9796 | #else /* !CONFIG_MMU */ |
| 9797 | |
| 9798 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9799 | { |
| 9800 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9801 | } |
| 9802 | |
| 9803 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9804 | { |
| 9805 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9806 | } |
| 9807 | |
| 9808 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9809 | unsigned long addr, unsigned long len, |
| 9810 | unsigned long pgoff, unsigned long flags) |
| 9811 | { |
| 9812 | void *ptr; |
| 9813 | |
| 9814 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9815 | if (IS_ERR(ptr)) |
| 9816 | return PTR_ERR(ptr); |
| 9817 | |
| 9818 | return (unsigned long) ptr; |
| 9819 | } |
| 9820 | |
| 9821 | #endif /* !CONFIG_MMU */ |
| 9822 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9823 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9824 | { |
| 9825 | DEFINE_WAIT(wait); |
| 9826 | |
| 9827 | do { |
| 9828 | if (!io_sqring_full(ctx)) |
| 9829 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9830 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9831 | |
| 9832 | if (!io_sqring_full(ctx)) |
| 9833 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9834 | schedule(); |
| 9835 | } while (!signal_pending(current)); |
| 9836 | |
| 9837 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9838 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9839 | } |
| 9840 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9841 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9842 | struct __kernel_timespec __user **ts, |
| 9843 | const sigset_t __user **sig) |
| 9844 | { |
| 9845 | struct io_uring_getevents_arg arg; |
| 9846 | |
| 9847 | /* |
| 9848 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9849 | * is just a pointer to the sigset_t. |
| 9850 | */ |
| 9851 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9852 | *sig = (const sigset_t __user *) argp; |
| 9853 | *ts = NULL; |
| 9854 | return 0; |
| 9855 | } |
| 9856 | |
| 9857 | /* |
| 9858 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9859 | * timespec and sigset_t pointers if good. |
| 9860 | */ |
| 9861 | if (*argsz != sizeof(arg)) |
| 9862 | return -EINVAL; |
| 9863 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9864 | return -EFAULT; |
| 9865 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9866 | *argsz = arg.sigmask_sz; |
| 9867 | *ts = u64_to_user_ptr(arg.ts); |
| 9868 | return 0; |
| 9869 | } |
| 9870 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9871 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9872 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9873 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9874 | { |
| 9875 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9876 | int submitted = 0; |
| 9877 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9878 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9879 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9880 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9881 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9882 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 9883 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9884 | return -EINVAL; |
| 9885 | |
| 9886 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9887 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9888 | return -EBADF; |
| 9889 | |
| 9890 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9891 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9892 | goto out_fput; |
| 9893 | |
| 9894 | ret = -ENXIO; |
| 9895 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9896 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9897 | goto out_fput; |
| 9898 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9899 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9900 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9901 | goto out; |
| 9902 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9903 | /* |
| 9904 | * For SQ polling, the thread will do all submissions and completions. |
| 9905 | * Just return the requested submit count, and wake the thread if |
| 9906 | * we were asked to. |
| 9907 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9908 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9909 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 9910 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9911 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 9912 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 9913 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 9914 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 9915 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9916 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9917 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9918 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9919 | ret = io_sqpoll_wait_sq(ctx); |
| 9920 | if (ret) |
| 9921 | goto out; |
| 9922 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9923 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9924 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9925 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9926 | if (unlikely(ret)) |
| 9927 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9928 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9929 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9930 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9931 | |
| 9932 | if (submitted != to_submit) |
| 9933 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9934 | } |
| 9935 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9936 | const sigset_t __user *sig; |
| 9937 | struct __kernel_timespec __user *ts; |
| 9938 | |
| 9939 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9940 | if (unlikely(ret)) |
| 9941 | goto out; |
| 9942 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9943 | min_complete = min(min_complete, ctx->cq_entries); |
| 9944 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9945 | /* |
| 9946 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9947 | * space applications don't need to do io completion events |
| 9948 | * polling again, they can rely on io_sq_thread to do polling |
| 9949 | * work, which can reduce cpu usage and uring_lock contention. |
| 9950 | */ |
| 9951 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9952 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9953 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9954 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9955 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9956 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9957 | } |
| 9958 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9959 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9960 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9961 | out_fput: |
| 9962 | fdput(f); |
| 9963 | return submitted ? submitted : ret; |
| 9964 | } |
| 9965 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9966 | #ifdef CONFIG_PROC_FS |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9967 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9968 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9969 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9970 | struct user_namespace *uns = seq_user_ns(m); |
| 9971 | struct group_info *gi; |
| 9972 | kernel_cap_t cap; |
| 9973 | unsigned __capi; |
| 9974 | int g; |
| 9975 | |
| 9976 | seq_printf(m, "%5d\n", id); |
| 9977 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9978 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9979 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9980 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9981 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9982 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9983 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9984 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9985 | seq_puts(m, "\n\tGroups:\t"); |
| 9986 | gi = cred->group_info; |
| 9987 | for (g = 0; g < gi->ngroups; g++) { |
| 9988 | seq_put_decimal_ull(m, g ? " " : "", |
| 9989 | from_kgid_munged(uns, gi->gid[g])); |
| 9990 | } |
| 9991 | seq_puts(m, "\n\tCapEff:\t"); |
| 9992 | cap = cred->cap_effective; |
| 9993 | CAP_FOR_EACH_U32(__capi) |
| 9994 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9995 | seq_putc(m, '\n'); |
| 9996 | return 0; |
| 9997 | } |
| 9998 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9999 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
| 10000 | struct seq_file *m) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10001 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10002 | struct io_sq_data *sq = NULL; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10003 | struct io_overflow_cqe *ocqe; |
| 10004 | struct io_rings *r = ctx->rings; |
| 10005 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; |
| 10006 | unsigned int cached_sq_head = ctx->cached_sq_head; |
| 10007 | unsigned int cached_cq_tail = ctx->cached_cq_tail; |
| 10008 | unsigned int sq_head = READ_ONCE(r->sq.head); |
| 10009 | unsigned int sq_tail = READ_ONCE(r->sq.tail); |
| 10010 | unsigned int cq_head = READ_ONCE(r->cq.head); |
| 10011 | unsigned int cq_tail = READ_ONCE(r->cq.tail); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10012 | bool has_lock; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10013 | unsigned int i; |
| 10014 | |
| 10015 | /* |
| 10016 | * we may get imprecise sqe and cqe info if uring is actively running |
| 10017 | * since we get cached_sq_head and cached_cq_tail without uring_lock |
| 10018 | * and sq_tail and cq_head are changed by userspace. But it's ok since |
| 10019 | * we usually use these info when it is stuck. |
| 10020 | */ |
| 10021 | seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask); |
| 10022 | seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask); |
| 10023 | seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask); |
| 10024 | seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask); |
| 10025 | seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask); |
| 10026 | seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask); |
| 10027 | seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head); |
| 10028 | for (i = cached_sq_head; i < sq_tail; i++) { |
| 10029 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]); |
| 10030 | |
| 10031 | if (likely(sq_idx <= sq_mask)) { |
| 10032 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; |
| 10033 | |
| 10034 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", |
| 10035 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data); |
| 10036 | } |
| 10037 | } |
| 10038 | seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head); |
| 10039 | for (i = cq_head; i < cached_cq_tail; i++) { |
| 10040 | struct io_uring_cqe *cqe = &r->cqes[i & cq_mask]; |
| 10041 | |
| 10042 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", |
| 10043 | i & cq_mask, cqe->user_data, cqe->res, cqe->flags); |
| 10044 | } |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10045 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10046 | /* |
| 10047 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10048 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10049 | * cases. If we fail to get the lock, we just don't iterate any |
| 10050 | * structures that could be going away outside the io_uring mutex. |
| 10051 | */ |
| 10052 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10053 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10054 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10055 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10056 | if (!sq->thread) |
| 10057 | sq = NULL; |
| 10058 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10059 | |
| 10060 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 10061 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10062 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10063 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10064 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10065 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10066 | if (f) |
| 10067 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10068 | else |
| 10069 | seq_printf(m, "%5u: <none>\n", i); |
| 10070 | } |
| 10071 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10072 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10073 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10074 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10075 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10076 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10077 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10078 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10079 | unsigned long index; |
| 10080 | const struct cred *cred; |
| 10081 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10082 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10083 | xa_for_each(&ctx->personalities, index, cred) |
| 10084 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10085 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10086 | if (has_lock) |
| 10087 | mutex_unlock(&ctx->uring_lock); |
| 10088 | |
| 10089 | seq_puts(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10090 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10091 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10092 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10093 | struct io_kiocb *req; |
| 10094 | |
| 10095 | hlist_for_each_entry(req, list, hash_node) |
| 10096 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10097 | req->task->task_works != NULL); |
| 10098 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10099 | |
| 10100 | seq_puts(m, "CqOverflowList:\n"); |
| 10101 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { |
| 10102 | struct io_uring_cqe *cqe = &ocqe->cqe; |
| 10103 | |
| 10104 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", |
| 10105 | cqe->user_data, cqe->res, cqe->flags); |
| 10106 | |
| 10107 | } |
| 10108 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10109 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10110 | } |
| 10111 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10112 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10113 | { |
| 10114 | struct io_ring_ctx *ctx = f->private_data; |
| 10115 | |
| 10116 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10117 | __io_uring_show_fdinfo(ctx, m); |
| 10118 | percpu_ref_put(&ctx->refs); |
| 10119 | } |
| 10120 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10121 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10122 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10123 | static const struct file_operations io_uring_fops = { |
| 10124 | .release = io_uring_release, |
| 10125 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10126 | #ifndef CONFIG_MMU |
| 10127 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10128 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10129 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10130 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10131 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10132 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10133 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10134 | }; |
| 10135 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10136 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10137 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10138 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10139 | struct io_rings *rings; |
| 10140 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10141 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10142 | /* make sure these are sane, as we already accounted them */ |
| 10143 | ctx->sq_entries = p->sq_entries; |
| 10144 | ctx->cq_entries = p->cq_entries; |
| 10145 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10146 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10147 | if (size == SIZE_MAX) |
| 10148 | return -EOVERFLOW; |
| 10149 | |
| 10150 | rings = io_mem_alloc(size); |
| 10151 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10152 | return -ENOMEM; |
| 10153 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10154 | ctx->rings = rings; |
| 10155 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10156 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10157 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10158 | rings->sq_ring_entries = p->sq_entries; |
| 10159 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10160 | |
| 10161 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10162 | if (size == SIZE_MAX) { |
| 10163 | io_mem_free(ctx->rings); |
| 10164 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10165 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10166 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10167 | |
| 10168 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10169 | if (!ctx->sq_sqes) { |
| 10170 | io_mem_free(ctx->rings); |
| 10171 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10172 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10173 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10174 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10175 | return 0; |
| 10176 | } |
| 10177 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10178 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10179 | { |
| 10180 | int ret, fd; |
| 10181 | |
| 10182 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10183 | if (fd < 0) |
| 10184 | return fd; |
| 10185 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10186 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10187 | if (ret) { |
| 10188 | put_unused_fd(fd); |
| 10189 | return ret; |
| 10190 | } |
| 10191 | fd_install(fd, file); |
| 10192 | return fd; |
| 10193 | } |
| 10194 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10195 | /* |
| 10196 | * Allocate an anonymous fd, this is what constitutes the application |
| 10197 | * visible backing of an io_uring instance. The application mmaps this |
| 10198 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10199 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10200 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10201 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10202 | { |
| 10203 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10204 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10205 | int ret; |
| 10206 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10207 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10208 | &ctx->ring_sock); |
| 10209 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10210 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10211 | #endif |
| 10212 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10213 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 10214 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10215 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10216 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10217 | sock_release(ctx->ring_sock); |
| 10218 | ctx->ring_sock = NULL; |
| 10219 | } else { |
| 10220 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10221 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10222 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10223 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10224 | } |
| 10225 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10226 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10227 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10228 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10229 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10230 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10231 | int ret; |
| 10232 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10233 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10234 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10235 | if (entries > IORING_MAX_ENTRIES) { |
| 10236 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10237 | return -EINVAL; |
| 10238 | entries = IORING_MAX_ENTRIES; |
| 10239 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10240 | |
| 10241 | /* |
| 10242 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10243 | * application to drive a higher depth than the size of the SQ ring, |
| 10244 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10245 | * some flexibility in overcommitting a bit. If the application has |
| 10246 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10247 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10248 | */ |
| 10249 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10250 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10251 | /* |
| 10252 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10253 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10254 | * any cq vs sq ring sizing. |
| 10255 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10256 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10257 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10258 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10259 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10260 | return -EINVAL; |
| 10261 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10262 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10263 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10264 | if (p->cq_entries < p->sq_entries) |
| 10265 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10266 | } else { |
| 10267 | p->cq_entries = 2 * p->sq_entries; |
| 10268 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10269 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10270 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10271 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10272 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10273 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10274 | if (!capable(CAP_IPC_LOCK)) |
| 10275 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10276 | |
| 10277 | /* |
| 10278 | * This is just grabbed for accounting purposes. When a process exits, |
| 10279 | * the mm is exited and dropped before the files, hence we need to hang |
| 10280 | * on to this mm purely for the purposes of being able to unaccount |
| 10281 | * memory (locked/pinned vm). It's not used for anything else. |
| 10282 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10283 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10284 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10285 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10286 | ret = io_allocate_scq_urings(ctx, p); |
| 10287 | if (ret) |
| 10288 | goto err; |
| 10289 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10290 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10291 | if (ret) |
| 10292 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10293 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10294 | ret = io_rsrc_node_switch_start(ctx); |
| 10295 | if (ret) |
| 10296 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10297 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10298 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10299 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10300 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10301 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10302 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10303 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10304 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10305 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10306 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10307 | |
| 10308 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10309 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10310 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10311 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10312 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10313 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10314 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10315 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10316 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10317 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10318 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10319 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10320 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10321 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
| 10322 | IORING_FEAT_RSRC_TAGS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10323 | |
| 10324 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10325 | ret = -EFAULT; |
| 10326 | goto err; |
| 10327 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10328 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10329 | file = io_uring_get_file(ctx); |
| 10330 | if (IS_ERR(file)) { |
| 10331 | ret = PTR_ERR(file); |
| 10332 | goto err; |
| 10333 | } |
| 10334 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10335 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10336 | * Install ring fd as the very last thing, so we don't risk someone |
| 10337 | * having closed it before we finish setup |
| 10338 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10339 | ret = io_uring_install_fd(ctx, file); |
| 10340 | if (ret < 0) { |
| 10341 | /* fput will clean it up */ |
| 10342 | fput(file); |
| 10343 | return ret; |
| 10344 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10345 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10346 | 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] | 10347 | return ret; |
| 10348 | err: |
| 10349 | io_ring_ctx_wait_and_kill(ctx); |
| 10350 | return ret; |
| 10351 | } |
| 10352 | |
| 10353 | /* |
| 10354 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10355 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10356 | * params structure passed in. |
| 10357 | */ |
| 10358 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10359 | { |
| 10360 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10361 | int i; |
| 10362 | |
| 10363 | if (copy_from_user(&p, params, sizeof(p))) |
| 10364 | return -EFAULT; |
| 10365 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10366 | if (p.resv[i]) |
| 10367 | return -EINVAL; |
| 10368 | } |
| 10369 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10370 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10371 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10372 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10373 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10374 | return -EINVAL; |
| 10375 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10376 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10377 | } |
| 10378 | |
| 10379 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10380 | struct io_uring_params __user *, params) |
| 10381 | { |
| 10382 | return io_uring_setup(entries, params); |
| 10383 | } |
| 10384 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10385 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
| 10386 | unsigned nr_args) |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10387 | { |
| 10388 | struct io_uring_probe *p; |
| 10389 | size_t size; |
| 10390 | int i, ret; |
| 10391 | |
| 10392 | size = struct_size(p, ops, nr_args); |
| 10393 | if (size == SIZE_MAX) |
| 10394 | return -EOVERFLOW; |
| 10395 | p = kzalloc(size, GFP_KERNEL); |
| 10396 | if (!p) |
| 10397 | return -ENOMEM; |
| 10398 | |
| 10399 | ret = -EFAULT; |
| 10400 | if (copy_from_user(p, arg, size)) |
| 10401 | goto out; |
| 10402 | ret = -EINVAL; |
| 10403 | if (memchr_inv(p, 0, size)) |
| 10404 | goto out; |
| 10405 | |
| 10406 | p->last_op = IORING_OP_LAST - 1; |
| 10407 | if (nr_args > IORING_OP_LAST) |
| 10408 | nr_args = IORING_OP_LAST; |
| 10409 | |
| 10410 | for (i = 0; i < nr_args; i++) { |
| 10411 | p->ops[i].op = i; |
| 10412 | if (!io_op_defs[i].not_supported) |
| 10413 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10414 | } |
| 10415 | p->ops_len = i; |
| 10416 | |
| 10417 | ret = 0; |
| 10418 | if (copy_to_user(arg, p, size)) |
| 10419 | ret = -EFAULT; |
| 10420 | out: |
| 10421 | kfree(p); |
| 10422 | return ret; |
| 10423 | } |
| 10424 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10425 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10426 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10427 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10428 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10429 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10430 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10431 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10432 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10433 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10434 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10435 | if (ret < 0) { |
| 10436 | put_cred(creds); |
| 10437 | return ret; |
| 10438 | } |
| 10439 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10440 | } |
| 10441 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10442 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
| 10443 | void __user *arg, unsigned int nr_args) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10444 | { |
| 10445 | struct io_uring_restriction *res; |
| 10446 | size_t size; |
| 10447 | int i, ret; |
| 10448 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10449 | /* Restrictions allowed only if rings started disabled */ |
| 10450 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10451 | return -EBADFD; |
| 10452 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10453 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10454 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10455 | return -EBUSY; |
| 10456 | |
| 10457 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10458 | return -EINVAL; |
| 10459 | |
| 10460 | size = array_size(nr_args, sizeof(*res)); |
| 10461 | if (size == SIZE_MAX) |
| 10462 | return -EOVERFLOW; |
| 10463 | |
| 10464 | res = memdup_user(arg, size); |
| 10465 | if (IS_ERR(res)) |
| 10466 | return PTR_ERR(res); |
| 10467 | |
| 10468 | ret = 0; |
| 10469 | |
| 10470 | for (i = 0; i < nr_args; i++) { |
| 10471 | switch (res[i].opcode) { |
| 10472 | case IORING_RESTRICTION_REGISTER_OP: |
| 10473 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10474 | ret = -EINVAL; |
| 10475 | goto out; |
| 10476 | } |
| 10477 | |
| 10478 | __set_bit(res[i].register_op, |
| 10479 | ctx->restrictions.register_op); |
| 10480 | break; |
| 10481 | case IORING_RESTRICTION_SQE_OP: |
| 10482 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10483 | ret = -EINVAL; |
| 10484 | goto out; |
| 10485 | } |
| 10486 | |
| 10487 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10488 | break; |
| 10489 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10490 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10491 | break; |
| 10492 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10493 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10494 | break; |
| 10495 | default: |
| 10496 | ret = -EINVAL; |
| 10497 | goto out; |
| 10498 | } |
| 10499 | } |
| 10500 | |
| 10501 | out: |
| 10502 | /* Reset all restrictions if an error happened */ |
| 10503 | if (ret != 0) |
| 10504 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10505 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10506 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10507 | |
| 10508 | kfree(res); |
| 10509 | return ret; |
| 10510 | } |
| 10511 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10512 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10513 | { |
| 10514 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10515 | return -EBADFD; |
| 10516 | |
| 10517 | if (ctx->restrictions.registered) |
| 10518 | ctx->restricted = 1; |
| 10519 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10520 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10521 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10522 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10523 | return 0; |
| 10524 | } |
| 10525 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10526 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10527 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10528 | unsigned nr_args) |
| 10529 | { |
| 10530 | __u32 tmp; |
| 10531 | int err; |
| 10532 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10533 | if (up->resv) |
| 10534 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10535 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10536 | return -EOVERFLOW; |
| 10537 | err = io_rsrc_node_switch_start(ctx); |
| 10538 | if (err) |
| 10539 | return err; |
| 10540 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10541 | switch (type) { |
| 10542 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10543 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10544 | case IORING_RSRC_BUFFER: |
| 10545 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10546 | } |
| 10547 | return -EINVAL; |
| 10548 | } |
| 10549 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10550 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10551 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10552 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10553 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10554 | |
| 10555 | if (!nr_args) |
| 10556 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10557 | memset(&up, 0, sizeof(up)); |
| 10558 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10559 | return -EFAULT; |
| 10560 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10561 | } |
| 10562 | |
| 10563 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10564 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10565 | { |
| 10566 | struct io_uring_rsrc_update2 up; |
| 10567 | |
| 10568 | if (size != sizeof(up)) |
| 10569 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10570 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10571 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10572 | if (!up.nr || up.resv) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10573 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10574 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10575 | } |
| 10576 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10577 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10578 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10579 | { |
| 10580 | struct io_uring_rsrc_register rr; |
| 10581 | |
| 10582 | /* keep it extendible */ |
| 10583 | if (size != sizeof(rr)) |
| 10584 | return -EINVAL; |
| 10585 | |
| 10586 | memset(&rr, 0, sizeof(rr)); |
| 10587 | if (copy_from_user(&rr, arg, size)) |
| 10588 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10589 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10590 | return -EINVAL; |
| 10591 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10592 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10593 | case IORING_RSRC_FILE: |
| 10594 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10595 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10596 | case IORING_RSRC_BUFFER: |
| 10597 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10598 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10599 | } |
| 10600 | return -EINVAL; |
| 10601 | } |
| 10602 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10603 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
| 10604 | void __user *arg, unsigned len) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10605 | { |
| 10606 | struct io_uring_task *tctx = current->io_uring; |
| 10607 | cpumask_var_t new_mask; |
| 10608 | int ret; |
| 10609 | |
| 10610 | if (!tctx || !tctx->io_wq) |
| 10611 | return -EINVAL; |
| 10612 | |
| 10613 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10614 | return -ENOMEM; |
| 10615 | |
| 10616 | cpumask_clear(new_mask); |
| 10617 | if (len > cpumask_size()) |
| 10618 | len = cpumask_size(); |
| 10619 | |
| 10620 | if (copy_from_user(new_mask, arg, len)) { |
| 10621 | free_cpumask_var(new_mask); |
| 10622 | return -EFAULT; |
| 10623 | } |
| 10624 | |
| 10625 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10626 | free_cpumask_var(new_mask); |
| 10627 | return ret; |
| 10628 | } |
| 10629 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10630 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10631 | { |
| 10632 | struct io_uring_task *tctx = current->io_uring; |
| 10633 | |
| 10634 | if (!tctx || !tctx->io_wq) |
| 10635 | return -EINVAL; |
| 10636 | |
| 10637 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 10638 | } |
| 10639 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10640 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 10641 | void __user *arg) |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10642 | { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10643 | struct io_uring_task *tctx = NULL; |
| 10644 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10645 | __u32 new_count[2]; |
| 10646 | int i, ret; |
| 10647 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10648 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 10649 | return -EFAULT; |
| 10650 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10651 | if (new_count[i] > INT_MAX) |
| 10652 | return -EINVAL; |
| 10653 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10654 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 10655 | sqd = ctx->sq_data; |
| 10656 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10657 | /* |
| 10658 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 10659 | * ordering. Fine to drop uring_lock here, we hold |
| 10660 | * a ref to the ctx. |
| 10661 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10662 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10663 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10664 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10665 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10666 | if (sqd->thread) |
| 10667 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10668 | } |
| 10669 | } else { |
| 10670 | tctx = current->io_uring; |
| 10671 | } |
| 10672 | |
| 10673 | ret = -EINVAL; |
| 10674 | if (!tctx || !tctx->io_wq) |
| 10675 | goto err; |
| 10676 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10677 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 10678 | if (ret) |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10679 | goto err; |
| 10680 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10681 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10682 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10683 | io_put_sq_data(sqd); |
| 10684 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10685 | |
| 10686 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 10687 | return -EFAULT; |
| 10688 | |
| 10689 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10690 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10691 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10692 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10693 | io_put_sq_data(sqd); |
| 10694 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10695 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10696 | } |
| 10697 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10698 | static bool io_register_op_must_quiesce(int op) |
| 10699 | { |
| 10700 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 10701 | case IORING_REGISTER_BUFFERS: |
| 10702 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 10703 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10704 | case IORING_UNREGISTER_FILES: |
| 10705 | case IORING_REGISTER_FILES_UPDATE: |
| 10706 | case IORING_REGISTER_PROBE: |
| 10707 | case IORING_REGISTER_PERSONALITY: |
| 10708 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10709 | case IORING_REGISTER_FILES2: |
| 10710 | case IORING_REGISTER_FILES_UPDATE2: |
| 10711 | case IORING_REGISTER_BUFFERS2: |
| 10712 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10713 | case IORING_REGISTER_IOWQ_AFF: |
| 10714 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10715 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10716 | return false; |
| 10717 | default: |
| 10718 | return true; |
| 10719 | } |
| 10720 | } |
| 10721 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10722 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10723 | { |
| 10724 | long ret; |
| 10725 | |
| 10726 | percpu_ref_kill(&ctx->refs); |
| 10727 | |
| 10728 | /* |
| 10729 | * Drop uring mutex before waiting for references to exit. If another |
| 10730 | * thread is currently inside io_uring_enter() it might need to grab the |
| 10731 | * uring_lock to make progress. If we hold it here across the drain |
| 10732 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10733 | * no new references will come in after we've killed the percpu ref. |
| 10734 | */ |
| 10735 | mutex_unlock(&ctx->uring_lock); |
| 10736 | do { |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10737 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
| 10738 | if (ret) { |
| 10739 | ret = min(0L, ret); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10740 | break; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10741 | } |
| 10742 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10743 | ret = io_run_task_work_sig(); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10744 | io_req_caches_free(ctx); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10745 | } while (ret >= 0); |
| 10746 | mutex_lock(&ctx->uring_lock); |
| 10747 | |
| 10748 | if (ret) |
| 10749 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 10750 | return ret; |
| 10751 | } |
| 10752 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10753 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10754 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10755 | __releases(ctx->uring_lock) |
| 10756 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10757 | { |
| 10758 | int ret; |
| 10759 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10760 | /* |
| 10761 | * We're inside the ring mutex, if the ref is already dying, then |
| 10762 | * someone else killed the ctx or is already going through |
| 10763 | * io_uring_register(). |
| 10764 | */ |
| 10765 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10766 | return -ENXIO; |
| 10767 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 10768 | if (ctx->restricted) { |
| 10769 | if (opcode >= IORING_REGISTER_LAST) |
| 10770 | return -EINVAL; |
| 10771 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 10772 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 10773 | return -EACCES; |
| 10774 | } |
| 10775 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10776 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10777 | ret = io_ctx_quiesce(ctx); |
| 10778 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 10779 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10780 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10781 | |
| 10782 | switch (opcode) { |
| 10783 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10784 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10785 | break; |
| 10786 | case IORING_UNREGISTER_BUFFERS: |
| 10787 | ret = -EINVAL; |
| 10788 | if (arg || nr_args) |
| 10789 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10790 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10791 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10792 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10793 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10794 | break; |
| 10795 | case IORING_UNREGISTER_FILES: |
| 10796 | ret = -EINVAL; |
| 10797 | if (arg || nr_args) |
| 10798 | break; |
| 10799 | ret = io_sqe_files_unregister(ctx); |
| 10800 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10801 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10802 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10803 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10804 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10805 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10806 | ret = -EINVAL; |
| 10807 | if (nr_args != 1) |
| 10808 | break; |
| 10809 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10810 | if (ret) |
| 10811 | break; |
| 10812 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10813 | ctx->eventfd_async = 1; |
| 10814 | else |
| 10815 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10816 | break; |
| 10817 | case IORING_UNREGISTER_EVENTFD: |
| 10818 | ret = -EINVAL; |
| 10819 | if (arg || nr_args) |
| 10820 | break; |
| 10821 | ret = io_eventfd_unregister(ctx); |
| 10822 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10823 | case IORING_REGISTER_PROBE: |
| 10824 | ret = -EINVAL; |
| 10825 | if (!arg || nr_args > 256) |
| 10826 | break; |
| 10827 | ret = io_probe(ctx, arg, nr_args); |
| 10828 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10829 | case IORING_REGISTER_PERSONALITY: |
| 10830 | ret = -EINVAL; |
| 10831 | if (arg || nr_args) |
| 10832 | break; |
| 10833 | ret = io_register_personality(ctx); |
| 10834 | break; |
| 10835 | case IORING_UNREGISTER_PERSONALITY: |
| 10836 | ret = -EINVAL; |
| 10837 | if (arg) |
| 10838 | break; |
| 10839 | ret = io_unregister_personality(ctx, nr_args); |
| 10840 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10841 | case IORING_REGISTER_ENABLE_RINGS: |
| 10842 | ret = -EINVAL; |
| 10843 | if (arg || nr_args) |
| 10844 | break; |
| 10845 | ret = io_register_enable_rings(ctx); |
| 10846 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10847 | case IORING_REGISTER_RESTRICTIONS: |
| 10848 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10849 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10850 | case IORING_REGISTER_FILES2: |
| 10851 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10852 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10853 | case IORING_REGISTER_FILES_UPDATE2: |
| 10854 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 10855 | IORING_RSRC_FILE); |
| 10856 | break; |
| 10857 | case IORING_REGISTER_BUFFERS2: |
| 10858 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 10859 | break; |
| 10860 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 10861 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 10862 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10863 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10864 | case IORING_REGISTER_IOWQ_AFF: |
| 10865 | ret = -EINVAL; |
| 10866 | if (!arg || !nr_args) |
| 10867 | break; |
| 10868 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 10869 | break; |
| 10870 | case IORING_UNREGISTER_IOWQ_AFF: |
| 10871 | ret = -EINVAL; |
| 10872 | if (arg || nr_args) |
| 10873 | break; |
| 10874 | ret = io_unregister_iowq_aff(ctx); |
| 10875 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10876 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 10877 | ret = -EINVAL; |
| 10878 | if (!arg || nr_args != 2) |
| 10879 | break; |
| 10880 | ret = io_register_iowq_max_workers(ctx, arg); |
| 10881 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10882 | default: |
| 10883 | ret = -EINVAL; |
| 10884 | break; |
| 10885 | } |
| 10886 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10887 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10888 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10889 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10890 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10891 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10892 | return ret; |
| 10893 | } |
| 10894 | |
| 10895 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10896 | void __user *, arg, unsigned int, nr_args) |
| 10897 | { |
| 10898 | struct io_ring_ctx *ctx; |
| 10899 | long ret = -EBADF; |
| 10900 | struct fd f; |
| 10901 | |
| 10902 | f = fdget(fd); |
| 10903 | if (!f.file) |
| 10904 | return -EBADF; |
| 10905 | |
| 10906 | ret = -EOPNOTSUPP; |
| 10907 | if (f.file->f_op != &io_uring_fops) |
| 10908 | goto out_fput; |
| 10909 | |
| 10910 | ctx = f.file->private_data; |
| 10911 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 10912 | io_run_task_work(); |
| 10913 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10914 | mutex_lock(&ctx->uring_lock); |
| 10915 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10916 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10917 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10918 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10919 | out_fput: |
| 10920 | fdput(f); |
| 10921 | return ret; |
| 10922 | } |
| 10923 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10924 | static int __init io_uring_init(void) |
| 10925 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10926 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10927 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10928 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10929 | } while (0) |
| 10930 | |
| 10931 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10932 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10933 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10934 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10935 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10936 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10937 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10938 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10939 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10940 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10941 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10942 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10943 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10944 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10945 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10946 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10947 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10948 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10949 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10950 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10951 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10952 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10953 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10954 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10955 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10956 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10957 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10958 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10959 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 10960 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10961 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10962 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 10963 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10964 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 10965 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 10966 | sizeof(struct io_uring_rsrc_update)); |
| 10967 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 10968 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 10969 | |
| 10970 | /* ->buf_index is u16 */ |
| 10971 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 10972 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 10973 | /* should fit into one byte */ |
| 10974 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 10975 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
| 10976 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 10977 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10978 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 10979 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 10980 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 10981 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 10982 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10983 | return 0; |
| 10984 | }; |
| 10985 | __initcall(io_uring_init); |