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 | \ |
| 112 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 113 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 114 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
| 115 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 116 | struct io_uring { |
| 117 | u32 head ____cacheline_aligned_in_smp; |
| 118 | u32 tail ____cacheline_aligned_in_smp; |
| 119 | }; |
| 120 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | * This data is shared with the application through the mmap at offsets |
| 123 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | * |
| 125 | * The offsets to the member fields are published through struct |
| 126 | * io_sqring_offsets when calling io_uring_setup. |
| 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 129 | /* |
| 130 | * Head and tail offsets into the ring; the offsets need to be |
| 131 | * masked to get valid indices. |
| 132 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 134 | * and the application controls tail of the sq ring and the head of the |
| 135 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 136 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 137 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 138 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 139 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 140 | * ring_entries - 1) |
| 141 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 142 | u32 sq_ring_mask, cq_ring_mask; |
| 143 | /* Ring sizes (constant, power of 2) */ |
| 144 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Number of invalid entries dropped by the kernel due to |
| 147 | * invalid index stored in array |
| 148 | * |
| 149 | * Written by the kernel, shouldn't be modified by the |
| 150 | * application (i.e. get number of "new events" by comparing to |
| 151 | * cached value). |
| 152 | * |
| 153 | * After a new SQ head value was read by the application this |
| 154 | * counter includes all submissions that were dropped reaching |
| 155 | * the new SQ head (and possibly more). |
| 156 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 157 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 158 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 159 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 160 | * |
| 161 | * Written by the kernel, shouldn't be modified by the |
| 162 | * application. |
| 163 | * |
| 164 | * The application needs a full memory barrier before checking |
| 165 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 166 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 167 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 168 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 169 | * Runtime CQ flags |
| 170 | * |
| 171 | * Written by the application, shouldn't be modified by the |
| 172 | * kernel. |
| 173 | */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 174 | u32 cq_flags; |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 175 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 176 | * Number of completion events lost because the queue was full; |
| 177 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 178 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 179 | * the completion queue. |
| 180 | * |
| 181 | * Written by the kernel, shouldn't be modified by the |
| 182 | * application (i.e. get number of "new events" by comparing to |
| 183 | * cached value). |
| 184 | * |
| 185 | * As completion events come in out of order this counter is not |
| 186 | * ordered with any other data. |
| 187 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 188 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 189 | /* |
| 190 | * Ring buffer of completion events. |
| 191 | * |
| 192 | * The kernel writes completion events fresh every time they are |
| 193 | * produced, so the application is allowed to modify pending |
| 194 | * entries. |
| 195 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 196 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 199 | enum io_uring_cmd_flags { |
| 200 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 201 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 204 | struct io_mapped_ubuf { |
| 205 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 206 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 207 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 208 | unsigned long acct_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 209 | struct bio_vec bvec[]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 212 | struct io_ring_ctx; |
| 213 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 214 | struct io_overflow_cqe { |
| 215 | struct io_uring_cqe cqe; |
| 216 | struct list_head list; |
| 217 | }; |
| 218 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 219 | struct io_fixed_file { |
| 220 | /* file * with additional FFS_* flags */ |
| 221 | unsigned long file_ptr; |
| 222 | }; |
| 223 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 224 | struct io_rsrc_put { |
| 225 | struct list_head list; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 226 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 227 | union { |
| 228 | void *rsrc; |
| 229 | struct file *file; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 230 | struct io_mapped_ubuf *buf; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 231 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 234 | struct io_file_table { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 235 | struct io_fixed_file *files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 238 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 239 | struct percpu_ref refs; |
| 240 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 241 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 242 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 243 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 244 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 245 | }; |
| 246 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 247 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 248 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 249 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 250 | struct io_ring_ctx *ctx; |
| 251 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 252 | u64 **tags; |
| 253 | unsigned int nr; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 254 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 255 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 256 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 257 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 260 | struct io_buffer { |
| 261 | struct list_head list; |
| 262 | __u64 addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 263 | __u32 len; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 264 | __u16 bid; |
| 265 | }; |
| 266 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 267 | struct io_restriction { |
| 268 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 269 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 270 | u8 sqe_flags_allowed; |
| 271 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 272 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 273 | }; |
| 274 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 275 | enum { |
| 276 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 277 | IO_SQ_THREAD_SHOULD_PARK, |
| 278 | }; |
| 279 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 280 | struct io_sq_data { |
| 281 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 282 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 283 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 284 | |
| 285 | /* ctx's that are using this sqd */ |
| 286 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 287 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 288 | struct task_struct *thread; |
| 289 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 290 | |
| 291 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 292 | int sq_cpu; |
| 293 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 294 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 295 | |
| 296 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 297 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 298 | }; |
| 299 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 300 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 301 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 302 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 303 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 304 | struct io_submit_link { |
| 305 | struct io_kiocb *head; |
| 306 | struct io_kiocb *last; |
| 307 | }; |
| 308 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 309 | struct io_submit_state { |
| 310 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 311 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * io_kiocb alloc cache |
| 315 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 316 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 317 | unsigned int free_reqs; |
| 318 | |
| 319 | bool plug_started; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 320 | bool need_plug; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * Batch completion logic |
| 324 | */ |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 325 | struct io_kiocb *compl_reqs[IO_COMPL_BATCH]; |
| 326 | unsigned int compl_nr; |
| 327 | /* inline/task_work completion list, under ->uring_lock */ |
| 328 | struct list_head free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 331 | struct io_ring_ctx { |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 332 | /* const or read-mostly hot data */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 333 | struct { |
| 334 | struct percpu_ref refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 335 | |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 336 | struct io_rings *rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 337 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 338 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 339 | unsigned int drain_next: 1; |
| 340 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 341 | unsigned int restricted: 1; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 342 | unsigned int off_timeout_used: 1; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 343 | unsigned int drain_active: 1; |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 344 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 345 | |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 346 | /* submission data */ |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 347 | struct { |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 348 | struct mutex uring_lock; |
| 349 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 352 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 353 | * |
| 354 | * This indirection could e.g. be used to assign fixed |
| 355 | * io_uring_sqe entries to operations and only submit them to |
| 356 | * the queue when needed. |
| 357 | * |
| 358 | * The kernel modifies neither the indices array nor the entries |
| 359 | * array. |
| 360 | */ |
| 361 | u32 *sq_array; |
Pavel Begunkov | c7af47c | 2021-06-14 23:37:20 +0100 | [diff] [blame] | 362 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 363 | unsigned cached_sq_head; |
| 364 | unsigned sq_entries; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 365 | struct list_head defer_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | * Fixed resources fast path, should be accessed only under |
| 369 | * uring_lock, and updated through io_uring_register(2) |
| 370 | */ |
| 371 | struct io_rsrc_node *rsrc_node; |
| 372 | struct io_file_table file_table; |
| 373 | unsigned nr_user_files; |
| 374 | unsigned nr_user_bufs; |
| 375 | struct io_mapped_ubuf **user_bufs; |
| 376 | |
| 377 | struct io_submit_state submit_state; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 378 | struct list_head timeout_list; |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 379 | struct list_head ltimeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 380 | struct list_head cq_overflow_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 381 | struct xarray io_buffers; |
| 382 | struct xarray personalities; |
| 383 | u32 pers_next; |
| 384 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 385 | } ____cacheline_aligned_in_smp; |
| 386 | |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 387 | /* IRQ completion list, under ->completion_lock */ |
| 388 | struct list_head locked_free_list; |
| 389 | unsigned int locked_free_nr; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 390 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 391 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 392 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 393 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 394 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 395 | struct list_head sqd_list; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 396 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 397 | unsigned long check_cq_overflow; |
| 398 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 399 | struct { |
| 400 | unsigned cached_cq_tail; |
| 401 | unsigned cq_entries; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 402 | struct eventfd_ctx *cq_ev_fd; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 403 | struct wait_queue_head poll_wait; |
| 404 | struct wait_queue_head cq_wait; |
| 405 | unsigned cq_extra; |
| 406 | atomic_t cq_timeouts; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 407 | unsigned cq_last_tm_flush; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 408 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 409 | |
| 410 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 411 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 412 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 413 | spinlock_t timeout_lock; |
| 414 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 415 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 416 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 417 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 418 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 419 | * manipulate the list, hence no extra locking is needed there. |
| 420 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 421 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 422 | struct hlist_head *cancel_hash; |
| 423 | unsigned cancel_hash_bits; |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 424 | bool poll_multi_queue; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 425 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 426 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 427 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 428 | |
Pavel Begunkov | b13a891 | 2021-05-16 22:58:07 +0100 | [diff] [blame] | 429 | /* slow path rsrc auxilary data, used by update/register */ |
| 430 | struct { |
| 431 | struct io_rsrc_node *rsrc_backup_node; |
| 432 | struct io_mapped_ubuf *dummy_ubuf; |
| 433 | struct io_rsrc_data *file_data; |
| 434 | struct io_rsrc_data *buf_data; |
| 435 | |
| 436 | struct delayed_work rsrc_put_work; |
| 437 | struct llist_head rsrc_put_llist; |
| 438 | struct list_head rsrc_ref_list; |
| 439 | spinlock_t rsrc_ref_lock; |
| 440 | }; |
| 441 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 442 | /* Keep this last, we don't need it for the fast path */ |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 443 | struct { |
| 444 | #if defined(CONFIG_UNIX) |
| 445 | struct socket *ring_sock; |
| 446 | #endif |
| 447 | /* hashed buffered write serialization */ |
| 448 | struct io_wq_hash *hash_map; |
| 449 | |
| 450 | /* Only used for accounting purposes */ |
| 451 | struct user_struct *user; |
| 452 | struct mm_struct *mm_account; |
| 453 | |
| 454 | /* ctx exit and cancelation */ |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 455 | struct llist_head fallback_llist; |
| 456 | struct delayed_work fallback_work; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 457 | struct work_struct exit_work; |
| 458 | struct list_head tctx_list; |
| 459 | struct completion ref_comp; |
| 460 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 463 | struct io_uring_task { |
| 464 | /* submission side */ |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 465 | int cached_refs; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 466 | struct xarray xa; |
| 467 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 468 | const struct io_ring_ctx *last; |
| 469 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 470 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 471 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 472 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 473 | |
| 474 | spinlock_t task_lock; |
| 475 | struct io_wq_work_list task_list; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 476 | struct callback_head task_work; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 477 | bool task_running; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 478 | }; |
| 479 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 480 | /* |
| 481 | * First field must be the file pointer in all the |
| 482 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 483 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 484 | struct io_poll_iocb { |
| 485 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 486 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 487 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 488 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 489 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 490 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 491 | }; |
| 492 | |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 493 | struct io_poll_update { |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 494 | struct file *file; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 495 | u64 old_user_data; |
| 496 | u64 new_user_data; |
| 497 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 498 | bool update_events; |
| 499 | bool update_user_data; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 502 | struct io_close { |
| 503 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 504 | int fd; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 505 | u32 file_slot; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 508 | struct io_timeout_data { |
| 509 | struct io_kiocb *req; |
| 510 | struct hrtimer timer; |
| 511 | struct timespec64 ts; |
| 512 | enum hrtimer_mode mode; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 513 | u32 flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 516 | struct io_accept { |
| 517 | struct file *file; |
| 518 | struct sockaddr __user *addr; |
| 519 | int __user *addr_len; |
| 520 | int flags; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 521 | u32 file_slot; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 522 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | struct io_sync { |
| 526 | struct file *file; |
| 527 | loff_t len; |
| 528 | loff_t off; |
| 529 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 530 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 533 | struct io_cancel { |
| 534 | struct file *file; |
| 535 | u64 addr; |
| 536 | }; |
| 537 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 538 | struct io_timeout { |
| 539 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 540 | u32 off; |
| 541 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 542 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 543 | /* head of the link, used by linked timeouts only */ |
| 544 | struct io_kiocb *head; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 545 | /* for linked completions */ |
| 546 | struct io_kiocb *prev; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 549 | struct io_timeout_rem { |
| 550 | struct file *file; |
| 551 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 552 | |
| 553 | /* timeout update */ |
| 554 | struct timespec64 ts; |
| 555 | u32 flags; |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 556 | bool ltimeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 557 | }; |
| 558 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 559 | struct io_rw { |
| 560 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 561 | struct kiocb kiocb; |
| 562 | u64 addr; |
| 563 | u64 len; |
| 564 | }; |
| 565 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 566 | struct io_connect { |
| 567 | struct file *file; |
| 568 | struct sockaddr __user *addr; |
| 569 | int addr_len; |
| 570 | }; |
| 571 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 572 | struct io_sr_msg { |
| 573 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 574 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 575 | struct compat_msghdr __user *umsg_compat; |
| 576 | struct user_msghdr __user *umsg; |
| 577 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 578 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 579 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 580 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 581 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 582 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | struct io_open { |
| 586 | struct file *file; |
| 587 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 588 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 589 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 590 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 591 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 594 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 595 | struct file *file; |
| 596 | u64 arg; |
| 597 | u32 nr_args; |
| 598 | u32 offset; |
| 599 | }; |
| 600 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 601 | struct io_fadvise { |
| 602 | struct file *file; |
| 603 | u64 offset; |
| 604 | u32 len; |
| 605 | u32 advice; |
| 606 | }; |
| 607 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 608 | struct io_madvise { |
| 609 | struct file *file; |
| 610 | u64 addr; |
| 611 | u32 len; |
| 612 | u32 advice; |
| 613 | }; |
| 614 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 615 | struct io_epoll { |
| 616 | struct file *file; |
| 617 | int epfd; |
| 618 | int op; |
| 619 | int fd; |
| 620 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 623 | struct io_splice { |
| 624 | struct file *file_out; |
| 625 | struct file *file_in; |
| 626 | loff_t off_out; |
| 627 | loff_t off_in; |
| 628 | u64 len; |
| 629 | unsigned int flags; |
| 630 | }; |
| 631 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 632 | struct io_provide_buf { |
| 633 | struct file *file; |
| 634 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 635 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 636 | __u32 bgid; |
| 637 | __u16 nbufs; |
| 638 | __u16 bid; |
| 639 | }; |
| 640 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 641 | struct io_statx { |
| 642 | struct file *file; |
| 643 | int dfd; |
| 644 | unsigned int mask; |
| 645 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 646 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 647 | struct statx __user *buffer; |
| 648 | }; |
| 649 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 650 | struct io_shutdown { |
| 651 | struct file *file; |
| 652 | int how; |
| 653 | }; |
| 654 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 655 | struct io_rename { |
| 656 | struct file *file; |
| 657 | int old_dfd; |
| 658 | int new_dfd; |
| 659 | struct filename *oldpath; |
| 660 | struct filename *newpath; |
| 661 | int flags; |
| 662 | }; |
| 663 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 664 | struct io_unlink { |
| 665 | struct file *file; |
| 666 | int dfd; |
| 667 | int flags; |
| 668 | struct filename *filename; |
| 669 | }; |
| 670 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 671 | struct io_mkdir { |
| 672 | struct file *file; |
| 673 | int dfd; |
| 674 | umode_t mode; |
| 675 | struct filename *filename; |
| 676 | }; |
| 677 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 678 | struct io_symlink { |
| 679 | struct file *file; |
| 680 | int new_dfd; |
| 681 | struct filename *oldpath; |
| 682 | struct filename *newpath; |
| 683 | }; |
| 684 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 685 | struct io_hardlink { |
| 686 | struct file *file; |
| 687 | int old_dfd; |
| 688 | int new_dfd; |
| 689 | struct filename *oldpath; |
| 690 | struct filename *newpath; |
| 691 | int flags; |
| 692 | }; |
| 693 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 694 | struct io_completion { |
| 695 | struct file *file; |
Pavel Begunkov | 8c3f9cd | 2021-02-28 22:35:15 +0000 | [diff] [blame] | 696 | u32 cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 697 | }; |
| 698 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 699 | struct io_async_connect { |
| 700 | struct sockaddr_storage address; |
| 701 | }; |
| 702 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 703 | struct io_async_msghdr { |
| 704 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 705 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 706 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 707 | struct sockaddr __user *uaddr; |
| 708 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 709 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 710 | }; |
| 711 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 712 | struct io_async_rw { |
| 713 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 714 | const struct iovec *free_iovec; |
| 715 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 716 | struct iov_iter_state iter_state; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 717 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 718 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 721 | enum { |
| 722 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 723 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 724 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 725 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 726 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 727 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 728 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 729 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 730 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 731 | REQ_F_INFLIGHT_BIT, |
| 732 | REQ_F_CUR_POS_BIT, |
| 733 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 734 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 735 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 736 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 737 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 738 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 739 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 740 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 741 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 742 | REQ_F_ARM_LTIMEOUT_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 743 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 744 | REQ_F_NOWAIT_READ_BIT, |
| 745 | REQ_F_NOWAIT_WRITE_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 746 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 747 | |
| 748 | /* not a real bit, just to check we're not overflowing the space */ |
| 749 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | enum { |
| 753 | /* ctx owns file */ |
| 754 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 755 | /* drain existing IO first */ |
| 756 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 757 | /* linked sqes */ |
| 758 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 759 | /* doesn't sever on completion < 0 */ |
| 760 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 761 | /* IOSQE_ASYNC */ |
| 762 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 763 | /* IOSQE_BUFFER_SELECT */ |
| 764 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 765 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 766 | /* fail rest of links */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 767 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 768 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 769 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 770 | /* read/write uses file position */ |
| 771 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 772 | /* must not punt to workers */ |
| 773 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 774 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 775 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 776 | /* needs cleanup */ |
| 777 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 778 | /* already went through poll handler */ |
| 779 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 780 | /* buffer already selected */ |
| 781 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 782 | /* completion is deferred through io_comp_state */ |
| 783 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 784 | /* caller should reissue async */ |
| 785 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 786 | /* supports async reads */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 787 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 788 | /* supports async writes */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 789 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 790 | /* regular file */ |
| 791 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 792 | /* has creds assigned */ |
| 793 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 794 | /* skip refcounting if not set */ |
| 795 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 796 | /* there is a linked timeout that has to be armed */ |
| 797 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
| 800 | struct async_poll { |
| 801 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 802 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 803 | }; |
| 804 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 805 | 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] | 806 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 807 | struct io_task_work { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 808 | union { |
| 809 | struct io_wq_work_node node; |
| 810 | struct llist_node fallback_node; |
| 811 | }; |
| 812 | io_req_tw_func_t func; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 815 | enum { |
| 816 | IORING_RSRC_FILE = 0, |
| 817 | IORING_RSRC_BUFFER = 1, |
| 818 | }; |
| 819 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 820 | /* |
| 821 | * NOTE! Each of the iocb union members has the file pointer |
| 822 | * as the first entry in their struct definition. So you can |
| 823 | * access the file pointer through any of the sub-structs, |
| 824 | * or directly as just 'ki_filp' in this struct. |
| 825 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 826 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 827 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 828 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 829 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 830 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 831 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 832 | struct io_accept accept; |
| 833 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 834 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 835 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 836 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 837 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 838 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 839 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 840 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 841 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 842 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 843 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 844 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 845 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 846 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 847 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 848 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 849 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 850 | struct io_unlink unlink; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 851 | struct io_mkdir mkdir; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 852 | struct io_symlink symlink; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 853 | struct io_hardlink hardlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 854 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 855 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 856 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 857 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 858 | /* opcode allocated if it needs to store data for async defer */ |
| 859 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 860 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 861 | /* polled IO has completed */ |
| 862 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 863 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 864 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 865 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 866 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 867 | struct io_ring_ctx *ctx; |
| 868 | unsigned int flags; |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 869 | atomic_t refs; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 870 | struct task_struct *task; |
| 871 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 872 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 873 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 874 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 875 | |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 876 | /* used with ctx->iopoll_list with reads/writes */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 877 | struct list_head inflight_entry; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 878 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 879 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 880 | struct hlist_node hash_node; |
| 881 | struct async_poll *apoll; |
| 882 | struct io_wq_work work; |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 883 | const struct cred *creds; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 884 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 885 | /* store used ubuf, so we can prevent reloading */ |
| 886 | struct io_mapped_ubuf *imu; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 887 | }; |
| 888 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 889 | struct io_tctx_node { |
| 890 | struct list_head ctx_node; |
| 891 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 892 | struct io_ring_ctx *ctx; |
| 893 | }; |
| 894 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 895 | struct io_defer_entry { |
| 896 | struct list_head list; |
| 897 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 898 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 899 | }; |
| 900 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 901 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 902 | /* needs req->file assigned */ |
| 903 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 904 | /* hash wq insertion if file is a regular file */ |
| 905 | unsigned hash_reg_file : 1; |
| 906 | /* unbound wq insertion if file is a non-regular file */ |
| 907 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 908 | /* opcode is not supported by this kernel */ |
| 909 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 910 | /* set if opcode supports polled "wait" */ |
| 911 | unsigned pollin : 1; |
| 912 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 913 | /* op supports buffer selection */ |
| 914 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 915 | /* do prep async if is going to be punted */ |
| 916 | unsigned needs_async_setup : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 917 | /* should block plug */ |
| 918 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 919 | /* size of async data needed, if any */ |
| 920 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 921 | }; |
| 922 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 923 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 924 | [IORING_OP_NOP] = {}, |
| 925 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 926 | .needs_file = 1, |
| 927 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 928 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 929 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 930 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 931 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 932 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 933 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 934 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 935 | .needs_file = 1, |
| 936 | .hash_reg_file = 1, |
| 937 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 938 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 939 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 940 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 941 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 942 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 943 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 944 | .needs_file = 1, |
| 945 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 946 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 947 | .needs_file = 1, |
| 948 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 949 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 950 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 951 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 952 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 953 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 954 | .needs_file = 1, |
| 955 | .hash_reg_file = 1, |
| 956 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 957 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 958 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 959 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 960 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 961 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 962 | .needs_file = 1, |
| 963 | .unbound_nonreg_file = 1, |
| 964 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 965 | [IORING_OP_POLL_REMOVE] = {}, |
| 966 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 967 | .needs_file = 1, |
| 968 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 969 | [IORING_OP_SENDMSG] = { |
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 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 973 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 974 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 975 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 976 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 977 | .needs_file = 1, |
| 978 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 979 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 980 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 981 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 982 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 983 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 984 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 985 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 986 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 987 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 988 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 989 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 990 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 991 | .needs_file = 1, |
| 992 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 993 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 994 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 995 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 996 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 997 | .async_size = sizeof(struct io_timeout_data), |
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_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1000 | .needs_file = 1, |
| 1001 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1002 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 1003 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1004 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1005 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1006 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1007 | .needs_file = 1, |
| 1008 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1009 | [IORING_OP_OPENAT] = {}, |
| 1010 | [IORING_OP_CLOSE] = {}, |
| 1011 | [IORING_OP_FILES_UPDATE] = {}, |
| 1012 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1013 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1014 | .needs_file = 1, |
| 1015 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1016 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1017 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1018 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1019 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1020 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1021 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1022 | .needs_file = 1, |
Jens Axboe | 7b3188e | 2021-08-30 19:37:41 -0600 | [diff] [blame] | 1023 | .hash_reg_file = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1024 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1025 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1026 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1027 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1028 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1029 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1030 | .needs_file = 1, |
| 1031 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1032 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1033 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1034 | .needs_file = 1, |
| 1035 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1036 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1037 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1038 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1039 | .needs_file = 1, |
| 1040 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1041 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1042 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1043 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1044 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1045 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1046 | [IORING_OP_EPOLL_CTL] = { |
| 1047 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1048 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1049 | [IORING_OP_SPLICE] = { |
| 1050 | .needs_file = 1, |
| 1051 | .hash_reg_file = 1, |
| 1052 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1053 | }, |
| 1054 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1055 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1056 | [IORING_OP_TEE] = { |
| 1057 | .needs_file = 1, |
| 1058 | .hash_reg_file = 1, |
| 1059 | .unbound_nonreg_file = 1, |
| 1060 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1061 | [IORING_OP_SHUTDOWN] = { |
| 1062 | .needs_file = 1, |
| 1063 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1064 | [IORING_OP_RENAMEAT] = {}, |
| 1065 | [IORING_OP_UNLINKAT] = {}, |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 1066 | [IORING_OP_MKDIRAT] = {}, |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 1067 | [IORING_OP_SYMLINKAT] = {}, |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 1068 | [IORING_OP_LINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1069 | }; |
| 1070 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1071 | /* requests with any of those set should undergo io_disarm_next() */ |
| 1072 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) |
| 1073 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1074 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 1075 | static void io_uring_del_tctx_node(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1076 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1077 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1078 | bool cancel_all); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 1079 | 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] | 1080 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1081 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1082 | long res, unsigned int cflags); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1083 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1084 | static void io_put_req_deferred(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1085 | static void io_dismantle_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1086 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 1087 | 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] | 1088 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 1089 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1090 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 1091 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1092 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1093 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1094 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1095 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1096 | static void io_req_task_queue(struct io_kiocb *req); |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1097 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1098 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1099 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1100 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 1101 | unsigned int issue_flags, u32 slot_index); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 1102 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1103 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 1104 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1105 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1106 | static struct kmem_cache *req_cachep; |
| 1107 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1108 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1109 | |
| 1110 | struct sock *io_uring_get_socket(struct file *file) |
| 1111 | { |
| 1112 | #if defined(CONFIG_UNIX) |
| 1113 | if (file->f_op == &io_uring_fops) { |
| 1114 | struct io_ring_ctx *ctx = file->private_data; |
| 1115 | |
| 1116 | return ctx->ring_sock->sk; |
| 1117 | } |
| 1118 | #endif |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1122 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1123 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
| 1124 | { |
| 1125 | if (!*locked) { |
| 1126 | mutex_lock(&ctx->uring_lock); |
| 1127 | *locked = true; |
| 1128 | } |
| 1129 | } |
| 1130 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1131 | #define io_for_each_link(pos, head) \ |
| 1132 | for (pos = (head); pos; pos = pos->link) |
| 1133 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1134 | /* |
| 1135 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1136 | * see commit f958d7b528b1 for details. |
| 1137 | */ |
| 1138 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1139 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1140 | |
| 1141 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1142 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1143 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1144 | return atomic_inc_not_zero(&req->refs); |
| 1145 | } |
| 1146 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1147 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1148 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1149 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
| 1150 | return true; |
| 1151 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1152 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1153 | return atomic_dec_and_test(&req->refs); |
| 1154 | } |
| 1155 | |
| 1156 | static inline void req_ref_put(struct io_kiocb *req) |
| 1157 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1158 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1159 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
| 1160 | } |
| 1161 | |
| 1162 | static inline void req_ref_get(struct io_kiocb *req) |
| 1163 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1164 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1165 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1166 | atomic_inc(&req->refs); |
| 1167 | } |
| 1168 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1169 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
| 1170 | { |
| 1171 | if (ctx->submit_state.compl_nr) |
| 1172 | __io_submit_flush_completions(ctx); |
| 1173 | } |
| 1174 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1175 | 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] | 1176 | { |
| 1177 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1178 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1179 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1183 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1184 | { |
| 1185 | __io_req_set_refcount(req, 1); |
| 1186 | } |
| 1187 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1188 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1189 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1190 | struct io_ring_ctx *ctx = req->ctx; |
| 1191 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1192 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1193 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1194 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1198 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1199 | { |
| 1200 | bool got = percpu_ref_tryget(ref); |
| 1201 | |
| 1202 | /* already at zero, wait for ->release() */ |
| 1203 | if (!got) |
| 1204 | wait_for_completion(compl); |
| 1205 | percpu_ref_resurrect(ref); |
| 1206 | if (got) |
| 1207 | percpu_ref_put(ref); |
| 1208 | } |
| 1209 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1210 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1211 | bool cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1212 | { |
| 1213 | struct io_kiocb *req; |
| 1214 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1215 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1216 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1217 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | |
| 1220 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1221 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1222 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1223 | } |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1227 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1228 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1229 | req->flags |= REQ_F_FAIL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1230 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1231 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1232 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1233 | { |
| 1234 | req_set_fail(req); |
| 1235 | req->result = res; |
| 1236 | } |
| 1237 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1238 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1239 | { |
| 1240 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1241 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1242 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1245 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1246 | { |
| 1247 | return !req->timeout.off; |
| 1248 | } |
| 1249 | |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1250 | static void io_fallback_req_func(struct work_struct *work) |
| 1251 | { |
| 1252 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1253 | fallback_work.work); |
| 1254 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1255 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1256 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1257 | |
| 1258 | percpu_ref_get(&ctx->refs); |
| 1259 | 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] | 1260 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1261 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1262 | if (locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1263 | io_submit_flush_completions(ctx); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1264 | mutex_unlock(&ctx->uring_lock); |
| 1265 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1266 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1267 | |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1270 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1271 | { |
| 1272 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1273 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1274 | |
| 1275 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1276 | if (!ctx) |
| 1277 | return NULL; |
| 1278 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1279 | /* |
| 1280 | * Use 5 bits less than the max cq entries, that should give us around |
| 1281 | * 32 entries per hash list if totally full and uniformly spread. |
| 1282 | */ |
| 1283 | hash_bits = ilog2(p->cq_entries); |
| 1284 | hash_bits -= 5; |
| 1285 | if (hash_bits <= 0) |
| 1286 | hash_bits = 1; |
| 1287 | ctx->cancel_hash_bits = hash_bits; |
| 1288 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1289 | GFP_KERNEL); |
| 1290 | if (!ctx->cancel_hash) |
| 1291 | goto err; |
| 1292 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1293 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1294 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1295 | if (!ctx->dummy_ubuf) |
| 1296 | goto err; |
| 1297 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1298 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1299 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1300 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1301 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1302 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1303 | |
| 1304 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1305 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1306 | INIT_LIST_HEAD(&ctx->sqd_list); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1307 | init_waitqueue_head(&ctx->poll_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1308 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1309 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1310 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1311 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1312 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1313 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1314 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1315 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1316 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1317 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1318 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1319 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1320 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1321 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1322 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1323 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1324 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1325 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1326 | INIT_LIST_HEAD(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1327 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1328 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1329 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1330 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1331 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1332 | kfree(ctx); |
| 1333 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1336 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1337 | { |
| 1338 | struct io_rings *r = ctx->rings; |
| 1339 | |
| 1340 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1341 | ctx->cq_extra--; |
| 1342 | } |
| 1343 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1344 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1345 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1346 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1347 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1348 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1349 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1350 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1351 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1352 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1353 | } |
| 1354 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1355 | #define FFS_ASYNC_READ 0x1UL |
| 1356 | #define FFS_ASYNC_WRITE 0x2UL |
| 1357 | #ifdef CONFIG_64BIT |
| 1358 | #define FFS_ISREG 0x4UL |
| 1359 | #else |
| 1360 | #define FFS_ISREG 0x0UL |
| 1361 | #endif |
| 1362 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) |
| 1363 | |
| 1364 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1365 | { |
| 1366 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); |
| 1367 | } |
| 1368 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1369 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1370 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1371 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1372 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1373 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1377 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
| 1378 | { |
| 1379 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1380 | } |
| 1381 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1382 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1383 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1384 | if (WARN_ON_ONCE(!req->link)) |
| 1385 | return NULL; |
| 1386 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1387 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1388 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1389 | |
| 1390 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1391 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1392 | __io_req_set_refcount(req->link, 2); |
| 1393 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1397 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1398 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1399 | return NULL; |
| 1400 | return __io_prep_linked_timeout(req); |
| 1401 | } |
| 1402 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1403 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1404 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1405 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1406 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1407 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1408 | if (!(req->flags & REQ_F_CREDS)) { |
| 1409 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1410 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1411 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1412 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1413 | req->work.list.next = NULL; |
| 1414 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1415 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1416 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1417 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1418 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1419 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1420 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1421 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1422 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1423 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1424 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1425 | |
| 1426 | switch (req->opcode) { |
| 1427 | case IORING_OP_SPLICE: |
| 1428 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1429 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1430 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1431 | break; |
| 1432 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1433 | } |
| 1434 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1435 | static void io_prep_async_link(struct io_kiocb *req) |
| 1436 | { |
| 1437 | struct io_kiocb *cur; |
| 1438 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1439 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1440 | struct io_ring_ctx *ctx = req->ctx; |
| 1441 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1442 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1443 | io_for_each_link(cur, req) |
| 1444 | io_prep_async_work(cur); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1445 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1446 | } else { |
| 1447 | io_for_each_link(cur, req) |
| 1448 | io_prep_async_work(cur); |
| 1449 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1450 | } |
| 1451 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1452 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1453 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1454 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1455 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1456 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1457 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1458 | /* must not take the lock, NULL it as a precaution */ |
| 1459 | locked = NULL; |
| 1460 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1461 | BUG_ON(!tctx); |
| 1462 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1463 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1464 | /* init ->work of the whole link before punting */ |
| 1465 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1469 | * happen, catch it here and ensure the request is marked as |
| 1470 | * canceled. That will make io-wq go through the usual work cancel |
| 1471 | * procedure rather than attempt to run this request (or create a new |
| 1472 | * worker for it). |
| 1473 | */ |
| 1474 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1475 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1476 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1477 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1478 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1479 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1480 | if (link) |
| 1481 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1482 | } |
| 1483 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1484 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1485 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1486 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1487 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1488 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1489 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1490 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1491 | if (status) |
| 1492 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1493 | atomic_set(&req->ctx->cq_timeouts, |
| 1494 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1495 | list_del_init(&req->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1496 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1497 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1501 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1502 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1503 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1504 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1505 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1506 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1507 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1508 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1509 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1510 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1511 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1512 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1513 | } |
| 1514 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1515 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1516 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1517 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1518 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1519 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1520 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1521 | while (!list_empty(&ctx->timeout_list)) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1522 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1523 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1524 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1525 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1526 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1527 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1528 | |
| 1529 | /* |
| 1530 | * Since seq can easily wrap around over time, subtract |
| 1531 | * the last seq at which timeouts were flushed before comparing. |
| 1532 | * Assuming not more than 2^31-1 events have happened since, |
| 1533 | * these subtractions won't have wrapped, so we can check if |
| 1534 | * target is in [last_seq, current_seq] by comparing the two. |
| 1535 | */ |
| 1536 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1537 | events_got = seq - ctx->cq_last_tm_flush; |
| 1538 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1539 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1540 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1541 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1542 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1543 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1544 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1545 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1546 | } |
| 1547 | |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1548 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1549 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1550 | if (ctx->off_timeout_used) |
| 1551 | io_flush_timeouts(ctx); |
| 1552 | if (ctx->drain_active) |
| 1553 | io_queue_deferred(ctx); |
| 1554 | } |
| 1555 | |
| 1556 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1557 | { |
| 1558 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1559 | __io_commit_cqring_flush(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1560 | /* order cqe stores with ring update */ |
| 1561 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1562 | } |
| 1563 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1564 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1565 | { |
| 1566 | struct io_rings *r = ctx->rings; |
| 1567 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1568 | 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] | 1569 | } |
| 1570 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1571 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1572 | { |
| 1573 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1574 | } |
| 1575 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1576 | 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] | 1577 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1578 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1579 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1580 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * writes to the cq entry need to come after reading head; the |
| 1583 | * control dependency is enough as we're using WRITE_ONCE to |
| 1584 | * fill the cq entry |
| 1585 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1586 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1587 | return NULL; |
| 1588 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1589 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1590 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1593 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1594 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1595 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1596 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1597 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1598 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1599 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1602 | /* |
| 1603 | * This should only get called when at least one event has been posted. |
| 1604 | * Some applications rely on the eventfd notification count only changing |
| 1605 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1606 | * 1:1 relationship between how many times this function is called (and |
| 1607 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1608 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1609 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1610 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1611 | /* |
| 1612 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1613 | * io_should_wake() handle the termination of the loop and only |
| 1614 | * wake as many waiters as we need to. |
| 1615 | */ |
| 1616 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1617 | wake_up_all(&ctx->cq_wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1618 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1619 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1620 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1621 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | 3f00838 | 2021-10-01 10:39:33 +0100 | [diff] [blame] | 1622 | if (waitqueue_active(&ctx->poll_wait)) |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1623 | wake_up_interruptible(&ctx->poll_wait); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1624 | } |
| 1625 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1626 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1627 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1628 | /* see waitqueue_active() comment */ |
| 1629 | smp_mb(); |
| 1630 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1631 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1632 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1633 | wake_up_all(&ctx->cq_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1634 | } |
| 1635 | if (io_should_trigger_evfd(ctx)) |
| 1636 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | 3f00838 | 2021-10-01 10:39:33 +0100 | [diff] [blame] | 1637 | if (waitqueue_active(&ctx->poll_wait)) |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1638 | wake_up_interruptible(&ctx->poll_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1641 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1642 | 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] | 1643 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1644 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1645 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1646 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1647 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1648 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1649 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1650 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1651 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1652 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1653 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1654 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1655 | if (!cqe && !force) |
| 1656 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1657 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1658 | struct io_overflow_cqe, list); |
| 1659 | if (cqe) |
| 1660 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1661 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1662 | io_account_cq_overflow(ctx); |
| 1663 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1664 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1665 | list_del(&ocqe->list); |
| 1666 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1669 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1670 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1671 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1672 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1673 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1674 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1675 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1676 | if (posted) |
| 1677 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1678 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1679 | if (posted) |
| 1680 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1681 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1684 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1685 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1686 | bool ret = true; |
| 1687 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1688 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1689 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1690 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1691 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1692 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1693 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1694 | mutex_unlock(&ctx->uring_lock); |
| 1695 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1696 | |
| 1697 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1700 | /* must to be called somewhat shortly after putting a request */ |
| 1701 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1702 | { |
| 1703 | struct io_uring_task *tctx = task->io_uring; |
| 1704 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1705 | if (likely(task == current)) { |
| 1706 | tctx->cached_refs += nr; |
| 1707 | } else { |
| 1708 | percpu_counter_sub(&tctx->inflight, nr); |
| 1709 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1710 | wake_up(&tctx->wait); |
| 1711 | put_task_struct_many(task, nr); |
| 1712 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1715 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1716 | { |
| 1717 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1718 | |
| 1719 | percpu_counter_add(&tctx->inflight, refill); |
| 1720 | refcount_add(refill, ¤t->usage); |
| 1721 | tctx->cached_refs += refill; |
| 1722 | } |
| 1723 | |
| 1724 | static inline void io_get_task_refs(int nr) |
| 1725 | { |
| 1726 | struct io_uring_task *tctx = current->io_uring; |
| 1727 | |
| 1728 | tctx->cached_refs -= nr; |
| 1729 | if (unlikely(tctx->cached_refs < 0)) |
| 1730 | io_task_refs_refill(tctx); |
| 1731 | } |
| 1732 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1733 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
| 1734 | long res, unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1735 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1736 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1737 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1738 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1739 | if (!ocqe) { |
| 1740 | /* |
| 1741 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1742 | * or cannot allocate an overflow entry, then we need to drop it |
| 1743 | * on the floor. |
| 1744 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1745 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1746 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1747 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1748 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1749 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1750 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1751 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1752 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1753 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1754 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1755 | ocqe->cqe.res = res; |
| 1756 | ocqe->cqe.flags = cflags; |
| 1757 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1758 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1761 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1762 | long res, unsigned int cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1763 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1764 | struct io_uring_cqe *cqe; |
| 1765 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1766 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | * If we can't get a cq entry, userspace overflowed the |
| 1770 | * submission (by quite a lot). Increment the overflow count in |
| 1771 | * the ring. |
| 1772 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1773 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1774 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1775 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1776 | WRITE_ONCE(cqe->res, res); |
| 1777 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1778 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1779 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1780 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1783 | /* not as hot to bloat with inlining */ |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1784 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1785 | long res, unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1786 | { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1787 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1790 | static void io_req_complete_post(struct io_kiocb *req, long res, |
| 1791 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1792 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1793 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1794 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1795 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1796 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1797 | /* |
| 1798 | * If we're the last reference to this request, add to our locked |
| 1799 | * free_list cache. |
| 1800 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1801 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1802 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1803 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1804 | io_disarm_next(req); |
| 1805 | if (req->link) { |
| 1806 | io_req_task_queue(req->link); |
| 1807 | req->link = NULL; |
| 1808 | } |
| 1809 | } |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1810 | io_dismantle_req(req); |
| 1811 | io_put_task(req->task, 1); |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1812 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1813 | ctx->locked_free_nr++; |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1814 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1815 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1816 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1817 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1818 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1821 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
| 1822 | { |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 1823 | return req->flags & IO_REQ_CLEAN_FLAGS; |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1824 | } |
| 1825 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1826 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1827 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1828 | { |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1829 | if (io_req_needs_clean(req)) |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1830 | io_clean_op(req); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1831 | req->result = res; |
| 1832 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1833 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1834 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1835 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1836 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1837 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1838 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1839 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1840 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1841 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1842 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1843 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1844 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1845 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1846 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1847 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1850 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
| 1851 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1852 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1853 | io_req_complete_post(req, res, 0); |
| 1854 | } |
| 1855 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 1856 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 1857 | { |
| 1858 | /* |
| 1859 | * We don't submit, fail them all, for that replace hardlinks with |
| 1860 | * normal links. Extra REQ_F_LINK is tolerated. |
| 1861 | */ |
| 1862 | req->flags &= ~REQ_F_HARDLINK; |
| 1863 | req->flags |= REQ_F_LINK; |
| 1864 | io_req_complete_failed(req, req->result); |
| 1865 | } |
| 1866 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1867 | /* |
| 1868 | * Don't initialise the fields below on every allocation, but do that in |
| 1869 | * advance and keep them valid across allocations. |
| 1870 | */ |
| 1871 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1872 | { |
| 1873 | req->ctx = ctx; |
| 1874 | req->link = NULL; |
| 1875 | req->async_data = NULL; |
| 1876 | /* not necessary, but safer to zero */ |
| 1877 | req->result = 0; |
| 1878 | } |
| 1879 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1880 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1881 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1882 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1883 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1884 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1885 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1886 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1889 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1890 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1891 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1892 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1893 | int nr; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1894 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1895 | /* |
| 1896 | * If we have more than a batch's worth of requests in our IRQ side |
| 1897 | * locked cache, grab the lock and move them over to our submission |
| 1898 | * side cache. |
| 1899 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1900 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1901 | io_flush_cached_locked_reqs(ctx, state); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1902 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1903 | nr = state->free_reqs; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1904 | while (!list_empty(&state->free_list)) { |
| 1905 | struct io_kiocb *req = list_first_entry(&state->free_list, |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1906 | struct io_kiocb, inflight_entry); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1907 | |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1908 | list_del(&req->inflight_entry); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1909 | state->reqs[nr++] = req; |
| 1910 | if (nr == ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1911 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1912 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1913 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1914 | state->free_reqs = nr; |
| 1915 | return nr != 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1918 | /* |
| 1919 | * A request might get retired back into the request caches even before opcode |
| 1920 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 1921 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 1922 | * and with extra caution to not get a request that is still worked on. |
| 1923 | */ |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1924 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1925 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1926 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1927 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1928 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
| 1929 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1930 | |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 1931 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1932 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1933 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
| 1934 | goto got_req; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1935 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1936 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1937 | state->reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1938 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1939 | /* |
| 1940 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1941 | * retry single alloc to be on the safe side. |
| 1942 | */ |
| 1943 | if (unlikely(ret <= 0)) { |
| 1944 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1945 | if (!state->reqs[0]) |
| 1946 | return NULL; |
| 1947 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1948 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1949 | |
| 1950 | for (i = 0; i < ret; i++) |
| 1951 | io_preinit_req(state->reqs[i], ctx); |
| 1952 | state->free_reqs = ret; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1953 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1954 | state->free_reqs--; |
| 1955 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1958 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1959 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1960 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1961 | fput(file); |
| 1962 | } |
| 1963 | |
Pavel Begunkov | 6b63952 | 2021-09-08 16:40:50 +0100 | [diff] [blame] | 1964 | static inline void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1965 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1966 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1967 | |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 1968 | if (io_req_needs_clean(req)) |
| 1969 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1970 | if (!(flags & REQ_F_FIXED_FILE)) |
| 1971 | io_put_file(req->file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1972 | if (req->fixed_rsrc_refs) |
| 1973 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 99ebe4e | 2021-06-26 21:40:49 +0100 | [diff] [blame] | 1974 | if (req->async_data) { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1975 | kfree(req->async_data); |
Pavel Begunkov | 99ebe4e | 2021-06-26 21:40:49 +0100 | [diff] [blame] | 1976 | req->async_data = NULL; |
| 1977 | } |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1978 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1979 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1980 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1981 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1982 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1983 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1984 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1985 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1986 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1987 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1988 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 1989 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1990 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 1991 | |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1992 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1993 | } |
| 1994 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1995 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1996 | { |
| 1997 | struct io_kiocb *nxt = req->link; |
| 1998 | |
| 1999 | req->link = nxt->link; |
| 2000 | nxt->link = NULL; |
| 2001 | } |
| 2002 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2003 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 2004 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2005 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2006 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2007 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2008 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 2009 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2010 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2011 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2012 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2013 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2014 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2015 | list_del(&link->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2016 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2017 | -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2018 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2019 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2020 | } |
| 2021 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2022 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2023 | } |
| 2024 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2025 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2026 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2027 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2028 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2029 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2030 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2031 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2032 | long res = -ECANCELED; |
| 2033 | |
| 2034 | if (link->flags & REQ_F_FAIL) |
| 2035 | res = link->result; |
| 2036 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2037 | nxt = link->link; |
| 2038 | link->link = NULL; |
| 2039 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2040 | trace_io_uring_fail_link(req, link); |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2041 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2042 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2043 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2044 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2045 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2046 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2047 | static bool io_disarm_next(struct io_kiocb *req) |
| 2048 | __must_hold(&req->ctx->completion_lock) |
| 2049 | { |
| 2050 | bool posted = false; |
| 2051 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2052 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2053 | struct io_kiocb *link = req->link; |
| 2054 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2055 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2056 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2057 | io_remove_next_linked(req); |
| 2058 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2059 | -ECANCELED, 0); |
| 2060 | io_put_req_deferred(link); |
| 2061 | posted = true; |
| 2062 | } |
| 2063 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2064 | struct io_ring_ctx *ctx = req->ctx; |
| 2065 | |
| 2066 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2067 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2068 | spin_unlock_irq(&ctx->timeout_lock); |
| 2069 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2070 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2071 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2072 | posted |= (req->link != NULL); |
| 2073 | io_fail_links(req); |
| 2074 | } |
| 2075 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2076 | } |
| 2077 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2078 | static void __io_req_find_next_prep(struct io_kiocb *req) |
| 2079 | { |
| 2080 | struct io_ring_ctx *ctx = req->ctx; |
| 2081 | bool posted; |
| 2082 | |
| 2083 | spin_lock(&ctx->completion_lock); |
| 2084 | posted = io_disarm_next(req); |
| 2085 | if (posted) |
| 2086 | io_commit_cqring(req->ctx); |
| 2087 | spin_unlock(&ctx->completion_lock); |
| 2088 | if (posted) |
| 2089 | io_cqring_ev_posted(ctx); |
| 2090 | } |
| 2091 | |
| 2092 | 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] | 2093 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2094 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2095 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2096 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
| 2097 | return NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2098 | /* |
| 2099 | * If LINK is set, we have dependent requests in this chain. If we |
| 2100 | * didn't fail this request, queue the first one up, moving any other |
| 2101 | * dependencies to the next request. In case of failure, fail the rest |
| 2102 | * of the chain. |
| 2103 | */ |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2104 | if (unlikely(req->flags & IO_DISARM_MASK)) |
| 2105 | __io_req_find_next_prep(req); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2106 | nxt = req->link; |
| 2107 | req->link = NULL; |
| 2108 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2109 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2110 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2111 | 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] | 2112 | { |
| 2113 | if (!ctx) |
| 2114 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2115 | if (*locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2116 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2117 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2118 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2119 | } |
| 2120 | percpu_ref_put(&ctx->refs); |
| 2121 | } |
| 2122 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2123 | static void tctx_task_work(struct callback_head *cb) |
| 2124 | { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2125 | bool locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2126 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2127 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2128 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2129 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2130 | while (1) { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2131 | struct io_wq_work_node *node; |
| 2132 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2133 | if (!tctx->task_list.first && locked) |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2134 | io_submit_flush_completions(ctx); |
| 2135 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2136 | spin_lock_irq(&tctx->task_lock); |
Pavel Begunkov | c6538be | 2021-06-17 18:14:08 +0100 | [diff] [blame] | 2137 | node = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2138 | INIT_WQ_LIST(&tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2139 | if (!node) |
| 2140 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2141 | spin_unlock_irq(&tctx->task_lock); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2142 | if (!node) |
| 2143 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2144 | |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2145 | do { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2146 | struct io_wq_work_node *next = node->next; |
| 2147 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2148 | io_task_work.node); |
| 2149 | |
| 2150 | if (req->ctx != ctx) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2151 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2152 | ctx = req->ctx; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2153 | /* if not contended, grab and improve batching */ |
| 2154 | locked = mutex_trylock(&ctx->uring_lock); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2155 | percpu_ref_get(&ctx->refs); |
| 2156 | } |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2157 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2158 | node = next; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2159 | } while (node); |
| 2160 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2161 | cond_resched(); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2162 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2163 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2164 | ctx_flush_and_put(ctx, &locked); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2167 | static void io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2168 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2169 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2170 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2171 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2172 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2173 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2174 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2175 | |
| 2176 | WARN_ON_ONCE(!tctx); |
| 2177 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2178 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2179 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2180 | running = tctx->task_running; |
| 2181 | if (!running) |
| 2182 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2183 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2186 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2187 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2188 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2189 | /* |
| 2190 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2191 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2192 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2193 | * will do the job. |
| 2194 | */ |
| 2195 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | d97ec62 | 2021-09-08 16:40:53 +0100 | [diff] [blame] | 2196 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
| 2197 | if (notify == TWA_NONE) |
| 2198 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2199 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2200 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2201 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2202 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2203 | tctx->task_running = false; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2204 | node = tctx->task_list.first; |
| 2205 | INIT_WQ_LIST(&tctx->task_list); |
| 2206 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2207 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2208 | while (node) { |
| 2209 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2210 | node = node->next; |
| 2211 | if (llist_add(&req->io_task_work.fallback_node, |
| 2212 | &req->ctx->fallback_llist)) |
| 2213 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2214 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2217 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2218 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2219 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2220 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2221 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2222 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2223 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2224 | } |
| 2225 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2226 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2227 | { |
| 2228 | struct io_ring_ctx *ctx = req->ctx; |
| 2229 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2230 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2231 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2232 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2233 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2234 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2235 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2236 | } |
| 2237 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2238 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2239 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2240 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2241 | req->io_task_work.func = io_req_task_cancel; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2242 | io_req_task_work_add(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2245 | static void io_req_task_queue(struct io_kiocb *req) |
| 2246 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2247 | req->io_task_work.func = io_req_task_submit; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2248 | io_req_task_work_add(req); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2251 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2252 | { |
| 2253 | req->io_task_work.func = io_queue_async_work; |
| 2254 | io_req_task_work_add(req); |
| 2255 | } |
| 2256 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2257 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2258 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2259 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2260 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2261 | if (nxt) |
| 2262 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2263 | } |
| 2264 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2265 | static void io_free_req(struct io_kiocb *req) |
| 2266 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2267 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2268 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2269 | } |
| 2270 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2271 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2272 | { |
| 2273 | io_free_req(req); |
| 2274 | } |
| 2275 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2276 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2277 | struct task_struct *task; |
| 2278 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2279 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2280 | }; |
| 2281 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2282 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2283 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2284 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2285 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2286 | rb->task = NULL; |
| 2287 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2288 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2289 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2290 | struct req_batch *rb) |
| 2291 | { |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2292 | if (rb->ctx_refs) |
| 2293 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 2294 | if (rb->task) |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 2295 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2296 | } |
| 2297 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2298 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2299 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2300 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2301 | io_queue_next(req); |
Pavel Begunkov | 9667065 | 2021-03-19 17:22:32 +0000 | [diff] [blame] | 2302 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2303 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2304 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2305 | if (rb->task) |
| 2306 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2307 | rb->task = req->task; |
| 2308 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2309 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2310 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2311 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2312 | |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2313 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2314 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2315 | else |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2316 | list_add(&req->inflight_entry, &state->free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2317 | } |
| 2318 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2319 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2320 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2321 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2322 | struct io_submit_state *state = &ctx->submit_state; |
| 2323 | int i, nr = state->compl_nr; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2324 | struct req_batch rb; |
| 2325 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2326 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2327 | for (i = 0; i < nr; i++) { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2328 | struct io_kiocb *req = state->compl_reqs[i]; |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2329 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2330 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
| 2331 | req->compl.cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2332 | } |
| 2333 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2334 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2335 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2336 | |
| 2337 | io_init_req_batch(&rb); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2338 | for (i = 0; i < nr; i++) { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2339 | struct io_kiocb *req = state->compl_reqs[i]; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2340 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2341 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2342 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | io_req_free_batch_finish(ctx, &rb); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2346 | state->compl_nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2347 | } |
| 2348 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2349 | /* |
| 2350 | * Drop reference to request, return next in chain (if there is one) if this |
| 2351 | * was the last reference to this request. |
| 2352 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2353 | 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] | 2354 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2355 | struct io_kiocb *nxt = NULL; |
| 2356 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2357 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2358 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2359 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2360 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2361 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2364 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2365 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2366 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2367 | io_free_req(req); |
| 2368 | } |
| 2369 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2370 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2371 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2372 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2373 | req->io_task_work.func = io_free_req_work; |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2374 | io_req_task_work_add(req); |
| 2375 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2376 | } |
| 2377 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2378 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2379 | { |
| 2380 | /* See comment at the top of this file */ |
| 2381 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2382 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2383 | } |
| 2384 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2385 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2386 | { |
| 2387 | struct io_rings *rings = ctx->rings; |
| 2388 | |
| 2389 | /* make sure SQ entry isn't read before tail */ |
| 2390 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2391 | } |
| 2392 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2393 | 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] | 2394 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2395 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2396 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2397 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2398 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2399 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2400 | kfree(kbuf); |
| 2401 | return cflags; |
| 2402 | } |
| 2403 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2404 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2405 | { |
| 2406 | struct io_buffer *kbuf; |
| 2407 | |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2408 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 2409 | return 0; |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2410 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2411 | return io_put_kbuf(req, kbuf); |
| 2412 | } |
| 2413 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2414 | static inline bool io_run_task_work(void) |
| 2415 | { |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2416 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2417 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2418 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2419 | return true; |
| 2420 | } |
| 2421 | |
| 2422 | return false; |
| 2423 | } |
| 2424 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2425 | /* |
| 2426 | * Find and free completed poll iocbs |
| 2427 | */ |
| 2428 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2429 | struct list_head *done) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2430 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2431 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2432 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2433 | |
| 2434 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2435 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2436 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2437 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2438 | while (!list_empty(done)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2439 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2440 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2441 | |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2442 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
| 2443 | io_put_rw_kbuf(req)); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2444 | (*nr_events)++; |
| 2445 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2446 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2447 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2448 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2449 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2450 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2451 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2452 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2455 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2456 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2457 | { |
| 2458 | struct io_kiocb *req, *tmp; |
Christoph Hellwig | d729cf9 | 2021-10-12 13:12:20 +0200 | [diff] [blame] | 2459 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2460 | DEFINE_IO_COMP_BATCH(iob); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2461 | LIST_HEAD(done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2462 | |
| 2463 | /* |
| 2464 | * Only spin for completions if we don't have multiple devices hanging |
| 2465 | * off our complete list, and we're under the requested amount. |
| 2466 | */ |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2467 | if (ctx->poll_multi_queue || *nr_events >= min) |
| 2468 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2469 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2470 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2471 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2472 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2473 | |
| 2474 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2475 | * Move completed and retryable entries to our local lists. |
| 2476 | * If we find a request that requires polling, break out |
| 2477 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2478 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2479 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2480 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2481 | continue; |
| 2482 | } |
| 2483 | if (!list_empty(&done)) |
| 2484 | break; |
| 2485 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2486 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2487 | if (unlikely(ret < 0)) |
| 2488 | return ret; |
| 2489 | else if (ret) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2490 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2491 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2492 | /* iopoll may have completed current req */ |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2493 | if (!rq_list_empty(iob.req_list) || |
| 2494 | READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2495 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2496 | } |
| 2497 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2498 | if (!rq_list_empty(iob.req_list)) |
| 2499 | iob.complete(&iob); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2500 | if (!list_empty(&done)) |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2501 | io_iopoll_complete(ctx, nr_events, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2502 | |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2503 | return 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2507 | * We can't just wait for polled events to come to us, we have to actively |
| 2508 | * find and complete them. |
| 2509 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2510 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2511 | { |
| 2512 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2513 | return; |
| 2514 | |
| 2515 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2516 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2517 | unsigned int nr_events = 0; |
| 2518 | |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2519 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2520 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2521 | /* let it sleep and repeat later if can't complete a request */ |
| 2522 | if (nr_events == 0) |
| 2523 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2524 | /* |
| 2525 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2526 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2527 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2528 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2529 | if (need_resched()) { |
| 2530 | mutex_unlock(&ctx->uring_lock); |
| 2531 | cond_resched(); |
| 2532 | mutex_lock(&ctx->uring_lock); |
| 2533 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2534 | } |
| 2535 | mutex_unlock(&ctx->uring_lock); |
| 2536 | } |
| 2537 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2538 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2539 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2540 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2541 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2542 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2543 | /* |
| 2544 | * We disallow the app entering submit/complete with polling, but we |
| 2545 | * still need to lock the ring to prevent racing with polled issue |
| 2546 | * that got punted to a workqueue. |
| 2547 | */ |
| 2548 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2549 | /* |
| 2550 | * Don't enter poll loop if we already have events pending. |
| 2551 | * If we do, we can potentially be spinning for commands that |
| 2552 | * already triggered a CQE (eg in error). |
| 2553 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2554 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2555 | __io_cqring_overflow_flush(ctx, false); |
| 2556 | if (io_cqring_events(ctx)) |
| 2557 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2558 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2559 | /* |
| 2560 | * If a submit got punted to a workqueue, we can have the |
| 2561 | * application entering polling for a command before it gets |
| 2562 | * issued. That app will hold the uring_lock for the duration |
| 2563 | * of the poll right here, so we need to take a breather every |
| 2564 | * now and then to ensure that the issue has a chance to add |
| 2565 | * the poll to the issued list. Otherwise we can spin here |
| 2566 | * forever, while the workqueue is stuck trying to acquire the |
| 2567 | * very same mutex. |
| 2568 | */ |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2569 | if (list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2570 | u32 tail = ctx->cached_cq_tail; |
| 2571 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2572 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2573 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2574 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2575 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2576 | /* some requests don't go through iopoll_list */ |
| 2577 | if (tail != ctx->cached_cq_tail || |
| 2578 | list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2579 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2580 | } |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2581 | ret = io_do_iopoll(ctx, &nr_events, min); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2582 | } while (!ret && nr_events < min && !need_resched()); |
| 2583 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2584 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2585 | return ret; |
| 2586 | } |
| 2587 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2588 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2589 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2590 | /* |
| 2591 | * Tell lockdep we inherited freeze protection from submission |
| 2592 | * thread. |
| 2593 | */ |
| 2594 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2595 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2596 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2597 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2598 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2602 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2603 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2604 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2605 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2606 | |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2607 | if (!rw) |
| 2608 | return !io_req_prep_async(req); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 2609 | iov_iter_restore(&rw->iter, &rw->iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2610 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2611 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2612 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2613 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2614 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2615 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2616 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2617 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2618 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2619 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2620 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2621 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2622 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2623 | /* |
| 2624 | * If ref is dying, we might be running poll reap from the exit work. |
| 2625 | * Don't attempt to reissue from that path, just let it fail with |
| 2626 | * -EAGAIN. |
| 2627 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2628 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2629 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2630 | /* |
| 2631 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2632 | * not in the original thread group (or in task context). |
| 2633 | */ |
| 2634 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2635 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2636 | return true; |
| 2637 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2638 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2639 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2640 | { |
| 2641 | return false; |
| 2642 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2643 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2644 | { |
| 2645 | return false; |
| 2646 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2647 | #endif |
| 2648 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2649 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2650 | { |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2651 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2652 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2653 | if (res != req->result) { |
| 2654 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2655 | io_rw_should_reissue(req)) { |
| 2656 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2657 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2658 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2659 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2660 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2661 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2662 | return false; |
| 2663 | } |
| 2664 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2665 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2666 | { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2667 | unsigned int cflags = io_put_rw_kbuf(req); |
| 2668 | long res = req->result; |
| 2669 | |
| 2670 | if (*locked) { |
| 2671 | struct io_ring_ctx *ctx = req->ctx; |
| 2672 | struct io_submit_state *state = &ctx->submit_state; |
| 2673 | |
| 2674 | io_req_complete_state(req, res, cflags); |
| 2675 | state->compl_reqs[state->compl_nr++] = req; |
| 2676 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) |
| 2677 | io_submit_flush_completions(ctx); |
| 2678 | } else { |
| 2679 | io_req_complete_post(req, res, cflags); |
| 2680 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2684 | unsigned int issue_flags) |
| 2685 | { |
| 2686 | if (__io_complete_rw_common(req, res)) |
| 2687 | return; |
Pavel Begunkov | 6363785 | 2021-09-02 00:38:22 +0100 | [diff] [blame] | 2688 | __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] | 2689 | } |
| 2690 | |
| 2691 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2692 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2693 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2694 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2695 | if (__io_complete_rw_common(req, res)) |
| 2696 | return; |
| 2697 | req->result = res; |
| 2698 | req->io_task_work.func = io_req_task_complete; |
| 2699 | io_req_task_work_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2702 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2703 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2704 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2705 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2706 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2707 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2708 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2709 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2710 | req->flags |= REQ_F_REISSUE; |
| 2711 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2712 | } |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2713 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2714 | |
| 2715 | WRITE_ONCE(req->result, res); |
Jens Axboe | b9b0e0d | 2021-02-23 08:18:36 -0700 | [diff] [blame] | 2716 | /* order with io_iopoll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2717 | smp_wmb(); |
| 2718 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2723 | * 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] | 2724 | * 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] | 2725 | * accessing the kiocb cookie. |
| 2726 | */ |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2727 | static void io_iopoll_req_issued(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2728 | { |
| 2729 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2730 | const bool in_async = io_wq_current_is_worker(); |
| 2731 | |
| 2732 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 2733 | if (unlikely(in_async)) |
| 2734 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2735 | |
| 2736 | /* |
| 2737 | * Track whether we have multiple files in our lists. This will impact |
| 2738 | * how we do polling eventually, not spinning if we're on potentially |
| 2739 | * different devices. |
| 2740 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2741 | if (list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2742 | ctx->poll_multi_queue = false; |
| 2743 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2744 | struct io_kiocb *list_req; |
| 2745 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2746 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2747 | inflight_entry); |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2748 | |
Christoph Hellwig | 30da1b4 | 2021-10-12 13:12:14 +0200 | [diff] [blame] | 2749 | if (list_req->file != req->file) |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2750 | ctx->poll_multi_queue = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * For fast devices, IO may have already completed. If it has, add |
| 2755 | * it to the front so we find it first. |
| 2756 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2757 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2758 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2759 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2760 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2761 | |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2762 | if (unlikely(in_async)) { |
| 2763 | /* |
| 2764 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2765 | * in sq thread task context or in io worker task context. If |
| 2766 | * current task context is sq thread, we don't need to check |
| 2767 | * whether should wake up sq thread. |
| 2768 | */ |
| 2769 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2770 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2771 | wake_up(&ctx->sq_data->wait); |
| 2772 | |
| 2773 | mutex_unlock(&ctx->uring_lock); |
| 2774 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2777 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2778 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2779 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2780 | } |
| 2781 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2782 | /* |
| 2783 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2784 | * any file. For now, just ensure that anything potentially problematic is done |
| 2785 | * inline. |
| 2786 | */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2787 | static bool __io_file_supports_nowait(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2788 | { |
| 2789 | umode_t mode = file_inode(file)->i_mode; |
| 2790 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2791 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2792 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2793 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2794 | return true; |
| 2795 | return false; |
| 2796 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2797 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2798 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2799 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2800 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2801 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2802 | file->f_op != &io_uring_fops) |
| 2803 | return true; |
| 2804 | return false; |
| 2805 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2806 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2807 | /* any ->read/write should understand O_NONBLOCK */ |
| 2808 | if (file->f_flags & O_NONBLOCK) |
| 2809 | return true; |
| 2810 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2811 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2812 | return false; |
| 2813 | |
| 2814 | if (rw == READ) |
| 2815 | return file->f_op->read_iter != NULL; |
| 2816 | |
| 2817 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2820 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2821 | { |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2822 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2823 | return true; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2824 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2825 | return true; |
| 2826 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2827 | return __io_file_supports_nowait(req->file, rw); |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2830 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2831 | int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2832 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2833 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2834 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2835 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2836 | unsigned ioprio; |
| 2837 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2838 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 2839 | 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] | 2840 | req->flags |= REQ_F_ISREG; |
| 2841 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2842 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2843 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2844 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2845 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2846 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2847 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2848 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2849 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2850 | if (unlikely(ret)) |
| 2851 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2852 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2853 | /* |
| 2854 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 2855 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 2856 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 2857 | */ |
| 2858 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 2859 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2860 | req->flags |= REQ_F_NOWAIT; |
| 2861 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2862 | ioprio = READ_ONCE(sqe->ioprio); |
| 2863 | if (ioprio) { |
| 2864 | ret = ioprio_check_cap(ioprio); |
| 2865 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2866 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2867 | |
| 2868 | kiocb->ki_ioprio = ioprio; |
| 2869 | } else |
| 2870 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2871 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2872 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2873 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2874 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2875 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2876 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 2877 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2878 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2879 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2880 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2881 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2882 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2883 | kiocb->ki_complete = io_complete_rw; |
| 2884 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2885 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2886 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2887 | req->opcode == IORING_OP_WRITE_FIXED) { |
| 2888 | req->imu = NULL; |
| 2889 | io_req_set_rsrc_node(req); |
| 2890 | } |
| 2891 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2892 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2893 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2894 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2895 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2899 | { |
| 2900 | switch (ret) { |
| 2901 | case -EIOCBQUEUED: |
| 2902 | break; |
| 2903 | case -ERESTARTSYS: |
| 2904 | case -ERESTARTNOINTR: |
| 2905 | case -ERESTARTNOHAND: |
| 2906 | case -ERESTART_RESTARTBLOCK: |
| 2907 | /* |
| 2908 | * We can't just restart the syscall, since previously |
| 2909 | * submitted sqes may already be in progress. Just fail this |
| 2910 | * IO with EINTR. |
| 2911 | */ |
| 2912 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2913 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2914 | default: |
| 2915 | kiocb->ki_complete(kiocb, ret, 0); |
| 2916 | } |
| 2917 | } |
| 2918 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2919 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2920 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2921 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2922 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2923 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2924 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2925 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2926 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2927 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2928 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2929 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2930 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2931 | } |
| 2932 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2933 | if (req->flags & REQ_F_CUR_POS) |
| 2934 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2935 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2936 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2937 | else |
| 2938 | io_rw_done(kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2939 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2940 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2941 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 2942 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2943 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2944 | } else { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2945 | unsigned int cflags = io_put_rw_kbuf(req); |
| 2946 | struct io_ring_ctx *ctx = req->ctx; |
| 2947 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2948 | req_set_fail(req); |
Hao Xu | 14cfbb7 | 2021-10-14 22:04:00 +0800 | [diff] [blame] | 2949 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2950 | mutex_lock(&ctx->uring_lock); |
| 2951 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2952 | mutex_unlock(&ctx->uring_lock); |
| 2953 | } else { |
| 2954 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2955 | } |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2956 | } |
| 2957 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2958 | } |
| 2959 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2960 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 2961 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2962 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2963 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2964 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2965 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2966 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2967 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2968 | return -EFAULT; |
| 2969 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 2970 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2971 | return -EFAULT; |
| 2972 | |
| 2973 | /* |
| 2974 | * May not be a start of buffer, set size appropriately |
| 2975 | * and advance us to the beginning. |
| 2976 | */ |
| 2977 | offset = buf_addr - imu->ubuf; |
| 2978 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2979 | |
| 2980 | if (offset) { |
| 2981 | /* |
| 2982 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2983 | * using the latter parts of a big fixed buffer - it iterates |
| 2984 | * over each segment manually. We can cheat a bit here, because |
| 2985 | * we know that: |
| 2986 | * |
| 2987 | * 1) it's a BVEC iter, we set it up |
| 2988 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2989 | * first and last bvec |
| 2990 | * |
| 2991 | * So just find our index, and adjust the iterator afterwards. |
| 2992 | * If the offset is within the first bvec (or the whole first |
| 2993 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2994 | * since we can just skip the first segment, which may not |
| 2995 | * be PAGE_SIZE aligned. |
| 2996 | */ |
| 2997 | const struct bio_vec *bvec = imu->bvec; |
| 2998 | |
| 2999 | if (offset <= bvec->bv_len) { |
| 3000 | iov_iter_advance(iter, offset); |
| 3001 | } else { |
| 3002 | unsigned long seg_skip; |
| 3003 | |
| 3004 | /* skip first vec */ |
| 3005 | offset -= bvec->bv_len; |
| 3006 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3007 | |
| 3008 | iter->bvec = bvec + seg_skip; |
| 3009 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3010 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3011 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3015 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3018 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 3019 | { |
| 3020 | struct io_ring_ctx *ctx = req->ctx; |
| 3021 | struct io_mapped_ubuf *imu = req->imu; |
| 3022 | u16 index, buf_index = req->buf_index; |
| 3023 | |
| 3024 | if (likely(!imu)) { |
| 3025 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3026 | return -EFAULT; |
| 3027 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3028 | imu = READ_ONCE(ctx->user_bufs[index]); |
| 3029 | req->imu = imu; |
| 3030 | } |
| 3031 | return __io_import_fixed(req, rw, iter, imu); |
| 3032 | } |
| 3033 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3034 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3035 | { |
| 3036 | if (needs_lock) |
| 3037 | mutex_unlock(&ctx->uring_lock); |
| 3038 | } |
| 3039 | |
| 3040 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3041 | { |
| 3042 | /* |
| 3043 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3044 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3045 | * The only exception is when we've detached the request and issue it |
| 3046 | * from an async worker thread, grab the lock for that case. |
| 3047 | */ |
| 3048 | if (needs_lock) |
| 3049 | mutex_lock(&ctx->uring_lock); |
| 3050 | } |
| 3051 | |
| 3052 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3053 | int bgid, struct io_buffer *kbuf, |
| 3054 | bool needs_lock) |
| 3055 | { |
| 3056 | struct io_buffer *head; |
| 3057 | |
| 3058 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3059 | return kbuf; |
| 3060 | |
| 3061 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3062 | |
| 3063 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3064 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3065 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3066 | if (head) { |
| 3067 | if (!list_empty(&head->list)) { |
| 3068 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3069 | list); |
| 3070 | list_del(&kbuf->list); |
| 3071 | } else { |
| 3072 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3073 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3074 | } |
| 3075 | if (*len > kbuf->len) |
| 3076 | *len = kbuf->len; |
| 3077 | } else { |
| 3078 | kbuf = ERR_PTR(-ENOBUFS); |
| 3079 | } |
| 3080 | |
| 3081 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3082 | |
| 3083 | return kbuf; |
| 3084 | } |
| 3085 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3086 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3087 | bool needs_lock) |
| 3088 | { |
| 3089 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3090 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3091 | |
| 3092 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3093 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3094 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3095 | if (IS_ERR(kbuf)) |
| 3096 | return kbuf; |
| 3097 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3098 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3099 | return u64_to_user_ptr(kbuf->addr); |
| 3100 | } |
| 3101 | |
| 3102 | #ifdef CONFIG_COMPAT |
| 3103 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3104 | bool needs_lock) |
| 3105 | { |
| 3106 | struct compat_iovec __user *uiov; |
| 3107 | compat_ssize_t clen; |
| 3108 | void __user *buf; |
| 3109 | ssize_t len; |
| 3110 | |
| 3111 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3112 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3113 | return -EFAULT; |
| 3114 | if (__get_user(clen, &uiov->iov_len)) |
| 3115 | return -EFAULT; |
| 3116 | if (clen < 0) |
| 3117 | return -EINVAL; |
| 3118 | |
| 3119 | len = clen; |
| 3120 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3121 | if (IS_ERR(buf)) |
| 3122 | return PTR_ERR(buf); |
| 3123 | iov[0].iov_base = buf; |
| 3124 | iov[0].iov_len = (compat_size_t) len; |
| 3125 | return 0; |
| 3126 | } |
| 3127 | #endif |
| 3128 | |
| 3129 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3130 | bool needs_lock) |
| 3131 | { |
| 3132 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3133 | void __user *buf; |
| 3134 | ssize_t len; |
| 3135 | |
| 3136 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3137 | return -EFAULT; |
| 3138 | |
| 3139 | len = iov[0].iov_len; |
| 3140 | if (len < 0) |
| 3141 | return -EINVAL; |
| 3142 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3143 | if (IS_ERR(buf)) |
| 3144 | return PTR_ERR(buf); |
| 3145 | iov[0].iov_base = buf; |
| 3146 | iov[0].iov_len = len; |
| 3147 | return 0; |
| 3148 | } |
| 3149 | |
| 3150 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3151 | bool needs_lock) |
| 3152 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3153 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3154 | struct io_buffer *kbuf; |
| 3155 | |
| 3156 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3157 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3158 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3159 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3160 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3161 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3162 | return -EINVAL; |
| 3163 | |
| 3164 | #ifdef CONFIG_COMPAT |
| 3165 | if (req->ctx->compat) |
| 3166 | return io_compat_import(req, iov, needs_lock); |
| 3167 | #endif |
| 3168 | |
| 3169 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3170 | } |
| 3171 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3172 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3173 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3174 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3175 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3176 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3177 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3178 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3179 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3180 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3181 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3182 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3183 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3184 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3185 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3186 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3187 | return -EINVAL; |
| 3188 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3189 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3190 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3191 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3192 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3193 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3194 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3197 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3198 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3199 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3202 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3203 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3204 | if (!ret) |
| 3205 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3206 | *iovec = NULL; |
| 3207 | return ret; |
| 3208 | } |
| 3209 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3210 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3211 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3214 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3215 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3216 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3217 | } |
| 3218 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3219 | /* |
| 3220 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3221 | * by looping over ->read() or ->write() manually. |
| 3222 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3223 | 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] | 3224 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3225 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3226 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3227 | ssize_t ret = 0; |
| 3228 | |
| 3229 | /* |
| 3230 | * Don't support polled IO through this interface, and we can't |
| 3231 | * support non-blocking either. For the latter, this just causes |
| 3232 | * the kiocb to be handled from an async context. |
| 3233 | */ |
| 3234 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3235 | return -EOPNOTSUPP; |
| 3236 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3237 | return -EAGAIN; |
| 3238 | |
| 3239 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3240 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3241 | ssize_t nr; |
| 3242 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3243 | if (!iov_iter_is_bvec(iter)) { |
| 3244 | iovec = iov_iter_iovec(iter); |
| 3245 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3246 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3247 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3248 | } |
| 3249 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3250 | if (rw == READ) { |
| 3251 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3252 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3253 | } else { |
| 3254 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3255 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | if (nr < 0) { |
| 3259 | if (!ret) |
| 3260 | ret = nr; |
| 3261 | break; |
| 3262 | } |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3263 | if (!iov_iter_is_bvec(iter)) { |
| 3264 | iov_iter_advance(iter, nr); |
| 3265 | } else { |
| 3266 | req->rw.len -= nr; |
| 3267 | req->rw.addr += nr; |
| 3268 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3269 | ret += nr; |
| 3270 | if (nr != iovec.iov_len) |
| 3271 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | return ret; |
| 3275 | } |
| 3276 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3277 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3278 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3279 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3280 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3281 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3282 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3283 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3284 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3285 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3286 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3287 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3288 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3289 | unsigned iov_off = 0; |
| 3290 | |
| 3291 | rw->iter.iov = rw->fast_iov; |
| 3292 | if (iter->iov != fast_iov) { |
| 3293 | iov_off = iter->iov - fast_iov; |
| 3294 | rw->iter.iov += iov_off; |
| 3295 | } |
| 3296 | if (rw->fast_iov != fast_iov) |
| 3297 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3298 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3299 | } else { |
| 3300 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
Hao Xu | 8d4af68 | 2021-09-22 18:15:22 +0800 | [diff] [blame] | 3304 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3305 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3306 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3307 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3308 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3309 | } |
| 3310 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3311 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3312 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3313 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3314 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3315 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3316 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3317 | if (!req->async_data) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3318 | struct io_async_rw *iorw; |
| 3319 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3320 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3321 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3322 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3323 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3324 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3325 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3326 | iorw = req->async_data; |
| 3327 | /* we've copied and mapped the iter, ensure state is saved */ |
| 3328 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3329 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3330 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3331 | } |
| 3332 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3333 | 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] | 3334 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3335 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3336 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3337 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3338 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3339 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3340 | if (unlikely(ret < 0)) |
| 3341 | return ret; |
| 3342 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3343 | iorw->bytes_done = 0; |
| 3344 | iorw->free_iovec = iov; |
| 3345 | if (iov) |
| 3346 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3347 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3348 | return 0; |
| 3349 | } |
| 3350 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3351 | 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] | 3352 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3353 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3354 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3355 | return io_prep_rw(req, sqe, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3358 | /* |
| 3359 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3360 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3361 | * This gets called when the page is unlocked, and we generally expect that to |
| 3362 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3363 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3364 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3365 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3366 | * slow path. |
| 3367 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3368 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3369 | int sync, void *arg) |
| 3370 | { |
| 3371 | struct wait_page_queue *wpq; |
| 3372 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3373 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3374 | |
| 3375 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3376 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3377 | if (!wake_page_match(wpq, key)) |
| 3378 | return 0; |
| 3379 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3380 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3381 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3382 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3383 | return 1; |
| 3384 | } |
| 3385 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3386 | /* |
| 3387 | * This controls whether a given IO request should be armed for async page |
| 3388 | * based retry. If we return false here, the request is handed to the async |
| 3389 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3390 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3391 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3392 | * will register a callback when the page is unlocked at IO completion. Through |
| 3393 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3394 | * That retry will attempt the buffered read again. The retry will generally |
| 3395 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3396 | * async worker threads for a blocking retry. |
| 3397 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3398 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3399 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3400 | struct io_async_rw *rw = req->async_data; |
| 3401 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3402 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3403 | |
| 3404 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3405 | if (req->flags & REQ_F_NOWAIT) |
| 3406 | return false; |
| 3407 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3408 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3409 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3410 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3411 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3412 | /* |
| 3413 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3414 | * support callback based unlocks |
| 3415 | */ |
| 3416 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3417 | return false; |
| 3418 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3419 | wait->wait.func = io_async_buf_func; |
| 3420 | wait->wait.private = req; |
| 3421 | wait->wait.flags = 0; |
| 3422 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3423 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3424 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3425 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3426 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3427 | } |
| 3428 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3429 | 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] | 3430 | { |
| 3431 | if (req->file->f_op->read_iter) |
| 3432 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3433 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3434 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3435 | else |
| 3436 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3437 | } |
| 3438 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3439 | static bool need_read_all(struct io_kiocb *req) |
| 3440 | { |
| 3441 | return req->flags & REQ_F_ISREG || |
| 3442 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3443 | } |
| 3444 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3445 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3446 | { |
| 3447 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3448 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3449 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3450 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3451 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3452 | struct iov_iter_state __state, *state; |
| 3453 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3454 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3455 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3456 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3457 | state = &rw->iter_state; |
| 3458 | /* |
| 3459 | * We come here from an earlier attempt, restore our state to |
| 3460 | * match in case it doesn't. It's cheap enough that we don't |
| 3461 | * need to make this conditional. |
| 3462 | */ |
| 3463 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3464 | iovec = NULL; |
| 3465 | } else { |
| 3466 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3467 | if (ret < 0) |
| 3468 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3469 | state = &__state; |
| 3470 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3471 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3472 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3473 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3474 | /* Ensure we clear previously set non-block flag */ |
| 3475 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3476 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3477 | else |
| 3478 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3479 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3480 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3481 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3482 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3483 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3484 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3485 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3486 | 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] | 3487 | if (unlikely(ret)) { |
| 3488 | kfree(iovec); |
| 3489 | return ret; |
| 3490 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3491 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3492 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3493 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3494 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3495 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3496 | /* IOPOLL retry should happen for io-wq threads */ |
| 3497 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3498 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3499 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3500 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3501 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3502 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3503 | } else if (ret == -EIOCBQUEUED) { |
| 3504 | goto out_free; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3505 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3506 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3507 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3508 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3509 | } |
| 3510 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3511 | /* |
| 3512 | * Don't depend on the iter state matching what was consumed, or being |
| 3513 | * untouched in case of error. Restore it and we'll advance it |
| 3514 | * manually if we need to. |
| 3515 | */ |
| 3516 | iov_iter_restore(iter, state); |
| 3517 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3518 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3519 | if (ret2) |
| 3520 | return ret2; |
| 3521 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3522 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3523 | rw = req->async_data; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3524 | /* |
| 3525 | * Now use our persistent iterator and state, if we aren't already. |
| 3526 | * We've restored and mapped the iter to match. |
| 3527 | */ |
| 3528 | if (iter != &rw->iter) { |
| 3529 | iter = &rw->iter; |
| 3530 | state = &rw->iter_state; |
| 3531 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3532 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3533 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3534 | /* |
| 3535 | * We end up here because of a partial read, either from |
| 3536 | * above or inside this loop. Advance the iter by the bytes |
| 3537 | * that were consumed. |
| 3538 | */ |
| 3539 | iov_iter_advance(iter, ret); |
| 3540 | if (!iov_iter_count(iter)) |
| 3541 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3542 | rw->bytes_done += ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3543 | iov_iter_save_state(iter, state); |
| 3544 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3545 | /* if we can retry, do so with the callbacks armed */ |
| 3546 | if (!io_rw_should_retry(req)) { |
| 3547 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3548 | return -EAGAIN; |
| 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3553 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3554 | * desired page gets unlocked. We can also get a partial read |
| 3555 | * here, and if we do, then just retry at the new offset. |
| 3556 | */ |
| 3557 | ret = io_iter_do_read(req, iter); |
| 3558 | if (ret == -EIOCBQUEUED) |
| 3559 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3560 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3561 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3562 | iov_iter_restore(iter, state); |
| 3563 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3564 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3565 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3566 | out_free: |
| 3567 | /* it's faster to check here then delegate to kfree */ |
| 3568 | if (iovec) |
| 3569 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3570 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3573 | 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] | 3574 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3575 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3576 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3577 | return io_prep_rw(req, sqe, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3580 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3581 | { |
| 3582 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3583 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3584 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3585 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3586 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3587 | struct iov_iter_state __state, *state; |
| 3588 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3589 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3590 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3591 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3592 | state = &rw->iter_state; |
| 3593 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3594 | iovec = NULL; |
| 3595 | } else { |
| 3596 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3597 | if (ret < 0) |
| 3598 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3599 | state = &__state; |
| 3600 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3601 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3602 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3603 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3604 | /* Ensure we clear previously set non-block flag */ |
| 3605 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3606 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3607 | else |
| 3608 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3609 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3610 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3611 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3612 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3613 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3614 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3615 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3616 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3617 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3618 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3619 | 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] | 3620 | if (unlikely(ret)) |
| 3621 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3622 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3623 | /* |
| 3624 | * Open-code file_start_write here to grab freeze protection, |
| 3625 | * which will be released by another thread in |
| 3626 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3627 | * released so that it doesn't complain about the held lock when |
| 3628 | * we return to userspace. |
| 3629 | */ |
| 3630 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3631 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3632 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3633 | SB_FREEZE_WRITE); |
| 3634 | } |
| 3635 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3636 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3637 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3638 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3639 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3640 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3641 | else |
| 3642 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3643 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3644 | if (req->flags & REQ_F_REISSUE) { |
| 3645 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3646 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3647 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3648 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3649 | /* |
| 3650 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3651 | * retry them without IOCB_NOWAIT. |
| 3652 | */ |
| 3653 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3654 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3655 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3656 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3657 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3658 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3659 | /* IOPOLL retry should happen for io-wq threads */ |
| 3660 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3661 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3662 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3663 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3664 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3665 | copy_iov: |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3666 | iov_iter_restore(iter, state); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3667 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3668 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3669 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3670 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3671 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3672 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3673 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3674 | return ret; |
| 3675 | } |
| 3676 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3677 | static int io_renameat_prep(struct io_kiocb *req, |
| 3678 | const struct io_uring_sqe *sqe) |
| 3679 | { |
| 3680 | struct io_rename *ren = &req->rename; |
| 3681 | const char __user *oldf, *newf; |
| 3682 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -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->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3686 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3687 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3688 | return -EBADF; |
| 3689 | |
| 3690 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3691 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3692 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3693 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3694 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3695 | |
| 3696 | ren->oldpath = getname(oldf); |
| 3697 | if (IS_ERR(ren->oldpath)) |
| 3698 | return PTR_ERR(ren->oldpath); |
| 3699 | |
| 3700 | ren->newpath = getname(newf); |
| 3701 | if (IS_ERR(ren->newpath)) { |
| 3702 | putname(ren->oldpath); |
| 3703 | return PTR_ERR(ren->newpath); |
| 3704 | } |
| 3705 | |
| 3706 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3707 | return 0; |
| 3708 | } |
| 3709 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3710 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3711 | { |
| 3712 | struct io_rename *ren = &req->rename; |
| 3713 | int ret; |
| 3714 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3715 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3716 | return -EAGAIN; |
| 3717 | |
| 3718 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3719 | ren->newpath, ren->flags); |
| 3720 | |
| 3721 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3722 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3723 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3724 | io_req_complete(req, ret); |
| 3725 | return 0; |
| 3726 | } |
| 3727 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3728 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3729 | const struct io_uring_sqe *sqe) |
| 3730 | { |
| 3731 | struct io_unlink *un = &req->unlink; |
| 3732 | const char __user *fname; |
| 3733 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3734 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3735 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3736 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3737 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3738 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3739 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3740 | return -EBADF; |
| 3741 | |
| 3742 | un->dfd = READ_ONCE(sqe->fd); |
| 3743 | |
| 3744 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3745 | if (un->flags & ~AT_REMOVEDIR) |
| 3746 | return -EINVAL; |
| 3747 | |
| 3748 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3749 | un->filename = getname(fname); |
| 3750 | if (IS_ERR(un->filename)) |
| 3751 | return PTR_ERR(un->filename); |
| 3752 | |
| 3753 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3754 | return 0; |
| 3755 | } |
| 3756 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3757 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3758 | { |
| 3759 | struct io_unlink *un = &req->unlink; |
| 3760 | int ret; |
| 3761 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3762 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3763 | return -EAGAIN; |
| 3764 | |
| 3765 | if (un->flags & AT_REMOVEDIR) |
| 3766 | ret = do_rmdir(un->dfd, un->filename); |
| 3767 | else |
| 3768 | ret = do_unlinkat(un->dfd, un->filename); |
| 3769 | |
| 3770 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3771 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3772 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3773 | io_req_complete(req, ret); |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3777 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3778 | const struct io_uring_sqe *sqe) |
| 3779 | { |
| 3780 | struct io_mkdir *mkd = &req->mkdir; |
| 3781 | const char __user *fname; |
| 3782 | |
| 3783 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3784 | return -EINVAL; |
| 3785 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3786 | sqe->splice_fd_in) |
| 3787 | return -EINVAL; |
| 3788 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3789 | return -EBADF; |
| 3790 | |
| 3791 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3792 | mkd->mode = READ_ONCE(sqe->len); |
| 3793 | |
| 3794 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3795 | mkd->filename = getname(fname); |
| 3796 | if (IS_ERR(mkd->filename)) |
| 3797 | return PTR_ERR(mkd->filename); |
| 3798 | |
| 3799 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3800 | return 0; |
| 3801 | } |
| 3802 | |
| 3803 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) |
| 3804 | { |
| 3805 | struct io_mkdir *mkd = &req->mkdir; |
| 3806 | int ret; |
| 3807 | |
| 3808 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3809 | return -EAGAIN; |
| 3810 | |
| 3811 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3812 | |
| 3813 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3814 | if (ret < 0) |
| 3815 | req_set_fail(req); |
| 3816 | io_req_complete(req, ret); |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3820 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3821 | const struct io_uring_sqe *sqe) |
| 3822 | { |
| 3823 | struct io_symlink *sl = &req->symlink; |
| 3824 | const char __user *oldpath, *newpath; |
| 3825 | |
| 3826 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3827 | return -EINVAL; |
| 3828 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 3829 | sqe->splice_fd_in) |
| 3830 | return -EINVAL; |
| 3831 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3832 | return -EBADF; |
| 3833 | |
| 3834 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 3835 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3836 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3837 | |
| 3838 | sl->oldpath = getname(oldpath); |
| 3839 | if (IS_ERR(sl->oldpath)) |
| 3840 | return PTR_ERR(sl->oldpath); |
| 3841 | |
| 3842 | sl->newpath = getname(newpath); |
| 3843 | if (IS_ERR(sl->newpath)) { |
| 3844 | putname(sl->oldpath); |
| 3845 | return PTR_ERR(sl->newpath); |
| 3846 | } |
| 3847 | |
| 3848 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3849 | return 0; |
| 3850 | } |
| 3851 | |
| 3852 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) |
| 3853 | { |
| 3854 | struct io_symlink *sl = &req->symlink; |
| 3855 | int ret; |
| 3856 | |
| 3857 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3858 | return -EAGAIN; |
| 3859 | |
| 3860 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 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 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3869 | static int io_linkat_prep(struct io_kiocb *req, |
| 3870 | const struct io_uring_sqe *sqe) |
| 3871 | { |
| 3872 | struct io_hardlink *lnk = &req->hardlink; |
| 3873 | const char __user *oldf, *newf; |
| 3874 | |
| 3875 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3876 | return -EINVAL; |
| 3877 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 3878 | return -EINVAL; |
| 3879 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3880 | return -EBADF; |
| 3881 | |
| 3882 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 3883 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 3884 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3885 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3886 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 3887 | |
| 3888 | lnk->oldpath = getname(oldf); |
| 3889 | if (IS_ERR(lnk->oldpath)) |
| 3890 | return PTR_ERR(lnk->oldpath); |
| 3891 | |
| 3892 | lnk->newpath = getname(newf); |
| 3893 | if (IS_ERR(lnk->newpath)) { |
| 3894 | putname(lnk->oldpath); |
| 3895 | return PTR_ERR(lnk->newpath); |
| 3896 | } |
| 3897 | |
| 3898 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3899 | return 0; |
| 3900 | } |
| 3901 | |
| 3902 | static int io_linkat(struct io_kiocb *req, int issue_flags) |
| 3903 | { |
| 3904 | struct io_hardlink *lnk = &req->hardlink; |
| 3905 | int ret; |
| 3906 | |
| 3907 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3908 | return -EAGAIN; |
| 3909 | |
| 3910 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 3911 | lnk->newpath, lnk->flags); |
| 3912 | |
| 3913 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3914 | if (ret < 0) |
| 3915 | req_set_fail(req); |
| 3916 | io_req_complete(req, ret); |
| 3917 | return 0; |
| 3918 | } |
| 3919 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3920 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3921 | const struct io_uring_sqe *sqe) |
| 3922 | { |
| 3923 | #if defined(CONFIG_NET) |
| 3924 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3925 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3926 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3927 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3928 | return -EINVAL; |
| 3929 | |
| 3930 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3931 | return 0; |
| 3932 | #else |
| 3933 | return -EOPNOTSUPP; |
| 3934 | #endif |
| 3935 | } |
| 3936 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3937 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3938 | { |
| 3939 | #if defined(CONFIG_NET) |
| 3940 | struct socket *sock; |
| 3941 | int ret; |
| 3942 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3943 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3944 | return -EAGAIN; |
| 3945 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3946 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3947 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3948 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3949 | |
| 3950 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3951 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3952 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3953 | io_req_complete(req, ret); |
| 3954 | return 0; |
| 3955 | #else |
| 3956 | return -EOPNOTSUPP; |
| 3957 | #endif |
| 3958 | } |
| 3959 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3960 | static int __io_splice_prep(struct io_kiocb *req, |
| 3961 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3962 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 3963 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3964 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3965 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3966 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3967 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3968 | |
| 3969 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3970 | sp->len = READ_ONCE(sqe->len); |
| 3971 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3972 | |
| 3973 | if (unlikely(sp->flags & ~valid_flags)) |
| 3974 | return -EINVAL; |
| 3975 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 3976 | 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] | 3977 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3978 | if (!sp->file_in) |
| 3979 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3980 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
| 3983 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3984 | static int io_tee_prep(struct io_kiocb *req, |
| 3985 | const struct io_uring_sqe *sqe) |
| 3986 | { |
| 3987 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3988 | return -EINVAL; |
| 3989 | return __io_splice_prep(req, sqe); |
| 3990 | } |
| 3991 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3992 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3993 | { |
| 3994 | struct io_splice *sp = &req->splice; |
| 3995 | struct file *in = sp->file_in; |
| 3996 | struct file *out = sp->file_out; |
| 3997 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3998 | long ret = 0; |
| 3999 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4000 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4001 | return -EAGAIN; |
| 4002 | if (sp->len) |
| 4003 | ret = do_tee(in, out, sp->len, flags); |
| 4004 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4005 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4006 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4007 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4008 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4009 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4010 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4011 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4012 | return 0; |
| 4013 | } |
| 4014 | |
| 4015 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4016 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4017 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4018 | |
| 4019 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 4020 | sp->off_out = READ_ONCE(sqe->off); |
| 4021 | return __io_splice_prep(req, sqe); |
| 4022 | } |
| 4023 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4024 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4025 | { |
| 4026 | struct io_splice *sp = &req->splice; |
| 4027 | struct file *in = sp->file_in; |
| 4028 | struct file *out = sp->file_out; |
| 4029 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4030 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4031 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4032 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4033 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 4034 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4035 | |
| 4036 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4037 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4038 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4039 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4040 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4041 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4042 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4043 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4044 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4045 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4046 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4047 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4048 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4049 | return 0; |
| 4050 | } |
| 4051 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4052 | /* |
| 4053 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4054 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4055 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4056 | { |
| 4057 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4058 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4059 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4060 | return -EINVAL; |
| 4061 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4062 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4066 | 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] | 4067 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4068 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4069 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4070 | if (!req->file) |
| 4071 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4072 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4073 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4074 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4075 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4076 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4077 | return -EINVAL; |
| 4078 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4079 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4080 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4081 | return -EINVAL; |
| 4082 | |
| 4083 | req->sync.off = READ_ONCE(sqe->off); |
| 4084 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4085 | return 0; |
| 4086 | } |
| 4087 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4088 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4089 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4090 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4091 | int ret; |
| 4092 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4093 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4094 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4095 | return -EAGAIN; |
| 4096 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4097 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4098 | end > 0 ? end : LLONG_MAX, |
| 4099 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4100 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4101 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4102 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4103 | return 0; |
| 4104 | } |
| 4105 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4106 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4107 | const struct io_uring_sqe *sqe) |
| 4108 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4109 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4110 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4111 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4112 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4113 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4114 | |
| 4115 | req->sync.off = READ_ONCE(sqe->off); |
| 4116 | req->sync.len = READ_ONCE(sqe->addr); |
| 4117 | req->sync.mode = READ_ONCE(sqe->len); |
| 4118 | return 0; |
| 4119 | } |
| 4120 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4121 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4122 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4123 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4124 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4125 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4126 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4127 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4128 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4129 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4130 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4131 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4132 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4133 | return 0; |
| 4134 | } |
| 4135 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4136 | 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] | 4137 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4138 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4139 | int ret; |
| 4140 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4141 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4142 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4143 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4144 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4145 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4146 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4147 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4148 | /* open.how should be already initialised */ |
| 4149 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4150 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4151 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4152 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4153 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4154 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4155 | if (IS_ERR(req->open.filename)) { |
| 4156 | ret = PTR_ERR(req->open.filename); |
| 4157 | req->open.filename = NULL; |
| 4158 | return ret; |
| 4159 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4160 | |
| 4161 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4162 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4163 | return -EINVAL; |
| 4164 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4165 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4166 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4167 | return 0; |
| 4168 | } |
| 4169 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4170 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4171 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4172 | u64 mode = READ_ONCE(sqe->len); |
| 4173 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4174 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4175 | req->open.how = build_open_how(flags, mode); |
| 4176 | return __io_openat_prep(req, sqe); |
| 4177 | } |
| 4178 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4179 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4180 | { |
| 4181 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4182 | size_t len; |
| 4183 | int ret; |
| 4184 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4185 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4186 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4187 | if (len < OPEN_HOW_SIZE_VER0) |
| 4188 | return -EINVAL; |
| 4189 | |
| 4190 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4191 | len); |
| 4192 | if (ret) |
| 4193 | return ret; |
| 4194 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4195 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4198 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4199 | { |
| 4200 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4201 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4202 | bool resolve_nonblock, nonblock_set; |
| 4203 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4204 | int ret; |
| 4205 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4206 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4207 | if (ret) |
| 4208 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4209 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4210 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4211 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4212 | /* |
| 4213 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4214 | * it'll always -EAGAIN |
| 4215 | */ |
| 4216 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4217 | return -EAGAIN; |
| 4218 | op.lookup_flags |= LOOKUP_CACHED; |
| 4219 | op.open_flag |= O_NONBLOCK; |
| 4220 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4221 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4222 | if (!fixed) { |
| 4223 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4224 | if (ret < 0) |
| 4225 | goto err; |
| 4226 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4227 | |
| 4228 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4229 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4230 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4231 | * We could hang on to this 'fd' on retrying, but seems like |
| 4232 | * marginal gain for something that is now known to be a slower |
| 4233 | * 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] | 4234 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4235 | if (!fixed) |
| 4236 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4237 | |
| 4238 | ret = PTR_ERR(file); |
| 4239 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4240 | if (ret == -EAGAIN && |
| 4241 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4242 | return -EAGAIN; |
| 4243 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4246 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4247 | file->f_flags &= ~O_NONBLOCK; |
| 4248 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4249 | |
| 4250 | if (!fixed) |
| 4251 | fd_install(ret, file); |
| 4252 | else |
| 4253 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4254 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4255 | err: |
| 4256 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4257 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4258 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4259 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4260 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4261 | return 0; |
| 4262 | } |
| 4263 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4264 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4265 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4266 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4267 | } |
| 4268 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4269 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4270 | const struct io_uring_sqe *sqe) |
| 4271 | { |
| 4272 | struct io_provide_buf *p = &req->pbuf; |
| 4273 | u64 tmp; |
| 4274 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4275 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4276 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4277 | return -EINVAL; |
| 4278 | |
| 4279 | tmp = READ_ONCE(sqe->fd); |
| 4280 | if (!tmp || tmp > USHRT_MAX) |
| 4281 | return -EINVAL; |
| 4282 | |
| 4283 | memset(p, 0, sizeof(*p)); |
| 4284 | p->nbufs = tmp; |
| 4285 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4286 | return 0; |
| 4287 | } |
| 4288 | |
| 4289 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4290 | int bgid, unsigned nbufs) |
| 4291 | { |
| 4292 | unsigned i = 0; |
| 4293 | |
| 4294 | /* shouldn't happen */ |
| 4295 | if (!nbufs) |
| 4296 | return 0; |
| 4297 | |
| 4298 | /* the head kbuf is the list itself */ |
| 4299 | while (!list_empty(&buf->list)) { |
| 4300 | struct io_buffer *nxt; |
| 4301 | |
| 4302 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4303 | list_del(&nxt->list); |
| 4304 | kfree(nxt); |
| 4305 | if (++i == nbufs) |
| 4306 | return i; |
| 4307 | } |
| 4308 | i++; |
| 4309 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4310 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4311 | |
| 4312 | return i; |
| 4313 | } |
| 4314 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4315 | 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] | 4316 | { |
| 4317 | struct io_provide_buf *p = &req->pbuf; |
| 4318 | struct io_ring_ctx *ctx = req->ctx; |
| 4319 | struct io_buffer *head; |
| 4320 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4321 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4322 | |
| 4323 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4324 | |
| 4325 | lockdep_assert_held(&ctx->uring_lock); |
| 4326 | |
| 4327 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4328 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4329 | if (head) |
| 4330 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4331 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4332 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4333 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4334 | /* complete before unlock, IOPOLL may need the lock */ |
| 4335 | __io_req_complete(req, issue_flags, ret, 0); |
| 4336 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4337 | return 0; |
| 4338 | } |
| 4339 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4340 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4341 | const struct io_uring_sqe *sqe) |
| 4342 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4343 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4344 | struct io_provide_buf *p = &req->pbuf; |
| 4345 | u64 tmp; |
| 4346 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4347 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4348 | return -EINVAL; |
| 4349 | |
| 4350 | tmp = READ_ONCE(sqe->fd); |
| 4351 | if (!tmp || tmp > USHRT_MAX) |
| 4352 | return -E2BIG; |
| 4353 | p->nbufs = tmp; |
| 4354 | p->addr = READ_ONCE(sqe->addr); |
| 4355 | p->len = READ_ONCE(sqe->len); |
| 4356 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4357 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4358 | &size)) |
| 4359 | return -EOVERFLOW; |
| 4360 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4361 | return -EOVERFLOW; |
| 4362 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4363 | size = (unsigned long)p->len * p->nbufs; |
| 4364 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4365 | return -EFAULT; |
| 4366 | |
| 4367 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4368 | tmp = READ_ONCE(sqe->off); |
| 4369 | if (tmp > USHRT_MAX) |
| 4370 | return -E2BIG; |
| 4371 | p->bid = tmp; |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
| 4375 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4376 | { |
| 4377 | struct io_buffer *buf; |
| 4378 | u64 addr = pbuf->addr; |
| 4379 | int i, bid = pbuf->bid; |
| 4380 | |
| 4381 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4382 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4383 | if (!buf) |
| 4384 | break; |
| 4385 | |
| 4386 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4387 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4388 | buf->bid = bid; |
| 4389 | addr += pbuf->len; |
| 4390 | bid++; |
| 4391 | if (!*head) { |
| 4392 | INIT_LIST_HEAD(&buf->list); |
| 4393 | *head = buf; |
| 4394 | } else { |
| 4395 | list_add_tail(&buf->list, &(*head)->list); |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | return i ? i : -ENOMEM; |
| 4400 | } |
| 4401 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4402 | 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] | 4403 | { |
| 4404 | struct io_provide_buf *p = &req->pbuf; |
| 4405 | struct io_ring_ctx *ctx = req->ctx; |
| 4406 | struct io_buffer *head, *list; |
| 4407 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4408 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4409 | |
| 4410 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4411 | |
| 4412 | lockdep_assert_held(&ctx->uring_lock); |
| 4413 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4414 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4415 | |
| 4416 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4417 | if (ret >= 0 && !list) { |
| 4418 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4419 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4420 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4421 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4422 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4423 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4424 | /* complete before unlock, IOPOLL may need the lock */ |
| 4425 | __io_req_complete(req, issue_flags, ret, 0); |
| 4426 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4427 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4430 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4431 | const struct io_uring_sqe *sqe) |
| 4432 | { |
| 4433 | #if defined(CONFIG_EPOLL) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4434 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4435 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4436 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4437 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4438 | |
| 4439 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4440 | req->epoll.op = READ_ONCE(sqe->len); |
| 4441 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4442 | |
| 4443 | if (ep_op_has_event(req->epoll.op)) { |
| 4444 | struct epoll_event __user *ev; |
| 4445 | |
| 4446 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4447 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4448 | return -EFAULT; |
| 4449 | } |
| 4450 | |
| 4451 | return 0; |
| 4452 | #else |
| 4453 | return -EOPNOTSUPP; |
| 4454 | #endif |
| 4455 | } |
| 4456 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4457 | 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] | 4458 | { |
| 4459 | #if defined(CONFIG_EPOLL) |
| 4460 | struct io_epoll *ie = &req->epoll; |
| 4461 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4462 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4463 | |
| 4464 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4465 | if (force_nonblock && ret == -EAGAIN) |
| 4466 | return -EAGAIN; |
| 4467 | |
| 4468 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4469 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4470 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4471 | return 0; |
| 4472 | #else |
| 4473 | return -EOPNOTSUPP; |
| 4474 | #endif |
| 4475 | } |
| 4476 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4477 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4478 | { |
| 4479 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4480 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4481 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4482 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4483 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4484 | |
| 4485 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4486 | req->madvise.len = READ_ONCE(sqe->len); |
| 4487 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4488 | return 0; |
| 4489 | #else |
| 4490 | return -EOPNOTSUPP; |
| 4491 | #endif |
| 4492 | } |
| 4493 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4494 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4495 | { |
| 4496 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4497 | struct io_madvise *ma = &req->madvise; |
| 4498 | int ret; |
| 4499 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4500 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4501 | return -EAGAIN; |
| 4502 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4503 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4504 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4505 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4506 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4507 | return 0; |
| 4508 | #else |
| 4509 | return -EOPNOTSUPP; |
| 4510 | #endif |
| 4511 | } |
| 4512 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4513 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4514 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4515 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4516 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4517 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4518 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4519 | |
| 4520 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4521 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4522 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4523 | return 0; |
| 4524 | } |
| 4525 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4526 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4527 | { |
| 4528 | struct io_fadvise *fa = &req->fadvise; |
| 4529 | int ret; |
| 4530 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4531 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4532 | switch (fa->advice) { |
| 4533 | case POSIX_FADV_NORMAL: |
| 4534 | case POSIX_FADV_RANDOM: |
| 4535 | case POSIX_FADV_SEQUENTIAL: |
| 4536 | break; |
| 4537 | default: |
| 4538 | return -EAGAIN; |
| 4539 | } |
| 4540 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4541 | |
| 4542 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4543 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4544 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4545 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | } |
| 4548 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4549 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4550 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4551 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4552 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4553 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4554 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4555 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4556 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4557 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4558 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4559 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4560 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4561 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4562 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4563 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4564 | return 0; |
| 4565 | } |
| 4566 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4567 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4568 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4569 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4570 | int ret; |
| 4571 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4572 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4573 | return -EAGAIN; |
| 4574 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4575 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4576 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4577 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4578 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4579 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4580 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4581 | return 0; |
| 4582 | } |
| 4583 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4584 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4585 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4586 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4587 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4588 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4589 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4590 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4591 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4592 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4593 | |
| 4594 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4595 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4596 | if (req->close.file_slot && req->close.fd) |
| 4597 | return -EINVAL; |
| 4598 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4599 | return 0; |
| 4600 | } |
| 4601 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4602 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4603 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4604 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4605 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4606 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4607 | struct file *file = NULL; |
| 4608 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4609 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4610 | if (req->close.file_slot) { |
| 4611 | ret = io_close_fixed(req, issue_flags); |
| 4612 | goto err; |
| 4613 | } |
| 4614 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4615 | spin_lock(&files->file_lock); |
| 4616 | fdt = files_fdtable(files); |
| 4617 | if (close->fd >= fdt->max_fds) { |
| 4618 | spin_unlock(&files->file_lock); |
| 4619 | goto err; |
| 4620 | } |
| 4621 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4622 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4623 | spin_unlock(&files->file_lock); |
| 4624 | file = NULL; |
| 4625 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4626 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4627 | |
| 4628 | /* 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] | 4629 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4630 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4631 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4632 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4633 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4634 | ret = __close_fd_get_file(close->fd, &file); |
| 4635 | spin_unlock(&files->file_lock); |
| 4636 | if (ret < 0) { |
| 4637 | if (ret == -ENOENT) |
| 4638 | ret = -EBADF; |
| 4639 | goto err; |
| 4640 | } |
| 4641 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4642 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4643 | ret = filp_close(file, current->files); |
| 4644 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4645 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4646 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4647 | if (file) |
| 4648 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4649 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4650 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4653 | 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] | 4654 | { |
| 4655 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4656 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4657 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4658 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4659 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4660 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4661 | return -EINVAL; |
| 4662 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4663 | req->sync.off = READ_ONCE(sqe->off); |
| 4664 | req->sync.len = READ_ONCE(sqe->len); |
| 4665 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4666 | return 0; |
| 4667 | } |
| 4668 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4669 | 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] | 4670 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4671 | int ret; |
| 4672 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4673 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4674 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4675 | return -EAGAIN; |
| 4676 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4677 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4678 | req->sync.flags); |
| 4679 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4680 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4681 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4682 | return 0; |
| 4683 | } |
| 4684 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4685 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4686 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4687 | struct io_async_msghdr *kmsg) |
| 4688 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4689 | struct io_async_msghdr *async_msg = req->async_data; |
| 4690 | |
| 4691 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4692 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4693 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4694 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4695 | return -ENOMEM; |
| 4696 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4697 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4698 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4699 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4700 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4701 | /* if were using fast_iov, set it to the new one */ |
| 4702 | if (!async_msg->free_iov) |
| 4703 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4704 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4705 | return -EAGAIN; |
| 4706 | } |
| 4707 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4708 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4709 | struct io_async_msghdr *iomsg) |
| 4710 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4711 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4712 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4713 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4714 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4715 | } |
| 4716 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4717 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4718 | { |
| 4719 | int ret; |
| 4720 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4721 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4722 | if (!ret) |
| 4723 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4724 | return ret; |
| 4725 | } |
| 4726 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4727 | 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] | 4728 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4729 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4730 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4732 | return -EINVAL; |
| 4733 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4734 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4735 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4736 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4737 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4738 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4739 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4740 | #ifdef CONFIG_COMPAT |
| 4741 | if (req->ctx->compat) |
| 4742 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4743 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4744 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4745 | } |
| 4746 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4747 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4748 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4749 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4750 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4751 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4752 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4753 | int ret; |
| 4754 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4755 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4756 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4757 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4758 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4759 | kmsg = req->async_data; |
| 4760 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4761 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4762 | if (ret) |
| 4763 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4764 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4767 | flags = req->sr_msg.msg_flags; |
| 4768 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4769 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4770 | if (flags & MSG_WAITALL) |
| 4771 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4772 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4773 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4774 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4775 | return io_setup_async_msg(req, kmsg); |
| 4776 | if (ret == -ERESTARTSYS) |
| 4777 | ret = -EINTR; |
| 4778 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4779 | /* fast path, check for non-NULL to avoid function call */ |
| 4780 | if (kmsg->free_iov) |
| 4781 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4782 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4783 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4784 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4785 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4786 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4787 | } |
| 4788 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4789 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4790 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4791 | struct io_sr_msg *sr = &req->sr_msg; |
| 4792 | struct msghdr msg; |
| 4793 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4794 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4795 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4796 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4797 | int ret; |
| 4798 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4799 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4800 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4801 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4802 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4803 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4804 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4805 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4806 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4807 | msg.msg_name = NULL; |
| 4808 | msg.msg_control = NULL; |
| 4809 | msg.msg_controllen = 0; |
| 4810 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4811 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4812 | flags = req->sr_msg.msg_flags; |
| 4813 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4814 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4815 | if (flags & MSG_WAITALL) |
| 4816 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4817 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4818 | msg.msg_flags = flags; |
| 4819 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4820 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4821 | return -EAGAIN; |
| 4822 | if (ret == -ERESTARTSYS) |
| 4823 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4824 | |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4825 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4826 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4827 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4828 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4829 | } |
| 4830 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4831 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4832 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4833 | { |
| 4834 | struct io_sr_msg *sr = &req->sr_msg; |
| 4835 | struct iovec __user *uiov; |
| 4836 | size_t iov_len; |
| 4837 | int ret; |
| 4838 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4839 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4840 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4841 | if (ret) |
| 4842 | return ret; |
| 4843 | |
| 4844 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4845 | if (iov_len > 1) |
| 4846 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4847 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4848 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4849 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4850 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4851 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4852 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4853 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4854 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4855 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4856 | if (ret > 0) |
| 4857 | ret = 0; |
| 4858 | } |
| 4859 | |
| 4860 | return ret; |
| 4861 | } |
| 4862 | |
| 4863 | #ifdef CONFIG_COMPAT |
| 4864 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4865 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4866 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4867 | struct io_sr_msg *sr = &req->sr_msg; |
| 4868 | struct compat_iovec __user *uiov; |
| 4869 | compat_uptr_t ptr; |
| 4870 | compat_size_t len; |
| 4871 | int ret; |
| 4872 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4873 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4874 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4875 | if (ret) |
| 4876 | return ret; |
| 4877 | |
| 4878 | uiov = compat_ptr(ptr); |
| 4879 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4880 | compat_ssize_t clen; |
| 4881 | |
| 4882 | if (len > 1) |
| 4883 | return -EINVAL; |
| 4884 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4885 | return -EFAULT; |
| 4886 | if (__get_user(clen, &uiov->iov_len)) |
| 4887 | return -EFAULT; |
| 4888 | if (clen < 0) |
| 4889 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4890 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4891 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4892 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4893 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4894 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4895 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4896 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4897 | if (ret < 0) |
| 4898 | return ret; |
| 4899 | } |
| 4900 | |
| 4901 | return 0; |
| 4902 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4903 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4904 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4905 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4906 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4907 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4908 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4909 | |
| 4910 | #ifdef CONFIG_COMPAT |
| 4911 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4912 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4913 | #endif |
| 4914 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4915 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4916 | } |
| 4917 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4918 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4919 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4920 | { |
| 4921 | struct io_sr_msg *sr = &req->sr_msg; |
| 4922 | struct io_buffer *kbuf; |
| 4923 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4924 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4925 | if (IS_ERR(kbuf)) |
| 4926 | return kbuf; |
| 4927 | |
| 4928 | sr->kbuf = kbuf; |
| 4929 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4930 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4933 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4934 | { |
| 4935 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4936 | } |
| 4937 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4938 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4939 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4940 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4941 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4942 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4943 | if (!ret) |
| 4944 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4945 | return ret; |
| 4946 | } |
| 4947 | |
| 4948 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4949 | { |
| 4950 | struct io_sr_msg *sr = &req->sr_msg; |
| 4951 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4952 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4953 | return -EINVAL; |
| 4954 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4955 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4956 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4957 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4958 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4959 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4960 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4961 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4962 | #ifdef CONFIG_COMPAT |
| 4963 | if (req->ctx->compat) |
| 4964 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4965 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4966 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4967 | } |
| 4968 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4969 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4970 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4971 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4972 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4973 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4974 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4975 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4976 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4977 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4978 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4979 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4980 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4981 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4982 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4983 | kmsg = req->async_data; |
| 4984 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4985 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4986 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4987 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4988 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4989 | } |
| 4990 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4991 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4992 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4993 | if (IS_ERR(kbuf)) |
| 4994 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4995 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4996 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4997 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4998 | 1, req->sr_msg.len); |
| 4999 | } |
| 5000 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5001 | flags = req->sr_msg.msg_flags; |
| 5002 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5003 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5004 | if (flags & MSG_WAITALL) |
| 5005 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 5006 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5007 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 5008 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5009 | if (force_nonblock && ret == -EAGAIN) |
| 5010 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5011 | if (ret == -ERESTARTSYS) |
| 5012 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5013 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5014 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5015 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5016 | /* fast path, check for non-NULL to avoid function call */ |
| 5017 | if (kmsg->free_iov) |
| 5018 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5019 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5020 | 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] | 5021 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5022 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5023 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5024 | } |
| 5025 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5026 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5027 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5028 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5029 | struct io_sr_msg *sr = &req->sr_msg; |
| 5030 | struct msghdr msg; |
| 5031 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5032 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5033 | struct iovec iov; |
| 5034 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5035 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5036 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5037 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5038 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5039 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5040 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5041 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5042 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5043 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5044 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5045 | if (IS_ERR(kbuf)) |
| 5046 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5047 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5048 | } |
| 5049 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5050 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5051 | if (unlikely(ret)) |
| 5052 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5053 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5054 | msg.msg_name = NULL; |
| 5055 | msg.msg_control = NULL; |
| 5056 | msg.msg_controllen = 0; |
| 5057 | msg.msg_namelen = 0; |
| 5058 | msg.msg_iocb = NULL; |
| 5059 | msg.msg_flags = 0; |
| 5060 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5061 | flags = req->sr_msg.msg_flags; |
| 5062 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5063 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5064 | if (flags & MSG_WAITALL) |
| 5065 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5066 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5067 | ret = sock_recvmsg(sock, &msg, flags); |
| 5068 | if (force_nonblock && ret == -EAGAIN) |
| 5069 | return -EAGAIN; |
| 5070 | if (ret == -ERESTARTSYS) |
| 5071 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5072 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5073 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5074 | cflags = io_put_recv_kbuf(req); |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5075 | 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] | 5076 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5077 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5078 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5081 | 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] | 5082 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5083 | struct io_accept *accept = &req->accept; |
| 5084 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5085 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5086 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5087 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5088 | return -EINVAL; |
| 5089 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5090 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5091 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5092 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5093 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5094 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5095 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5096 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5097 | (accept->flags & SOCK_CLOEXEC))) |
| 5098 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5099 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5100 | return -EINVAL; |
| 5101 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5102 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5103 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5104 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5105 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5106 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5107 | { |
| 5108 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5109 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5110 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5111 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5112 | struct file *file; |
| 5113 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5114 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5115 | if (req->file->f_flags & O_NONBLOCK) |
| 5116 | req->flags |= REQ_F_NOWAIT; |
| 5117 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5118 | if (!fixed) { |
| 5119 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5120 | if (unlikely(fd < 0)) |
| 5121 | return fd; |
| 5122 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5123 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5124 | accept->flags); |
| 5125 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5126 | if (!fixed) |
| 5127 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5128 | ret = PTR_ERR(file); |
| 5129 | if (ret == -EAGAIN && force_nonblock) |
| 5130 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5131 | if (ret == -ERESTARTSYS) |
| 5132 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5133 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5134 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5135 | fd_install(fd, file); |
| 5136 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5137 | } else { |
| 5138 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5139 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5140 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5141 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5142 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5143 | } |
| 5144 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5145 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5146 | { |
| 5147 | struct io_async_connect *io = req->async_data; |
| 5148 | struct io_connect *conn = &req->connect; |
| 5149 | |
| 5150 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5151 | } |
| 5152 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5153 | 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] | 5154 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5155 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5156 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5157 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5158 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5159 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5160 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5161 | return -EINVAL; |
| 5162 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5163 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5164 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5165 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5166 | } |
| 5167 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5168 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5169 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5170 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5171 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5172 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5173 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5174 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5175 | if (req->async_data) { |
| 5176 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5177 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5178 | ret = move_addr_to_kernel(req->connect.addr, |
| 5179 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5180 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5181 | if (ret) |
| 5182 | goto out; |
| 5183 | io = &__io; |
| 5184 | } |
| 5185 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5186 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5187 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5188 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5189 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5190 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5191 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5192 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5193 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5194 | ret = -ENOMEM; |
| 5195 | goto out; |
| 5196 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5197 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5198 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5199 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5200 | if (ret == -ERESTARTSYS) |
| 5201 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5202 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5203 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5204 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5205 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5206 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5207 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5208 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5209 | #define IO_NETOP_FN(op) \ |
| 5210 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5211 | { \ |
| 5212 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5213 | } |
| 5214 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5215 | #define IO_NETOP_PREP(op) \ |
| 5216 | IO_NETOP_FN(op) \ |
| 5217 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5218 | { \ |
| 5219 | return -EOPNOTSUPP; \ |
| 5220 | } \ |
| 5221 | |
| 5222 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5223 | IO_NETOP_PREP(op) \ |
| 5224 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5225 | { \ |
| 5226 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5227 | } |
| 5228 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5229 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5230 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5231 | IO_NETOP_PREP_ASYNC(connect); |
| 5232 | IO_NETOP_PREP(accept); |
| 5233 | IO_NETOP_FN(send); |
| 5234 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5235 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5236 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5237 | struct io_poll_table { |
| 5238 | struct poll_table_struct pt; |
| 5239 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5240 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5241 | int error; |
| 5242 | }; |
| 5243 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5244 | 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] | 5245 | __poll_t mask, io_req_tw_func_t func) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5246 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5247 | /* for instances that support it check for an event match first: */ |
| 5248 | if (mask && !(mask & poll->events)) |
| 5249 | return 0; |
| 5250 | |
| 5251 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5252 | |
| 5253 | list_del_init(&poll->wait.entry); |
| 5254 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5255 | req->result = mask; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5256 | req->io_task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5257 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5258 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5259 | * If this fails, then the task is exiting. When a task exits, the |
| 5260 | * work gets canceled, so just cancel this request as well instead |
| 5261 | * of executing it. We can't safely execute it anyway, as we may not |
| 5262 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5263 | */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5264 | io_req_task_work_add(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5265 | return 1; |
| 5266 | } |
| 5267 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5268 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5269 | __acquires(&req->ctx->completion_lock) |
| 5270 | { |
| 5271 | struct io_ring_ctx *ctx = req->ctx; |
| 5272 | |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 5273 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5274 | if (unlikely(req->task->flags & PF_EXITING)) |
| 5275 | WRITE_ONCE(poll->canceled, true); |
| 5276 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5277 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5278 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5279 | |
| 5280 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5281 | } |
| 5282 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5283 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5284 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5285 | add_wait_queue(poll->head, &poll->wait); |
| 5286 | return true; |
| 5287 | } |
| 5288 | |
| 5289 | return false; |
| 5290 | } |
| 5291 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5292 | 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] | 5293 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5294 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5295 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5296 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5297 | return req->apoll->double_poll; |
| 5298 | } |
| 5299 | |
| 5300 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5301 | { |
| 5302 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5303 | return &req->poll; |
| 5304 | return &req->apoll->poll; |
| 5305 | } |
| 5306 | |
| 5307 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5308 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5309 | { |
| 5310 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5311 | |
| 5312 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5313 | |
| 5314 | if (poll && poll->head) { |
| 5315 | struct wait_queue_head *head = poll->head; |
| 5316 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5317 | spin_lock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5318 | list_del_init(&poll->wait.entry); |
| 5319 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5320 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5321 | poll->head = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5322 | spin_unlock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5323 | } |
| 5324 | } |
| 5325 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5326 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5327 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5328 | { |
| 5329 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5330 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5331 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5332 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5333 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 5334 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5335 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5336 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5337 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5338 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5339 | if (req->poll.events & EPOLLONESHOT) |
| 5340 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5341 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
| 5342 | req->poll.events |= EPOLLONESHOT; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5343 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5344 | } |
Hao Xu | 7b289c3 | 2021-04-13 15:20:39 +0800 | [diff] [blame] | 5345 | if (flags & IORING_CQE_F_MORE) |
| 5346 | ctx->cq_extra++; |
| 5347 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5348 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5349 | } |
| 5350 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5351 | static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
| 5352 | __must_hold(&req->ctx->completion_lock) |
| 5353 | { |
| 5354 | bool done; |
| 5355 | |
| 5356 | done = __io_poll_complete(req, mask); |
| 5357 | io_commit_cqring(req->ctx); |
| 5358 | return done; |
| 5359 | } |
| 5360 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5361 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5362 | { |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5363 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5364 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5365 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5366 | if (io_poll_rewait(req, &req->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5367 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5368 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5369 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5370 | |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5371 | if (req->poll.done) { |
| 5372 | spin_unlock(&ctx->completion_lock); |
| 5373 | return; |
| 5374 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5375 | done = __io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5376 | if (done) { |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5377 | io_poll_remove_double(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5378 | hash_del(&req->hash_node); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5379 | req->poll.done = true; |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5380 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5381 | req->result = 0; |
| 5382 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 5383 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5384 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5385 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5386 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5387 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5388 | if (done) { |
| 5389 | nxt = io_put_req_find_next(req); |
| 5390 | if (nxt) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5391 | io_req_task_submit(nxt, locked); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5392 | } |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 5393 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5394 | } |
| 5395 | |
| 5396 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5397 | int sync, void *key) |
| 5398 | { |
| 5399 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5400 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5401 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5402 | unsigned long flags; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5403 | |
| 5404 | /* for instances that support it check for an event match first: */ |
| 5405 | if (mask && !(mask & poll->events)) |
| 5406 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5407 | if (!(poll->events & EPOLLONESHOT)) |
| 5408 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5409 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5410 | list_del_init(&wait->entry); |
| 5411 | |
Jens Axboe | 9ce85ef | 2021-07-09 08:20:28 -0600 | [diff] [blame] | 5412 | if (poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5413 | bool done; |
| 5414 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5415 | spin_lock_irqsave(&poll->head->lock, flags); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5416 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5417 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5418 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5419 | /* make sure double remove sees this as being gone */ |
| 5420 | wait->private = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5421 | spin_unlock_irqrestore(&poll->head->lock, flags); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5422 | if (!done) { |
| 5423 | /* use wait func handler, so it matches the rq type */ |
| 5424 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5425 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5426 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5427 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5428 | return 1; |
| 5429 | } |
| 5430 | |
| 5431 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5432 | wait_queue_func_t wake_func) |
| 5433 | { |
| 5434 | poll->head = NULL; |
| 5435 | poll->done = false; |
| 5436 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 5437 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5438 | /* mask in events that we always want/need */ |
| 5439 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5440 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5441 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5442 | } |
| 5443 | |
| 5444 | 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] | 5445 | struct wait_queue_head *head, |
| 5446 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5447 | { |
| 5448 | struct io_kiocb *req = pt->req; |
| 5449 | |
| 5450 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5451 | * The file being polled uses multiple waitqueues for poll handling |
| 5452 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5453 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5454 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5455 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5456 | struct io_poll_iocb *poll_one = poll; |
| 5457 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5458 | /* double add on the same waitqueue head, ignore */ |
| 5459 | if (poll_one->head == head) |
| 5460 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5461 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5462 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5463 | if ((*poll_ptr)->head == head) |
| 5464 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5465 | pt->error = -EINVAL; |
| 5466 | return; |
| 5467 | } |
Jens Axboe | ea6a693d | 2021-04-15 09:47:13 -0600 | [diff] [blame] | 5468 | /* |
| 5469 | * Can't handle multishot for double wait for now, turn it |
| 5470 | * into one-shot mode. |
| 5471 | */ |
Pavel Begunkov | 7a27472 | 2021-05-17 12:43:34 +0100 | [diff] [blame] | 5472 | if (!(poll_one->events & EPOLLONESHOT)) |
| 5473 | poll_one->events |= EPOLLONESHOT; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5474 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5475 | if (!poll) { |
| 5476 | pt->error = -ENOMEM; |
| 5477 | return; |
| 5478 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5479 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5480 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5481 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5482 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5483 | } |
| 5484 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5485 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5486 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5487 | |
| 5488 | if (poll->events & EPOLLEXCLUSIVE) |
| 5489 | add_wait_queue_exclusive(head, &poll->wait); |
| 5490 | else |
| 5491 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5495 | struct poll_table_struct *p) |
| 5496 | { |
| 5497 | 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] | 5498 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5499 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5500 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5501 | } |
| 5502 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5503 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5504 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5505 | struct async_poll *apoll = req->apoll; |
| 5506 | struct io_ring_ctx *ctx = req->ctx; |
| 5507 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5508 | 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] | 5509 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5510 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5511 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5512 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5513 | } |
| 5514 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5515 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5516 | io_poll_remove_double(req); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5517 | apoll->poll.done = true; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5518 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5519 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5520 | if (!READ_ONCE(apoll->poll.canceled)) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5521 | io_req_task_submit(req, locked); |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5522 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5523 | io_req_complete_failed(req, -ECANCELED); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5527 | void *key) |
| 5528 | { |
| 5529 | struct io_kiocb *req = wait->private; |
| 5530 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5531 | |
| 5532 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5533 | key_to_poll(key)); |
| 5534 | |
| 5535 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5536 | } |
| 5537 | |
| 5538 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5539 | { |
| 5540 | struct io_ring_ctx *ctx = req->ctx; |
| 5541 | struct hlist_head *list; |
| 5542 | |
| 5543 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5544 | hlist_add_head(&req->hash_node, list); |
| 5545 | } |
| 5546 | |
| 5547 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5548 | struct io_poll_iocb *poll, |
| 5549 | struct io_poll_table *ipt, __poll_t mask, |
| 5550 | wait_queue_func_t wake_func) |
| 5551 | __acquires(&ctx->completion_lock) |
| 5552 | { |
| 5553 | struct io_ring_ctx *ctx = req->ctx; |
| 5554 | bool cancel = false; |
| 5555 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5556 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5557 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5558 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5559 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5560 | |
| 5561 | ipt->pt._key = mask; |
| 5562 | ipt->req = req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5563 | ipt->error = 0; |
| 5564 | ipt->nr_entries = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5565 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5566 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5567 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
| 5568 | ipt->error = -EINVAL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5569 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5570 | spin_lock(&ctx->completion_lock); |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5571 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
Pavel Begunkov | 46fee9a | 2021-07-20 10:50:44 +0100 | [diff] [blame] | 5572 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5573 | if (likely(poll->head)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5574 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5575 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5576 | if (ipt->error) |
| 5577 | cancel = true; |
| 5578 | ipt->error = 0; |
| 5579 | mask = 0; |
| 5580 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5581 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5582 | list_del_init(&poll->wait.entry); |
| 5583 | else if (cancel) |
| 5584 | WRITE_ONCE(poll->canceled, true); |
| 5585 | else if (!poll->done) /* actually waiting for an event */ |
| 5586 | io_poll_req_insert(req); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5587 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | return mask; |
| 5591 | } |
| 5592 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5593 | enum { |
| 5594 | IO_APOLL_OK, |
| 5595 | IO_APOLL_ABORTED, |
| 5596 | IO_APOLL_READY |
| 5597 | }; |
| 5598 | |
| 5599 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5600 | { |
| 5601 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5602 | struct io_ring_ctx *ctx = req->ctx; |
| 5603 | struct async_poll *apoll; |
| 5604 | struct io_poll_table ipt; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5605 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5606 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5607 | |
| 5608 | if (!req->file || !file_can_poll(req->file)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5609 | return IO_APOLL_ABORTED; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5610 | if (req->flags & REQ_F_POLLED) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5611 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5612 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5613 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5614 | |
| 5615 | if (def->pollin) { |
| 5616 | rw = READ; |
| 5617 | mask |= POLLIN | POLLRDNORM; |
| 5618 | |
| 5619 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5620 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5621 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5622 | mask &= ~POLLIN; |
| 5623 | } else { |
| 5624 | rw = WRITE; |
| 5625 | mask |= POLLOUT | POLLWRNORM; |
| 5626 | } |
| 5627 | |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5628 | /* 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] | 5629 | if (!io_file_supports_nowait(req, rw)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5630 | return IO_APOLL_ABORTED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5631 | |
| 5632 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5633 | if (unlikely(!apoll)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5634 | return IO_APOLL_ABORTED; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5635 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5636 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5637 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5638 | ipt.pt._qproc = io_async_queue_proc; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5639 | io_req_set_refcount(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5640 | |
| 5641 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5642 | io_async_wake); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5643 | spin_unlock(&ctx->completion_lock); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5644 | if (ret || ipt.error) |
| 5645 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5646 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5647 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5648 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5649 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5653 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5654 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5655 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5656 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5657 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5658 | if (!poll->head) |
| 5659 | return false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5660 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5661 | if (do_cancel) |
| 5662 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5663 | if (!list_empty(&poll->wait.entry)) { |
| 5664 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5665 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5666 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5667 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5668 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5669 | return do_complete; |
| 5670 | } |
| 5671 | |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5672 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5673 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5674 | { |
| 5675 | bool do_complete; |
| 5676 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5677 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5678 | 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] | 5679 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5680 | if (do_complete) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5681 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5682 | io_commit_cqring(req->ctx); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5683 | req_set_fail(req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5684 | io_put_req_deferred(req); |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5685 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5686 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5687 | } |
| 5688 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5689 | /* |
| 5690 | * Returns true if we found and killed one or more poll requests |
| 5691 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5692 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 5693 | bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5694 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5695 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5696 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5697 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5698 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5699 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5700 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5701 | struct hlist_head *list; |
| 5702 | |
| 5703 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5704 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 5705 | if (io_match_task(req, tsk, cancel_all)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5706 | posted += io_poll_remove_one(req); |
| 5707 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5708 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5709 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5710 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5711 | if (posted) |
| 5712 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5713 | |
| 5714 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5715 | } |
| 5716 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5717 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5718 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5719 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5720 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5721 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5722 | struct io_kiocb *req; |
| 5723 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5724 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5725 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5726 | if (sqe_addr != req->user_data) |
| 5727 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5728 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5729 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5730 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5731 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5732 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5733 | } |
| 5734 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5735 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5736 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5737 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5738 | { |
| 5739 | struct io_kiocb *req; |
| 5740 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5741 | req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5742 | if (!req) |
| 5743 | return -ENOENT; |
| 5744 | if (io_poll_remove_one(req)) |
| 5745 | return 0; |
| 5746 | |
| 5747 | return -EALREADY; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5750 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5751 | unsigned int flags) |
| 5752 | { |
| 5753 | u32 events; |
| 5754 | |
| 5755 | events = READ_ONCE(sqe->poll32_events); |
| 5756 | #ifdef __BIG_ENDIAN |
| 5757 | events = swahw32(events); |
| 5758 | #endif |
| 5759 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5760 | events |= EPOLLONESHOT; |
| 5761 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5762 | } |
| 5763 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5764 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5765 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5766 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5767 | struct io_poll_update *upd = &req->poll_update; |
| 5768 | u32 flags; |
| 5769 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5770 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5771 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5772 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5773 | return -EINVAL; |
| 5774 | flags = READ_ONCE(sqe->len); |
| 5775 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5776 | IORING_POLL_ADD_MULTI)) |
| 5777 | return -EINVAL; |
| 5778 | /* meaningless without update */ |
| 5779 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5780 | return -EINVAL; |
| 5781 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5782 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5783 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5784 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5785 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5786 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5787 | if (!upd->update_user_data && upd->new_user_data) |
| 5788 | return -EINVAL; |
| 5789 | if (upd->update_events) |
| 5790 | upd->events = io_poll_parse_events(sqe, flags); |
| 5791 | else if (sqe->poll32_events) |
| 5792 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5793 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
| 5796 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5797 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5798 | void *key) |
| 5799 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5800 | struct io_kiocb *req = wait->private; |
| 5801 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5802 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5803 | 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] | 5804 | } |
| 5805 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5806 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5807 | struct poll_table_struct *p) |
| 5808 | { |
| 5809 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5810 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5811 | __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] | 5812 | } |
| 5813 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5814 | 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] | 5815 | { |
| 5816 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5817 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5818 | |
| 5819 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5820 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5821 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5822 | return -EINVAL; |
| 5823 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5824 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5825 | return -EINVAL; |
| 5826 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5827 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5828 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5829 | return 0; |
| 5830 | } |
| 5831 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5832 | 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] | 5833 | { |
| 5834 | struct io_poll_iocb *poll = &req->poll; |
| 5835 | struct io_ring_ctx *ctx = req->ctx; |
| 5836 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5837 | __poll_t mask; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5838 | bool done; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5839 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5840 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5841 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5842 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5843 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5844 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5845 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5846 | ipt.error = 0; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5847 | done = io_poll_complete(req, mask); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5848 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5849 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5850 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5851 | if (mask) { |
| 5852 | io_cqring_ev_posted(ctx); |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5853 | if (done) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5854 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5855 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5856 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5857 | } |
| 5858 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5859 | 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] | 5860 | { |
| 5861 | struct io_ring_ctx *ctx = req->ctx; |
| 5862 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5863 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5864 | int ret; |
| 5865 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5866 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5867 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5868 | if (!preq) { |
| 5869 | ret = -ENOENT; |
| 5870 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5871 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5872 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5873 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
| 5874 | completing = true; |
| 5875 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; |
| 5876 | goto err; |
| 5877 | } |
| 5878 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5879 | /* |
| 5880 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5881 | * update those. For multishot, if we're racing with completion, just |
| 5882 | * let completion re-add it. |
| 5883 | */ |
| 5884 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5885 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5886 | ret = -EALREADY; |
| 5887 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5888 | } |
| 5889 | /* we now have a detached poll request. reissue. */ |
| 5890 | ret = 0; |
| 5891 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5892 | if (ret < 0) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5893 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5894 | req_set_fail(req); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5895 | io_req_complete(req, ret); |
| 5896 | return 0; |
| 5897 | } |
| 5898 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5899 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5900 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5901 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5902 | preq->poll.events |= IO_POLL_UNMASK; |
| 5903 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5904 | if (req->poll_update.update_user_data) |
| 5905 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5906 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5907 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5908 | /* complete update request, we're done with it */ |
| 5909 | io_req_complete(req, ret); |
| 5910 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5911 | if (!completing) { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5912 | ret = io_poll_add(preq, issue_flags); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5913 | if (ret < 0) { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5914 | req_set_fail(preq); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5915 | io_req_complete(preq, ret); |
| 5916 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5917 | } |
| 5918 | return 0; |
| 5919 | } |
| 5920 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5921 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5922 | { |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5923 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 5924 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5925 | } |
| 5926 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5927 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5928 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5929 | struct io_timeout_data *data = container_of(timer, |
| 5930 | struct io_timeout_data, timer); |
| 5931 | struct io_kiocb *req = data->req; |
| 5932 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5933 | unsigned long flags; |
| 5934 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5935 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5936 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5937 | atomic_set(&req->ctx->cq_timeouts, |
| 5938 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5939 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5940 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5941 | req->io_task_work.func = io_req_task_timeout; |
| 5942 | io_req_task_work_add(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5943 | return HRTIMER_NORESTART; |
| 5944 | } |
| 5945 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5946 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5947 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5948 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5949 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5950 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5951 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5952 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5953 | |
| 5954 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5955 | found = user_data == req->user_data; |
| 5956 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5957 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5958 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5959 | if (!found) |
| 5960 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5961 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5962 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5963 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5964 | return ERR_PTR(-EALREADY); |
| 5965 | list_del_init(&req->timeout.list); |
| 5966 | return req; |
| 5967 | } |
| 5968 | |
| 5969 | 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] | 5970 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5971 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5972 | { |
| 5973 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5974 | |
| 5975 | if (IS_ERR(req)) |
| 5976 | return PTR_ERR(req); |
| 5977 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5978 | req_set_fail(req); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5979 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5980 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5981 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5982 | } |
| 5983 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 5984 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 5985 | { |
| 5986 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 5987 | case IORING_TIMEOUT_BOOTTIME: |
| 5988 | return CLOCK_BOOTTIME; |
| 5989 | case IORING_TIMEOUT_REALTIME: |
| 5990 | return CLOCK_REALTIME; |
| 5991 | default: |
| 5992 | /* can't happen, vetted at prep time */ |
| 5993 | WARN_ON_ONCE(1); |
| 5994 | fallthrough; |
| 5995 | case 0: |
| 5996 | return CLOCK_MONOTONIC; |
| 5997 | } |
| 5998 | } |
| 5999 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6000 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6001 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 6002 | __must_hold(&ctx->timeout_lock) |
| 6003 | { |
| 6004 | struct io_timeout_data *io; |
| 6005 | struct io_kiocb *req; |
| 6006 | bool found = false; |
| 6007 | |
| 6008 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 6009 | found = user_data == req->user_data; |
| 6010 | if (found) |
| 6011 | break; |
| 6012 | } |
| 6013 | if (!found) |
| 6014 | return -ENOENT; |
| 6015 | |
| 6016 | io = req->async_data; |
| 6017 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 6018 | return -EALREADY; |
| 6019 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 6020 | io->timer.function = io_link_timeout_fn; |
| 6021 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6025 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6026 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6027 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6028 | { |
| 6029 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6030 | struct io_timeout_data *data; |
| 6031 | |
| 6032 | if (IS_ERR(req)) |
| 6033 | return PTR_ERR(req); |
| 6034 | |
| 6035 | req->timeout.off = 0; /* noseq */ |
| 6036 | data = req->async_data; |
| 6037 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6038 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6039 | data->timer.function = io_timeout_fn; |
| 6040 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 6041 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6042 | } |
| 6043 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6044 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 6045 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6046 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6047 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 6048 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6049 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6050 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6051 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6052 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6053 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6054 | return -EINVAL; |
| 6055 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6056 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6057 | tr->addr = READ_ONCE(sqe->addr); |
| 6058 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6059 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6060 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6061 | return -EINVAL; |
| 6062 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6063 | tr->ltimeout = true; |
| 6064 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6065 | return -EINVAL; |
| 6066 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6067 | return -EFAULT; |
| 6068 | } else if (tr->flags) { |
| 6069 | /* timeout removal doesn't support flags */ |
| 6070 | return -EINVAL; |
| 6071 | } |
| 6072 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6073 | return 0; |
| 6074 | } |
| 6075 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6076 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6077 | { |
| 6078 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6079 | : HRTIMER_MODE_REL; |
| 6080 | } |
| 6081 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6082 | /* |
| 6083 | * Remove or update an existing timeout command |
| 6084 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6085 | 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] | 6086 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6087 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6088 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6089 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6090 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6091 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6092 | spin_lock(&ctx->completion_lock); |
| 6093 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6094 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6095 | spin_unlock_irq(&ctx->timeout_lock); |
| 6096 | spin_unlock(&ctx->completion_lock); |
| 6097 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6098 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6099 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6100 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6101 | if (tr->ltimeout) |
| 6102 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6103 | else |
| 6104 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6105 | spin_unlock_irq(&ctx->timeout_lock); |
| 6106 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6107 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6108 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6109 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6110 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6111 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6112 | } |
| 6113 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6114 | 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] | 6115 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6116 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6117 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6118 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6119 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6120 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6121 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6122 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6123 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6124 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6125 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6126 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6127 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6128 | flags = READ_ONCE(sqe->timeout_flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6129 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
| 6130 | return -EINVAL; |
| 6131 | /* more than one clock specified is invalid, obviously */ |
| 6132 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6133 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6134 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6135 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6136 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6137 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6138 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6139 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6140 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6141 | return -ENOMEM; |
| 6142 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6143 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6144 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6145 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6146 | |
| 6147 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6148 | return -EFAULT; |
| 6149 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6150 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6151 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6152 | |
| 6153 | if (is_timeout_link) { |
| 6154 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6155 | |
| 6156 | if (!link->head) |
| 6157 | return -EINVAL; |
| 6158 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6159 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6160 | req->timeout.head = link->last; |
| 6161 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6162 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6163 | return 0; |
| 6164 | } |
| 6165 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6166 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6167 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6168 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6169 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6170 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6171 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6172 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6173 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6174 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6175 | /* |
| 6176 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6177 | * timeout event to be satisfied. If it isn't set, then this is |
| 6178 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6179 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6180 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6181 | entry = ctx->timeout_list.prev; |
| 6182 | goto add; |
| 6183 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6184 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6185 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6186 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6187 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6188 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6189 | * This is safe because ->completion_lock is held, and submissions |
| 6190 | * and completions are never mixed in the same ->completion_lock section. |
| 6191 | */ |
| 6192 | ctx->cq_last_tm_flush = tail; |
| 6193 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6194 | /* |
| 6195 | * Insertion sort, ensuring the first entry in the list is always |
| 6196 | * the one we need first. |
| 6197 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6198 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6199 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6200 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6201 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6202 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6203 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6204 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6205 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6206 | break; |
| 6207 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6208 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6209 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6210 | data->timer.function = io_timeout_fn; |
| 6211 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6212 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6213 | return 0; |
| 6214 | } |
| 6215 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6216 | struct io_cancel_data { |
| 6217 | struct io_ring_ctx *ctx; |
| 6218 | u64 user_data; |
| 6219 | }; |
| 6220 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6221 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6222 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6223 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6224 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6225 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6226 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6227 | } |
| 6228 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6229 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6230 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6231 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6232 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6233 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6234 | int ret = 0; |
| 6235 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6236 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6237 | return -ENOENT; |
| 6238 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6239 | 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] | 6240 | switch (cancel_ret) { |
| 6241 | case IO_WQ_CANCEL_OK: |
| 6242 | ret = 0; |
| 6243 | break; |
| 6244 | case IO_WQ_CANCEL_RUNNING: |
| 6245 | ret = -EALREADY; |
| 6246 | break; |
| 6247 | case IO_WQ_CANCEL_NOTFOUND: |
| 6248 | ret = -ENOENT; |
| 6249 | break; |
| 6250 | } |
| 6251 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6252 | return ret; |
| 6253 | } |
| 6254 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6255 | 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] | 6256 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6257 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6258 | int ret; |
| 6259 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6260 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6261 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6262 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6263 | if (ret != -ENOENT) |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6264 | return ret; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6265 | |
| 6266 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6267 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6268 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6269 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6270 | if (ret != -ENOENT) |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6271 | goto out; |
| 6272 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6273 | out: |
| 6274 | spin_unlock(&ctx->completion_lock); |
| 6275 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6276 | } |
| 6277 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6278 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6279 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6280 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6281 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6282 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6283 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6284 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6285 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6286 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6287 | return -EINVAL; |
| 6288 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6289 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6293 | 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] | 6294 | { |
| 6295 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6296 | u64 sqe_addr = req->cancel.addr; |
| 6297 | struct io_tctx_node *node; |
| 6298 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6299 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6300 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6301 | if (ret != -ENOENT) |
| 6302 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6303 | |
| 6304 | /* slow path, try all io-wq's */ |
| 6305 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 6306 | ret = -ENOENT; |
| 6307 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6308 | struct io_uring_task *tctx = node->task->io_uring; |
| 6309 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6310 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6311 | if (ret != -ENOENT) |
| 6312 | break; |
| 6313 | } |
| 6314 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6315 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6316 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6317 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6318 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6319 | return 0; |
| 6320 | } |
| 6321 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6322 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6323 | const struct io_uring_sqe *sqe) |
| 6324 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6325 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6326 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6327 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6328 | return -EINVAL; |
| 6329 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6330 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6331 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6332 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6333 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6334 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6335 | return 0; |
| 6336 | } |
| 6337 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6338 | 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] | 6339 | { |
| 6340 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6341 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6342 | int ret; |
| 6343 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6344 | up.offset = req->rsrc_update.offset; |
| 6345 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6346 | up.nr = 0; |
| 6347 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6348 | up.resv = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6349 | |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6350 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6351 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6352 | &up, req->rsrc_update.nr_args); |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6353 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6354 | |
| 6355 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6356 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6357 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6358 | return 0; |
| 6359 | } |
| 6360 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6361 | 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] | 6362 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6363 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6364 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6365 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6366 | case IORING_OP_READV: |
| 6367 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6368 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6369 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6370 | case IORING_OP_WRITEV: |
| 6371 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6372 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6373 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6374 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6375 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6376 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6377 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6378 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6379 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6380 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6381 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6382 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6383 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6384 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6385 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6386 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6387 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6388 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6389 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6390 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6391 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6392 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6393 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6394 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6395 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6396 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6397 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6398 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6399 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6400 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6401 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6402 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6403 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6404 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6405 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6406 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6407 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6408 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6409 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6410 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6411 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6412 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6413 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6414 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6415 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6416 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6417 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6418 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6419 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6420 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6421 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6422 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6423 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6424 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6425 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6426 | case IORING_OP_SHUTDOWN: |
| 6427 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6428 | case IORING_OP_RENAMEAT: |
| 6429 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6430 | case IORING_OP_UNLINKAT: |
| 6431 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6432 | case IORING_OP_MKDIRAT: |
| 6433 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6434 | case IORING_OP_SYMLINKAT: |
| 6435 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6436 | case IORING_OP_LINKAT: |
| 6437 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6438 | } |
| 6439 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6440 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6441 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6442 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6443 | } |
| 6444 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6445 | static int io_req_prep_async(struct io_kiocb *req) |
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 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6448 | return 0; |
| 6449 | if (WARN_ON_ONCE(req->async_data)) |
| 6450 | return -EFAULT; |
| 6451 | if (io_alloc_async_data(req)) |
| 6452 | return -EAGAIN; |
| 6453 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6454 | switch (req->opcode) { |
| 6455 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6456 | return io_rw_prep_async(req, READ); |
| 6457 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6458 | return io_rw_prep_async(req, WRITE); |
| 6459 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6460 | return io_sendmsg_prep_async(req); |
| 6461 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6462 | return io_recvmsg_prep_async(req); |
| 6463 | case IORING_OP_CONNECT: |
| 6464 | return io_connect_prep_async(req); |
| 6465 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6466 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6467 | req->opcode); |
| 6468 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6469 | } |
| 6470 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6471 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6472 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6473 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6474 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6475 | /* need original cached_sq_head, but it was increased for each req */ |
| 6476 | io_for_each_link(req, req) |
| 6477 | seq--; |
| 6478 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6479 | } |
| 6480 | |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6481 | static bool io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6482 | { |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6483 | struct io_kiocb *pos; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6484 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6485 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6486 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6487 | u32 seq; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6488 | |
Pavel Begunkov | b8ce1b9 | 2021-08-31 14:13:11 +0100 | [diff] [blame] | 6489 | if (req->flags & REQ_F_FAIL) { |
| 6490 | io_req_complete_fail_submit(req); |
| 6491 | return true; |
| 6492 | } |
| 6493 | |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6494 | /* |
| 6495 | * If we need to drain a request in the middle of a link, drain the |
| 6496 | * head request and the next request/link after the current link. |
| 6497 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be |
| 6498 | * maintained for every request of our link. |
| 6499 | */ |
| 6500 | if (ctx->drain_next) { |
| 6501 | req->flags |= REQ_F_IO_DRAIN; |
| 6502 | ctx->drain_next = false; |
| 6503 | } |
| 6504 | /* not interested in head, start from the first linked */ |
| 6505 | io_for_each_link(pos, req->link) { |
| 6506 | if (pos->flags & REQ_F_IO_DRAIN) { |
| 6507 | ctx->drain_next = true; |
| 6508 | req->flags |= REQ_F_IO_DRAIN; |
| 6509 | break; |
| 6510 | } |
| 6511 | } |
| 6512 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6513 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6514 | if (likely(list_empty_careful(&ctx->defer_list) && |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6515 | !(req->flags & REQ_F_IO_DRAIN))) { |
| 6516 | ctx->drain_active = false; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6517 | return false; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6518 | } |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6519 | |
| 6520 | seq = io_get_sequence(req); |
| 6521 | /* Still a chance to pass the sequence check */ |
| 6522 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6523 | return false; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6524 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6525 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6526 | if (ret) |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6527 | goto fail; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6528 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6529 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6530 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6531 | ret = -ENOMEM; |
| 6532 | fail: |
| 6533 | io_req_complete_failed(req, ret); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6534 | return true; |
| 6535 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6536 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6537 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6538 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6539 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6540 | kfree(de); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6541 | io_queue_async_work(req, NULL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6542 | return true; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6543 | } |
| 6544 | |
| 6545 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6546 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6547 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6548 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6549 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6550 | return true; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6551 | } |
| 6552 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6553 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6554 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6555 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6556 | switch (req->opcode) { |
| 6557 | case IORING_OP_READV: |
| 6558 | case IORING_OP_READ_FIXED: |
| 6559 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6560 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6561 | break; |
| 6562 | case IORING_OP_RECVMSG: |
| 6563 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6564 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6565 | break; |
| 6566 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6567 | } |
| 6568 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6569 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6570 | switch (req->opcode) { |
| 6571 | case IORING_OP_READV: |
| 6572 | case IORING_OP_READ_FIXED: |
| 6573 | case IORING_OP_READ: |
| 6574 | case IORING_OP_WRITEV: |
| 6575 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6576 | case IORING_OP_WRITE: { |
| 6577 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6578 | |
| 6579 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6580 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6581 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6582 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6583 | case IORING_OP_SENDMSG: { |
| 6584 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6585 | |
| 6586 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6587 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6588 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6589 | case IORING_OP_SPLICE: |
| 6590 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6591 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6592 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6593 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6594 | case IORING_OP_OPENAT: |
| 6595 | case IORING_OP_OPENAT2: |
| 6596 | if (req->open.filename) |
| 6597 | putname(req->open.filename); |
| 6598 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6599 | case IORING_OP_RENAMEAT: |
| 6600 | putname(req->rename.oldpath); |
| 6601 | putname(req->rename.newpath); |
| 6602 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6603 | case IORING_OP_UNLINKAT: |
| 6604 | putname(req->unlink.filename); |
| 6605 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6606 | case IORING_OP_MKDIRAT: |
| 6607 | putname(req->mkdir.filename); |
| 6608 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6609 | case IORING_OP_SYMLINKAT: |
| 6610 | putname(req->symlink.oldpath); |
| 6611 | putname(req->symlink.newpath); |
| 6612 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6613 | case IORING_OP_LINKAT: |
| 6614 | putname(req->hardlink.oldpath); |
| 6615 | putname(req->hardlink.newpath); |
| 6616 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6617 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6618 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6619 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6620 | kfree(req->apoll->double_poll); |
| 6621 | kfree(req->apoll); |
| 6622 | req->apoll = NULL; |
| 6623 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6624 | if (req->flags & REQ_F_INFLIGHT) { |
| 6625 | struct io_uring_task *tctx = req->task->io_uring; |
| 6626 | |
| 6627 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6628 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6629 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6630 | put_cred(req->creds); |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6631 | |
| 6632 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6633 | } |
| 6634 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6635 | 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] | 6636 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6637 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6638 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6639 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6640 | |
Pavel Begunkov | 6878b40 | 2021-09-24 21:59:41 +0100 | [diff] [blame^] | 6641 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6642 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6643 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6644 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6645 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6646 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6647 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6648 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6649 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6650 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6651 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6652 | break; |
| 6653 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6654 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6655 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6656 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6657 | break; |
| 6658 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6659 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6660 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6661 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6662 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6663 | break; |
| 6664 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6665 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6666 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6667 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6668 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6669 | break; |
| 6670 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6671 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6672 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6673 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6674 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6675 | break; |
| 6676 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6677 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6678 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6679 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6680 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6681 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6682 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6683 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6684 | break; |
| 6685 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6686 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6687 | break; |
| 6688 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6689 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6690 | break; |
| 6691 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6692 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6693 | break; |
| 6694 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6695 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6696 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6697 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6698 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6699 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6700 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6701 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6702 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6703 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6704 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6705 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6706 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6707 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6708 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6709 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6710 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6711 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6712 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6713 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6714 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6715 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6716 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6717 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6718 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6719 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6720 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6721 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6722 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6723 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6724 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6725 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6726 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6727 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6728 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6729 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6730 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6731 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6732 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6733 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6734 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6735 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6736 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6737 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6738 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6739 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6740 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6741 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6742 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6743 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6744 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6745 | case IORING_OP_MKDIRAT: |
| 6746 | ret = io_mkdirat(req, issue_flags); |
| 6747 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6748 | case IORING_OP_SYMLINKAT: |
| 6749 | ret = io_symlinkat(req, issue_flags); |
| 6750 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6751 | case IORING_OP_LINKAT: |
| 6752 | ret = io_linkat(req, issue_flags); |
| 6753 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6754 | default: |
| 6755 | ret = -EINVAL; |
| 6756 | break; |
| 6757 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6758 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6759 | if (creds) |
| 6760 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6761 | if (ret) |
| 6762 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6763 | /* 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] | 6764 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
| 6765 | io_iopoll_req_issued(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6766 | |
| 6767 | return 0; |
| 6768 | } |
| 6769 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 6770 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 6771 | { |
| 6772 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6773 | |
| 6774 | req = io_put_req_find_next(req); |
| 6775 | return req ? &req->work : NULL; |
| 6776 | } |
| 6777 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6778 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6779 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6780 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6781 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6782 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6783 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6784 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 6785 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 6786 | __io_req_set_refcount(req, 2); |
| 6787 | else |
| 6788 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6789 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6790 | timeout = io_prep_linked_timeout(req); |
| 6791 | if (timeout) |
| 6792 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6793 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6794 | /* 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] | 6795 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6796 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6797 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6798 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6799 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6800 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6801 | /* |
| 6802 | * We can get EAGAIN for polled IO even though we're |
| 6803 | * forcing a sync submission from here, since we can't |
| 6804 | * wait for request slots on the block side. |
| 6805 | */ |
| 6806 | if (ret != -EAGAIN) |
| 6807 | break; |
| 6808 | cond_resched(); |
| 6809 | } while (1); |
| 6810 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6811 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6812 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6813 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6814 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6815 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6816 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6817 | 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] | 6818 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6819 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6820 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6823 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6824 | int index) |
| 6825 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6826 | 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] | 6827 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6828 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6829 | } |
| 6830 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6831 | 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] | 6832 | { |
| 6833 | unsigned long file_ptr = (unsigned long) file; |
| 6834 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6835 | if (__io_file_supports_nowait(file, READ)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6836 | file_ptr |= FFS_ASYNC_READ; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6837 | if (__io_file_supports_nowait(file, WRITE)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6838 | file_ptr |= FFS_ASYNC_WRITE; |
| 6839 | if (S_ISREG(file_inode(file)->i_mode)) |
| 6840 | file_ptr |= FFS_ISREG; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6841 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6842 | } |
| 6843 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6844 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
| 6845 | struct io_kiocb *req, int fd) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6846 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6847 | struct file *file; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6848 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6849 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6850 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
| 6851 | return NULL; |
| 6852 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6853 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 6854 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6855 | file_ptr &= ~FFS_MASK; |
| 6856 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 6857 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6858 | io_req_set_rsrc_node(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6859 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6860 | } |
| 6861 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6862 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6863 | struct io_kiocb *req, int fd) |
| 6864 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6865 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6866 | |
| 6867 | trace_io_uring_file_get(ctx, fd); |
| 6868 | |
| 6869 | /* we don't allow fixed io_uring files */ |
| 6870 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6871 | io_req_track_inflight(req); |
| 6872 | return file; |
| 6873 | } |
| 6874 | |
| 6875 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6876 | struct io_kiocb *req, int fd, bool fixed) |
| 6877 | { |
| 6878 | if (fixed) |
| 6879 | return io_file_get_fixed(ctx, req, fd); |
| 6880 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6881 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6882 | } |
| 6883 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6884 | 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] | 6885 | { |
| 6886 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6887 | int ret; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6888 | |
| 6889 | if (prev) { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6890 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6891 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6892 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6893 | } else { |
| 6894 | io_req_complete_post(req, -ETIME, 0); |
| 6895 | } |
| 6896 | } |
| 6897 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6898 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6899 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6900 | struct io_timeout_data *data = container_of(timer, |
| 6901 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6902 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6903 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6904 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6905 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6906 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6907 | prev = req->timeout.head; |
| 6908 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6909 | |
| 6910 | /* |
| 6911 | * We don't expect the list to be empty, that will only happen if we |
| 6912 | * race with the completion of the linked work. |
| 6913 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6914 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6915 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6916 | if (!req_ref_inc_not_zero(prev)) |
| 6917 | prev = NULL; |
| 6918 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6919 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6920 | req->timeout.prev = prev; |
| 6921 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6922 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6923 | req->io_task_work.func = io_req_task_link_timeout; |
| 6924 | io_req_task_work_add(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6925 | return HRTIMER_NORESTART; |
| 6926 | } |
| 6927 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6928 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6929 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6930 | struct io_ring_ctx *ctx = req->ctx; |
| 6931 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6932 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6933 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6934 | * If the back reference is NULL, then our linked request finished |
| 6935 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6936 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6937 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6938 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6939 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6940 | data->timer.function = io_link_timeout_fn; |
| 6941 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6942 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6943 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6944 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6945 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6946 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6947 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6948 | } |
| 6949 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6950 | static void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 6951 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6952 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6953 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 6954 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6955 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 6956 | issue_sqe: |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6957 | 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] | 6958 | |
| 6959 | /* |
| 6960 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6961 | * doesn't support non-blocking read/write attempts |
| 6962 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6963 | if (likely(!ret)) { |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6964 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6965 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 6966 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6967 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 6968 | state->compl_reqs[state->compl_nr++] = req; |
| 6969 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) |
Pavel Begunkov | 2a2758f | 2021-06-17 18:14:00 +0100 | [diff] [blame] | 6970 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6971 | return; |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6972 | } |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6973 | |
| 6974 | linked_timeout = io_prep_linked_timeout(req); |
| 6975 | if (linked_timeout) |
| 6976 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6977 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6978 | linked_timeout = io_prep_linked_timeout(req); |
| 6979 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 6980 | switch (io_arm_poll_handler(req)) { |
| 6981 | case IO_APOLL_READY: |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6982 | if (linked_timeout) |
| 6983 | io_unprep_linked_timeout(req); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 6984 | goto issue_sqe; |
| 6985 | case IO_APOLL_ABORTED: |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6986 | /* |
| 6987 | * Queued up for async execution, worker will release |
| 6988 | * submit reference when the iocb is actually submitted. |
| 6989 | */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6990 | io_queue_async_work(req, NULL); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 6991 | break; |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6992 | } |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 6993 | |
| 6994 | if (linked_timeout) |
| 6995 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6996 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6997 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6998 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6999 | } |
| 7000 | |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7001 | static inline void io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7002 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7003 | { |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 7004 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7005 | return; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7006 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7007 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7008 | __io_queue_sqe(req); |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7009 | } else if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 7010 | io_req_complete_fail_submit(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7011 | } else { |
| 7012 | int ret = io_req_prep_async(req); |
| 7013 | |
| 7014 | if (unlikely(ret)) |
| 7015 | io_req_complete_failed(req, ret); |
| 7016 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7017 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 7018 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7019 | } |
| 7020 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7021 | /* |
| 7022 | * Check SQE restrictions (opcode and flags). |
| 7023 | * |
| 7024 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 7025 | */ |
| 7026 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 7027 | struct io_kiocb *req, |
| 7028 | unsigned int sqe_flags) |
| 7029 | { |
Pavel Begunkov | 4cfb25b | 2021-06-26 21:40:47 +0100 | [diff] [blame] | 7030 | if (likely(!ctx->restricted)) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7031 | return true; |
| 7032 | |
| 7033 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 7034 | return false; |
| 7035 | |
| 7036 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 7037 | ctx->restrictions.sqe_flags_required) |
| 7038 | return false; |
| 7039 | |
| 7040 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 7041 | ctx->restrictions.sqe_flags_required)) |
| 7042 | return false; |
| 7043 | |
| 7044 | return true; |
| 7045 | } |
| 7046 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7047 | 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] | 7048 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7049 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7050 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7051 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7052 | unsigned int sqe_flags; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7053 | int personality, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7054 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 7055 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7056 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7057 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 7058 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7059 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7060 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7061 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7062 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7063 | |
| 7064 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 7065 | return -EINVAL; |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7066 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
| 7067 | /* enforce forwards compatibility on users */ |
| 7068 | if (sqe_flags & ~SQE_VALID_FLAGS) |
| 7069 | return -EINVAL; |
| 7070 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 7071 | !io_op_defs[req->opcode].buffer_select) |
| 7072 | return -EOPNOTSUPP; |
| 7073 | if (sqe_flags & IOSQE_IO_DRAIN) |
| 7074 | ctx->drain_active = true; |
| 7075 | } |
Pavel Begunkov | 4cfb25b | 2021-06-26 21:40:47 +0100 | [diff] [blame] | 7076 | if (!io_check_restriction(ctx, req, sqe_flags)) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7077 | return -EACCES; |
| 7078 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7079 | personality = READ_ONCE(sqe->personality); |
| 7080 | if (personality) { |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7081 | req->creds = xa_load(&ctx->personalities, personality); |
| 7082 | if (!req->creds) |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7083 | return -EINVAL; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7084 | get_cred(req->creds); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 7085 | req->flags |= REQ_F_CREDS; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7086 | } |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7087 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7088 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7089 | /* |
| 7090 | * Plug now if we have more than 1 IO left after this, and the target |
| 7091 | * is potentially a read/write to block based storage. |
| 7092 | */ |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7093 | if (state->need_plug && io_op_defs[req->opcode].plug) { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7094 | blk_start_plug(&state->plug); |
| 7095 | state->plug_started = true; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7096 | state->need_plug = false; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7097 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7098 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7099 | if (io_op_defs[req->opcode].needs_file) { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7100 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7101 | (sqe_flags & IOSQE_FIXED_FILE)); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7102 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7103 | ret = -EBADF; |
| 7104 | } |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 7105 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7106 | } |
| 7107 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7108 | 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] | 7109 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7110 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7111 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7112 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7113 | int ret; |
| 7114 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7115 | ret = io_init_req(ctx, req, sqe); |
| 7116 | if (unlikely(ret)) { |
| 7117 | fail_req: |
Jens Axboe | a87acfd | 2021-09-11 16:04:50 -0600 | [diff] [blame] | 7118 | trace_io_uring_req_failed(sqe, ret); |
| 7119 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7120 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7121 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7122 | /* |
| 7123 | * we can judge a link req is failed or cancelled by if |
| 7124 | * REQ_F_FAIL is set, but the head is an exception since |
| 7125 | * it may be set REQ_F_FAIL because of other req's failure |
| 7126 | * so let's leverage req->result to distinguish if a head |
| 7127 | * is set REQ_F_FAIL because of its failure or other req's |
| 7128 | * failure so that we can set the correct ret code for it. |
| 7129 | * init result here to avoid affecting the normal path. |
| 7130 | */ |
| 7131 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7132 | req_fail_link_node(link->head, -ECANCELED); |
| 7133 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7134 | /* |
| 7135 | * the current req is a normal req, we should return |
| 7136 | * error and thus break the submittion loop. |
| 7137 | */ |
| 7138 | io_req_complete_failed(req, ret); |
| 7139 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7140 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7141 | req_fail_link_node(req, ret); |
| 7142 | } else { |
| 7143 | ret = io_req_prep(req, sqe); |
| 7144 | if (unlikely(ret)) |
| 7145 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7146 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7147 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7148 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7149 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7150 | req->flags, true, |
| 7151 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7152 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7153 | /* |
| 7154 | * If we already have a head request, queue this one for async |
| 7155 | * submittal once the head completes. If we don't have a head but |
| 7156 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7157 | * submitted sync once the chain is complete. If none of those |
| 7158 | * conditions are true (normal request), then just queue it. |
| 7159 | */ |
| 7160 | if (link->head) { |
| 7161 | struct io_kiocb *head = link->head; |
| 7162 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7163 | if (!(req->flags & REQ_F_FAIL)) { |
| 7164 | ret = io_req_prep_async(req); |
| 7165 | if (unlikely(ret)) { |
| 7166 | req_fail_link_node(req, ret); |
| 7167 | if (!(head->flags & REQ_F_FAIL)) |
| 7168 | req_fail_link_node(head, -ECANCELED); |
| 7169 | } |
| 7170 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7171 | trace_io_uring_link(ctx, req, head); |
| 7172 | link->last->link = req; |
| 7173 | link->last = req; |
| 7174 | |
| 7175 | /* last request of a link, enqueue the link */ |
| 7176 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7177 | link->head = NULL; |
Pavel Begunkov | 5e15920 | 2021-06-14 23:37:26 +0100 | [diff] [blame] | 7178 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7179 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7180 | } else { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7181 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7182 | link->head = req; |
| 7183 | link->last = req; |
| 7184 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7185 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | return 0; |
| 7190 | } |
| 7191 | |
| 7192 | /* |
| 7193 | * Batched submission is done, ensure local IO is flushed out. |
| 7194 | */ |
| 7195 | static void io_submit_state_end(struct io_submit_state *state, |
| 7196 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 7197 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7198 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7199 | io_queue_sqe(state->link.head); |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 7200 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7201 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7202 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7203 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7204 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7205 | /* |
| 7206 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7207 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7208 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7209 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7210 | { |
| 7211 | state->plug_started = false; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7212 | state->need_plug = max_ios > 2; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7213 | /* set only head, no need to init link_last in advance */ |
| 7214 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7215 | } |
| 7216 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7217 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7218 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7219 | struct io_rings *rings = ctx->rings; |
| 7220 | |
| 7221 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7222 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7223 | * since once we write the new head, the application could |
| 7224 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7225 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7226 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7227 | } |
| 7228 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7229 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7230 | * 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] | 7231 | * that is mapped by userspace. This means that care needs to be taken to |
| 7232 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7233 | * being a good citizen. If members of the sqe are validated and then later |
| 7234 | * 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] | 7235 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7236 | */ |
| 7237 | 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] | 7238 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7239 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7240 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7241 | |
| 7242 | /* |
| 7243 | * The cached sq head (or cq tail) serves two purposes: |
| 7244 | * |
| 7245 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7246 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7247 | * 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] | 7248 | * though the application is the one updating it. |
| 7249 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7250 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7251 | if (likely(head < ctx->sq_entries)) |
| 7252 | return &ctx->sq_sqes[head]; |
| 7253 | |
| 7254 | /* drop invalid entries */ |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7255 | ctx->cq_extra--; |
| 7256 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7257 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7258 | return NULL; |
| 7259 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7260 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7261 | 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] | 7262 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7263 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7264 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7265 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7266 | /* make sure SQ entry isn't read before tail */ |
| 7267 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 7268 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 7269 | return -EAGAIN; |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7270 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7271 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7272 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7273 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7274 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7275 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7276 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7277 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7278 | if (unlikely(!req)) { |
| 7279 | if (!submitted) |
| 7280 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7281 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7282 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7283 | sqe = io_get_sqe(ctx); |
| 7284 | if (unlikely(!sqe)) { |
Hao Xu | 0c6e1d7 | 2021-08-26 01:58:56 +0800 | [diff] [blame] | 7285 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7286 | break; |
| 7287 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7288 | /* will complete beyond this point, count as submitted */ |
| 7289 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7290 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7291 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7292 | } |
| 7293 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7294 | if (unlikely(submitted != nr)) { |
| 7295 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7296 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7297 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7298 | current->io_uring->cached_refs += unused; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7299 | percpu_ref_put_many(&ctx->refs, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7300 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7301 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7302 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7303 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7304 | io_commit_sqring(ctx); |
| 7305 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7306 | return submitted; |
| 7307 | } |
| 7308 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7309 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7310 | { |
| 7311 | return READ_ONCE(sqd->state); |
| 7312 | } |
| 7313 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7314 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7315 | { |
| 7316 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7317 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7318 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7319 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7320 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7321 | } |
| 7322 | |
| 7323 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7324 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7325 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7326 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7327 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7328 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7329 | } |
| 7330 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7331 | 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] | 7332 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7333 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7334 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7335 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7336 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7337 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7338 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7339 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7340 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7341 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 7342 | unsigned nr_events = 0; |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7343 | const struct cred *creds = NULL; |
| 7344 | |
| 7345 | if (ctx->sq_creds != current_cred()) |
| 7346 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7347 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7348 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7349 | if (!list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 7350 | io_do_iopoll(ctx, &nr_events, 0); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7351 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7352 | /* |
| 7353 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7354 | * but also it is relied upon by io_ring_exit_work() |
| 7355 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7356 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7357 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7358 | ret = io_submit_sqes(ctx, to_submit); |
| 7359 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7360 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7361 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7362 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7363 | if (creds) |
| 7364 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7365 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7366 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7367 | return ret; |
| 7368 | } |
| 7369 | |
| 7370 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7371 | { |
| 7372 | struct io_ring_ctx *ctx; |
| 7373 | unsigned sq_thread_idle = 0; |
| 7374 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7375 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7376 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7377 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7378 | } |
| 7379 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7380 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7381 | { |
| 7382 | bool did_sig = false; |
| 7383 | struct ksignal ksig; |
| 7384 | |
| 7385 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7386 | signal_pending(current)) { |
| 7387 | mutex_unlock(&sqd->lock); |
| 7388 | if (signal_pending(current)) |
| 7389 | did_sig = get_signal(&ksig); |
| 7390 | cond_resched(); |
| 7391 | mutex_lock(&sqd->lock); |
| 7392 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7393 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7394 | } |
| 7395 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7396 | static int io_sq_thread(void *data) |
| 7397 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7398 | struct io_sq_data *sqd = data; |
| 7399 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7400 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7401 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7402 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7403 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7404 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7405 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7406 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7407 | if (sqd->sq_cpu != -1) |
| 7408 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7409 | else |
| 7410 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7411 | current->flags |= PF_NO_SETAFFINITY; |
| 7412 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7413 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7414 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7415 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7416 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7417 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7418 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7419 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7420 | timeout = jiffies + sqd->sq_thread_idle; |
| 7421 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7422 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7423 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7424 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7425 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7426 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7427 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7428 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7429 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7430 | if (io_run_task_work()) |
| 7431 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7432 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7433 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7434 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7435 | if (sqt_spin) |
| 7436 | timeout = jiffies + sqd->sq_thread_idle; |
| 7437 | continue; |
| 7438 | } |
| 7439 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7440 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7441 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7442 | bool needs_sched = true; |
| 7443 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7444 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7445 | io_ring_set_wakeup_flag(ctx); |
| 7446 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7447 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7448 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7449 | needs_sched = false; |
| 7450 | break; |
| 7451 | } |
| 7452 | if (io_sqring_entries(ctx)) { |
| 7453 | needs_sched = false; |
| 7454 | break; |
| 7455 | } |
| 7456 | } |
| 7457 | |
| 7458 | if (needs_sched) { |
| 7459 | mutex_unlock(&sqd->lock); |
| 7460 | schedule(); |
| 7461 | mutex_lock(&sqd->lock); |
| 7462 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7463 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7464 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7465 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7466 | |
| 7467 | finish_wait(&sqd->wait, &wait); |
| 7468 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7469 | } |
| 7470 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7471 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7472 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7473 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7474 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7475 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7476 | mutex_unlock(&sqd->lock); |
| 7477 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7478 | complete(&sqd->exited); |
| 7479 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7480 | } |
| 7481 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7482 | struct io_wait_queue { |
| 7483 | struct wait_queue_entry wq; |
| 7484 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7485 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7486 | unsigned nr_timeouts; |
| 7487 | }; |
| 7488 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7489 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7490 | { |
| 7491 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7492 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7493 | |
| 7494 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7495 | * 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] | 7496 | * started waiting. For timeouts, we always want to return to userspace, |
| 7497 | * regardless of event count. |
| 7498 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7499 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7500 | } |
| 7501 | |
| 7502 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7503 | int wake_flags, void *key) |
| 7504 | { |
| 7505 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7506 | wq); |
| 7507 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7508 | /* |
| 7509 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7510 | * the task, and the next invocation will do it. |
| 7511 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7512 | 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] | 7513 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7514 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7515 | } |
| 7516 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7517 | static int io_run_task_work_sig(void) |
| 7518 | { |
| 7519 | if (io_run_task_work()) |
| 7520 | return 1; |
| 7521 | if (!signal_pending(current)) |
| 7522 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7523 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7524 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7525 | return -EINTR; |
| 7526 | } |
| 7527 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7528 | /* when returns >0, the caller should retry */ |
| 7529 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7530 | struct io_wait_queue *iowq, |
| 7531 | signed long *timeout) |
| 7532 | { |
| 7533 | int ret; |
| 7534 | |
| 7535 | /* make sure we run task_work before checking for signals */ |
| 7536 | ret = io_run_task_work_sig(); |
| 7537 | if (ret || io_should_wake(iowq)) |
| 7538 | return ret; |
| 7539 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7540 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7541 | return 1; |
| 7542 | |
| 7543 | *timeout = schedule_timeout(*timeout); |
| 7544 | return !*timeout ? -ETIME : 1; |
| 7545 | } |
| 7546 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7547 | /* |
| 7548 | * Wait until events become available, if we don't already have some. The |
| 7549 | * application must reap them itself, as they reside on the shared cq ring. |
| 7550 | */ |
| 7551 | 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] | 7552 | const sigset_t __user *sig, size_t sigsz, |
| 7553 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7554 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7555 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7556 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7557 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7558 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7559 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7560 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7561 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7562 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7563 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7564 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7565 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7566 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7567 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7568 | if (uts) { |
| 7569 | struct timespec64 ts; |
| 7570 | |
| 7571 | if (get_timespec64(&ts, uts)) |
| 7572 | return -EFAULT; |
| 7573 | timeout = timespec64_to_jiffies(&ts); |
| 7574 | } |
| 7575 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7576 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7577 | #ifdef CONFIG_COMPAT |
| 7578 | if (in_compat_syscall()) |
| 7579 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7580 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7581 | else |
| 7582 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7583 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7584 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7585 | if (ret) |
| 7586 | return ret; |
| 7587 | } |
| 7588 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7589 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7590 | iowq.wq.private = current; |
| 7591 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7592 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7593 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7594 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7595 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7596 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7597 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7598 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7599 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7600 | ret = -EBUSY; |
| 7601 | break; |
| 7602 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7603 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7604 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7605 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7606 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7607 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7608 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7609 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7610 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7611 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7612 | 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] | 7613 | } |
| 7614 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7615 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7616 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7617 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7618 | |
| 7619 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7620 | kfree(table[i]); |
| 7621 | kfree(table); |
| 7622 | } |
| 7623 | |
| 7624 | static void **io_alloc_page_table(size_t size) |
| 7625 | { |
| 7626 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7627 | size_t init_size = size; |
| 7628 | void **table; |
| 7629 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7630 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7631 | if (!table) |
| 7632 | return NULL; |
| 7633 | |
| 7634 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7635 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7636 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7637 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7638 | if (!table[i]) { |
| 7639 | io_free_page_table(table, init_size); |
| 7640 | return NULL; |
| 7641 | } |
| 7642 | size -= this_size; |
| 7643 | } |
| 7644 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7645 | } |
| 7646 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7647 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7648 | { |
| 7649 | percpu_ref_exit(&ref_node->refs); |
| 7650 | kfree(ref_node); |
| 7651 | } |
| 7652 | |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7653 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
| 7654 | { |
| 7655 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7656 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7657 | unsigned long flags; |
| 7658 | bool first_add = false; |
| 7659 | |
| 7660 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7661 | node->done = true; |
| 7662 | |
| 7663 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7664 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7665 | struct io_rsrc_node, node); |
| 7666 | /* recycle ref nodes in order */ |
| 7667 | if (!node->done) |
| 7668 | break; |
| 7669 | list_del(&node->node); |
| 7670 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7671 | } |
| 7672 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7673 | |
| 7674 | if (first_add) |
| 7675 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
| 7676 | } |
| 7677 | |
| 7678 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
| 7679 | { |
| 7680 | struct io_rsrc_node *ref_node; |
| 7681 | |
| 7682 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7683 | if (!ref_node) |
| 7684 | return NULL; |
| 7685 | |
| 7686 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 7687 | 0, GFP_KERNEL)) { |
| 7688 | kfree(ref_node); |
| 7689 | return NULL; |
| 7690 | } |
| 7691 | INIT_LIST_HEAD(&ref_node->node); |
| 7692 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 7693 | ref_node->done = false; |
| 7694 | return ref_node; |
| 7695 | } |
| 7696 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7697 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7698 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7699 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7700 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7701 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7702 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7703 | if (data_to_kill) { |
| 7704 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7705 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7706 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7707 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7708 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7709 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7710 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7711 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7712 | percpu_ref_kill(&rsrc_node->refs); |
| 7713 | ctx->rsrc_node = NULL; |
| 7714 | } |
| 7715 | |
| 7716 | if (!ctx->rsrc_node) { |
| 7717 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7718 | ctx->rsrc_backup_node = NULL; |
| 7719 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7720 | } |
| 7721 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7722 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7723 | { |
| 7724 | if (ctx->rsrc_backup_node) |
| 7725 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7726 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7727 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7728 | } |
| 7729 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7730 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7731 | { |
| 7732 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7733 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7734 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7735 | if (data->quiesce) |
| 7736 | return -ENXIO; |
| 7737 | |
| 7738 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7739 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7740 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7741 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7742 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7743 | io_rsrc_node_switch(ctx, data); |
| 7744 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7745 | /* kill initial ref, already quiesced if zero */ |
| 7746 | if (atomic_dec_and_test(&data->refs)) |
| 7747 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7748 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7749 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7750 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7751 | if (!ret) { |
| 7752 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7753 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7754 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7755 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7756 | atomic_inc(&data->refs); |
| 7757 | /* wait for all works potentially completing data->done */ |
| 7758 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7759 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7760 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7761 | ret = io_run_task_work_sig(); |
| 7762 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7763 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7764 | data->quiesce = false; |
| 7765 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7766 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7769 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 7770 | { |
| 7771 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 7772 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 7773 | |
| 7774 | return &data->tags[table_idx][off]; |
| 7775 | } |
| 7776 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7777 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7778 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7779 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 7780 | |
| 7781 | if (data->tags) |
| 7782 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7783 | kfree(data); |
| 7784 | } |
| 7785 | |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7786 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 7787 | u64 __user *utags, unsigned nr, |
| 7788 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7789 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7790 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7791 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7792 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7793 | |
| 7794 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7795 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7796 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7797 | 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] | 7798 | if (!data->tags) { |
| 7799 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7800 | return -ENOMEM; |
| 7801 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7802 | |
| 7803 | data->nr = nr; |
| 7804 | data->ctx = ctx; |
| 7805 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7806 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7807 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7808 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 7809 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 7810 | |
| 7811 | if (copy_from_user(tag_slot, &utags[i], |
| 7812 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7813 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7814 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7815 | } |
| 7816 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7817 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7818 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7819 | *pdata = data; |
| 7820 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7821 | fail: |
| 7822 | io_rsrc_data_free(data); |
| 7823 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7826 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 7827 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7828 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 7829 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7830 | return !!table->files; |
| 7831 | } |
| 7832 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7833 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7834 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7835 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7836 | table->files = NULL; |
| 7837 | } |
| 7838 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7839 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7840 | { |
| 7841 | #if defined(CONFIG_UNIX) |
| 7842 | if (ctx->ring_sock) { |
| 7843 | struct sock *sock = ctx->ring_sock->sk; |
| 7844 | struct sk_buff *skb; |
| 7845 | |
| 7846 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7847 | kfree_skb(skb); |
| 7848 | } |
| 7849 | #else |
| 7850 | int i; |
| 7851 | |
| 7852 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7853 | struct file *file; |
| 7854 | |
| 7855 | file = io_file_from_index(ctx, i); |
| 7856 | if (file) |
| 7857 | fput(file); |
| 7858 | } |
| 7859 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7860 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7861 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7862 | ctx->file_data = NULL; |
| 7863 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7864 | } |
| 7865 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7866 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7867 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7868 | int ret; |
| 7869 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7870 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7871 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7872 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7873 | if (!ret) |
| 7874 | __io_sqe_files_unregister(ctx); |
| 7875 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7876 | } |
| 7877 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7878 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7879 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7880 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7881 | WARN_ON_ONCE(sqd->thread == current); |
| 7882 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7883 | /* |
| 7884 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7885 | * other threads incrementing park_pending and setting the bit. |
| 7886 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7887 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7888 | if (atomic_dec_return(&sqd->park_pending)) |
| 7889 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7890 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7891 | } |
| 7892 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7893 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7894 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7895 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7896 | WARN_ON_ONCE(sqd->thread == current); |
| 7897 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7898 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7899 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7900 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7901 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7902 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7903 | } |
| 7904 | |
| 7905 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7906 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7907 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7908 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7909 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7910 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7911 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7912 | if (sqd->thread) |
| 7913 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7914 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7915 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7916 | } |
| 7917 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7918 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7919 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7920 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7921 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 7922 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7923 | io_sq_thread_stop(sqd); |
| 7924 | kfree(sqd); |
| 7925 | } |
| 7926 | } |
| 7927 | |
| 7928 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7929 | { |
| 7930 | struct io_sq_data *sqd = ctx->sq_data; |
| 7931 | |
| 7932 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7933 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7934 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7935 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7936 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7937 | |
| 7938 | io_put_sq_data(sqd); |
| 7939 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7940 | } |
| 7941 | } |
| 7942 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7943 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7944 | { |
| 7945 | struct io_ring_ctx *ctx_attach; |
| 7946 | struct io_sq_data *sqd; |
| 7947 | struct fd f; |
| 7948 | |
| 7949 | f = fdget(p->wq_fd); |
| 7950 | if (!f.file) |
| 7951 | return ERR_PTR(-ENXIO); |
| 7952 | if (f.file->f_op != &io_uring_fops) { |
| 7953 | fdput(f); |
| 7954 | return ERR_PTR(-EINVAL); |
| 7955 | } |
| 7956 | |
| 7957 | ctx_attach = f.file->private_data; |
| 7958 | sqd = ctx_attach->sq_data; |
| 7959 | if (!sqd) { |
| 7960 | fdput(f); |
| 7961 | return ERR_PTR(-EINVAL); |
| 7962 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7963 | if (sqd->task_tgid != current->tgid) { |
| 7964 | fdput(f); |
| 7965 | return ERR_PTR(-EPERM); |
| 7966 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7967 | |
| 7968 | refcount_inc(&sqd->refs); |
| 7969 | fdput(f); |
| 7970 | return sqd; |
| 7971 | } |
| 7972 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7973 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 7974 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7975 | { |
| 7976 | struct io_sq_data *sqd; |
| 7977 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7978 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7979 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 7980 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7981 | if (!IS_ERR(sqd)) { |
| 7982 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7983 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7984 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7985 | /* fall through for EPERM case, setup new sqd/task */ |
| 7986 | if (PTR_ERR(sqd) != -EPERM) |
| 7987 | return sqd; |
| 7988 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7989 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7990 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7991 | if (!sqd) |
| 7992 | return ERR_PTR(-ENOMEM); |
| 7993 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7994 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7995 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7996 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7997 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7998 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7999 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8000 | return sqd; |
| 8001 | } |
| 8002 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8003 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8004 | /* |
| 8005 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 8006 | * the io_uring can be safely unregistered on process exit, even if we have |
| 8007 | * loops in the file referencing. |
| 8008 | */ |
| 8009 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 8010 | { |
| 8011 | struct sock *sk = ctx->ring_sock->sk; |
| 8012 | struct scm_fp_list *fpl; |
| 8013 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8014 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8015 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8016 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 8017 | if (!fpl) |
| 8018 | return -ENOMEM; |
| 8019 | |
| 8020 | skb = alloc_skb(0, GFP_KERNEL); |
| 8021 | if (!skb) { |
| 8022 | kfree(fpl); |
| 8023 | return -ENOMEM; |
| 8024 | } |
| 8025 | |
| 8026 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8027 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8028 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8029 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8030 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8031 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8032 | |
| 8033 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8034 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8035 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8036 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 8037 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8038 | } |
| 8039 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8040 | if (nr_files) { |
| 8041 | fpl->max = SCM_MAX_FD; |
| 8042 | fpl->count = nr_files; |
| 8043 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8044 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8045 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 8046 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8047 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8048 | for (i = 0; i < nr_files; i++) |
| 8049 | fput(fpl->fp[i]); |
| 8050 | } else { |
| 8051 | kfree_skb(skb); |
| 8052 | kfree(fpl); |
| 8053 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8054 | |
| 8055 | return 0; |
| 8056 | } |
| 8057 | |
| 8058 | /* |
| 8059 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8060 | * causes regular reference counting to break down. We rely on the UNIX |
| 8061 | * garbage collection to take care of this problem for us. |
| 8062 | */ |
| 8063 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8064 | { |
| 8065 | unsigned left, total; |
| 8066 | int ret = 0; |
| 8067 | |
| 8068 | total = 0; |
| 8069 | left = ctx->nr_user_files; |
| 8070 | while (left) { |
| 8071 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8072 | |
| 8073 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8074 | if (ret) |
| 8075 | break; |
| 8076 | left -= this_files; |
| 8077 | total += this_files; |
| 8078 | } |
| 8079 | |
| 8080 | if (!ret) |
| 8081 | return 0; |
| 8082 | |
| 8083 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8084 | struct file *file = io_file_from_index(ctx, total); |
| 8085 | |
| 8086 | if (file) |
| 8087 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8088 | total++; |
| 8089 | } |
| 8090 | |
| 8091 | return ret; |
| 8092 | } |
| 8093 | #else |
| 8094 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8095 | { |
| 8096 | return 0; |
| 8097 | } |
| 8098 | #endif |
| 8099 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8100 | 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] | 8101 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8102 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8103 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8104 | struct sock *sock = ctx->ring_sock->sk; |
| 8105 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8106 | struct sk_buff *skb; |
| 8107 | int i; |
| 8108 | |
| 8109 | __skb_queue_head_init(&list); |
| 8110 | |
| 8111 | /* |
| 8112 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8113 | * remove this entry and rearrange the file array. |
| 8114 | */ |
| 8115 | skb = skb_dequeue(head); |
| 8116 | while (skb) { |
| 8117 | struct scm_fp_list *fp; |
| 8118 | |
| 8119 | fp = UNIXCB(skb).fp; |
| 8120 | for (i = 0; i < fp->count; i++) { |
| 8121 | int left; |
| 8122 | |
| 8123 | if (fp->fp[i] != file) |
| 8124 | continue; |
| 8125 | |
| 8126 | unix_notinflight(fp->user, fp->fp[i]); |
| 8127 | left = fp->count - 1 - i; |
| 8128 | if (left) { |
| 8129 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8130 | left * sizeof(struct file *)); |
| 8131 | } |
| 8132 | fp->count--; |
| 8133 | if (!fp->count) { |
| 8134 | kfree_skb(skb); |
| 8135 | skb = NULL; |
| 8136 | } else { |
| 8137 | __skb_queue_tail(&list, skb); |
| 8138 | } |
| 8139 | fput(file); |
| 8140 | file = NULL; |
| 8141 | break; |
| 8142 | } |
| 8143 | |
| 8144 | if (!file) |
| 8145 | break; |
| 8146 | |
| 8147 | __skb_queue_tail(&list, skb); |
| 8148 | |
| 8149 | skb = skb_dequeue(head); |
| 8150 | } |
| 8151 | |
| 8152 | if (skb_peek(&list)) { |
| 8153 | spin_lock_irq(&head->lock); |
| 8154 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8155 | __skb_queue_tail(head, skb); |
| 8156 | spin_unlock_irq(&head->lock); |
| 8157 | } |
| 8158 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8159 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8160 | #endif |
| 8161 | } |
| 8162 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8163 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8164 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8165 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8166 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8167 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8168 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8169 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8170 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8171 | |
| 8172 | if (prsrc->tag) { |
| 8173 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8174 | |
| 8175 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8176 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8177 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | 2840f71 | 2021-04-27 16:13:51 +0100 | [diff] [blame] | 8178 | ctx->cq_extra++; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8179 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8180 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8181 | io_cqring_ev_posted(ctx); |
| 8182 | io_ring_submit_unlock(ctx, lock_ring); |
| 8183 | } |
| 8184 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8185 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8186 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8187 | } |
| 8188 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8189 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8190 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8191 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8192 | } |
| 8193 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8194 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8195 | { |
| 8196 | struct io_ring_ctx *ctx; |
| 8197 | struct llist_node *node; |
| 8198 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8199 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8200 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8201 | |
| 8202 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8203 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8204 | struct llist_node *next = node->next; |
| 8205 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8206 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8207 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8208 | node = next; |
| 8209 | } |
| 8210 | } |
| 8211 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8212 | 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] | 8213 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8214 | { |
| 8215 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8216 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8217 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8218 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8219 | |
| 8220 | if (ctx->file_data) |
| 8221 | return -EBUSY; |
| 8222 | if (!nr_args) |
| 8223 | return -EINVAL; |
| 8224 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8225 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8226 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8227 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8228 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8229 | if (ret) |
| 8230 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8231 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8232 | &ctx->file_data); |
| 8233 | if (ret) |
| 8234 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8235 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8236 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8237 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8238 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8239 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8240 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8241 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8242 | ret = -EFAULT; |
| 8243 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8244 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8245 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8246 | if (fd == -1) { |
| 8247 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8248 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8249 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8250 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8251 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8252 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8253 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8254 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8255 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8256 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8257 | |
| 8258 | /* |
| 8259 | * Don't allow io_uring instances to be registered. If UNIX |
| 8260 | * isn't enabled, then this causes a reference cycle and this |
| 8261 | * instance can never get freed. If UNIX is enabled we'll |
| 8262 | * handle it just fine, but there's still no point in allowing |
| 8263 | * a ring fd as it doesn't support regular read/write anyway. |
| 8264 | */ |
| 8265 | if (file->f_op == &io_uring_fops) { |
| 8266 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8267 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8268 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8269 | 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] | 8270 | } |
| 8271 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8272 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8273 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8274 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8275 | return ret; |
| 8276 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8277 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8278 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8279 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8280 | out_fput: |
| 8281 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8282 | file = io_file_from_index(ctx, i); |
| 8283 | if (file) |
| 8284 | fput(file); |
| 8285 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8286 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8287 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8288 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8289 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8290 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8291 | return ret; |
| 8292 | } |
| 8293 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8294 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8295 | int index) |
| 8296 | { |
| 8297 | #if defined(CONFIG_UNIX) |
| 8298 | struct sock *sock = ctx->ring_sock->sk; |
| 8299 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8300 | struct sk_buff *skb; |
| 8301 | |
| 8302 | /* |
| 8303 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8304 | * file set. If there's no room, fall back to allocating a new skb |
| 8305 | * and filling it in. |
| 8306 | */ |
| 8307 | spin_lock_irq(&head->lock); |
| 8308 | skb = skb_peek(head); |
| 8309 | if (skb) { |
| 8310 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8311 | |
| 8312 | if (fpl->count < SCM_MAX_FD) { |
| 8313 | __skb_unlink(skb, head); |
| 8314 | spin_unlock_irq(&head->lock); |
| 8315 | fpl->fp[fpl->count] = get_file(file); |
| 8316 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8317 | fpl->count++; |
| 8318 | spin_lock_irq(&head->lock); |
| 8319 | __skb_queue_head(head, skb); |
| 8320 | } else { |
| 8321 | skb = NULL; |
| 8322 | } |
| 8323 | } |
| 8324 | spin_unlock_irq(&head->lock); |
| 8325 | |
| 8326 | if (skb) { |
| 8327 | fput(file); |
| 8328 | return 0; |
| 8329 | } |
| 8330 | |
| 8331 | return __io_sqe_files_scm(ctx, 1, index); |
| 8332 | #else |
| 8333 | return 0; |
| 8334 | #endif |
| 8335 | } |
| 8336 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8337 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8338 | struct io_rsrc_node *node, void *rsrc) |
| 8339 | { |
| 8340 | struct io_rsrc_put *prsrc; |
| 8341 | |
| 8342 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8343 | if (!prsrc) |
| 8344 | return -ENOMEM; |
| 8345 | |
| 8346 | prsrc->tag = *io_get_tag_slot(data, idx); |
| 8347 | prsrc->rsrc = rsrc; |
| 8348 | list_add(&prsrc->list, &node->rsrc_list); |
| 8349 | return 0; |
| 8350 | } |
| 8351 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8352 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8353 | unsigned int issue_flags, u32 slot_index) |
| 8354 | { |
| 8355 | struct io_ring_ctx *ctx = req->ctx; |
| 8356 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8357 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8358 | struct io_fixed_file *file_slot; |
| 8359 | int ret = -EBADF; |
| 8360 | |
| 8361 | io_ring_submit_lock(ctx, !force_nonblock); |
| 8362 | if (file->f_op == &io_uring_fops) |
| 8363 | goto err; |
| 8364 | ret = -ENXIO; |
| 8365 | if (!ctx->file_data) |
| 8366 | goto err; |
| 8367 | ret = -EINVAL; |
| 8368 | if (slot_index >= ctx->nr_user_files) |
| 8369 | goto err; |
| 8370 | |
| 8371 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8372 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8373 | |
| 8374 | if (file_slot->file_ptr) { |
| 8375 | struct file *old_file; |
| 8376 | |
| 8377 | ret = io_rsrc_node_switch_start(ctx); |
| 8378 | if (ret) |
| 8379 | goto err; |
| 8380 | |
| 8381 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8382 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8383 | ctx->rsrc_node, old_file); |
| 8384 | if (ret) |
| 8385 | goto err; |
| 8386 | file_slot->file_ptr = 0; |
| 8387 | needs_switch = true; |
| 8388 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8389 | |
| 8390 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8391 | io_fixed_file_set(file_slot, file); |
| 8392 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8393 | if (ret) { |
| 8394 | file_slot->file_ptr = 0; |
| 8395 | goto err; |
| 8396 | } |
| 8397 | |
| 8398 | ret = 0; |
| 8399 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8400 | if (needs_switch) |
| 8401 | io_rsrc_node_switch(ctx, ctx->file_data); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8402 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 8403 | if (ret) |
| 8404 | fput(file); |
| 8405 | return ret; |
| 8406 | } |
| 8407 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8408 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8409 | { |
| 8410 | unsigned int offset = req->close.file_slot - 1; |
| 8411 | struct io_ring_ctx *ctx = req->ctx; |
| 8412 | struct io_fixed_file *file_slot; |
| 8413 | struct file *file; |
| 8414 | int ret, i; |
| 8415 | |
| 8416 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8417 | ret = -ENXIO; |
| 8418 | if (unlikely(!ctx->file_data)) |
| 8419 | goto out; |
| 8420 | ret = -EINVAL; |
| 8421 | if (offset >= ctx->nr_user_files) |
| 8422 | goto out; |
| 8423 | ret = io_rsrc_node_switch_start(ctx); |
| 8424 | if (ret) |
| 8425 | goto out; |
| 8426 | |
| 8427 | i = array_index_nospec(offset, ctx->nr_user_files); |
| 8428 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
| 8429 | ret = -EBADF; |
| 8430 | if (!file_slot->file_ptr) |
| 8431 | goto out; |
| 8432 | |
| 8433 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8434 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8435 | if (ret) |
| 8436 | goto out; |
| 8437 | |
| 8438 | file_slot->file_ptr = 0; |
| 8439 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8440 | ret = 0; |
| 8441 | out: |
| 8442 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8443 | return ret; |
| 8444 | } |
| 8445 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8446 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8447 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8448 | unsigned nr_args) |
| 8449 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8450 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8451 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8452 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8453 | struct io_fixed_file *file_slot; |
| 8454 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8455 | int fd, i, err = 0; |
| 8456 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8457 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8458 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8459 | if (!ctx->file_data) |
| 8460 | return -ENXIO; |
| 8461 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8462 | return -EINVAL; |
| 8463 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8464 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8465 | u64 tag = 0; |
| 8466 | |
| 8467 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8468 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8469 | err = -EFAULT; |
| 8470 | break; |
| 8471 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8472 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8473 | err = -EINVAL; |
| 8474 | break; |
| 8475 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8476 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8477 | continue; |
| 8478 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8479 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8480 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8481 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8482 | if (file_slot->file_ptr) { |
| 8483 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8484 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 8485 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8486 | if (err) |
| 8487 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8488 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8489 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8490 | } |
| 8491 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8492 | file = fget(fd); |
| 8493 | if (!file) { |
| 8494 | err = -EBADF; |
| 8495 | break; |
| 8496 | } |
| 8497 | /* |
| 8498 | * Don't allow io_uring instances to be registered. If |
| 8499 | * UNIX isn't enabled, then this causes a reference |
| 8500 | * cycle and this instance can never get freed. If UNIX |
| 8501 | * is enabled we'll handle it just fine, but there's |
| 8502 | * still no point in allowing a ring fd as it doesn't |
| 8503 | * support regular read/write anyway. |
| 8504 | */ |
| 8505 | if (file->f_op == &io_uring_fops) { |
| 8506 | fput(file); |
| 8507 | err = -EBADF; |
| 8508 | break; |
| 8509 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8510 | *io_get_tag_slot(data, up->offset + done) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8511 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8512 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8513 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8514 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8515 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8516 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8517 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8518 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8519 | } |
| 8520 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8521 | if (needs_switch) |
| 8522 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8523 | return done ? done : err; |
| 8524 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8525 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8526 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8527 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8528 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8529 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8530 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8531 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8532 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8533 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8534 | hash = ctx->hash_map; |
| 8535 | if (!hash) { |
| 8536 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8537 | if (!hash) { |
| 8538 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8539 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8540 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8541 | refcount_set(&hash->refs, 1); |
| 8542 | init_waitqueue_head(&hash->wait); |
| 8543 | ctx->hash_map = hash; |
| 8544 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8545 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8546 | |
| 8547 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8548 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8549 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8550 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8551 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8552 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8553 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8554 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8555 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8556 | } |
| 8557 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8558 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 8559 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8560 | { |
| 8561 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8562 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8563 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8564 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8565 | if (unlikely(!tctx)) |
| 8566 | return -ENOMEM; |
| 8567 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8568 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8569 | if (unlikely(ret)) { |
| 8570 | kfree(tctx); |
| 8571 | return ret; |
| 8572 | } |
| 8573 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8574 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8575 | if (IS_ERR(tctx->io_wq)) { |
| 8576 | ret = PTR_ERR(tctx->io_wq); |
| 8577 | percpu_counter_destroy(&tctx->inflight); |
| 8578 | kfree(tctx); |
| 8579 | return ret; |
| 8580 | } |
| 8581 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8582 | xa_init(&tctx->xa); |
| 8583 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8584 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8585 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8586 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8587 | spin_lock_init(&tctx->task_lock); |
| 8588 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8589 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8590 | return 0; |
| 8591 | } |
| 8592 | |
| 8593 | void __io_uring_free(struct task_struct *tsk) |
| 8594 | { |
| 8595 | struct io_uring_task *tctx = tsk->io_uring; |
| 8596 | |
| 8597 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8598 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8599 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8600 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8601 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8602 | kfree(tctx); |
| 8603 | tsk->io_uring = NULL; |
| 8604 | } |
| 8605 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8606 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8607 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8608 | { |
| 8609 | int ret; |
| 8610 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8611 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8612 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8613 | IORING_SETUP_ATTACH_WQ) { |
| 8614 | struct fd f; |
| 8615 | |
| 8616 | f = fdget(p->wq_fd); |
| 8617 | if (!f.file) |
| 8618 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8619 | if (f.file->f_op != &io_uring_fops) { |
| 8620 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8621 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8622 | } |
| 8623 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8624 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8625 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8626 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8627 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8628 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8629 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8630 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8631 | if (IS_ERR(sqd)) { |
| 8632 | ret = PTR_ERR(sqd); |
| 8633 | goto err; |
| 8634 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8635 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8636 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8637 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8638 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8639 | if (!ctx->sq_thread_idle) |
| 8640 | ctx->sq_thread_idle = HZ; |
| 8641 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8642 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8643 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8644 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8645 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8646 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8647 | io_sq_thread_unpark(sqd); |
| 8648 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8649 | if (ret < 0) |
| 8650 | goto err; |
| 8651 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8652 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8653 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8654 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8655 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8656 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8657 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8658 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8659 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8660 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8661 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8662 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8663 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8664 | |
| 8665 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8666 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8667 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 8668 | if (IS_ERR(tsk)) { |
| 8669 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8670 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8671 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8672 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8673 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8674 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8675 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8676 | if (ret) |
| 8677 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8678 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8679 | /* Can't have SQ_AFF without SQPOLL */ |
| 8680 | ret = -EINVAL; |
| 8681 | goto err; |
| 8682 | } |
| 8683 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8684 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8685 | err_sqpoll: |
| 8686 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8687 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8688 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8689 | return ret; |
| 8690 | } |
| 8691 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8692 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8693 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8694 | { |
| 8695 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8696 | } |
| 8697 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8698 | static inline int __io_account_mem(struct user_struct *user, |
| 8699 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8700 | { |
| 8701 | unsigned long page_limit, cur_pages, new_pages; |
| 8702 | |
| 8703 | /* Don't allow more pages than we can safely lock */ |
| 8704 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8705 | |
| 8706 | do { |
| 8707 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8708 | new_pages = cur_pages + nr_pages; |
| 8709 | if (new_pages > page_limit) |
| 8710 | return -ENOMEM; |
| 8711 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8712 | new_pages) != cur_pages); |
| 8713 | |
| 8714 | return 0; |
| 8715 | } |
| 8716 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8717 | 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] | 8718 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8719 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8720 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8721 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8722 | if (ctx->mm_account) |
| 8723 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8724 | } |
| 8725 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8726 | 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] | 8727 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8728 | int ret; |
| 8729 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8730 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8731 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8732 | if (ret) |
| 8733 | return ret; |
| 8734 | } |
| 8735 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8736 | if (ctx->mm_account) |
| 8737 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8738 | |
| 8739 | return 0; |
| 8740 | } |
| 8741 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8742 | static void io_mem_free(void *ptr) |
| 8743 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8744 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8745 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8746 | if (!ptr) |
| 8747 | return; |
| 8748 | |
| 8749 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8750 | if (put_page_testzero(page)) |
| 8751 | free_compound_page(page); |
| 8752 | } |
| 8753 | |
| 8754 | static void *io_mem_alloc(size_t size) |
| 8755 | { |
| 8756 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8757 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8758 | |
| 8759 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8760 | } |
| 8761 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8762 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8763 | size_t *sq_offset) |
| 8764 | { |
| 8765 | struct io_rings *rings; |
| 8766 | size_t off, sq_array_size; |
| 8767 | |
| 8768 | off = struct_size(rings, cqes, cq_entries); |
| 8769 | if (off == SIZE_MAX) |
| 8770 | return SIZE_MAX; |
| 8771 | |
| 8772 | #ifdef CONFIG_SMP |
| 8773 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8774 | if (off == 0) |
| 8775 | return SIZE_MAX; |
| 8776 | #endif |
| 8777 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8778 | if (sq_offset) |
| 8779 | *sq_offset = off; |
| 8780 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8781 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8782 | if (sq_array_size == SIZE_MAX) |
| 8783 | return SIZE_MAX; |
| 8784 | |
| 8785 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8786 | return SIZE_MAX; |
| 8787 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8788 | return off; |
| 8789 | } |
| 8790 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8791 | 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] | 8792 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8793 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8794 | unsigned int i; |
| 8795 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8796 | if (imu != ctx->dummy_ubuf) { |
| 8797 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8798 | unpin_user_page(imu->bvec[i].bv_page); |
| 8799 | if (imu->acct_pages) |
| 8800 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8801 | kvfree(imu); |
| 8802 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8803 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8806 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 8807 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 8808 | io_buffer_unmap(ctx, &prsrc->buf); |
| 8809 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8813 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8814 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8815 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8816 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8817 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8818 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 8819 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8820 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8821 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8822 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8823 | } |
| 8824 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8825 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 8826 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8827 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8828 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8829 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8830 | return -ENXIO; |
| 8831 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8832 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
| 8833 | if (!ret) |
| 8834 | __io_sqe_buffers_unregister(ctx); |
| 8835 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8836 | } |
| 8837 | |
| 8838 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8839 | void __user *arg, unsigned index) |
| 8840 | { |
| 8841 | struct iovec __user *src; |
| 8842 | |
| 8843 | #ifdef CONFIG_COMPAT |
| 8844 | if (ctx->compat) { |
| 8845 | struct compat_iovec __user *ciovs; |
| 8846 | struct compat_iovec ciov; |
| 8847 | |
| 8848 | ciovs = (struct compat_iovec __user *) arg; |
| 8849 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8850 | return -EFAULT; |
| 8851 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8852 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8853 | dst->iov_len = ciov.iov_len; |
| 8854 | return 0; |
| 8855 | } |
| 8856 | #endif |
| 8857 | src = (struct iovec __user *) arg; |
| 8858 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8859 | return -EFAULT; |
| 8860 | return 0; |
| 8861 | } |
| 8862 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8863 | /* |
| 8864 | * Not super efficient, but this is just a registration time. And we do cache |
| 8865 | * the last compound head, so generally we'll only do a full search if we don't |
| 8866 | * match that one. |
| 8867 | * |
| 8868 | * We check if the given compound head page has already been accounted, to |
| 8869 | * avoid double accounting it. This allows us to account the full size of the |
| 8870 | * page, not just the constituent pages of a huge page. |
| 8871 | */ |
| 8872 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8873 | int nr_pages, struct page *hpage) |
| 8874 | { |
| 8875 | int i, j; |
| 8876 | |
| 8877 | /* check current page array */ |
| 8878 | for (i = 0; i < nr_pages; i++) { |
| 8879 | if (!PageCompound(pages[i])) |
| 8880 | continue; |
| 8881 | if (compound_head(pages[i]) == hpage) |
| 8882 | return true; |
| 8883 | } |
| 8884 | |
| 8885 | /* check previously registered pages */ |
| 8886 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8887 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8888 | |
| 8889 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8890 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8891 | continue; |
| 8892 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8893 | return true; |
| 8894 | } |
| 8895 | } |
| 8896 | |
| 8897 | return false; |
| 8898 | } |
| 8899 | |
| 8900 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8901 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8902 | struct page **last_hpage) |
| 8903 | { |
| 8904 | int i, ret; |
| 8905 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 8906 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8907 | for (i = 0; i < nr_pages; i++) { |
| 8908 | if (!PageCompound(pages[i])) { |
| 8909 | imu->acct_pages++; |
| 8910 | } else { |
| 8911 | struct page *hpage; |
| 8912 | |
| 8913 | hpage = compound_head(pages[i]); |
| 8914 | if (hpage == *last_hpage) |
| 8915 | continue; |
| 8916 | *last_hpage = hpage; |
| 8917 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8918 | continue; |
| 8919 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8920 | } |
| 8921 | } |
| 8922 | |
| 8923 | if (!imu->acct_pages) |
| 8924 | return 0; |
| 8925 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8926 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8927 | if (ret) |
| 8928 | imu->acct_pages = 0; |
| 8929 | return ret; |
| 8930 | } |
| 8931 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8932 | 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] | 8933 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8934 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8935 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8936 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8937 | struct vm_area_struct **vmas = NULL; |
| 8938 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8939 | unsigned long off, start, end, ubuf; |
| 8940 | size_t size; |
| 8941 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8942 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8943 | if (!iov->iov_base) { |
| 8944 | *pimu = ctx->dummy_ubuf; |
| 8945 | return 0; |
| 8946 | } |
| 8947 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8948 | ubuf = (unsigned long) iov->iov_base; |
| 8949 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8950 | start = ubuf >> PAGE_SHIFT; |
| 8951 | nr_pages = end - start; |
| 8952 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8953 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8954 | ret = -ENOMEM; |
| 8955 | |
| 8956 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8957 | if (!pages) |
| 8958 | goto done; |
| 8959 | |
| 8960 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8961 | GFP_KERNEL); |
| 8962 | if (!vmas) |
| 8963 | goto done; |
| 8964 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8965 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 8966 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8967 | goto done; |
| 8968 | |
| 8969 | ret = 0; |
| 8970 | mmap_read_lock(current->mm); |
| 8971 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8972 | pages, vmas); |
| 8973 | if (pret == nr_pages) { |
| 8974 | /* don't support file backed memory */ |
| 8975 | for (i = 0; i < nr_pages; i++) { |
| 8976 | struct vm_area_struct *vma = vmas[i]; |
| 8977 | |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 8978 | if (vma_is_shmem(vma)) |
| 8979 | continue; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8980 | if (vma->vm_file && |
| 8981 | !is_file_hugepages(vma->vm_file)) { |
| 8982 | ret = -EOPNOTSUPP; |
| 8983 | break; |
| 8984 | } |
| 8985 | } |
| 8986 | } else { |
| 8987 | ret = pret < 0 ? pret : -EFAULT; |
| 8988 | } |
| 8989 | mmap_read_unlock(current->mm); |
| 8990 | if (ret) { |
| 8991 | /* |
| 8992 | * if we did partial map, or found file backed vmas, |
| 8993 | * release any pages we did get |
| 8994 | */ |
| 8995 | if (pret > 0) |
| 8996 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8997 | goto done; |
| 8998 | } |
| 8999 | |
| 9000 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 9001 | if (ret) { |
| 9002 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9003 | goto done; |
| 9004 | } |
| 9005 | |
| 9006 | off = ubuf & ~PAGE_MASK; |
| 9007 | size = iov->iov_len; |
| 9008 | for (i = 0; i < nr_pages; i++) { |
| 9009 | size_t vec_len; |
| 9010 | |
| 9011 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 9012 | imu->bvec[i].bv_page = pages[i]; |
| 9013 | imu->bvec[i].bv_len = vec_len; |
| 9014 | imu->bvec[i].bv_offset = off; |
| 9015 | off = 0; |
| 9016 | size -= vec_len; |
| 9017 | } |
| 9018 | /* store original address for later verification */ |
| 9019 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9020 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9021 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9022 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9023 | ret = 0; |
| 9024 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9025 | if (ret) |
| 9026 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9027 | kvfree(pages); |
| 9028 | kvfree(vmas); |
| 9029 | return ret; |
| 9030 | } |
| 9031 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9032 | 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] | 9033 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9034 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 9035 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9036 | } |
| 9037 | |
| 9038 | static int io_buffer_validate(struct iovec *iov) |
| 9039 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9040 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 9041 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9042 | /* |
| 9043 | * Don't impose further limits on the size and buffer |
| 9044 | * constraints here, we'll -EINVAL later when IO is |
| 9045 | * submitted if they are wrong. |
| 9046 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9047 | if (!iov->iov_base) |
| 9048 | return iov->iov_len ? -EFAULT : 0; |
| 9049 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9050 | return -EFAULT; |
| 9051 | |
| 9052 | /* arbitrary limit, but we need something */ |
| 9053 | if (iov->iov_len > SZ_1G) |
| 9054 | return -EFAULT; |
| 9055 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9056 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9057 | return -EOVERFLOW; |
| 9058 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9059 | return 0; |
| 9060 | } |
| 9061 | |
| 9062 | 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] | 9063 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9064 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9065 | struct page *last_hpage = NULL; |
| 9066 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9067 | int i, ret; |
| 9068 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9069 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9070 | if (ctx->user_bufs) |
| 9071 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9072 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9073 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9074 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9075 | if (ret) |
| 9076 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9077 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9078 | if (ret) |
| 9079 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9080 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9081 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9082 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9083 | return ret; |
| 9084 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9085 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9086 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9087 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9088 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9089 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9090 | ret = io_buffer_validate(&iov); |
| 9091 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9092 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9093 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9094 | ret = -EINVAL; |
| 9095 | break; |
| 9096 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9097 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9098 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9099 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9100 | if (ret) |
| 9101 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9102 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9103 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9104 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9105 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9106 | ctx->buf_data = data; |
| 9107 | if (ret) |
| 9108 | __io_sqe_buffers_unregister(ctx); |
| 9109 | else |
| 9110 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9111 | return ret; |
| 9112 | } |
| 9113 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9114 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9115 | struct io_uring_rsrc_update2 *up, |
| 9116 | unsigned int nr_args) |
| 9117 | { |
| 9118 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9119 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9120 | struct page *last_hpage = NULL; |
| 9121 | bool needs_switch = false; |
| 9122 | __u32 done; |
| 9123 | int i, err; |
| 9124 | |
| 9125 | if (!ctx->buf_data) |
| 9126 | return -ENXIO; |
| 9127 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9128 | return -EINVAL; |
| 9129 | |
| 9130 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9131 | struct io_mapped_ubuf *imu; |
| 9132 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9133 | u64 tag = 0; |
| 9134 | |
| 9135 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9136 | if (err) |
| 9137 | break; |
| 9138 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9139 | err = -EFAULT; |
| 9140 | break; |
| 9141 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9142 | err = io_buffer_validate(&iov); |
| 9143 | if (err) |
| 9144 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9145 | if (!iov.iov_base && tag) { |
| 9146 | err = -EINVAL; |
| 9147 | break; |
| 9148 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9149 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9150 | if (err) |
| 9151 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9152 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9153 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9154 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9155 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
| 9156 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9157 | if (unlikely(err)) { |
| 9158 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9159 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9160 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9161 | ctx->user_bufs[i] = NULL; |
| 9162 | needs_switch = true; |
| 9163 | } |
| 9164 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9165 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9166 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9167 | } |
| 9168 | |
| 9169 | if (needs_switch) |
| 9170 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9171 | return done ? done : err; |
| 9172 | } |
| 9173 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9174 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9175 | { |
| 9176 | __s32 __user *fds = arg; |
| 9177 | int fd; |
| 9178 | |
| 9179 | if (ctx->cq_ev_fd) |
| 9180 | return -EBUSY; |
| 9181 | |
| 9182 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9183 | return -EFAULT; |
| 9184 | |
| 9185 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9186 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9187 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9188 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9189 | ctx->cq_ev_fd = NULL; |
| 9190 | return ret; |
| 9191 | } |
| 9192 | |
| 9193 | return 0; |
| 9194 | } |
| 9195 | |
| 9196 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9197 | { |
| 9198 | if (ctx->cq_ev_fd) { |
| 9199 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9200 | ctx->cq_ev_fd = NULL; |
| 9201 | return 0; |
| 9202 | } |
| 9203 | |
| 9204 | return -ENXIO; |
| 9205 | } |
| 9206 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9207 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9208 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9209 | struct io_buffer *buf; |
| 9210 | unsigned long index; |
| 9211 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9212 | xa_for_each(&ctx->io_buffers, index, buf) { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9213 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9214 | cond_resched(); |
| 9215 | } |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9216 | } |
| 9217 | |
Pavel Begunkov | 7255834 | 2021-08-09 20:18:09 +0100 | [diff] [blame] | 9218 | static void io_req_cache_free(struct list_head *list) |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9219 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 9220 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9221 | |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 9222 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
| 9223 | list_del(&req->inflight_entry); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9224 | kmem_cache_free(req_cachep, req); |
| 9225 | } |
| 9226 | } |
| 9227 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9228 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9229 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9230 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9231 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9232 | mutex_lock(&ctx->uring_lock); |
| 9233 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9234 | if (state->free_reqs) { |
| 9235 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
| 9236 | state->free_reqs = 0; |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 9237 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9238 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9239 | io_flush_cached_locked_reqs(ctx, state); |
| 9240 | io_req_cache_free(&state->free_list); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9241 | mutex_unlock(&ctx->uring_lock); |
| 9242 | } |
| 9243 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9244 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9245 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9246 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9247 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9248 | } |
| 9249 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9250 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 9251 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9252 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9253 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9254 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9255 | mmdrop(ctx->mm_account); |
| 9256 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9257 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9258 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9259 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9260 | io_wait_rsrc_data(ctx->buf_data); |
| 9261 | io_wait_rsrc_data(ctx->file_data); |
| 9262 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9263 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9264 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9265 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9266 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9267 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9268 | if (ctx->rings) |
| 9269 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9270 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9271 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9272 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9273 | if (ctx->sq_creds) |
| 9274 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9275 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9276 | /* there are no registered resources left, nobody uses it */ |
| 9277 | if (ctx->rsrc_node) |
| 9278 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9279 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9280 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9281 | flush_delayed_work(&ctx->rsrc_put_work); |
| 9282 | |
| 9283 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9284 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9285 | |
| 9286 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9287 | if (ctx->ring_sock) { |
| 9288 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9289 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9290 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9291 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9292 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9293 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9294 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9295 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9296 | |
| 9297 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9298 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9299 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9300 | if (ctx->hash_map) |
| 9301 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9302 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9303 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9304 | kfree(ctx); |
| 9305 | } |
| 9306 | |
| 9307 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9308 | { |
| 9309 | struct io_ring_ctx *ctx = file->private_data; |
| 9310 | __poll_t mask = 0; |
| 9311 | |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 9312 | poll_wait(file, &ctx->poll_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9313 | /* |
| 9314 | * synchronizes with barrier from wq_has_sleeper call in |
| 9315 | * io_commit_cqring |
| 9316 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9317 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9318 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9319 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9320 | |
| 9321 | /* |
| 9322 | * Don't flush cqring overflow list here, just do a simple check. |
| 9323 | * Otherwise there could possible be ABBA deadlock: |
| 9324 | * CPU0 CPU1 |
| 9325 | * ---- ---- |
| 9326 | * lock(&ctx->uring_lock); |
| 9327 | * lock(&ep->mtx); |
| 9328 | * lock(&ctx->uring_lock); |
| 9329 | * lock(&ep->mtx); |
| 9330 | * |
| 9331 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9332 | * pushs them to do the flush. |
| 9333 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9334 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9335 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9336 | |
| 9337 | return mask; |
| 9338 | } |
| 9339 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9340 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9341 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9342 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9343 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9344 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9345 | if (creds) { |
| 9346 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9347 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9348 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9349 | |
| 9350 | return -EINVAL; |
| 9351 | } |
| 9352 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9353 | struct io_tctx_exit { |
| 9354 | struct callback_head task_work; |
| 9355 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9356 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9357 | }; |
| 9358 | |
| 9359 | static void io_tctx_exit_cb(struct callback_head *cb) |
| 9360 | { |
| 9361 | struct io_uring_task *tctx = current->io_uring; |
| 9362 | struct io_tctx_exit *work; |
| 9363 | |
| 9364 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9365 | /* |
| 9366 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9367 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 9368 | */ |
| 9369 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9370 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9371 | complete(&work->completion); |
| 9372 | } |
| 9373 | |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9374 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 9375 | { |
| 9376 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9377 | |
| 9378 | return req->ctx == data; |
| 9379 | } |
| 9380 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9381 | static void io_ring_exit_work(struct work_struct *work) |
| 9382 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9383 | 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] | 9384 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9385 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9386 | struct io_tctx_exit exit; |
| 9387 | struct io_tctx_node *node; |
| 9388 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9389 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9390 | /* |
| 9391 | * If we're doing polled IO and end up having requests being |
| 9392 | * submitted async (out-of-line), then completions can come in while |
| 9393 | * we're waiting for refs to drop. We need to reap these manually, |
| 9394 | * as nobody else will be looking for them. |
| 9395 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9396 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9397 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9398 | if (ctx->sq_data) { |
| 9399 | struct io_sq_data *sqd = ctx->sq_data; |
| 9400 | struct task_struct *tsk; |
| 9401 | |
| 9402 | io_sq_thread_park(sqd); |
| 9403 | tsk = sqd->thread; |
| 9404 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9405 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9406 | io_cancel_ctx_cb, ctx, true); |
| 9407 | io_sq_thread_unpark(sqd); |
| 9408 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9409 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9410 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9411 | /* there is little hope left, don't run it too often */ |
| 9412 | interval = HZ * 60; |
| 9413 | } |
| 9414 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9415 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9416 | init_completion(&exit.completion); |
| 9417 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9418 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9419 | /* |
| 9420 | * Some may use context even when all refs and requests have been put, |
| 9421 | * 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] | 9422 | * completion_lock, see io_req_task_submit(). Apart from other work, |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9423 | * this lock/unlock section also waits them to finish. |
| 9424 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9425 | mutex_lock(&ctx->uring_lock); |
| 9426 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9427 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9428 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9429 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9430 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9431 | /* don't spin on a single task if cancellation failed */ |
| 9432 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9433 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9434 | if (WARN_ON_ONCE(ret)) |
| 9435 | continue; |
| 9436 | wake_up_process(node->task); |
| 9437 | |
| 9438 | mutex_unlock(&ctx->uring_lock); |
| 9439 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9440 | mutex_lock(&ctx->uring_lock); |
| 9441 | } |
| 9442 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9443 | spin_lock(&ctx->completion_lock); |
| 9444 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9445 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9446 | io_ring_ctx_free(ctx); |
| 9447 | } |
| 9448 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9449 | /* Returns true if we found and killed one or more timeouts */ |
| 9450 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9451 | bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9452 | { |
| 9453 | struct io_kiocb *req, *tmp; |
| 9454 | int canceled = 0; |
| 9455 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9456 | spin_lock(&ctx->completion_lock); |
| 9457 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9458 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9459 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9460 | io_kill_timeout(req, -ECANCELED); |
| 9461 | canceled++; |
| 9462 | } |
| 9463 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9464 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9465 | if (canceled != 0) |
| 9466 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9467 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9468 | if (canceled != 0) |
| 9469 | io_cqring_ev_posted(ctx); |
| 9470 | return canceled != 0; |
| 9471 | } |
| 9472 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9473 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 9474 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9475 | unsigned long index; |
| 9476 | struct creds *creds; |
| 9477 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9478 | mutex_lock(&ctx->uring_lock); |
| 9479 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9480 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9481 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9482 | xa_for_each(&ctx->personalities, index, creds) |
| 9483 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9484 | mutex_unlock(&ctx->uring_lock); |
| 9485 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9486 | io_kill_timeouts(ctx, NULL, true); |
| 9487 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9488 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9489 | /* 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] | 9490 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9491 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9492 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9493 | /* |
| 9494 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9495 | * if we're exiting a ton of rings at the same time. It just adds |
| 9496 | * noise and overhead, there's no discernable change in runtime |
| 9497 | * over using system_wq. |
| 9498 | */ |
| 9499 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9500 | } |
| 9501 | |
| 9502 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9503 | { |
| 9504 | struct io_ring_ctx *ctx = file->private_data; |
| 9505 | |
| 9506 | file->private_data = NULL; |
| 9507 | io_ring_ctx_wait_and_kill(ctx); |
| 9508 | return 0; |
| 9509 | } |
| 9510 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9511 | struct io_task_cancel { |
| 9512 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9513 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9514 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9515 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9516 | 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] | 9517 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9518 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9519 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9520 | bool ret; |
| 9521 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9522 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9523 | struct io_ring_ctx *ctx = req->ctx; |
| 9524 | |
| 9525 | /* protect against races with linked timeouts */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9526 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9527 | ret = io_match_task(req, cancel->task, cancel->all); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9528 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9529 | } else { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9530 | ret = io_match_task(req, cancel->task, cancel->all); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9531 | } |
| 9532 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9533 | } |
| 9534 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9535 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9536 | struct task_struct *task, bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9537 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9538 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9539 | LIST_HEAD(list); |
| 9540 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9541 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9542 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9543 | if (io_match_task(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9544 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9545 | break; |
| 9546 | } |
| 9547 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9548 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9549 | if (list_empty(&list)) |
| 9550 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9551 | |
| 9552 | while (!list_empty(&list)) { |
| 9553 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9554 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9555 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9556 | kfree(de); |
| 9557 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9558 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9559 | } |
| 9560 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9561 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
| 9562 | { |
| 9563 | struct io_tctx_node *node; |
| 9564 | enum io_wq_cancel cret; |
| 9565 | bool ret = false; |
| 9566 | |
| 9567 | mutex_lock(&ctx->uring_lock); |
| 9568 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9569 | struct io_uring_task *tctx = node->task->io_uring; |
| 9570 | |
| 9571 | /* |
| 9572 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9573 | * killed after ctx nodes, which requires to take the lock. |
| 9574 | */ |
| 9575 | if (!tctx || !tctx->io_wq) |
| 9576 | continue; |
| 9577 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9578 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9579 | } |
| 9580 | mutex_unlock(&ctx->uring_lock); |
| 9581 | |
| 9582 | return ret; |
| 9583 | } |
| 9584 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9585 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9586 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9587 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9588 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9589 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9590 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9591 | |
| 9592 | while (1) { |
| 9593 | enum io_wq_cancel cret; |
| 9594 | bool ret = false; |
| 9595 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9596 | if (!task) { |
| 9597 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9598 | } else if (tctx && tctx->io_wq) { |
| 9599 | /* |
| 9600 | * Cancels requests of all rings, not only @ctx, but |
| 9601 | * it's fine as the task is in exit/exec. |
| 9602 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9603 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9604 | &cancel, true); |
| 9605 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9606 | } |
| 9607 | |
| 9608 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9609 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9610 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9611 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 9612 | io_iopoll_try_reap_events(ctx); |
| 9613 | ret = true; |
| 9614 | } |
| 9615 | } |
| 9616 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9617 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9618 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9619 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9620 | if (task) |
| 9621 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9622 | if (!ret) |
| 9623 | break; |
| 9624 | cond_resched(); |
| 9625 | } |
| 9626 | } |
| 9627 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9628 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9629 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9630 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9631 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9632 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9633 | |
| 9634 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9635 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9636 | if (unlikely(ret)) |
| 9637 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9638 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9639 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9640 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 9641 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 9642 | if (!node) |
| 9643 | return -ENOMEM; |
| 9644 | node->ctx = ctx; |
| 9645 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9646 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9647 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 9648 | node, GFP_KERNEL)); |
| 9649 | if (ret) { |
| 9650 | kfree(node); |
| 9651 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9652 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9653 | |
| 9654 | mutex_lock(&ctx->uring_lock); |
| 9655 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 9656 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9657 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9658 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9659 | return 0; |
| 9660 | } |
| 9661 | |
| 9662 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9663 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9664 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9665 | 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] | 9666 | { |
| 9667 | struct io_uring_task *tctx = current->io_uring; |
| 9668 | |
| 9669 | if (likely(tctx && tctx->last == ctx)) |
| 9670 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9671 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9672 | } |
| 9673 | |
| 9674 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9675 | * Remove this io_uring_file -> task mapping. |
| 9676 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9677 | static void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9678 | { |
| 9679 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9680 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9681 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 9682 | if (!tctx) |
| 9683 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9684 | node = xa_erase(&tctx->xa, index); |
| 9685 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9686 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9687 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9688 | WARN_ON_ONCE(current != node->task); |
| 9689 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 9690 | |
| 9691 | mutex_lock(&node->ctx->uring_lock); |
| 9692 | list_del(&node->ctx_node); |
| 9693 | mutex_unlock(&node->ctx->uring_lock); |
| 9694 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9695 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9696 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9697 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9698 | } |
| 9699 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9700 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9701 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9702 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9703 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9704 | unsigned long index; |
| 9705 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9706 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9707 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9708 | cond_resched(); |
| 9709 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9710 | if (wq) { |
| 9711 | /* |
| 9712 | * Must be after io_uring_del_task_file() (removes nodes under |
| 9713 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 9714 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9715 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 9716 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9717 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9718 | } |
| 9719 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9720 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9721 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9722 | if (tracked) |
| 9723 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9724 | return percpu_counter_sum(&tctx->inflight); |
| 9725 | } |
| 9726 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9727 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
| 9728 | { |
| 9729 | struct io_uring_task *tctx = task->io_uring; |
| 9730 | unsigned int refs = tctx->cached_refs; |
| 9731 | |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9732 | if (refs) { |
| 9733 | tctx->cached_refs = 0; |
| 9734 | percpu_counter_sub(&tctx->inflight, refs); |
| 9735 | put_task_struct_many(task, refs); |
| 9736 | } |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9737 | } |
| 9738 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9739 | /* |
| 9740 | * Find any io_uring ctx that this task has registered or done IO on, and cancel |
| 9741 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. |
| 9742 | */ |
| 9743 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9744 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9745 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 9746 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9747 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9748 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9749 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9750 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 9751 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 9752 | if (!current->io_uring) |
| 9753 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 9754 | if (tctx->io_wq) |
| 9755 | io_wq_exit_start(tctx->io_wq); |
| 9756 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9757 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9758 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9759 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9760 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9761 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9762 | if (!inflight) |
| 9763 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9764 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9765 | if (!sqd) { |
| 9766 | struct io_tctx_node *node; |
| 9767 | unsigned long index; |
| 9768 | |
| 9769 | xa_for_each(&tctx->xa, index, node) { |
| 9770 | /* sqpoll task will cancel all its requests */ |
| 9771 | if (node->ctx->sq_data) |
| 9772 | continue; |
| 9773 | io_uring_try_cancel_requests(node->ctx, current, |
| 9774 | cancel_all); |
| 9775 | } |
| 9776 | } else { |
| 9777 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 9778 | io_uring_try_cancel_requests(ctx, current, |
| 9779 | cancel_all); |
| 9780 | } |
| 9781 | |
| 9782 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9783 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9784 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9785 | * If we've seen completions, retry without waiting. This |
| 9786 | * avoids a race where a completion comes in before we did |
| 9787 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9788 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9789 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9790 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9791 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9792 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9793 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9794 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9795 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9796 | if (cancel_all) { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9797 | /* for exec all current's requests should be gone, kill tctx */ |
| 9798 | __io_uring_free(current); |
| 9799 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9800 | } |
| 9801 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9802 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9803 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9804 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9805 | } |
| 9806 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9807 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9808 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9809 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9810 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9811 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9812 | struct page *page; |
| 9813 | void *ptr; |
| 9814 | |
| 9815 | switch (offset) { |
| 9816 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9817 | case IORING_OFF_CQ_RING: |
| 9818 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9819 | break; |
| 9820 | case IORING_OFF_SQES: |
| 9821 | ptr = ctx->sq_sqes; |
| 9822 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9823 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9824 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9825 | } |
| 9826 | |
| 9827 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9828 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9829 | return ERR_PTR(-EINVAL); |
| 9830 | |
| 9831 | return ptr; |
| 9832 | } |
| 9833 | |
| 9834 | #ifdef CONFIG_MMU |
| 9835 | |
| 9836 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9837 | { |
| 9838 | size_t sz = vma->vm_end - vma->vm_start; |
| 9839 | unsigned long pfn; |
| 9840 | void *ptr; |
| 9841 | |
| 9842 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9843 | if (IS_ERR(ptr)) |
| 9844 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9845 | |
| 9846 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9847 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9848 | } |
| 9849 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9850 | #else /* !CONFIG_MMU */ |
| 9851 | |
| 9852 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9853 | { |
| 9854 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9855 | } |
| 9856 | |
| 9857 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9858 | { |
| 9859 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9860 | } |
| 9861 | |
| 9862 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9863 | unsigned long addr, unsigned long len, |
| 9864 | unsigned long pgoff, unsigned long flags) |
| 9865 | { |
| 9866 | void *ptr; |
| 9867 | |
| 9868 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9869 | if (IS_ERR(ptr)) |
| 9870 | return PTR_ERR(ptr); |
| 9871 | |
| 9872 | return (unsigned long) ptr; |
| 9873 | } |
| 9874 | |
| 9875 | #endif /* !CONFIG_MMU */ |
| 9876 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9877 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9878 | { |
| 9879 | DEFINE_WAIT(wait); |
| 9880 | |
| 9881 | do { |
| 9882 | if (!io_sqring_full(ctx)) |
| 9883 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9884 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9885 | |
| 9886 | if (!io_sqring_full(ctx)) |
| 9887 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9888 | schedule(); |
| 9889 | } while (!signal_pending(current)); |
| 9890 | |
| 9891 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9892 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9893 | } |
| 9894 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9895 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9896 | struct __kernel_timespec __user **ts, |
| 9897 | const sigset_t __user **sig) |
| 9898 | { |
| 9899 | struct io_uring_getevents_arg arg; |
| 9900 | |
| 9901 | /* |
| 9902 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9903 | * is just a pointer to the sigset_t. |
| 9904 | */ |
| 9905 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9906 | *sig = (const sigset_t __user *) argp; |
| 9907 | *ts = NULL; |
| 9908 | return 0; |
| 9909 | } |
| 9910 | |
| 9911 | /* |
| 9912 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9913 | * timespec and sigset_t pointers if good. |
| 9914 | */ |
| 9915 | if (*argsz != sizeof(arg)) |
| 9916 | return -EINVAL; |
| 9917 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9918 | return -EFAULT; |
| 9919 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9920 | *argsz = arg.sigmask_sz; |
| 9921 | *ts = u64_to_user_ptr(arg.ts); |
| 9922 | return 0; |
| 9923 | } |
| 9924 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9925 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9926 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9927 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9928 | { |
| 9929 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9930 | int submitted = 0; |
| 9931 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9932 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9933 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9934 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9935 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9936 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 9937 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9938 | return -EINVAL; |
| 9939 | |
| 9940 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9941 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9942 | return -EBADF; |
| 9943 | |
| 9944 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9945 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9946 | goto out_fput; |
| 9947 | |
| 9948 | ret = -ENXIO; |
| 9949 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9950 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9951 | goto out_fput; |
| 9952 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9953 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9954 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9955 | goto out; |
| 9956 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9957 | /* |
| 9958 | * For SQ polling, the thread will do all submissions and completions. |
| 9959 | * Just return the requested submit count, and wake the thread if |
| 9960 | * we were asked to. |
| 9961 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9962 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9963 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 9964 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9965 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 9966 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 9967 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 9968 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 9969 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9970 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9971 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9972 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9973 | ret = io_sqpoll_wait_sq(ctx); |
| 9974 | if (ret) |
| 9975 | goto out; |
| 9976 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9977 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9978 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9979 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9980 | if (unlikely(ret)) |
| 9981 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9982 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9983 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9984 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9985 | |
| 9986 | if (submitted != to_submit) |
| 9987 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9988 | } |
| 9989 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9990 | const sigset_t __user *sig; |
| 9991 | struct __kernel_timespec __user *ts; |
| 9992 | |
| 9993 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9994 | if (unlikely(ret)) |
| 9995 | goto out; |
| 9996 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9997 | min_complete = min(min_complete, ctx->cq_entries); |
| 9998 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9999 | /* |
| 10000 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 10001 | * space applications don't need to do io completion events |
| 10002 | * polling again, they can rely on io_sq_thread to do polling |
| 10003 | * work, which can reduce cpu usage and uring_lock contention. |
| 10004 | */ |
| 10005 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 10006 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 10007 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10008 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10009 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10010 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10011 | } |
| 10012 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10013 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 10014 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10015 | out_fput: |
| 10016 | fdput(f); |
| 10017 | return submitted ? submitted : ret; |
| 10018 | } |
| 10019 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10020 | #ifdef CONFIG_PROC_FS |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10021 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
| 10022 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10023 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10024 | struct user_namespace *uns = seq_user_ns(m); |
| 10025 | struct group_info *gi; |
| 10026 | kernel_cap_t cap; |
| 10027 | unsigned __capi; |
| 10028 | int g; |
| 10029 | |
| 10030 | seq_printf(m, "%5d\n", id); |
| 10031 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 10032 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 10033 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 10034 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 10035 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 10036 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 10037 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 10038 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 10039 | seq_puts(m, "\n\tGroups:\t"); |
| 10040 | gi = cred->group_info; |
| 10041 | for (g = 0; g < gi->ngroups; g++) { |
| 10042 | seq_put_decimal_ull(m, g ? " " : "", |
| 10043 | from_kgid_munged(uns, gi->gid[g])); |
| 10044 | } |
| 10045 | seq_puts(m, "\n\tCapEff:\t"); |
| 10046 | cap = cred->cap_effective; |
| 10047 | CAP_FOR_EACH_U32(__capi) |
| 10048 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 10049 | seq_putc(m, '\n'); |
| 10050 | return 0; |
| 10051 | } |
| 10052 | |
| 10053 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 10054 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10055 | struct io_sq_data *sq = NULL; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10056 | struct io_overflow_cqe *ocqe; |
| 10057 | struct io_rings *r = ctx->rings; |
| 10058 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; |
| 10059 | unsigned int cached_sq_head = ctx->cached_sq_head; |
| 10060 | unsigned int cached_cq_tail = ctx->cached_cq_tail; |
| 10061 | unsigned int sq_head = READ_ONCE(r->sq.head); |
| 10062 | unsigned int sq_tail = READ_ONCE(r->sq.tail); |
| 10063 | unsigned int cq_head = READ_ONCE(r->cq.head); |
| 10064 | unsigned int cq_tail = READ_ONCE(r->cq.tail); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10065 | bool has_lock; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10066 | unsigned int i; |
| 10067 | |
| 10068 | /* |
| 10069 | * we may get imprecise sqe and cqe info if uring is actively running |
| 10070 | * since we get cached_sq_head and cached_cq_tail without uring_lock |
| 10071 | * and sq_tail and cq_head are changed by userspace. But it's ok since |
| 10072 | * we usually use these info when it is stuck. |
| 10073 | */ |
| 10074 | seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask); |
| 10075 | seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask); |
| 10076 | seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask); |
| 10077 | seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask); |
| 10078 | seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask); |
| 10079 | seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask); |
| 10080 | seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head); |
| 10081 | for (i = cached_sq_head; i < sq_tail; i++) { |
| 10082 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]); |
| 10083 | |
| 10084 | if (likely(sq_idx <= sq_mask)) { |
| 10085 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; |
| 10086 | |
| 10087 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", |
| 10088 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data); |
| 10089 | } |
| 10090 | } |
| 10091 | seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head); |
| 10092 | for (i = cq_head; i < cached_cq_tail; i++) { |
| 10093 | struct io_uring_cqe *cqe = &r->cqes[i & cq_mask]; |
| 10094 | |
| 10095 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", |
| 10096 | i & cq_mask, cqe->user_data, cqe->res, cqe->flags); |
| 10097 | } |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10098 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10099 | /* |
| 10100 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10101 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10102 | * cases. If we fail to get the lock, we just don't iterate any |
| 10103 | * structures that could be going away outside the io_uring mutex. |
| 10104 | */ |
| 10105 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10106 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10107 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10108 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10109 | if (!sq->thread) |
| 10110 | sq = NULL; |
| 10111 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10112 | |
| 10113 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 10114 | 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] | 10115 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10116 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10117 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10118 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10119 | if (f) |
| 10120 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10121 | else |
| 10122 | seq_printf(m, "%5u: <none>\n", i); |
| 10123 | } |
| 10124 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10125 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10126 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10127 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10128 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10129 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10130 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10131 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10132 | unsigned long index; |
| 10133 | const struct cred *cred; |
| 10134 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10135 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10136 | xa_for_each(&ctx->personalities, index, cred) |
| 10137 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10138 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10139 | if (has_lock) |
| 10140 | mutex_unlock(&ctx->uring_lock); |
| 10141 | |
| 10142 | seq_puts(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10143 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10144 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10145 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10146 | struct io_kiocb *req; |
| 10147 | |
| 10148 | hlist_for_each_entry(req, list, hash_node) |
| 10149 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10150 | req->task->task_works != NULL); |
| 10151 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10152 | |
| 10153 | seq_puts(m, "CqOverflowList:\n"); |
| 10154 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { |
| 10155 | struct io_uring_cqe *cqe = &ocqe->cqe; |
| 10156 | |
| 10157 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", |
| 10158 | cqe->user_data, cqe->res, cqe->flags); |
| 10159 | |
| 10160 | } |
| 10161 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10162 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10163 | } |
| 10164 | |
| 10165 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 10166 | { |
| 10167 | struct io_ring_ctx *ctx = f->private_data; |
| 10168 | |
| 10169 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10170 | __io_uring_show_fdinfo(ctx, m); |
| 10171 | percpu_ref_put(&ctx->refs); |
| 10172 | } |
| 10173 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10174 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10175 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10176 | static const struct file_operations io_uring_fops = { |
| 10177 | .release = io_uring_release, |
| 10178 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10179 | #ifndef CONFIG_MMU |
| 10180 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10181 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10182 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10183 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10184 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10185 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10186 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10187 | }; |
| 10188 | |
| 10189 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10190 | struct io_uring_params *p) |
| 10191 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10192 | struct io_rings *rings; |
| 10193 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10194 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10195 | /* make sure these are sane, as we already accounted them */ |
| 10196 | ctx->sq_entries = p->sq_entries; |
| 10197 | ctx->cq_entries = p->cq_entries; |
| 10198 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10199 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10200 | if (size == SIZE_MAX) |
| 10201 | return -EOVERFLOW; |
| 10202 | |
| 10203 | rings = io_mem_alloc(size); |
| 10204 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10205 | return -ENOMEM; |
| 10206 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10207 | ctx->rings = rings; |
| 10208 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10209 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10210 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10211 | rings->sq_ring_entries = p->sq_entries; |
| 10212 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10213 | |
| 10214 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10215 | if (size == SIZE_MAX) { |
| 10216 | io_mem_free(ctx->rings); |
| 10217 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10218 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10219 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10220 | |
| 10221 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10222 | if (!ctx->sq_sqes) { |
| 10223 | io_mem_free(ctx->rings); |
| 10224 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10225 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10226 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10227 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10228 | return 0; |
| 10229 | } |
| 10230 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10231 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10232 | { |
| 10233 | int ret, fd; |
| 10234 | |
| 10235 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10236 | if (fd < 0) |
| 10237 | return fd; |
| 10238 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10239 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10240 | if (ret) { |
| 10241 | put_unused_fd(fd); |
| 10242 | return ret; |
| 10243 | } |
| 10244 | fd_install(fd, file); |
| 10245 | return fd; |
| 10246 | } |
| 10247 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10248 | /* |
| 10249 | * Allocate an anonymous fd, this is what constitutes the application |
| 10250 | * visible backing of an io_uring instance. The application mmaps this |
| 10251 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10252 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10253 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10254 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10255 | { |
| 10256 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10257 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10258 | int ret; |
| 10259 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10260 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10261 | &ctx->ring_sock); |
| 10262 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10263 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10264 | #endif |
| 10265 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10266 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 10267 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10268 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10269 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10270 | sock_release(ctx->ring_sock); |
| 10271 | ctx->ring_sock = NULL; |
| 10272 | } else { |
| 10273 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10274 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10275 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10276 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10277 | } |
| 10278 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10279 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10280 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10281 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10282 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10283 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10284 | int ret; |
| 10285 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10286 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10287 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10288 | if (entries > IORING_MAX_ENTRIES) { |
| 10289 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10290 | return -EINVAL; |
| 10291 | entries = IORING_MAX_ENTRIES; |
| 10292 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10293 | |
| 10294 | /* |
| 10295 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10296 | * application to drive a higher depth than the size of the SQ ring, |
| 10297 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10298 | * some flexibility in overcommitting a bit. If the application has |
| 10299 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10300 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10301 | */ |
| 10302 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10303 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10304 | /* |
| 10305 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10306 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10307 | * any cq vs sq ring sizing. |
| 10308 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10309 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10310 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10311 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10312 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10313 | return -EINVAL; |
| 10314 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10315 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10316 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10317 | if (p->cq_entries < p->sq_entries) |
| 10318 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10319 | } else { |
| 10320 | p->cq_entries = 2 * p->sq_entries; |
| 10321 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10322 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10323 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10324 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10325 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10326 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10327 | if (!capable(CAP_IPC_LOCK)) |
| 10328 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10329 | |
| 10330 | /* |
| 10331 | * This is just grabbed for accounting purposes. When a process exits, |
| 10332 | * the mm is exited and dropped before the files, hence we need to hang |
| 10333 | * on to this mm purely for the purposes of being able to unaccount |
| 10334 | * memory (locked/pinned vm). It's not used for anything else. |
| 10335 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10336 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10337 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10338 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10339 | ret = io_allocate_scq_urings(ctx, p); |
| 10340 | if (ret) |
| 10341 | goto err; |
| 10342 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10343 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10344 | if (ret) |
| 10345 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10346 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10347 | ret = io_rsrc_node_switch_start(ctx); |
| 10348 | if (ret) |
| 10349 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10350 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10351 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10352 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10353 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10354 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10355 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10356 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10357 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10358 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10359 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10360 | |
| 10361 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10362 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10363 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10364 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10365 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10366 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10367 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10368 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10369 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10370 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10371 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10372 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10373 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10374 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
| 10375 | IORING_FEAT_RSRC_TAGS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10376 | |
| 10377 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10378 | ret = -EFAULT; |
| 10379 | goto err; |
| 10380 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10381 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10382 | file = io_uring_get_file(ctx); |
| 10383 | if (IS_ERR(file)) { |
| 10384 | ret = PTR_ERR(file); |
| 10385 | goto err; |
| 10386 | } |
| 10387 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10388 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10389 | * Install ring fd as the very last thing, so we don't risk someone |
| 10390 | * having closed it before we finish setup |
| 10391 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10392 | ret = io_uring_install_fd(ctx, file); |
| 10393 | if (ret < 0) { |
| 10394 | /* fput will clean it up */ |
| 10395 | fput(file); |
| 10396 | return ret; |
| 10397 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10398 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10399 | 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] | 10400 | return ret; |
| 10401 | err: |
| 10402 | io_ring_ctx_wait_and_kill(ctx); |
| 10403 | return ret; |
| 10404 | } |
| 10405 | |
| 10406 | /* |
| 10407 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10408 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10409 | * params structure passed in. |
| 10410 | */ |
| 10411 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10412 | { |
| 10413 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10414 | int i; |
| 10415 | |
| 10416 | if (copy_from_user(&p, params, sizeof(p))) |
| 10417 | return -EFAULT; |
| 10418 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10419 | if (p.resv[i]) |
| 10420 | return -EINVAL; |
| 10421 | } |
| 10422 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10423 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10424 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10425 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10426 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10427 | return -EINVAL; |
| 10428 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10429 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10430 | } |
| 10431 | |
| 10432 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10433 | struct io_uring_params __user *, params) |
| 10434 | { |
| 10435 | return io_uring_setup(entries, params); |
| 10436 | } |
| 10437 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10438 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 10439 | { |
| 10440 | struct io_uring_probe *p; |
| 10441 | size_t size; |
| 10442 | int i, ret; |
| 10443 | |
| 10444 | size = struct_size(p, ops, nr_args); |
| 10445 | if (size == SIZE_MAX) |
| 10446 | return -EOVERFLOW; |
| 10447 | p = kzalloc(size, GFP_KERNEL); |
| 10448 | if (!p) |
| 10449 | return -ENOMEM; |
| 10450 | |
| 10451 | ret = -EFAULT; |
| 10452 | if (copy_from_user(p, arg, size)) |
| 10453 | goto out; |
| 10454 | ret = -EINVAL; |
| 10455 | if (memchr_inv(p, 0, size)) |
| 10456 | goto out; |
| 10457 | |
| 10458 | p->last_op = IORING_OP_LAST - 1; |
| 10459 | if (nr_args > IORING_OP_LAST) |
| 10460 | nr_args = IORING_OP_LAST; |
| 10461 | |
| 10462 | for (i = 0; i < nr_args; i++) { |
| 10463 | p->ops[i].op = i; |
| 10464 | if (!io_op_defs[i].not_supported) |
| 10465 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10466 | } |
| 10467 | p->ops_len = i; |
| 10468 | |
| 10469 | ret = 0; |
| 10470 | if (copy_to_user(arg, p, size)) |
| 10471 | ret = -EFAULT; |
| 10472 | out: |
| 10473 | kfree(p); |
| 10474 | return ret; |
| 10475 | } |
| 10476 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10477 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10478 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10479 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10480 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10481 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10482 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10483 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10484 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10485 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10486 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10487 | if (ret < 0) { |
| 10488 | put_cred(creds); |
| 10489 | return ret; |
| 10490 | } |
| 10491 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10492 | } |
| 10493 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10494 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 10495 | unsigned int nr_args) |
| 10496 | { |
| 10497 | struct io_uring_restriction *res; |
| 10498 | size_t size; |
| 10499 | int i, ret; |
| 10500 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10501 | /* Restrictions allowed only if rings started disabled */ |
| 10502 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10503 | return -EBADFD; |
| 10504 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10505 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10506 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10507 | return -EBUSY; |
| 10508 | |
| 10509 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10510 | return -EINVAL; |
| 10511 | |
| 10512 | size = array_size(nr_args, sizeof(*res)); |
| 10513 | if (size == SIZE_MAX) |
| 10514 | return -EOVERFLOW; |
| 10515 | |
| 10516 | res = memdup_user(arg, size); |
| 10517 | if (IS_ERR(res)) |
| 10518 | return PTR_ERR(res); |
| 10519 | |
| 10520 | ret = 0; |
| 10521 | |
| 10522 | for (i = 0; i < nr_args; i++) { |
| 10523 | switch (res[i].opcode) { |
| 10524 | case IORING_RESTRICTION_REGISTER_OP: |
| 10525 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10526 | ret = -EINVAL; |
| 10527 | goto out; |
| 10528 | } |
| 10529 | |
| 10530 | __set_bit(res[i].register_op, |
| 10531 | ctx->restrictions.register_op); |
| 10532 | break; |
| 10533 | case IORING_RESTRICTION_SQE_OP: |
| 10534 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10535 | ret = -EINVAL; |
| 10536 | goto out; |
| 10537 | } |
| 10538 | |
| 10539 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10540 | break; |
| 10541 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10542 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10543 | break; |
| 10544 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10545 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10546 | break; |
| 10547 | default: |
| 10548 | ret = -EINVAL; |
| 10549 | goto out; |
| 10550 | } |
| 10551 | } |
| 10552 | |
| 10553 | out: |
| 10554 | /* Reset all restrictions if an error happened */ |
| 10555 | if (ret != 0) |
| 10556 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10557 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10558 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10559 | |
| 10560 | kfree(res); |
| 10561 | return ret; |
| 10562 | } |
| 10563 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10564 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10565 | { |
| 10566 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10567 | return -EBADFD; |
| 10568 | |
| 10569 | if (ctx->restrictions.registered) |
| 10570 | ctx->restricted = 1; |
| 10571 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10572 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10573 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10574 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10575 | return 0; |
| 10576 | } |
| 10577 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10578 | 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] | 10579 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10580 | unsigned nr_args) |
| 10581 | { |
| 10582 | __u32 tmp; |
| 10583 | int err; |
| 10584 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10585 | if (up->resv) |
| 10586 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10587 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10588 | return -EOVERFLOW; |
| 10589 | err = io_rsrc_node_switch_start(ctx); |
| 10590 | if (err) |
| 10591 | return err; |
| 10592 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10593 | switch (type) { |
| 10594 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10595 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10596 | case IORING_RSRC_BUFFER: |
| 10597 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10598 | } |
| 10599 | return -EINVAL; |
| 10600 | } |
| 10601 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10602 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10603 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10604 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10605 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10606 | |
| 10607 | if (!nr_args) |
| 10608 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10609 | memset(&up, 0, sizeof(up)); |
| 10610 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10611 | return -EFAULT; |
| 10612 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10613 | } |
| 10614 | |
| 10615 | 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] | 10616 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10617 | { |
| 10618 | struct io_uring_rsrc_update2 up; |
| 10619 | |
| 10620 | if (size != sizeof(up)) |
| 10621 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10622 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10623 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10624 | if (!up.nr || up.resv) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10625 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10626 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10627 | } |
| 10628 | |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10629 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10630 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10631 | { |
| 10632 | struct io_uring_rsrc_register rr; |
| 10633 | |
| 10634 | /* keep it extendible */ |
| 10635 | if (size != sizeof(rr)) |
| 10636 | return -EINVAL; |
| 10637 | |
| 10638 | memset(&rr, 0, sizeof(rr)); |
| 10639 | if (copy_from_user(&rr, arg, size)) |
| 10640 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10641 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10642 | return -EINVAL; |
| 10643 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10644 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10645 | case IORING_RSRC_FILE: |
| 10646 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10647 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10648 | case IORING_RSRC_BUFFER: |
| 10649 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10650 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10651 | } |
| 10652 | return -EINVAL; |
| 10653 | } |
| 10654 | |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10655 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
| 10656 | unsigned len) |
| 10657 | { |
| 10658 | struct io_uring_task *tctx = current->io_uring; |
| 10659 | cpumask_var_t new_mask; |
| 10660 | int ret; |
| 10661 | |
| 10662 | if (!tctx || !tctx->io_wq) |
| 10663 | return -EINVAL; |
| 10664 | |
| 10665 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10666 | return -ENOMEM; |
| 10667 | |
| 10668 | cpumask_clear(new_mask); |
| 10669 | if (len > cpumask_size()) |
| 10670 | len = cpumask_size(); |
| 10671 | |
| 10672 | if (copy_from_user(new_mask, arg, len)) { |
| 10673 | free_cpumask_var(new_mask); |
| 10674 | return -EFAULT; |
| 10675 | } |
| 10676 | |
| 10677 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10678 | free_cpumask_var(new_mask); |
| 10679 | return ret; |
| 10680 | } |
| 10681 | |
| 10682 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
| 10683 | { |
| 10684 | struct io_uring_task *tctx = current->io_uring; |
| 10685 | |
| 10686 | if (!tctx || !tctx->io_wq) |
| 10687 | return -EINVAL; |
| 10688 | |
| 10689 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 10690 | } |
| 10691 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10692 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 10693 | void __user *arg) |
| 10694 | { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10695 | struct io_uring_task *tctx = NULL; |
| 10696 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10697 | __u32 new_count[2]; |
| 10698 | int i, ret; |
| 10699 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10700 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 10701 | return -EFAULT; |
| 10702 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10703 | if (new_count[i] > INT_MAX) |
| 10704 | return -EINVAL; |
| 10705 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10706 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 10707 | sqd = ctx->sq_data; |
| 10708 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10709 | /* |
| 10710 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 10711 | * ordering. Fine to drop uring_lock here, we hold |
| 10712 | * a ref to the ctx. |
| 10713 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10714 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10715 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10716 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10717 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10718 | if (sqd->thread) |
| 10719 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10720 | } |
| 10721 | } else { |
| 10722 | tctx = current->io_uring; |
| 10723 | } |
| 10724 | |
| 10725 | ret = -EINVAL; |
| 10726 | if (!tctx || !tctx->io_wq) |
| 10727 | goto err; |
| 10728 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10729 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 10730 | if (ret) |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10731 | goto err; |
| 10732 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10733 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10734 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10735 | io_put_sq_data(sqd); |
| 10736 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10737 | |
| 10738 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 10739 | return -EFAULT; |
| 10740 | |
| 10741 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10742 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10743 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10744 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10745 | io_put_sq_data(sqd); |
| 10746 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10747 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10748 | } |
| 10749 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10750 | static bool io_register_op_must_quiesce(int op) |
| 10751 | { |
| 10752 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 10753 | case IORING_REGISTER_BUFFERS: |
| 10754 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 10755 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10756 | case IORING_UNREGISTER_FILES: |
| 10757 | case IORING_REGISTER_FILES_UPDATE: |
| 10758 | case IORING_REGISTER_PROBE: |
| 10759 | case IORING_REGISTER_PERSONALITY: |
| 10760 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10761 | case IORING_REGISTER_FILES2: |
| 10762 | case IORING_REGISTER_FILES_UPDATE2: |
| 10763 | case IORING_REGISTER_BUFFERS2: |
| 10764 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10765 | case IORING_REGISTER_IOWQ_AFF: |
| 10766 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10767 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10768 | return false; |
| 10769 | default: |
| 10770 | return true; |
| 10771 | } |
| 10772 | } |
| 10773 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10774 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
| 10775 | { |
| 10776 | long ret; |
| 10777 | |
| 10778 | percpu_ref_kill(&ctx->refs); |
| 10779 | |
| 10780 | /* |
| 10781 | * Drop uring mutex before waiting for references to exit. If another |
| 10782 | * thread is currently inside io_uring_enter() it might need to grab the |
| 10783 | * uring_lock to make progress. If we hold it here across the drain |
| 10784 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10785 | * no new references will come in after we've killed the percpu ref. |
| 10786 | */ |
| 10787 | mutex_unlock(&ctx->uring_lock); |
| 10788 | do { |
| 10789 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10790 | if (!ret) |
| 10791 | break; |
| 10792 | ret = io_run_task_work_sig(); |
| 10793 | } while (ret >= 0); |
| 10794 | mutex_lock(&ctx->uring_lock); |
| 10795 | |
| 10796 | if (ret) |
| 10797 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 10798 | return ret; |
| 10799 | } |
| 10800 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10801 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10802 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10803 | __releases(ctx->uring_lock) |
| 10804 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10805 | { |
| 10806 | int ret; |
| 10807 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10808 | /* |
| 10809 | * We're inside the ring mutex, if the ref is already dying, then |
| 10810 | * someone else killed the ctx or is already going through |
| 10811 | * io_uring_register(). |
| 10812 | */ |
| 10813 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10814 | return -ENXIO; |
| 10815 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 10816 | if (ctx->restricted) { |
| 10817 | if (opcode >= IORING_REGISTER_LAST) |
| 10818 | return -EINVAL; |
| 10819 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 10820 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 10821 | return -EACCES; |
| 10822 | } |
| 10823 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10824 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10825 | ret = io_ctx_quiesce(ctx); |
| 10826 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 10827 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10828 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10829 | |
| 10830 | switch (opcode) { |
| 10831 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10832 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10833 | break; |
| 10834 | case IORING_UNREGISTER_BUFFERS: |
| 10835 | ret = -EINVAL; |
| 10836 | if (arg || nr_args) |
| 10837 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10838 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10839 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10840 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10841 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10842 | break; |
| 10843 | case IORING_UNREGISTER_FILES: |
| 10844 | ret = -EINVAL; |
| 10845 | if (arg || nr_args) |
| 10846 | break; |
| 10847 | ret = io_sqe_files_unregister(ctx); |
| 10848 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10849 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10850 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10851 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10852 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10853 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10854 | ret = -EINVAL; |
| 10855 | if (nr_args != 1) |
| 10856 | break; |
| 10857 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10858 | if (ret) |
| 10859 | break; |
| 10860 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10861 | ctx->eventfd_async = 1; |
| 10862 | else |
| 10863 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10864 | break; |
| 10865 | case IORING_UNREGISTER_EVENTFD: |
| 10866 | ret = -EINVAL; |
| 10867 | if (arg || nr_args) |
| 10868 | break; |
| 10869 | ret = io_eventfd_unregister(ctx); |
| 10870 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10871 | case IORING_REGISTER_PROBE: |
| 10872 | ret = -EINVAL; |
| 10873 | if (!arg || nr_args > 256) |
| 10874 | break; |
| 10875 | ret = io_probe(ctx, arg, nr_args); |
| 10876 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10877 | case IORING_REGISTER_PERSONALITY: |
| 10878 | ret = -EINVAL; |
| 10879 | if (arg || nr_args) |
| 10880 | break; |
| 10881 | ret = io_register_personality(ctx); |
| 10882 | break; |
| 10883 | case IORING_UNREGISTER_PERSONALITY: |
| 10884 | ret = -EINVAL; |
| 10885 | if (arg) |
| 10886 | break; |
| 10887 | ret = io_unregister_personality(ctx, nr_args); |
| 10888 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10889 | case IORING_REGISTER_ENABLE_RINGS: |
| 10890 | ret = -EINVAL; |
| 10891 | if (arg || nr_args) |
| 10892 | break; |
| 10893 | ret = io_register_enable_rings(ctx); |
| 10894 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10895 | case IORING_REGISTER_RESTRICTIONS: |
| 10896 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10897 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10898 | case IORING_REGISTER_FILES2: |
| 10899 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10900 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10901 | case IORING_REGISTER_FILES_UPDATE2: |
| 10902 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 10903 | IORING_RSRC_FILE); |
| 10904 | break; |
| 10905 | case IORING_REGISTER_BUFFERS2: |
| 10906 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 10907 | break; |
| 10908 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 10909 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 10910 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10911 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10912 | case IORING_REGISTER_IOWQ_AFF: |
| 10913 | ret = -EINVAL; |
| 10914 | if (!arg || !nr_args) |
| 10915 | break; |
| 10916 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 10917 | break; |
| 10918 | case IORING_UNREGISTER_IOWQ_AFF: |
| 10919 | ret = -EINVAL; |
| 10920 | if (arg || nr_args) |
| 10921 | break; |
| 10922 | ret = io_unregister_iowq_aff(ctx); |
| 10923 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10924 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 10925 | ret = -EINVAL; |
| 10926 | if (!arg || nr_args != 2) |
| 10927 | break; |
| 10928 | ret = io_register_iowq_max_workers(ctx, arg); |
| 10929 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10930 | default: |
| 10931 | ret = -EINVAL; |
| 10932 | break; |
| 10933 | } |
| 10934 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10935 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10936 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10937 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10938 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10939 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10940 | return ret; |
| 10941 | } |
| 10942 | |
| 10943 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10944 | void __user *, arg, unsigned int, nr_args) |
| 10945 | { |
| 10946 | struct io_ring_ctx *ctx; |
| 10947 | long ret = -EBADF; |
| 10948 | struct fd f; |
| 10949 | |
| 10950 | f = fdget(fd); |
| 10951 | if (!f.file) |
| 10952 | return -EBADF; |
| 10953 | |
| 10954 | ret = -EOPNOTSUPP; |
| 10955 | if (f.file->f_op != &io_uring_fops) |
| 10956 | goto out_fput; |
| 10957 | |
| 10958 | ctx = f.file->private_data; |
| 10959 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 10960 | io_run_task_work(); |
| 10961 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10962 | mutex_lock(&ctx->uring_lock); |
| 10963 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10964 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10965 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10966 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10967 | out_fput: |
| 10968 | fdput(f); |
| 10969 | return ret; |
| 10970 | } |
| 10971 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10972 | static int __init io_uring_init(void) |
| 10973 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10974 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10975 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10976 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10977 | } while (0) |
| 10978 | |
| 10979 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10980 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10981 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10982 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10983 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10984 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10985 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10986 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10987 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10988 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10989 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10990 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10991 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10992 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10993 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10994 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10995 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10996 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10997 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10998 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10999 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 11000 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 11001 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 11002 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 11003 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 11004 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11005 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11006 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 11007 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11008 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11009 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11010 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 11011 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11012 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11013 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 11014 | sizeof(struct io_uring_rsrc_update)); |
| 11015 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 11016 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 11017 | |
| 11018 | /* ->buf_index is u16 */ |
| 11019 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 11020 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11021 | /* should fit into one byte */ |
| 11022 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 11023 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
| 11024 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11025 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 11026 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 11027 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11028 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 11029 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 11030 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11031 | return 0; |
| 11032 | }; |
| 11033 | __initcall(io_uring_init); |