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 | |
Jens Axboe | f435c66 | 2022-05-23 17:05:03 -0600 | [diff] [blame] | 88 | #include "../fs/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 | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 108 | IOSQE_BUFFER_SELECT) |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 109 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
| 110 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 111 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 112 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
| 113 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 114 | struct io_uring { |
| 115 | u32 head ____cacheline_aligned_in_smp; |
| 116 | u32 tail ____cacheline_aligned_in_smp; |
| 117 | }; |
| 118 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 119 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 120 | * This data is shared with the application through the mmap at offsets |
| 121 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | * |
| 123 | * The offsets to the member fields are published through struct |
| 124 | * io_sqring_offsets when calling io_uring_setup. |
| 125 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 126 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Head and tail offsets into the ring; the offsets need to be |
| 129 | * masked to get valid indices. |
| 130 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 131 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 132 | * and the application controls tail of the sq ring and the head of the |
| 133 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 134 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 135 | struct io_uring sq, cq; |
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 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 138 | * ring_entries - 1) |
| 139 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 140 | u32 sq_ring_mask, cq_ring_mask; |
| 141 | /* Ring sizes (constant, power of 2) */ |
| 142 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Number of invalid entries dropped by the kernel due to |
| 145 | * invalid index stored in array |
| 146 | * |
| 147 | * Written by the kernel, shouldn't be modified by the |
| 148 | * application (i.e. get number of "new events" by comparing to |
| 149 | * cached value). |
| 150 | * |
| 151 | * After a new SQ head value was read by the application this |
| 152 | * counter includes all submissions that were dropped reaching |
| 153 | * the new SQ head (and possibly more). |
| 154 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 155 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 156 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 157 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 158 | * |
| 159 | * Written by the kernel, shouldn't be modified by the |
| 160 | * application. |
| 161 | * |
| 162 | * The application needs a full memory barrier before checking |
| 163 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 164 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 165 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 166 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 167 | * Runtime CQ flags |
| 168 | * |
| 169 | * Written by the application, shouldn't be modified by the |
| 170 | * kernel. |
| 171 | */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 172 | u32 cq_flags; |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 173 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 174 | * Number of completion events lost because the queue was full; |
| 175 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 176 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 177 | * the completion queue. |
| 178 | * |
| 179 | * Written by the kernel, shouldn't be modified by the |
| 180 | * application (i.e. get number of "new events" by comparing to |
| 181 | * cached value). |
| 182 | * |
| 183 | * As completion events come in out of order this counter is not |
| 184 | * ordered with any other data. |
| 185 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 186 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 187 | /* |
| 188 | * Ring buffer of completion events. |
| 189 | * |
| 190 | * The kernel writes completion events fresh every time they are |
| 191 | * produced, so the application is allowed to modify pending |
| 192 | * entries. |
| 193 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 194 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 197 | enum io_uring_cmd_flags { |
| 198 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 199 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 202 | struct io_mapped_ubuf { |
| 203 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 204 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 205 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 206 | unsigned long acct_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 207 | struct bio_vec bvec[]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 210 | struct io_ring_ctx; |
| 211 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 212 | struct io_overflow_cqe { |
| 213 | struct io_uring_cqe cqe; |
| 214 | struct list_head list; |
| 215 | }; |
| 216 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 217 | struct io_fixed_file { |
| 218 | /* file * with additional FFS_* flags */ |
| 219 | unsigned long file_ptr; |
| 220 | }; |
| 221 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 222 | struct io_rsrc_put { |
| 223 | struct list_head list; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 224 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 225 | union { |
| 226 | void *rsrc; |
| 227 | struct file *file; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 228 | struct io_mapped_ubuf *buf; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 229 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 232 | struct io_file_table { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 233 | struct io_fixed_file *files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 236 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 237 | struct percpu_ref refs; |
| 238 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 239 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 240 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 241 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 242 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 243 | }; |
| 244 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 245 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 246 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 247 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 248 | struct io_ring_ctx *ctx; |
| 249 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 250 | u64 **tags; |
| 251 | unsigned int nr; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 252 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 253 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 254 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 255 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 258 | struct io_buffer { |
| 259 | struct list_head list; |
| 260 | __u64 addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 261 | __u32 len; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 262 | __u16 bid; |
| 263 | }; |
| 264 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 265 | struct io_restriction { |
| 266 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 267 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 268 | u8 sqe_flags_allowed; |
| 269 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 270 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 271 | }; |
| 272 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 273 | enum { |
| 274 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 275 | IO_SQ_THREAD_SHOULD_PARK, |
| 276 | }; |
| 277 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 278 | struct io_sq_data { |
| 279 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 280 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 281 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 282 | |
| 283 | /* ctx's that are using this sqd */ |
| 284 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 285 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 286 | struct task_struct *thread; |
| 287 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 288 | |
| 289 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 290 | int sq_cpu; |
| 291 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 292 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 293 | |
| 294 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 295 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 296 | }; |
| 297 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 298 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 299 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 300 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 301 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 302 | struct io_submit_link { |
| 303 | struct io_kiocb *head; |
| 304 | struct io_kiocb *last; |
| 305 | }; |
| 306 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 307 | struct io_submit_state { |
| 308 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 309 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * io_kiocb alloc cache |
| 313 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 314 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 315 | unsigned int free_reqs; |
| 316 | |
| 317 | bool plug_started; |
| 318 | |
| 319 | /* |
| 320 | * Batch completion logic |
| 321 | */ |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 322 | struct io_kiocb *compl_reqs[IO_COMPL_BATCH]; |
| 323 | unsigned int compl_nr; |
| 324 | /* inline/task_work completion list, under ->uring_lock */ |
| 325 | struct list_head free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 326 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 327 | unsigned int ios_left; |
| 328 | }; |
| 329 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 330 | struct io_ring_ctx { |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 331 | /* const or read-mostly hot data */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 332 | struct { |
| 333 | struct percpu_ref refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 334 | |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 335 | struct io_rings *rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 336 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 337 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 338 | unsigned int drain_next: 1; |
| 339 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 340 | unsigned int restricted: 1; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 341 | unsigned int off_timeout_used: 1; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 342 | unsigned int drain_active: 1; |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 343 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 344 | |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 345 | /* submission data */ |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 346 | struct { |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 347 | struct mutex uring_lock; |
| 348 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 351 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 352 | * |
| 353 | * This indirection could e.g. be used to assign fixed |
| 354 | * io_uring_sqe entries to operations and only submit them to |
| 355 | * the queue when needed. |
| 356 | * |
| 357 | * The kernel modifies neither the indices array nor the entries |
| 358 | * array. |
| 359 | */ |
| 360 | u32 *sq_array; |
Pavel Begunkov | c7af47c | 2021-06-14 23:37:20 +0100 | [diff] [blame] | 361 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 362 | unsigned cached_sq_head; |
| 363 | unsigned sq_entries; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 364 | struct list_head defer_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Fixed resources fast path, should be accessed only under |
| 368 | * uring_lock, and updated through io_uring_register(2) |
| 369 | */ |
| 370 | struct io_rsrc_node *rsrc_node; |
| 371 | struct io_file_table file_table; |
| 372 | unsigned nr_user_files; |
| 373 | unsigned nr_user_bufs; |
| 374 | struct io_mapped_ubuf **user_bufs; |
| 375 | |
| 376 | struct io_submit_state submit_state; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 377 | struct list_head timeout_list; |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 378 | struct list_head ltimeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 379 | struct list_head cq_overflow_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 380 | struct xarray io_buffers; |
| 381 | struct xarray personalities; |
| 382 | u32 pers_next; |
| 383 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 384 | } ____cacheline_aligned_in_smp; |
| 385 | |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 386 | /* IRQ completion list, under ->completion_lock */ |
| 387 | struct list_head locked_free_list; |
| 388 | unsigned int locked_free_nr; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 389 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 390 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 391 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 392 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 393 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 394 | struct list_head sqd_list; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 395 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 396 | unsigned long check_cq_overflow; |
| 397 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 398 | struct { |
| 399 | unsigned cached_cq_tail; |
| 400 | unsigned cq_entries; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 401 | struct eventfd_ctx *cq_ev_fd; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 402 | struct wait_queue_head poll_wait; |
| 403 | struct wait_queue_head cq_wait; |
| 404 | unsigned cq_extra; |
| 405 | atomic_t cq_timeouts; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 406 | unsigned cq_last_tm_flush; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 407 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 408 | |
| 409 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 410 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 411 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 412 | spinlock_t timeout_lock; |
| 413 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 414 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 415 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 416 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 417 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 418 | * manipulate the list, hence no extra locking is needed there. |
| 419 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 420 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 421 | struct hlist_head *cancel_hash; |
| 422 | unsigned cancel_hash_bits; |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 423 | bool poll_multi_queue; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 424 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 425 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 426 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 427 | |
Pavel Begunkov | b13a891 | 2021-05-16 22:58:07 +0100 | [diff] [blame] | 428 | /* slow path rsrc auxilary data, used by update/register */ |
| 429 | struct { |
| 430 | struct io_rsrc_node *rsrc_backup_node; |
| 431 | struct io_mapped_ubuf *dummy_ubuf; |
| 432 | struct io_rsrc_data *file_data; |
| 433 | struct io_rsrc_data *buf_data; |
| 434 | |
| 435 | struct delayed_work rsrc_put_work; |
| 436 | struct llist_head rsrc_put_llist; |
| 437 | struct list_head rsrc_ref_list; |
| 438 | spinlock_t rsrc_ref_lock; |
| 439 | }; |
| 440 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 441 | /* Keep this last, we don't need it for the fast path */ |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 442 | struct { |
| 443 | #if defined(CONFIG_UNIX) |
| 444 | struct socket *ring_sock; |
| 445 | #endif |
| 446 | /* hashed buffered write serialization */ |
| 447 | struct io_wq_hash *hash_map; |
| 448 | |
| 449 | /* Only used for accounting purposes */ |
| 450 | struct user_struct *user; |
| 451 | struct mm_struct *mm_account; |
| 452 | |
| 453 | /* ctx exit and cancelation */ |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 454 | struct llist_head fallback_llist; |
| 455 | struct delayed_work fallback_work; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 456 | struct work_struct exit_work; |
| 457 | struct list_head tctx_list; |
| 458 | struct completion ref_comp; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 459 | u32 iowq_limits[2]; |
| 460 | bool iowq_limits_set; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 461 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 464 | struct io_uring_task { |
| 465 | /* submission side */ |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 466 | int cached_refs; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 467 | struct xarray xa; |
| 468 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 469 | const struct io_ring_ctx *last; |
| 470 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 471 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 472 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 473 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 474 | |
| 475 | spinlock_t task_lock; |
| 476 | struct io_wq_work_list task_list; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 477 | struct callback_head task_work; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 478 | bool task_running; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 479 | }; |
| 480 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 481 | /* |
| 482 | * First field must be the file pointer in all the |
| 483 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 484 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 485 | struct io_poll_iocb { |
| 486 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 487 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 488 | __poll_t events; |
Jens Axboe | 345fb36 | 2023-03-06 13:28:57 -0700 | [diff] [blame] | 489 | int retries; |
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 | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 582 | size_t done_io; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 583 | struct io_buffer *kbuf; |
Jens Axboe | 34a7e50 | 2023-06-23 07:38:14 -0600 | [diff] [blame] | 584 | void __user *msg_control; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 585 | }; |
| 586 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 587 | struct io_open { |
| 588 | struct file *file; |
| 589 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 590 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 591 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 592 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 593 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 594 | }; |
| 595 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 596 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 597 | struct file *file; |
| 598 | u64 arg; |
| 599 | u32 nr_args; |
| 600 | u32 offset; |
| 601 | }; |
| 602 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 603 | struct io_fadvise { |
| 604 | struct file *file; |
| 605 | u64 offset; |
| 606 | u32 len; |
| 607 | u32 advice; |
| 608 | }; |
| 609 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 610 | struct io_madvise { |
| 611 | struct file *file; |
| 612 | u64 addr; |
| 613 | u32 len; |
| 614 | u32 advice; |
| 615 | }; |
| 616 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 617 | struct io_epoll { |
| 618 | struct file *file; |
| 619 | int epfd; |
| 620 | int op; |
| 621 | int fd; |
| 622 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 625 | struct io_splice { |
| 626 | struct file *file_out; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 627 | loff_t off_out; |
| 628 | loff_t off_in; |
| 629 | u64 len; |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 630 | int splice_fd_in; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 631 | unsigned int flags; |
| 632 | }; |
| 633 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 634 | struct io_provide_buf { |
| 635 | struct file *file; |
| 636 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 637 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 638 | __u32 bgid; |
| 639 | __u16 nbufs; |
| 640 | __u16 bid; |
| 641 | }; |
| 642 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 643 | struct io_statx { |
| 644 | struct file *file; |
| 645 | int dfd; |
| 646 | unsigned int mask; |
| 647 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 648 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 649 | struct statx __user *buffer; |
| 650 | }; |
| 651 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 652 | struct io_shutdown { |
| 653 | struct file *file; |
| 654 | int how; |
| 655 | }; |
| 656 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 657 | struct io_rename { |
| 658 | struct file *file; |
| 659 | int old_dfd; |
| 660 | int new_dfd; |
| 661 | struct filename *oldpath; |
| 662 | struct filename *newpath; |
| 663 | int flags; |
| 664 | }; |
| 665 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 666 | struct io_unlink { |
| 667 | struct file *file; |
| 668 | int dfd; |
| 669 | int flags; |
| 670 | struct filename *filename; |
| 671 | }; |
| 672 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 673 | struct io_mkdir { |
| 674 | struct file *file; |
| 675 | int dfd; |
| 676 | umode_t mode; |
| 677 | struct filename *filename; |
| 678 | }; |
| 679 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 680 | struct io_symlink { |
| 681 | struct file *file; |
| 682 | int new_dfd; |
| 683 | struct filename *oldpath; |
| 684 | struct filename *newpath; |
| 685 | }; |
| 686 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 687 | struct io_hardlink { |
| 688 | struct file *file; |
| 689 | int old_dfd; |
| 690 | int new_dfd; |
| 691 | struct filename *oldpath; |
| 692 | struct filename *newpath; |
| 693 | int flags; |
| 694 | }; |
| 695 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 696 | struct io_completion { |
| 697 | struct file *file; |
Pavel Begunkov | 8c3f9cd | 2021-02-28 22:35:15 +0000 | [diff] [blame] | 698 | u32 cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 699 | }; |
| 700 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 701 | struct io_async_connect { |
| 702 | struct sockaddr_storage address; |
| 703 | }; |
| 704 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 705 | struct io_async_msghdr { |
| 706 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 707 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 708 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 709 | struct sockaddr __user *uaddr; |
| 710 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 711 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 712 | }; |
| 713 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 714 | struct io_async_rw { |
| 715 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 716 | const struct iovec *free_iovec; |
| 717 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 718 | struct iov_iter_state iter_state; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 719 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 720 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 721 | }; |
| 722 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 723 | enum { |
| 724 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 725 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 726 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 727 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 728 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 729 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 730 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 731 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 732 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 733 | REQ_F_INFLIGHT_BIT, |
| 734 | REQ_F_CUR_POS_BIT, |
| 735 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 736 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 737 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 738 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 739 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 740 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 741 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 742 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 743 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 744 | REQ_F_ARM_LTIMEOUT_BIT, |
Jens Axboe | 390b881 | 2022-03-23 09:30:05 -0600 | [diff] [blame] | 745 | REQ_F_PARTIAL_IO_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 746 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 747 | REQ_F_NOWAIT_READ_BIT, |
| 748 | REQ_F_NOWAIT_WRITE_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 749 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 750 | |
| 751 | /* not a real bit, just to check we're not overflowing the space */ |
| 752 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | enum { |
| 756 | /* ctx owns file */ |
| 757 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 758 | /* drain existing IO first */ |
| 759 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 760 | /* linked sqes */ |
| 761 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 762 | /* doesn't sever on completion < 0 */ |
| 763 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 764 | /* IOSQE_ASYNC */ |
| 765 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 766 | /* IOSQE_BUFFER_SELECT */ |
| 767 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 768 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 769 | /* fail rest of links */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 770 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 771 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 772 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 773 | /* read/write uses file position */ |
| 774 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 775 | /* must not punt to workers */ |
| 776 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 777 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 778 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 779 | /* needs cleanup */ |
| 780 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 781 | /* already went through poll handler */ |
| 782 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 783 | /* buffer already selected */ |
| 784 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 785 | /* completion is deferred through io_comp_state */ |
| 786 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 787 | /* caller should reissue async */ |
| 788 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 789 | /* supports async reads */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 790 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 791 | /* supports async writes */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 792 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 793 | /* regular file */ |
| 794 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 795 | /* has creds assigned */ |
| 796 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 797 | /* skip refcounting if not set */ |
| 798 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 799 | /* there is a linked timeout that has to be armed */ |
| 800 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Jens Axboe | 390b881 | 2022-03-23 09:30:05 -0600 | [diff] [blame] | 801 | /* request has already done partial IO */ |
| 802 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | struct async_poll { |
| 806 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 807 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 808 | }; |
| 809 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 810 | 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] | 811 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 812 | struct io_task_work { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 813 | union { |
| 814 | struct io_wq_work_node node; |
| 815 | struct llist_node fallback_node; |
| 816 | }; |
| 817 | io_req_tw_func_t func; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 818 | }; |
| 819 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 820 | enum { |
| 821 | IORING_RSRC_FILE = 0, |
| 822 | IORING_RSRC_BUFFER = 1, |
| 823 | }; |
| 824 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 825 | /* |
| 826 | * NOTE! Each of the iocb union members has the file pointer |
| 827 | * as the first entry in their struct definition. So you can |
| 828 | * access the file pointer through any of the sub-structs, |
| 829 | * or directly as just 'ki_filp' in this struct. |
| 830 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 831 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 832 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 833 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 834 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 835 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 836 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 837 | struct io_accept accept; |
| 838 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 839 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 840 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 841 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 842 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 843 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 844 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 845 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 846 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 847 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 848 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 849 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 850 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 851 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 852 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 853 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 854 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 855 | struct io_unlink unlink; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 856 | struct io_mkdir mkdir; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 857 | struct io_symlink symlink; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 858 | struct io_hardlink hardlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 859 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 860 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 861 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 862 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 863 | /* opcode allocated if it needs to store data for async defer */ |
| 864 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 865 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 866 | /* polled IO has completed */ |
| 867 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 868 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 869 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 870 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 871 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 872 | struct io_ring_ctx *ctx; |
| 873 | unsigned int flags; |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 874 | atomic_t refs; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 875 | struct task_struct *task; |
| 876 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 877 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 878 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 879 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 880 | |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 881 | /* used with ctx->iopoll_list with reads/writes */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 882 | struct list_head inflight_entry; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 883 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 884 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 885 | struct hlist_node hash_node; |
| 886 | struct async_poll *apoll; |
| 887 | struct io_wq_work work; |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 888 | const struct cred *creds; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 889 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 890 | /* store used ubuf, so we can prevent reloading */ |
| 891 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 892 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
| 893 | struct io_buffer *kbuf; |
| 894 | atomic_t poll_refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 895 | }; |
| 896 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 897 | struct io_tctx_node { |
| 898 | struct list_head ctx_node; |
| 899 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 900 | struct io_ring_ctx *ctx; |
| 901 | }; |
| 902 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 903 | struct io_defer_entry { |
| 904 | struct list_head list; |
| 905 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 906 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 907 | }; |
| 908 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 909 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 910 | /* needs req->file assigned */ |
| 911 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 912 | /* hash wq insertion if file is a regular file */ |
| 913 | unsigned hash_reg_file : 1; |
| 914 | /* unbound wq insertion if file is a non-regular file */ |
| 915 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 916 | /* opcode is not supported by this kernel */ |
| 917 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 918 | /* set if opcode supports polled "wait" */ |
| 919 | unsigned pollin : 1; |
| 920 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 921 | /* op supports buffer selection */ |
| 922 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 923 | /* do prep async if is going to be punted */ |
| 924 | unsigned needs_async_setup : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 925 | /* should block plug */ |
| 926 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 927 | /* size of async data needed, if any */ |
| 928 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 929 | }; |
| 930 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 931 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 932 | [IORING_OP_NOP] = {}, |
| 933 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 934 | .needs_file = 1, |
| 935 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 936 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 937 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 938 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 939 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 940 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 941 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 942 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 943 | .needs_file = 1, |
| 944 | .hash_reg_file = 1, |
| 945 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 946 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 947 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 948 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 949 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 950 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 951 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 952 | .needs_file = 1, |
| 953 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 954 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 955 | .needs_file = 1, |
| 956 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 957 | .pollin = 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_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 962 | .needs_file = 1, |
| 963 | .hash_reg_file = 1, |
| 964 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 965 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 966 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 967 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 968 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 969 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 970 | .needs_file = 1, |
| 971 | .unbound_nonreg_file = 1, |
| 972 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 973 | [IORING_OP_POLL_REMOVE] = {}, |
| 974 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 975 | .needs_file = 1, |
| 976 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 977 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 978 | .needs_file = 1, |
| 979 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 980 | .pollout = 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_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 985 | .needs_file = 1, |
| 986 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 987 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 988 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 989 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 990 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 991 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 992 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 993 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 994 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 995 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 996 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 997 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 998 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 999 | .needs_file = 1, |
| 1000 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1001 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1002 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1003 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 1004 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1005 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1006 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1007 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1008 | .needs_file = 1, |
| 1009 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1010 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 1011 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1012 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1013 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1014 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1015 | .needs_file = 1, |
| 1016 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1017 | [IORING_OP_OPENAT] = {}, |
| 1018 | [IORING_OP_CLOSE] = {}, |
| 1019 | [IORING_OP_FILES_UPDATE] = {}, |
| 1020 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1021 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1022 | .needs_file = 1, |
| 1023 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1024 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1025 | .buffer_select = 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_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1030 | .needs_file = 1, |
Jens Axboe | 7b3188e | 2021-08-30 19:37:41 -0600 | [diff] [blame] | 1031 | .hash_reg_file = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1032 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1033 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1034 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1035 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1036 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1037 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1038 | .needs_file = 1, |
| 1039 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1040 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1041 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1042 | .needs_file = 1, |
| 1043 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1044 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1045 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1046 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1047 | .needs_file = 1, |
| 1048 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1049 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1050 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1051 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1052 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1053 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1054 | [IORING_OP_EPOLL_CTL] = { |
| 1055 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1056 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1057 | [IORING_OP_SPLICE] = { |
| 1058 | .needs_file = 1, |
| 1059 | .hash_reg_file = 1, |
| 1060 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1061 | }, |
| 1062 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1063 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1064 | [IORING_OP_TEE] = { |
| 1065 | .needs_file = 1, |
| 1066 | .hash_reg_file = 1, |
| 1067 | .unbound_nonreg_file = 1, |
| 1068 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1069 | [IORING_OP_SHUTDOWN] = { |
| 1070 | .needs_file = 1, |
| 1071 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1072 | [IORING_OP_RENAMEAT] = {}, |
| 1073 | [IORING_OP_UNLINKAT] = {}, |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 1074 | [IORING_OP_MKDIRAT] = {}, |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 1075 | [IORING_OP_SYMLINKAT] = {}, |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 1076 | [IORING_OP_LINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1077 | }; |
| 1078 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1079 | /* requests with any of those set should undergo io_disarm_next() */ |
| 1080 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) |
| 1081 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1082 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 1083 | static void io_uring_del_tctx_node(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1084 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1085 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1086 | bool cancel_all); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 1087 | 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] | 1088 | |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1089 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
| 1090 | |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1091 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1092 | static void io_put_req_deferred(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1093 | static void io_dismantle_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1094 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 1095 | 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] | 1096 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 1097 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1098 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 1099 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 1100 | struct io_kiocb *req, int fd, bool fixed, |
| 1101 | unsigned int issue_flags); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1102 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1103 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1104 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1105 | static void io_req_task_queue(struct io_kiocb *req); |
Pavel Begunkov | 2a2758f | 2021-06-17 18:14:00 +0100 | [diff] [blame] | 1106 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1107 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1108 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1109 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 1110 | unsigned int issue_flags, u32 slot_index); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 1111 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1112 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 1113 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1114 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1115 | static struct kmem_cache *req_cachep; |
| 1116 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1117 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1118 | |
| 1119 | struct sock *io_uring_get_socket(struct file *file) |
| 1120 | { |
| 1121 | #if defined(CONFIG_UNIX) |
| 1122 | if (file->f_op == &io_uring_fops) { |
| 1123 | struct io_ring_ctx *ctx = file->private_data; |
| 1124 | |
| 1125 | return ctx->ring_sock->sk; |
| 1126 | } |
| 1127 | #endif |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1131 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1132 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
| 1133 | { |
| 1134 | if (!*locked) { |
| 1135 | mutex_lock(&ctx->uring_lock); |
| 1136 | *locked = true; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1140 | #define io_for_each_link(pos, head) \ |
| 1141 | for (pos = (head); pos; pos = pos->link) |
| 1142 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1143 | /* |
| 1144 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1145 | * see commit f958d7b528b1 for details. |
| 1146 | */ |
| 1147 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1148 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1149 | |
| 1150 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1151 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1152 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1153 | return atomic_inc_not_zero(&req->refs); |
| 1154 | } |
| 1155 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1156 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1157 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1158 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
| 1159 | return true; |
| 1160 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1161 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1162 | return atomic_dec_and_test(&req->refs); |
| 1163 | } |
| 1164 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1165 | static inline void req_ref_get(struct io_kiocb *req) |
| 1166 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1167 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1168 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1169 | atomic_inc(&req->refs); |
| 1170 | } |
| 1171 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1172 | 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] | 1173 | { |
| 1174 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1175 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1176 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1180 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1181 | { |
| 1182 | __io_req_set_refcount(req, 1); |
| 1183 | } |
| 1184 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1185 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1186 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1187 | struct io_ring_ctx *ctx = req->ctx; |
| 1188 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1189 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1190 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1191 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1195 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1196 | { |
| 1197 | bool got = percpu_ref_tryget(ref); |
| 1198 | |
| 1199 | /* already at zero, wait for ->release() */ |
| 1200 | if (!got) |
| 1201 | wait_for_completion(compl); |
| 1202 | percpu_ref_resurrect(ref); |
| 1203 | if (got) |
| 1204 | percpu_ref_put(ref); |
| 1205 | } |
| 1206 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1207 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1208 | bool cancel_all) |
Pavel Begunkov | 1c939a5 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1209 | __must_hold(&req->ctx->timeout_lock) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1210 | { |
| 1211 | struct io_kiocb *req; |
| 1212 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1213 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1214 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1215 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1216 | return true; |
| 1217 | |
| 1218 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1219 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1220 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1221 | } |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
Pavel Begunkov | 1c939a5 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1225 | static bool io_match_linked(struct io_kiocb *head) |
| 1226 | { |
| 1227 | struct io_kiocb *req; |
| 1228 | |
| 1229 | io_for_each_link(req, head) { |
| 1230 | if (req->flags & REQ_F_INFLIGHT) |
| 1231 | return true; |
| 1232 | } |
| 1233 | return false; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * As io_match_task() but protected against racing with linked timeouts. |
| 1238 | * User must not hold timeout_lock. |
| 1239 | */ |
| 1240 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, |
| 1241 | bool cancel_all) |
| 1242 | { |
| 1243 | bool matched; |
| 1244 | |
| 1245 | if (task && head->task != task) |
| 1246 | return false; |
| 1247 | if (cancel_all) |
| 1248 | return true; |
| 1249 | |
| 1250 | if (head->flags & REQ_F_LINK_TIMEOUT) { |
| 1251 | struct io_ring_ctx *ctx = head->ctx; |
| 1252 | |
| 1253 | /* protect against races with linked timeouts */ |
| 1254 | spin_lock_irq(&ctx->timeout_lock); |
| 1255 | matched = io_match_linked(head); |
| 1256 | spin_unlock_irq(&ctx->timeout_lock); |
| 1257 | } else { |
| 1258 | matched = io_match_linked(head); |
| 1259 | } |
| 1260 | return matched; |
| 1261 | } |
| 1262 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1263 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1264 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1265 | req->flags |= REQ_F_FAIL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1266 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1267 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1268 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1269 | { |
| 1270 | req_set_fail(req); |
| 1271 | req->result = res; |
| 1272 | } |
| 1273 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1274 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1275 | { |
| 1276 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1277 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1278 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1281 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1282 | { |
| 1283 | return !req->timeout.off; |
| 1284 | } |
| 1285 | |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1286 | static void io_fallback_req_func(struct work_struct *work) |
| 1287 | { |
| 1288 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1289 | fallback_work.work); |
| 1290 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1291 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1292 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1293 | |
| 1294 | percpu_ref_get(&ctx->refs); |
| 1295 | 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] | 1296 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1297 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1298 | if (locked) { |
| 1299 | if (ctx->submit_state.compl_nr) |
| 1300 | io_submit_flush_completions(ctx); |
| 1301 | mutex_unlock(&ctx->uring_lock); |
| 1302 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1303 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1304 | |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1307 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1308 | { |
| 1309 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1310 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1311 | |
| 1312 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1313 | if (!ctx) |
| 1314 | return NULL; |
| 1315 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1316 | /* |
| 1317 | * Use 5 bits less than the max cq entries, that should give us around |
| 1318 | * 32 entries per hash list if totally full and uniformly spread. |
| 1319 | */ |
| 1320 | hash_bits = ilog2(p->cq_entries); |
| 1321 | hash_bits -= 5; |
| 1322 | if (hash_bits <= 0) |
| 1323 | hash_bits = 1; |
| 1324 | ctx->cancel_hash_bits = hash_bits; |
| 1325 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1326 | GFP_KERNEL); |
| 1327 | if (!ctx->cancel_hash) |
| 1328 | goto err; |
| 1329 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1330 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1331 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1332 | if (!ctx->dummy_ubuf) |
| 1333 | goto err; |
| 1334 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1335 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1336 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1337 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1338 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1339 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1340 | |
| 1341 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1342 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1343 | INIT_LIST_HEAD(&ctx->sqd_list); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1344 | init_waitqueue_head(&ctx->poll_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1345 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1346 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1347 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1348 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1349 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1350 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1351 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1352 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1353 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1354 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1355 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1356 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1357 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1358 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1359 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1360 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1361 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1362 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1363 | INIT_LIST_HEAD(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1364 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1365 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1366 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1367 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1368 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1369 | kfree(ctx); |
| 1370 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1373 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1374 | { |
| 1375 | struct io_rings *r = ctx->rings; |
| 1376 | |
| 1377 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1378 | ctx->cq_extra--; |
| 1379 | } |
| 1380 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1381 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1382 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1383 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1384 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1385 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1386 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1387 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1388 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1389 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1390 | } |
| 1391 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1392 | #define FFS_ASYNC_READ 0x1UL |
| 1393 | #define FFS_ASYNC_WRITE 0x2UL |
| 1394 | #ifdef CONFIG_64BIT |
| 1395 | #define FFS_ISREG 0x4UL |
| 1396 | #else |
| 1397 | #define FFS_ISREG 0x0UL |
| 1398 | #endif |
| 1399 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) |
| 1400 | |
| 1401 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1402 | { |
| 1403 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); |
| 1404 | } |
| 1405 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1406 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1407 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1408 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1409 | req->flags |= REQ_F_INFLIGHT; |
Jens Axboe | 3746d62 | 2022-06-23 11:06:43 -0600 | [diff] [blame] | 1410 | atomic_inc(&req->task->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1414 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1415 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1416 | if (WARN_ON_ONCE(!req->link)) |
| 1417 | return NULL; |
| 1418 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1419 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1420 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1421 | |
| 1422 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1423 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1424 | __io_req_set_refcount(req->link, 2); |
| 1425 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1429 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1430 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1431 | return NULL; |
| 1432 | return __io_prep_linked_timeout(req); |
| 1433 | } |
| 1434 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1435 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1436 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1437 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1438 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1439 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1440 | if (!(req->flags & REQ_F_CREDS)) { |
| 1441 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1442 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1443 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1444 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1445 | req->work.list.next = NULL; |
| 1446 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1447 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1448 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1449 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1450 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1451 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1452 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1453 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1454 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1455 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1456 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1457 | } |
| 1458 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1459 | static void io_prep_async_link(struct io_kiocb *req) |
| 1460 | { |
| 1461 | struct io_kiocb *cur; |
| 1462 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1463 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1464 | struct io_ring_ctx *ctx = req->ctx; |
| 1465 | |
Pavel Begunkov | 09eb40f | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1466 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1467 | io_for_each_link(cur, req) |
| 1468 | io_prep_async_work(cur); |
Pavel Begunkov | 09eb40f | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1469 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1470 | } else { |
| 1471 | io_for_each_link(cur, req) |
| 1472 | io_prep_async_work(cur); |
| 1473 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1474 | } |
| 1475 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1476 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1477 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1478 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1479 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1480 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1481 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1482 | /* must not take the lock, NULL it as a precaution */ |
| 1483 | locked = NULL; |
| 1484 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1485 | BUG_ON(!tctx); |
| 1486 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1487 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1488 | /* init ->work of the whole link before punting */ |
| 1489 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1490 | |
| 1491 | /* |
| 1492 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1493 | * happen, catch it here and ensure the request is marked as |
| 1494 | * canceled. That will make io-wq go through the usual work cancel |
| 1495 | * procedure rather than attempt to run this request (or create a new |
| 1496 | * worker for it). |
| 1497 | */ |
| 1498 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1499 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1500 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1501 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1502 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1503 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1504 | if (link) |
| 1505 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1506 | } |
| 1507 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1508 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1509 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1510 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1511 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1512 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1513 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1514 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1515 | if (status) |
| 1516 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1517 | atomic_set(&req->ctx->cq_timeouts, |
| 1518 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1519 | list_del_init(&req->timeout.list); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1520 | io_fill_cqe_req(req, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1521 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1525 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1526 | { |
Jens Axboe | fb34885 | 2023-07-11 09:35:30 -0600 | [diff] [blame] | 1527 | lockdep_assert_held(&ctx->completion_lock); |
| 1528 | |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1529 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1530 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1531 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1532 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1533 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1534 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1535 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1536 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1537 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1538 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1539 | } |
| 1540 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1541 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1542 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1543 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1544 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Jens Axboe | ba7261a | 2022-04-08 11:08:58 -0600 | [diff] [blame] | 1545 | struct io_kiocb *req, *tmp; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1546 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1547 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | ba7261a | 2022-04-08 11:08:58 -0600 | [diff] [blame] | 1548 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1549 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1550 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1551 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1552 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1553 | |
| 1554 | /* |
| 1555 | * Since seq can easily wrap around over time, subtract |
| 1556 | * the last seq at which timeouts were flushed before comparing. |
| 1557 | * Assuming not more than 2^31-1 events have happened since, |
| 1558 | * these subtractions won't have wrapped, so we can check if |
| 1559 | * target is in [last_seq, current_seq] by comparing the two. |
| 1560 | */ |
| 1561 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1562 | events_got = seq - ctx->cq_last_tm_flush; |
| 1563 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1564 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1565 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1566 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1567 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1568 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1569 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1570 | } |
| 1571 | |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1572 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1573 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1574 | if (ctx->off_timeout_used) |
| 1575 | io_flush_timeouts(ctx); |
| 1576 | if (ctx->drain_active) |
| 1577 | io_queue_deferred(ctx); |
| 1578 | } |
| 1579 | |
Jens Axboe | fb34885 | 2023-07-11 09:35:30 -0600 | [diff] [blame] | 1580 | static inline bool io_commit_needs_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1581 | { |
Jens Axboe | fb34885 | 2023-07-11 09:35:30 -0600 | [diff] [blame] | 1582 | return ctx->off_timeout_used || ctx->drain_active; |
| 1583 | } |
| 1584 | |
| 1585 | static inline void __io_commit_cqring(struct io_ring_ctx *ctx) |
| 1586 | { |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1587 | /* order cqe stores with ring update */ |
| 1588 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1589 | } |
| 1590 | |
Jens Axboe | fb34885 | 2023-07-11 09:35:30 -0600 | [diff] [blame] | 1591 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1592 | { |
| 1593 | if (unlikely(io_commit_needs_flush(ctx))) |
| 1594 | __io_commit_cqring_flush(ctx); |
| 1595 | __io_commit_cqring(ctx); |
| 1596 | } |
| 1597 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1598 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1599 | { |
| 1600 | struct io_rings *r = ctx->rings; |
| 1601 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1602 | 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] | 1603 | } |
| 1604 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1605 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1606 | { |
| 1607 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1608 | } |
| 1609 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1610 | 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] | 1611 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1612 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1613 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1614 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1615 | /* |
| 1616 | * writes to the cq entry need to come after reading head; the |
| 1617 | * control dependency is enough as we're using WRITE_ONCE to |
| 1618 | * fill the cq entry |
| 1619 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1620 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1621 | return NULL; |
| 1622 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1623 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1624 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1627 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1628 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1629 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1630 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1631 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1632 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1633 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1636 | /* |
| 1637 | * This should only get called when at least one event has been posted. |
| 1638 | * Some applications rely on the eventfd notification count only changing |
| 1639 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1640 | * 1:1 relationship between how many times this function is called (and |
| 1641 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1642 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1643 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1644 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1645 | /* |
| 1646 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1647 | * io_should_wake() handle the termination of the loop and only |
| 1648 | * wake as many waiters as we need to. |
| 1649 | */ |
| 1650 | if (wq_has_sleeper(&ctx->cq_wait)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1651 | __wake_up(&ctx->cq_wait, TASK_NORMAL, 0, |
| 1652 | poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1653 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1654 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1655 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1656 | eventfd_signal_mask(ctx->cq_ev_fd, 1, EPOLL_URING_WAKE); |
Pavel Begunkov | 3f00838 | 2021-10-01 10:39:33 +0100 | [diff] [blame] | 1657 | if (waitqueue_active(&ctx->poll_wait)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1658 | __wake_up(&ctx->poll_wait, TASK_INTERRUPTIBLE, 0, |
| 1659 | poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1660 | } |
| 1661 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1662 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1663 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1664 | /* see waitqueue_active() comment */ |
| 1665 | smp_mb(); |
| 1666 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1667 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1668 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1669 | __wake_up(&ctx->cq_wait, TASK_NORMAL, 0, |
| 1670 | poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1671 | } |
| 1672 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1673 | eventfd_signal_mask(ctx->cq_ev_fd, 1, EPOLL_URING_WAKE); |
Pavel Begunkov | 3f00838 | 2021-10-01 10:39:33 +0100 | [diff] [blame] | 1674 | if (waitqueue_active(&ctx->poll_wait)) |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 1675 | __wake_up(&ctx->poll_wait, TASK_INTERRUPTIBLE, 0, |
| 1676 | poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1679 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1680 | 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] | 1681 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1682 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1683 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1684 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1685 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1686 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1687 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1688 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1689 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1690 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1691 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1692 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1693 | if (!cqe && !force) |
| 1694 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1695 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1696 | struct io_overflow_cqe, list); |
| 1697 | if (cqe) |
| 1698 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1699 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1700 | io_account_cq_overflow(ctx); |
| 1701 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1702 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1703 | list_del(&ocqe->list); |
| 1704 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1707 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1708 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1709 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1710 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1711 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1712 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1713 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1714 | if (posted) |
| 1715 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1716 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1717 | if (posted) |
| 1718 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1719 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1722 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1723 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1724 | bool ret = true; |
| 1725 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1726 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1727 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1728 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1729 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1730 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1731 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1732 | mutex_unlock(&ctx->uring_lock); |
| 1733 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1734 | |
| 1735 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1738 | /* must to be called somewhat shortly after putting a request */ |
| 1739 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1740 | { |
| 1741 | struct io_uring_task *tctx = task->io_uring; |
| 1742 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1743 | if (likely(task == current)) { |
| 1744 | tctx->cached_refs += nr; |
| 1745 | } else { |
| 1746 | percpu_counter_sub(&tctx->inflight, nr); |
| 1747 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1748 | wake_up(&tctx->wait); |
| 1749 | put_task_struct_many(task, nr); |
| 1750 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1753 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1754 | { |
| 1755 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1756 | |
| 1757 | percpu_counter_add(&tctx->inflight, refill); |
| 1758 | refcount_add(refill, ¤t->usage); |
| 1759 | tctx->cached_refs += refill; |
| 1760 | } |
| 1761 | |
| 1762 | static inline void io_get_task_refs(int nr) |
| 1763 | { |
| 1764 | struct io_uring_task *tctx = current->io_uring; |
| 1765 | |
| 1766 | tctx->cached_refs -= nr; |
| 1767 | if (unlikely(tctx->cached_refs < 0)) |
| 1768 | io_task_refs_refill(tctx); |
| 1769 | } |
| 1770 | |
Pavel Begunkov | b168b1a | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 1771 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
| 1772 | { |
| 1773 | struct io_uring_task *tctx = task->io_uring; |
| 1774 | unsigned int refs = tctx->cached_refs; |
| 1775 | |
| 1776 | if (refs) { |
| 1777 | tctx->cached_refs = 0; |
| 1778 | percpu_counter_sub(&tctx->inflight, refs); |
| 1779 | put_task_struct_many(task, refs); |
| 1780 | } |
| 1781 | } |
| 1782 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1783 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1784 | s32 res, u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1785 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1786 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1787 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1788 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1789 | if (!ocqe) { |
| 1790 | /* |
| 1791 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1792 | * or cannot allocate an overflow entry, then we need to drop it |
| 1793 | * on the floor. |
| 1794 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1795 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1796 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1797 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1798 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1799 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1800 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1801 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1802 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1803 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1804 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1805 | ocqe->cqe.res = res; |
| 1806 | ocqe->cqe.flags = cflags; |
| 1807 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1808 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1811 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
| 1812 | s32 res, u32 cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1813 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1814 | struct io_uring_cqe *cqe; |
| 1815 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1816 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | * If we can't get a cq entry, userspace overflowed the |
| 1820 | * submission (by quite a lot). Increment the overflow count in |
| 1821 | * the ring. |
| 1822 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1823 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1824 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1825 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1826 | WRITE_ONCE(cqe->res, res); |
| 1827 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1828 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1829 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1830 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1833 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1834 | { |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1835 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
| 1836 | } |
| 1837 | |
| 1838 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
| 1839 | s32 res, u32 cflags) |
| 1840 | { |
| 1841 | ctx->cq_extra++; |
| 1842 | return __io_fill_cqe(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1845 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
| 1846 | u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1847 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1848 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1849 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1850 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 1851 | __io_fill_cqe(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1852 | /* |
| 1853 | * If we're the last reference to this request, add to our locked |
| 1854 | * free_list cache. |
| 1855 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1856 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1857 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1858 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1859 | io_disarm_next(req); |
| 1860 | if (req->link) { |
| 1861 | io_req_task_queue(req->link); |
| 1862 | req->link = NULL; |
| 1863 | } |
| 1864 | } |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1865 | io_dismantle_req(req); |
| 1866 | io_put_task(req->task, 1); |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1867 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1868 | ctx->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1869 | } else { |
| 1870 | if (!percpu_ref_tryget(&ctx->refs)) |
| 1871 | req = NULL; |
| 1872 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1873 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1874 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1875 | |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1876 | if (req) { |
| 1877 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1878 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1879 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1882 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
| 1883 | { |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 1884 | return req->flags & IO_REQ_CLEAN_FLAGS; |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1885 | } |
| 1886 | |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1887 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
| 1888 | u32 cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1889 | { |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1890 | if (io_req_needs_clean(req)) |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1891 | io_clean_op(req); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1892 | req->result = res; |
| 1893 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1894 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1895 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1896 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1897 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1898 | s32 res, u32 cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1899 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1900 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1901 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1902 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1903 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1904 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1905 | |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1906 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1907 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1908 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 1911 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1912 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1913 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1914 | io_req_complete_post(req, res, 0); |
| 1915 | } |
| 1916 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 1917 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 1918 | { |
| 1919 | /* |
| 1920 | * We don't submit, fail them all, for that replace hardlinks with |
| 1921 | * normal links. Extra REQ_F_LINK is tolerated. |
| 1922 | */ |
| 1923 | req->flags &= ~REQ_F_HARDLINK; |
| 1924 | req->flags |= REQ_F_LINK; |
| 1925 | io_req_complete_failed(req, req->result); |
| 1926 | } |
| 1927 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1928 | /* |
| 1929 | * Don't initialise the fields below on every allocation, but do that in |
| 1930 | * advance and keep them valid across allocations. |
| 1931 | */ |
| 1932 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1933 | { |
| 1934 | req->ctx = ctx; |
| 1935 | req->link = NULL; |
| 1936 | req->async_data = NULL; |
| 1937 | /* not necessary, but safer to zero */ |
| 1938 | req->result = 0; |
| 1939 | } |
| 1940 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1941 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1942 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1943 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1944 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1945 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1946 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1947 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1950 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1951 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1952 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1953 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1954 | int nr; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1955 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1956 | /* |
| 1957 | * If we have more than a batch's worth of requests in our IRQ side |
| 1958 | * locked cache, grab the lock and move them over to our submission |
| 1959 | * side cache. |
| 1960 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1961 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1962 | io_flush_cached_locked_reqs(ctx, state); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1963 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1964 | nr = state->free_reqs; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1965 | while (!list_empty(&state->free_list)) { |
| 1966 | struct io_kiocb *req = list_first_entry(&state->free_list, |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1967 | struct io_kiocb, inflight_entry); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1968 | |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 1969 | list_del(&req->inflight_entry); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1970 | state->reqs[nr++] = req; |
| 1971 | if (nr == ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1972 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1973 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1974 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1975 | state->free_reqs = nr; |
| 1976 | return nr != 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1979 | /* |
| 1980 | * A request might get retired back into the request caches even before opcode |
| 1981 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 1982 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 1983 | * and with extra caution to not get a request that is still worked on. |
| 1984 | */ |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1985 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1986 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1987 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1988 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1989 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
| 1990 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1991 | |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 1992 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1993 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1994 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
| 1995 | goto got_req; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1996 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1997 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1998 | state->reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1999 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2000 | /* |
| 2001 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2002 | * retry single alloc to be on the safe side. |
| 2003 | */ |
| 2004 | if (unlikely(ret <= 0)) { |
| 2005 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2006 | if (!state->reqs[0]) |
| 2007 | return NULL; |
| 2008 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2009 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2010 | |
| 2011 | for (i = 0; i < ret; i++) |
| 2012 | io_preinit_req(state->reqs[i], ctx); |
| 2013 | state->free_reqs = ret; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2014 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 2015 | state->free_reqs--; |
| 2016 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2019 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2020 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2021 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2022 | fput(file); |
| 2023 | } |
| 2024 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2025 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 2027 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2028 | |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 2029 | if (io_req_needs_clean(req)) |
| 2030 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2031 | if (!(flags & REQ_F_FIXED_FILE)) |
| 2032 | io_put_file(req->file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 2033 | if (req->fixed_rsrc_refs) |
| 2034 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 99ebe4e | 2021-06-26 21:40:49 +0100 | [diff] [blame] | 2035 | if (req->async_data) { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 2036 | kfree(req->async_data); |
Pavel Begunkov | 99ebe4e | 2021-06-26 21:40:49 +0100 | [diff] [blame] | 2037 | req->async_data = NULL; |
| 2038 | } |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2039 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2040 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2041 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2042 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2043 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2044 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2045 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2046 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2047 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2048 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 2049 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 2050 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2051 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 2052 | |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2053 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2054 | } |
| 2055 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2056 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2057 | { |
| 2058 | struct io_kiocb *nxt = req->link; |
| 2059 | |
| 2060 | req->link = nxt->link; |
| 2061 | nxt->link = NULL; |
| 2062 | } |
| 2063 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2064 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 2065 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2066 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2067 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2068 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2069 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 2070 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2071 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2072 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2073 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2074 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2075 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2076 | list_del(&link->timeout.list); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 2077 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2078 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2079 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2080 | } |
| 2081 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2082 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2083 | } |
| 2084 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2085 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2086 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2087 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2088 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2089 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2090 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2091 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2092 | long res = -ECANCELED; |
| 2093 | |
| 2094 | if (link->flags & REQ_F_FAIL) |
| 2095 | res = link->result; |
| 2096 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2097 | nxt = link->link; |
| 2098 | link->link = NULL; |
| 2099 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2100 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 2101 | io_fill_cqe_req(link, res, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2102 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2103 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2104 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2105 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2106 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2107 | static bool io_disarm_next(struct io_kiocb *req) |
| 2108 | __must_hold(&req->ctx->completion_lock) |
| 2109 | { |
| 2110 | bool posted = false; |
| 2111 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2112 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2113 | struct io_kiocb *link = req->link; |
| 2114 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2115 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2116 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2117 | io_remove_next_linked(req); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 2118 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2119 | io_put_req_deferred(link); |
| 2120 | posted = true; |
| 2121 | } |
| 2122 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2123 | struct io_ring_ctx *ctx = req->ctx; |
| 2124 | |
| 2125 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2126 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2127 | spin_unlock_irq(&ctx->timeout_lock); |
| 2128 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2129 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2130 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2131 | posted |= (req->link != NULL); |
| 2132 | io_fail_links(req); |
| 2133 | } |
| 2134 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2135 | } |
| 2136 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2137 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2138 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2139 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2140 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2141 | /* |
| 2142 | * If LINK is set, we have dependent requests in this chain. If we |
| 2143 | * didn't fail this request, queue the first one up, moving any other |
| 2144 | * dependencies to the next request. In case of failure, fail the rest |
| 2145 | * of the chain. |
| 2146 | */ |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2147 | if (req->flags & IO_DISARM_MASK) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2148 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2149 | bool posted; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2150 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2151 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2152 | posted = io_disarm_next(req); |
| 2153 | if (posted) |
| 2154 | io_commit_cqring(req->ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2155 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2156 | if (posted) |
| 2157 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2158 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2159 | nxt = req->link; |
| 2160 | req->link = NULL; |
| 2161 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2162 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2163 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2164 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2165 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 2166 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2167 | return NULL; |
| 2168 | return __io_req_find_next(req); |
| 2169 | } |
| 2170 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2171 | 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] | 2172 | { |
| 2173 | if (!ctx) |
| 2174 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2175 | if (*locked) { |
Hao Xu | 99c8bc5 | 2021-08-21 06:19:54 +0800 | [diff] [blame] | 2176 | if (ctx->submit_state.compl_nr) |
| 2177 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2178 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2179 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2180 | } |
| 2181 | percpu_ref_put(&ctx->refs); |
| 2182 | } |
| 2183 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2184 | static void tctx_task_work(struct callback_head *cb) |
| 2185 | { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2186 | bool locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2187 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2188 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2189 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2190 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2191 | while (1) { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2192 | struct io_wq_work_node *node; |
| 2193 | |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2194 | if (!tctx->task_list.first && locked && ctx->submit_state.compl_nr) |
| 2195 | io_submit_flush_completions(ctx); |
| 2196 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2197 | spin_lock_irq(&tctx->task_lock); |
Pavel Begunkov | c6538be | 2021-06-17 18:14:08 +0100 | [diff] [blame] | 2198 | node = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2199 | INIT_WQ_LIST(&tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2200 | if (!node) |
| 2201 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2202 | spin_unlock_irq(&tctx->task_lock); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2203 | if (!node) |
| 2204 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2205 | |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2206 | do { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2207 | struct io_wq_work_node *next = node->next; |
| 2208 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2209 | io_task_work.node); |
| 2210 | |
| 2211 | if (req->ctx != ctx) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2212 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2213 | ctx = req->ctx; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2214 | /* if not contended, grab and improve batching */ |
| 2215 | locked = mutex_trylock(&ctx->uring_lock); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2216 | percpu_ref_get(&ctx->refs); |
| 2217 | } |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2218 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2219 | node = next; |
Jens Axboe | d9e1cfa | 2023-07-17 10:27:20 -0600 | [diff] [blame] | 2220 | if (unlikely(need_resched())) { |
| 2221 | ctx_flush_and_put(ctx, &locked); |
| 2222 | ctx = NULL; |
| 2223 | cond_resched(); |
| 2224 | } |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2225 | } while (node); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2226 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2227 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2228 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | b168b1a | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 2229 | |
| 2230 | /* relaxed read is enough as only the task itself sets ->in_idle */ |
| 2231 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 2232 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2235 | static void io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2236 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2237 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2238 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2239 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2240 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2241 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2242 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2243 | |
| 2244 | WARN_ON_ONCE(!tctx); |
| 2245 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2246 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2247 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2248 | running = tctx->task_running; |
| 2249 | if (!running) |
| 2250 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2251 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2252 | |
| 2253 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2254 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2255 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2256 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2257 | /* |
| 2258 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2259 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2260 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2261 | * will do the job. |
| 2262 | */ |
| 2263 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2264 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
| 2265 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2266 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2267 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2268 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2269 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2270 | tctx->task_running = false; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2271 | node = tctx->task_list.first; |
| 2272 | INIT_WQ_LIST(&tctx->task_list); |
| 2273 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2274 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2275 | while (node) { |
| 2276 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2277 | node = node->next; |
| 2278 | if (llist_add(&req->io_task_work.fallback_node, |
| 2279 | &req->ctx->fallback_llist)) |
| 2280 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2281 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2284 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2285 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2286 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2287 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2288 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2289 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2290 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2291 | } |
| 2292 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2293 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2294 | { |
| 2295 | struct io_ring_ctx *ctx = req->ctx; |
| 2296 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2297 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2298 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2299 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2300 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2301 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2302 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2303 | } |
| 2304 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2305 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2306 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2307 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2308 | req->io_task_work.func = io_req_task_cancel; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2309 | io_req_task_work_add(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2312 | static void io_req_task_queue(struct io_kiocb *req) |
| 2313 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2314 | req->io_task_work.func = io_req_task_submit; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2315 | io_req_task_work_add(req); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2318 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2319 | { |
| 2320 | req->io_task_work.func = io_queue_async_work; |
| 2321 | io_req_task_work_add(req); |
| 2322 | } |
| 2323 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2324 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2325 | { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2326 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2327 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2328 | if (nxt) |
| 2329 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2330 | } |
| 2331 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2332 | static void io_free_req(struct io_kiocb *req) |
| 2333 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2334 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2335 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2336 | } |
| 2337 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2338 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2339 | { |
| 2340 | io_free_req(req); |
| 2341 | } |
| 2342 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2343 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2344 | struct task_struct *task; |
| 2345 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2346 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2347 | }; |
| 2348 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2349 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2350 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2351 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2352 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2353 | rb->task = NULL; |
| 2354 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2355 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2356 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2357 | struct req_batch *rb) |
| 2358 | { |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2359 | if (rb->ctx_refs) |
| 2360 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 2361 | if (rb->task) |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 2362 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2363 | } |
| 2364 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2365 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2366 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2367 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2368 | io_queue_next(req); |
Pavel Begunkov | 9667065 | 2021-03-19 17:22:32 +0000 | [diff] [blame] | 2369 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2370 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2371 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2372 | if (rb->task) |
| 2373 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2374 | rb->task = req->task; |
| 2375 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2376 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2377 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2378 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2379 | |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2380 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2381 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2382 | else |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2383 | list_add(&req->inflight_entry, &state->free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2384 | } |
| 2385 | |
Pavel Begunkov | 2a2758f | 2021-06-17 18:14:00 +0100 | [diff] [blame] | 2386 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2387 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2388 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2389 | struct io_submit_state *state = &ctx->submit_state; |
| 2390 | int i, nr = state->compl_nr; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2391 | struct req_batch rb; |
| 2392 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2393 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2394 | for (i = 0; i < nr; i++) { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2395 | struct io_kiocb *req = state->compl_reqs[i]; |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2396 | |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 2397 | __io_fill_cqe(ctx, req->user_data, req->result, |
| 2398 | req->compl.cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2399 | } |
| 2400 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2401 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2402 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2403 | |
| 2404 | io_init_req_batch(&rb); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2405 | for (i = 0; i < nr; i++) { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2406 | struct io_kiocb *req = state->compl_reqs[i]; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2407 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2408 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2409 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | io_req_free_batch_finish(ctx, &rb); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2413 | state->compl_nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2414 | } |
| 2415 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2416 | /* |
| 2417 | * Drop reference to request, return next in chain (if there is one) if this |
| 2418 | * was the last reference to this request. |
| 2419 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2420 | 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] | 2421 | { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2422 | struct io_kiocb *nxt = NULL; |
| 2423 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2424 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2425 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2426 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2427 | } |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2428 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2431 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2432 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2433 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2434 | io_free_req(req); |
| 2435 | } |
| 2436 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2437 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2438 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2439 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2440 | req->io_task_work.func = io_free_req_work; |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2441 | io_req_task_work_add(req); |
| 2442 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2445 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2446 | { |
| 2447 | /* See comment at the top of this file */ |
| 2448 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2449 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2450 | } |
| 2451 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2452 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2453 | { |
| 2454 | struct io_rings *rings = ctx->rings; |
| 2455 | |
| 2456 | /* make sure SQ entry isn't read before tail */ |
| 2457 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2458 | } |
| 2459 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2460 | 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] | 2461 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2462 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2463 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2464 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2465 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2466 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2467 | kfree(kbuf); |
| 2468 | return cflags; |
| 2469 | } |
| 2470 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2471 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2472 | { |
| 2473 | struct io_buffer *kbuf; |
| 2474 | |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2475 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 2476 | return 0; |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2477 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2478 | return io_put_kbuf(req, kbuf); |
| 2479 | } |
| 2480 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2481 | static inline bool io_run_task_work(void) |
| 2482 | { |
Jens Axboe | 54df6c5 | 2023-03-06 13:15:06 -0700 | [diff] [blame] | 2483 | /* |
| 2484 | * PF_IO_WORKER never returns to userspace, so check here if we have |
| 2485 | * notify work that needs processing. |
| 2486 | */ |
| 2487 | if (current->flags & PF_IO_WORKER && |
Jens Axboe | 337eb88 | 2023-03-06 13:16:38 -0700 | [diff] [blame] | 2488 | test_thread_flag(TIF_NOTIFY_RESUME)) { |
| 2489 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 54df6c5 | 2023-03-06 13:15:06 -0700 | [diff] [blame] | 2490 | tracehook_notify_resume(NULL); |
Jens Axboe | 337eb88 | 2023-03-06 13:16:38 -0700 | [diff] [blame] | 2491 | } |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2492 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2493 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2494 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2495 | return true; |
| 2496 | } |
| 2497 | |
| 2498 | return false; |
| 2499 | } |
| 2500 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2501 | /* |
| 2502 | * Find and free completed poll iocbs |
| 2503 | */ |
| 2504 | 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] | 2505 | struct list_head *done) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2506 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2507 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2509 | |
| 2510 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2511 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2512 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2513 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2514 | while (!list_empty(done)) { |
Pavel Begunkov | ed4629d | 2023-01-14 09:14:03 -0700 | [diff] [blame] | 2515 | struct io_uring_cqe *cqe; |
| 2516 | unsigned cflags; |
| 2517 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2518 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2519 | list_del(&req->inflight_entry); |
Pavel Begunkov | ed4629d | 2023-01-14 09:14:03 -0700 | [diff] [blame] | 2520 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2521 | (*nr_events)++; |
| 2522 | |
Pavel Begunkov | ed4629d | 2023-01-14 09:14:03 -0700 | [diff] [blame] | 2523 | cqe = io_get_cqe(ctx); |
| 2524 | if (cqe) { |
| 2525 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 2526 | WRITE_ONCE(cqe->res, req->result); |
| 2527 | WRITE_ONCE(cqe->flags, cflags); |
| 2528 | } else { |
| 2529 | spin_lock(&ctx->completion_lock); |
| 2530 | io_cqring_event_overflow(ctx, req->user_data, |
| 2531 | req->result, cflags); |
| 2532 | spin_unlock(&ctx->completion_lock); |
| 2533 | } |
| 2534 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2535 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2536 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2537 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2538 | |
Jens Axboe | fb34885 | 2023-07-11 09:35:30 -0600 | [diff] [blame] | 2539 | if (io_commit_needs_flush(ctx)) { |
| 2540 | spin_lock(&ctx->completion_lock); |
| 2541 | __io_commit_cqring_flush(ctx); |
| 2542 | spin_unlock(&ctx->completion_lock); |
| 2543 | } |
| 2544 | __io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2545 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2546 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2549 | 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] | 2550 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2551 | { |
| 2552 | struct io_kiocb *req, *tmp; |
| 2553 | LIST_HEAD(done); |
| 2554 | bool spin; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2555 | |
| 2556 | /* |
| 2557 | * Only spin for completions if we don't have multiple devices hanging |
| 2558 | * off our complete list, and we're under the requested amount. |
| 2559 | */ |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2560 | spin = !ctx->poll_multi_queue && *nr_events < min; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2561 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2562 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2563 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2564 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2565 | |
| 2566 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2567 | * Move completed and retryable entries to our local lists. |
| 2568 | * If we find a request that requires polling, break out |
| 2569 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2570 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2571 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2572 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2573 | continue; |
| 2574 | } |
| 2575 | if (!list_empty(&done)) |
| 2576 | break; |
| 2577 | |
| 2578 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2579 | if (unlikely(ret < 0)) |
| 2580 | return ret; |
| 2581 | else if (ret) |
| 2582 | spin = false; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2583 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2584 | /* iopoll may have completed current req */ |
| 2585 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2586 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | if (!list_empty(&done)) |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2590 | io_iopoll_complete(ctx, nr_events, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2591 | |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2592 | return 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2596 | * We can't just wait for polled events to come to us, we have to actively |
| 2597 | * find and complete them. |
| 2598 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2599 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2600 | { |
| 2601 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2602 | return; |
| 2603 | |
Pavel Begunkov | ea3291c | 2023-12-03 15:37:53 +0000 | [diff] [blame^] | 2604 | percpu_ref_get(&ctx->refs); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2605 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2606 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2607 | unsigned int nr_events = 0; |
| 2608 | |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2609 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2610 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2611 | /* let it sleep and repeat later if can't complete a request */ |
| 2612 | if (nr_events == 0) |
| 2613 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2614 | /* |
| 2615 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2616 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2617 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2618 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2619 | if (need_resched()) { |
| 2620 | mutex_unlock(&ctx->uring_lock); |
| 2621 | cond_resched(); |
| 2622 | mutex_lock(&ctx->uring_lock); |
| 2623 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2624 | } |
| 2625 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | ea3291c | 2023-12-03 15:37:53 +0000 | [diff] [blame^] | 2626 | percpu_ref_put(&ctx->refs); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2629 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2630 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2631 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2632 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2633 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2634 | /* |
| 2635 | * We disallow the app entering submit/complete with polling, but we |
| 2636 | * still need to lock the ring to prevent racing with polled issue |
| 2637 | * that got punted to a workqueue. |
| 2638 | */ |
| 2639 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2640 | /* |
| 2641 | * Don't enter poll loop if we already have events pending. |
| 2642 | * If we do, we can potentially be spinning for commands that |
| 2643 | * already triggered a CQE (eg in error). |
| 2644 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2645 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2646 | __io_cqring_overflow_flush(ctx, false); |
| 2647 | if (io_cqring_events(ctx)) |
| 2648 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2649 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2650 | /* |
| 2651 | * If a submit got punted to a workqueue, we can have the |
| 2652 | * application entering polling for a command before it gets |
| 2653 | * issued. That app will hold the uring_lock for the duration |
| 2654 | * of the poll right here, so we need to take a breather every |
| 2655 | * now and then to ensure that the issue has a chance to add |
| 2656 | * the poll to the issued list. Otherwise we can spin here |
| 2657 | * forever, while the workqueue is stuck trying to acquire the |
| 2658 | * very same mutex. |
| 2659 | */ |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2660 | if (list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2661 | u32 tail = ctx->cached_cq_tail; |
| 2662 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2663 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2664 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2665 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2666 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2667 | /* some requests don't go through iopoll_list */ |
| 2668 | if (tail != ctx->cached_cq_tail || |
| 2669 | list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2670 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2671 | } |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 2672 | ret = io_do_iopoll(ctx, &nr_events, min); |
Pavel Begunkov | c298482 | 2023-09-12 15:02:01 +0100 | [diff] [blame] | 2673 | |
| 2674 | if (task_sigpending(current)) { |
| 2675 | ret = -EINTR; |
| 2676 | goto out; |
| 2677 | } |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2678 | } while (!ret && nr_events < min && !need_resched()); |
| 2679 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2680 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2681 | return ret; |
| 2682 | } |
| 2683 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2684 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2685 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2686 | /* |
| 2687 | * Tell lockdep we inherited freeze protection from submission |
| 2688 | * thread. |
| 2689 | */ |
| 2690 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2691 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2692 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2693 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2694 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2695 | } |
| 2696 | } |
| 2697 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2698 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2699 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2700 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2701 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2702 | |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2703 | if (!rw) |
| 2704 | return !io_req_prep_async(req); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 2705 | iov_iter_restore(&rw->iter, &rw->iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2706 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2707 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2708 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2709 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2710 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2711 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2712 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2713 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2714 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2715 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2716 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2717 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2718 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2719 | /* |
| 2720 | * If ref is dying, we might be running poll reap from the exit work. |
| 2721 | * Don't attempt to reissue from that path, just let it fail with |
| 2722 | * -EAGAIN. |
| 2723 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2724 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2725 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2726 | /* |
| 2727 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2728 | * not in the original thread group (or in task context). |
| 2729 | */ |
| 2730 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2731 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2732 | return true; |
| 2733 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2734 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2735 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2736 | { |
| 2737 | return false; |
| 2738 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2739 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2740 | { |
| 2741 | return false; |
| 2742 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2743 | #endif |
| 2744 | |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 2745 | /* |
| 2746 | * Trigger the notifications after having done some IO, and finish the write |
| 2747 | * accounting, if any. |
| 2748 | */ |
| 2749 | static void io_req_io_end(struct io_kiocb *req) |
| 2750 | { |
| 2751 | struct io_rw *rw = &req->rw; |
| 2752 | |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 2753 | if (rw->kiocb.ki_flags & IOCB_WRITE) { |
| 2754 | kiocb_end_write(req); |
| 2755 | fsnotify_modify(req->file); |
| 2756 | } else { |
| 2757 | fsnotify_access(req->file); |
| 2758 | } |
| 2759 | } |
| 2760 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2761 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2762 | { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2763 | if (res != req->result) { |
| 2764 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2765 | io_rw_should_reissue(req)) { |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * Reissue will start accounting again, finish the |
| 2768 | * current cycle. |
| 2769 | */ |
| 2770 | io_req_io_end(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2771 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2772 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2773 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2774 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2775 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2776 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2777 | return false; |
| 2778 | } |
| 2779 | |
Harshit Mogalapalli | e326ee0 | 2023-01-10 08:46:47 -0800 | [diff] [blame] | 2780 | static inline int io_fixup_rw_res(struct io_kiocb *req, long res) |
Pavel Begunkov | e857457 | 2022-10-16 22:42:56 +0100 | [diff] [blame] | 2781 | { |
| 2782 | struct io_async_rw *io = req->async_data; |
| 2783 | |
| 2784 | /* add previously done IO, if any */ |
| 2785 | if (io && io->bytes_done > 0) { |
| 2786 | if (res < 0) |
| 2787 | res = io->bytes_done; |
| 2788 | else |
| 2789 | res += io->bytes_done; |
| 2790 | } |
| 2791 | return res; |
| 2792 | } |
| 2793 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2794 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2795 | { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2796 | unsigned int cflags = io_put_rw_kbuf(req); |
Pavel Begunkov | 5c0ea4c | 2022-08-29 14:30:12 +0100 | [diff] [blame] | 2797 | int res = req->result; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2798 | |
| 2799 | if (*locked) { |
| 2800 | struct io_ring_ctx *ctx = req->ctx; |
| 2801 | struct io_submit_state *state = &ctx->submit_state; |
| 2802 | |
| 2803 | io_req_complete_state(req, res, cflags); |
| 2804 | state->compl_reqs[state->compl_nr++] = req; |
| 2805 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) |
| 2806 | io_submit_flush_completions(ctx); |
| 2807 | } else { |
| 2808 | io_req_complete_post(req, res, cflags); |
| 2809 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2810 | } |
| 2811 | |
Jens Axboe | 89a410d | 2023-01-21 13:38:51 -0700 | [diff] [blame] | 2812 | static void io_req_rw_complete(struct io_kiocb *req, bool *locked) |
| 2813 | { |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 2814 | io_req_io_end(req); |
Jens Axboe | 89a410d | 2023-01-21 13:38:51 -0700 | [diff] [blame] | 2815 | io_req_task_complete(req, locked); |
| 2816 | } |
| 2817 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2818 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2819 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2820 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2821 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2822 | if (__io_complete_rw_common(req, res)) |
| 2823 | return; |
Pavel Begunkov | e857457 | 2022-10-16 22:42:56 +0100 | [diff] [blame] | 2824 | req->result = io_fixup_rw_res(req, res); |
Jens Axboe | 89a410d | 2023-01-21 13:38:51 -0700 | [diff] [blame] | 2825 | req->io_task_work.func = io_req_rw_complete; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2826 | io_req_task_work_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2829 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2830 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2831 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2832 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2833 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2834 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2835 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2836 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2837 | req->flags |= REQ_F_REISSUE; |
| 2838 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2839 | } |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2840 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2841 | |
| 2842 | WRITE_ONCE(req->result, res); |
Jens Axboe | b9b0e0d | 2021-02-23 08:18:36 -0700 | [diff] [blame] | 2843 | /* order with io_iopoll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2844 | smp_wmb(); |
| 2845 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /* |
| 2849 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2850 | * 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] | 2851 | * 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] | 2852 | * accessing the kiocb cookie. |
| 2853 | */ |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2854 | static void io_iopoll_req_issued(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2855 | { |
| 2856 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2857 | const bool in_async = io_wq_current_is_worker(); |
| 2858 | |
| 2859 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 2860 | if (unlikely(in_async)) |
| 2861 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2862 | |
| 2863 | /* |
| 2864 | * Track whether we have multiple files in our lists. This will impact |
| 2865 | * how we do polling eventually, not spinning if we're on potentially |
| 2866 | * different devices. |
| 2867 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2868 | if (list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2869 | ctx->poll_multi_queue = false; |
| 2870 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2871 | struct io_kiocb *list_req; |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2872 | unsigned int queue_num0, queue_num1; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2873 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2874 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2875 | inflight_entry); |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2876 | |
| 2877 | if (list_req->file != req->file) { |
| 2878 | ctx->poll_multi_queue = true; |
| 2879 | } else { |
| 2880 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); |
| 2881 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); |
| 2882 | if (queue_num0 != queue_num1) |
| 2883 | ctx->poll_multi_queue = true; |
| 2884 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * For fast devices, IO may have already completed. If it has, add |
| 2889 | * it to the front so we find it first. |
| 2890 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2891 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2892 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2893 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2894 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2895 | |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2896 | if (unlikely(in_async)) { |
| 2897 | /* |
| 2898 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2899 | * in sq thread task context or in io worker task context. If |
| 2900 | * current task context is sq thread, we don't need to check |
| 2901 | * whether should wake up sq thread. |
| 2902 | */ |
| 2903 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2904 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2905 | wake_up(&ctx->sq_data->wait); |
| 2906 | |
| 2907 | mutex_unlock(&ctx->uring_lock); |
| 2908 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2911 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2912 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2913 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2914 | } |
| 2915 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2916 | /* |
| 2917 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2918 | * any file. For now, just ensure that anything potentially problematic is done |
| 2919 | * inline. |
| 2920 | */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2921 | static bool __io_file_supports_nowait(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2922 | { |
| 2923 | umode_t mode = file_inode(file)->i_mode; |
| 2924 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2925 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2926 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2927 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2928 | return true; |
| 2929 | return false; |
| 2930 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2931 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2932 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2933 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2934 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2935 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2936 | file->f_op != &io_uring_fops) |
| 2937 | return true; |
| 2938 | return false; |
| 2939 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2940 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2941 | /* any ->read/write should understand O_NONBLOCK */ |
| 2942 | if (file->f_flags & O_NONBLOCK) |
| 2943 | return true; |
| 2944 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2945 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2946 | return false; |
| 2947 | |
| 2948 | if (rw == READ) |
| 2949 | return file->f_op->read_iter != NULL; |
| 2950 | |
| 2951 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2954 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2955 | { |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2956 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2957 | return true; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2958 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2959 | return true; |
| 2960 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 2961 | return __io_file_supports_nowait(req->file, rw); |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2964 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2965 | int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2966 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2967 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2968 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2969 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2970 | unsigned ioprio; |
| 2971 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2972 | |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 2973 | 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] | 2974 | req->flags |= REQ_F_ISREG; |
| 2975 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2976 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2977 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2978 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2979 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2980 | if (unlikely(ret)) |
| 2981 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2982 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2983 | /* |
| 2984 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 2985 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 2986 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 2987 | */ |
| 2988 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 2989 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2990 | req->flags |= REQ_F_NOWAIT; |
| 2991 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2992 | ioprio = READ_ONCE(sqe->ioprio); |
| 2993 | if (ioprio) { |
| 2994 | ret = ioprio_check_cap(ioprio); |
| 2995 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2996 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2997 | |
| 2998 | kiocb->ki_ioprio = ioprio; |
| 2999 | } else |
| 3000 | kiocb->ki_ioprio = get_current_ioprio(); |
| 3001 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3002 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3003 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 3004 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3005 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3006 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 3007 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3008 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3009 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3010 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3011 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3012 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3013 | kiocb->ki_complete = io_complete_rw; |
| 3014 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3015 | |
Pavel Begunkov | ea512d5 | 2022-06-09 08:34:35 +0100 | [diff] [blame] | 3016 | /* used for fixed read/write too - just read unconditionally */ |
| 3017 | req->buf_index = READ_ONCE(sqe->buf_index); |
| 3018 | req->imu = NULL; |
| 3019 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3020 | if (req->opcode == IORING_OP_READ_FIXED || |
| 3021 | req->opcode == IORING_OP_WRITE_FIXED) { |
Pavel Begunkov | ea512d5 | 2022-06-09 08:34:35 +0100 | [diff] [blame] | 3022 | struct io_ring_ctx *ctx = req->ctx; |
| 3023 | u16 index; |
| 3024 | |
| 3025 | if (unlikely(req->buf_index >= ctx->nr_user_bufs)) |
| 3026 | return -EFAULT; |
| 3027 | index = array_index_nospec(req->buf_index, ctx->nr_user_bufs); |
| 3028 | req->imu = ctx->user_bufs[index]; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3029 | io_req_set_rsrc_node(req); |
| 3030 | } |
| 3031 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3032 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3033 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3034 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3038 | { |
| 3039 | switch (ret) { |
| 3040 | case -EIOCBQUEUED: |
| 3041 | break; |
| 3042 | case -ERESTARTSYS: |
| 3043 | case -ERESTARTNOINTR: |
| 3044 | case -ERESTARTNOHAND: |
| 3045 | case -ERESTART_RESTARTBLOCK: |
| 3046 | /* |
| 3047 | * We can't just restart the syscall, since previously |
| 3048 | * submitted sqes may already be in progress. Just fail this |
| 3049 | * IO with EINTR. |
| 3050 | */ |
| 3051 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3052 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3053 | default: |
| 3054 | kiocb->ki_complete(kiocb, ret, 0); |
| 3055 | } |
| 3056 | } |
| 3057 | |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3058 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
Dylan Yudaken | ff8a070 | 2022-02-22 02:55:02 -0800 | [diff] [blame] | 3059 | { |
| 3060 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3061 | |
Jens Axboe | 311b298 | 2022-04-11 09:48:30 -0600 | [diff] [blame] | 3062 | if (kiocb->ki_pos != -1) |
| 3063 | return &kiocb->ki_pos; |
| 3064 | |
| 3065 | if (!(req->file->f_mode & FMODE_STREAM)) { |
| 3066 | req->flags |= REQ_F_CUR_POS; |
| 3067 | kiocb->ki_pos = req->file->f_pos; |
| 3068 | return &kiocb->ki_pos; |
Dylan Yudaken | ff8a070 | 2022-02-22 02:55:02 -0800 | [diff] [blame] | 3069 | } |
Jens Axboe | 311b298 | 2022-04-11 09:48:30 -0600 | [diff] [blame] | 3070 | |
| 3071 | kiocb->ki_pos = 0; |
| 3072 | return NULL; |
Dylan Yudaken | ff8a070 | 2022-02-22 02:55:02 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3075 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3076 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3077 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3078 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3079 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3080 | if (req->flags & REQ_F_CUR_POS) |
| 3081 | req->file->f_pos = kiocb->ki_pos; |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 3082 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) { |
| 3083 | if (!__io_complete_rw_common(req, ret)) { |
| 3084 | /* |
| 3085 | * Safe to call io_end from here as we're inline |
| 3086 | * from the submission path. |
| 3087 | */ |
| 3088 | io_req_io_end(req); |
| 3089 | __io_req_complete(req, issue_flags, |
| 3090 | io_fixup_rw_res(req, ret), |
| 3091 | io_put_rw_kbuf(req)); |
| 3092 | } |
| 3093 | } else { |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3094 | io_rw_done(kiocb, ret); |
Jens Axboe | b10acfc | 2023-01-22 10:36:37 -0700 | [diff] [blame] | 3095 | } |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3096 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 3097 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3098 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 3099 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 3100 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 3101 | } else { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 3102 | unsigned int cflags = io_put_rw_kbuf(req); |
| 3103 | struct io_ring_ctx *ctx = req->ctx; |
| 3104 | |
Pavel Begunkov | e857457 | 2022-10-16 22:42:56 +0100 | [diff] [blame] | 3105 | ret = io_fixup_rw_res(req, ret); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3106 | req_set_fail(req); |
Hao Xu | 14cfbb7 | 2021-10-14 22:04:00 +0800 | [diff] [blame] | 3107 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 3108 | mutex_lock(&ctx->uring_lock); |
| 3109 | __io_req_complete(req, issue_flags, ret, cflags); |
| 3110 | mutex_unlock(&ctx->uring_lock); |
| 3111 | } else { |
| 3112 | __io_req_complete(req, issue_flags, ret, cflags); |
| 3113 | } |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3114 | } |
| 3115 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3116 | } |
| 3117 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3118 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 3119 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3120 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3121 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3122 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3123 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3124 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3125 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3126 | return -EFAULT; |
| 3127 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 3128 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3129 | return -EFAULT; |
| 3130 | |
| 3131 | /* |
| 3132 | * May not be a start of buffer, set size appropriately |
| 3133 | * and advance us to the beginning. |
| 3134 | */ |
| 3135 | offset = buf_addr - imu->ubuf; |
| 3136 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3137 | |
| 3138 | if (offset) { |
| 3139 | /* |
| 3140 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3141 | * using the latter parts of a big fixed buffer - it iterates |
| 3142 | * over each segment manually. We can cheat a bit here, because |
| 3143 | * we know that: |
| 3144 | * |
| 3145 | * 1) it's a BVEC iter, we set it up |
| 3146 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3147 | * first and last bvec |
| 3148 | * |
| 3149 | * So just find our index, and adjust the iterator afterwards. |
| 3150 | * If the offset is within the first bvec (or the whole first |
| 3151 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3152 | * since we can just skip the first segment, which may not |
| 3153 | * be PAGE_SIZE aligned. |
| 3154 | */ |
| 3155 | const struct bio_vec *bvec = imu->bvec; |
| 3156 | |
Keith Busch | 313a34d | 2023-11-20 14:18:31 -0800 | [diff] [blame] | 3157 | if (offset < bvec->bv_len) { |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3158 | iov_iter_advance(iter, offset); |
| 3159 | } else { |
| 3160 | unsigned long seg_skip; |
| 3161 | |
| 3162 | /* skip first vec */ |
| 3163 | offset -= bvec->bv_len; |
| 3164 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3165 | |
| 3166 | iter->bvec = bvec + seg_skip; |
| 3167 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3168 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3169 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3170 | } |
| 3171 | } |
| 3172 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3173 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3176 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 3177 | { |
Pavel Begunkov | ea512d5 | 2022-06-09 08:34:35 +0100 | [diff] [blame] | 3178 | if (WARN_ON_ONCE(!req->imu)) |
| 3179 | return -EFAULT; |
| 3180 | return __io_import_fixed(req, rw, iter, req->imu); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3181 | } |
| 3182 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3183 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3184 | { |
| 3185 | if (needs_lock) |
| 3186 | mutex_unlock(&ctx->uring_lock); |
| 3187 | } |
| 3188 | |
| 3189 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3190 | { |
| 3191 | /* |
| 3192 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3193 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3194 | * The only exception is when we've detached the request and issue it |
| 3195 | * from an async worker thread, grab the lock for that case. |
| 3196 | */ |
| 3197 | if (needs_lock) |
| 3198 | mutex_lock(&ctx->uring_lock); |
| 3199 | } |
| 3200 | |
| 3201 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3202 | int bgid, struct io_buffer *kbuf, |
| 3203 | bool needs_lock) |
| 3204 | { |
| 3205 | struct io_buffer *head; |
| 3206 | |
| 3207 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3208 | return kbuf; |
| 3209 | |
| 3210 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3211 | |
| 3212 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3213 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3214 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3215 | if (head) { |
| 3216 | if (!list_empty(&head->list)) { |
| 3217 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3218 | list); |
| 3219 | list_del(&kbuf->list); |
| 3220 | } else { |
| 3221 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3222 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3223 | } |
| 3224 | if (*len > kbuf->len) |
| 3225 | *len = kbuf->len; |
| 3226 | } else { |
| 3227 | kbuf = ERR_PTR(-ENOBUFS); |
| 3228 | } |
| 3229 | |
| 3230 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3231 | |
| 3232 | return kbuf; |
| 3233 | } |
| 3234 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3235 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3236 | bool needs_lock) |
| 3237 | { |
| 3238 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3239 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3240 | |
| 3241 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3242 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3243 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3244 | if (IS_ERR(kbuf)) |
| 3245 | return kbuf; |
| 3246 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3247 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3248 | return u64_to_user_ptr(kbuf->addr); |
| 3249 | } |
| 3250 | |
| 3251 | #ifdef CONFIG_COMPAT |
| 3252 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3253 | bool needs_lock) |
| 3254 | { |
| 3255 | struct compat_iovec __user *uiov; |
| 3256 | compat_ssize_t clen; |
| 3257 | void __user *buf; |
| 3258 | ssize_t len; |
| 3259 | |
| 3260 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3261 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3262 | return -EFAULT; |
| 3263 | if (__get_user(clen, &uiov->iov_len)) |
| 3264 | return -EFAULT; |
| 3265 | if (clen < 0) |
| 3266 | return -EINVAL; |
| 3267 | |
| 3268 | len = clen; |
| 3269 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3270 | if (IS_ERR(buf)) |
| 3271 | return PTR_ERR(buf); |
| 3272 | iov[0].iov_base = buf; |
| 3273 | iov[0].iov_len = (compat_size_t) len; |
| 3274 | return 0; |
| 3275 | } |
| 3276 | #endif |
| 3277 | |
| 3278 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3279 | bool needs_lock) |
| 3280 | { |
| 3281 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3282 | void __user *buf; |
| 3283 | ssize_t len; |
| 3284 | |
| 3285 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3286 | return -EFAULT; |
| 3287 | |
| 3288 | len = iov[0].iov_len; |
| 3289 | if (len < 0) |
| 3290 | return -EINVAL; |
| 3291 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3292 | if (IS_ERR(buf)) |
| 3293 | return PTR_ERR(buf); |
| 3294 | iov[0].iov_base = buf; |
| 3295 | iov[0].iov_len = len; |
| 3296 | return 0; |
| 3297 | } |
| 3298 | |
| 3299 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3300 | bool needs_lock) |
| 3301 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3302 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3303 | struct io_buffer *kbuf; |
| 3304 | |
| 3305 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3306 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3307 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3308 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3309 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3310 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3311 | return -EINVAL; |
| 3312 | |
| 3313 | #ifdef CONFIG_COMPAT |
| 3314 | if (req->ctx->compat) |
| 3315 | return io_compat_import(req, iov, needs_lock); |
| 3316 | #endif |
| 3317 | |
| 3318 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3319 | } |
| 3320 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3321 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 3322 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3323 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3324 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3325 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3326 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3327 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3328 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3329 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3330 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3331 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3332 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3333 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3334 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3335 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3336 | return -EINVAL; |
| 3337 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3338 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3339 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3340 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3341 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3342 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3343 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3346 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3347 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3348 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3351 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3352 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3353 | if (!ret) |
| 3354 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3355 | *iovec = NULL; |
| 3356 | return ret; |
| 3357 | } |
| 3358 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3359 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3360 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3363 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3364 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3365 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3366 | } |
| 3367 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3368 | /* |
| 3369 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3370 | * by looping over ->read() or ->write() manually. |
| 3371 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3372 | 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] | 3373 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3374 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3375 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3376 | ssize_t ret = 0; |
Dylan Yudaken | b7958ca | 2022-02-22 02:55:01 -0800 | [diff] [blame] | 3377 | loff_t *ppos; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3378 | |
| 3379 | /* |
| 3380 | * Don't support polled IO through this interface, and we can't |
| 3381 | * support non-blocking either. For the latter, this just causes |
| 3382 | * the kiocb to be handled from an async context. |
| 3383 | */ |
| 3384 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3385 | return -EOPNOTSUPP; |
| 3386 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3387 | return -EAGAIN; |
| 3388 | |
Dylan Yudaken | b7958ca | 2022-02-22 02:55:01 -0800 | [diff] [blame] | 3389 | ppos = io_kiocb_ppos(kiocb); |
| 3390 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3391 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3392 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3393 | ssize_t nr; |
| 3394 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3395 | if (!iov_iter_is_bvec(iter)) { |
| 3396 | iovec = iov_iter_iovec(iter); |
| 3397 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3398 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3399 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3400 | } |
| 3401 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3402 | if (rw == READ) { |
| 3403 | nr = file->f_op->read(file, iovec.iov_base, |
Dylan Yudaken | b7958ca | 2022-02-22 02:55:01 -0800 | [diff] [blame] | 3404 | iovec.iov_len, ppos); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3405 | } else { |
| 3406 | nr = file->f_op->write(file, iovec.iov_base, |
Dylan Yudaken | b7958ca | 2022-02-22 02:55:01 -0800 | [diff] [blame] | 3407 | iovec.iov_len, ppos); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | if (nr < 0) { |
| 3411 | if (!ret) |
| 3412 | ret = nr; |
| 3413 | break; |
| 3414 | } |
Jens Axboe | 109dda4 | 2022-03-18 11:28:13 -0600 | [diff] [blame] | 3415 | ret += nr; |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3416 | if (!iov_iter_is_bvec(iter)) { |
| 3417 | iov_iter_advance(iter, nr); |
| 3418 | } else { |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3419 | req->rw.addr += nr; |
Jens Axboe | 109dda4 | 2022-03-18 11:28:13 -0600 | [diff] [blame] | 3420 | req->rw.len -= nr; |
| 3421 | if (!req->rw.len) |
| 3422 | break; |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3423 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3424 | if (nr != iovec.iov_len) |
| 3425 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | return ret; |
| 3429 | } |
| 3430 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3431 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3432 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3433 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3434 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3435 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3436 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3437 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3438 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3439 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3440 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3441 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3442 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3443 | unsigned iov_off = 0; |
| 3444 | |
| 3445 | rw->iter.iov = rw->fast_iov; |
| 3446 | if (iter->iov != fast_iov) { |
| 3447 | iov_off = iter->iov - fast_iov; |
| 3448 | rw->iter.iov += iov_off; |
| 3449 | } |
| 3450 | if (rw->fast_iov != fast_iov) |
| 3451 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3452 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3453 | } else { |
| 3454 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3458 | static inline int io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3459 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3460 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3461 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3462 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3463 | } |
| 3464 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3465 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3466 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3467 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3468 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3469 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3470 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3471 | if (!req->async_data) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3472 | struct io_async_rw *iorw; |
| 3473 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3474 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3475 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3476 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3477 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3478 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3479 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3480 | iorw = req->async_data; |
| 3481 | /* we've copied and mapped the iter, ensure state is saved */ |
| 3482 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3483 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3484 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3487 | 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] | 3488 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3489 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3490 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3491 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3492 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3493 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3494 | if (unlikely(ret < 0)) |
| 3495 | return ret; |
| 3496 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3497 | iorw->bytes_done = 0; |
| 3498 | iorw->free_iovec = iov; |
| 3499 | if (iov) |
| 3500 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3501 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3502 | return 0; |
| 3503 | } |
| 3504 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3505 | 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] | 3506 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3507 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3508 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3509 | return io_prep_rw(req, sqe, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3512 | /* |
| 3513 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3514 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3515 | * This gets called when the page is unlocked, and we generally expect that to |
| 3516 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3517 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3518 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3519 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3520 | * slow path. |
| 3521 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3522 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3523 | int sync, void *arg) |
| 3524 | { |
| 3525 | struct wait_page_queue *wpq; |
| 3526 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3527 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3528 | |
| 3529 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3530 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3531 | if (!wake_page_match(wpq, key)) |
| 3532 | return 0; |
| 3533 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3534 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3535 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3536 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3537 | return 1; |
| 3538 | } |
| 3539 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3540 | /* |
| 3541 | * This controls whether a given IO request should be armed for async page |
| 3542 | * based retry. If we return false here, the request is handed to the async |
| 3543 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3544 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3545 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3546 | * will register a callback when the page is unlocked at IO completion. Through |
| 3547 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3548 | * That retry will attempt the buffered read again. The retry will generally |
| 3549 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3550 | * async worker threads for a blocking retry. |
| 3551 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3552 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3553 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3554 | struct io_async_rw *rw = req->async_data; |
| 3555 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3556 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3557 | |
| 3558 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3559 | if (req->flags & REQ_F_NOWAIT) |
| 3560 | return false; |
| 3561 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3562 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3563 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3564 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3565 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3566 | /* |
| 3567 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3568 | * support callback based unlocks |
| 3569 | */ |
| 3570 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3571 | return false; |
| 3572 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3573 | wait->wait.func = io_async_buf_func; |
| 3574 | wait->wait.private = req; |
| 3575 | wait->wait.flags = 0; |
| 3576 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3577 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3578 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3579 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3580 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3581 | } |
| 3582 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3583 | 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] | 3584 | { |
| 3585 | if (req->file->f_op->read_iter) |
| 3586 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3587 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3588 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3589 | else |
| 3590 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3591 | } |
| 3592 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3593 | static bool need_read_all(struct io_kiocb *req) |
| 3594 | { |
| 3595 | return req->flags & REQ_F_ISREG || |
| 3596 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3597 | } |
| 3598 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3599 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3600 | { |
| 3601 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3602 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3603 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3604 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3605 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3606 | struct iov_iter_state __state, *state; |
| 3607 | ssize_t ret, ret2; |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3608 | loff_t *ppos; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3609 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3610 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3611 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3612 | state = &rw->iter_state; |
| 3613 | /* |
| 3614 | * We come here from an earlier attempt, restore our state to |
| 3615 | * match in case it doesn't. It's cheap enough that we don't |
| 3616 | * need to make this conditional. |
| 3617 | */ |
| 3618 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3619 | iovec = NULL; |
| 3620 | } else { |
| 3621 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3622 | if (ret < 0) |
| 3623 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3624 | state = &__state; |
| 3625 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3626 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3627 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3628 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3629 | /* Ensure we clear previously set non-block flag */ |
| 3630 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3631 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3632 | else |
| 3633 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3634 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3635 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3636 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3637 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3638 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3639 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3640 | |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3641 | ppos = io_kiocb_update_pos(req); |
Dylan Yudaken | ff8a070 | 2022-02-22 02:55:02 -0800 | [diff] [blame] | 3642 | |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3643 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3644 | if (unlikely(ret)) { |
| 3645 | kfree(iovec); |
| 3646 | return ret; |
| 3647 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3648 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3649 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3650 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3651 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3652 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3653 | /* IOPOLL retry should happen for io-wq threads */ |
| 3654 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3655 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3656 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3657 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3658 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3659 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3660 | } else if (ret == -EIOCBQUEUED) { |
| 3661 | goto out_free; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3662 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3663 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3664 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3665 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3666 | } |
| 3667 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3668 | /* |
| 3669 | * Don't depend on the iter state matching what was consumed, or being |
| 3670 | * untouched in case of error. Restore it and we'll advance it |
| 3671 | * manually if we need to. |
| 3672 | */ |
| 3673 | iov_iter_restore(iter, state); |
| 3674 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3675 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3676 | if (ret2) |
| 3677 | return ret2; |
| 3678 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3679 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3680 | rw = req->async_data; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3681 | /* |
| 3682 | * Now use our persistent iterator and state, if we aren't already. |
| 3683 | * We've restored and mapped the iter to match. |
| 3684 | */ |
| 3685 | if (iter != &rw->iter) { |
| 3686 | iter = &rw->iter; |
| 3687 | state = &rw->iter_state; |
| 3688 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3689 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3690 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3691 | /* |
| 3692 | * We end up here because of a partial read, either from |
| 3693 | * above or inside this loop. Advance the iter by the bytes |
| 3694 | * that were consumed. |
| 3695 | */ |
| 3696 | iov_iter_advance(iter, ret); |
| 3697 | if (!iov_iter_count(iter)) |
| 3698 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3699 | rw->bytes_done += ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3700 | iov_iter_save_state(iter, state); |
| 3701 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3702 | /* if we can retry, do so with the callbacks armed */ |
| 3703 | if (!io_rw_should_retry(req)) { |
| 3704 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3705 | return -EAGAIN; |
| 3706 | } |
| 3707 | |
Pavel Begunkov | 98aada6 | 2022-10-16 22:42:58 +0100 | [diff] [blame] | 3708 | req->result = iov_iter_count(iter); |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3709 | /* |
| 3710 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3711 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3712 | * desired page gets unlocked. We can also get a partial read |
| 3713 | * here, and if we do, then just retry at the new offset. |
| 3714 | */ |
| 3715 | ret = io_iter_do_read(req, iter); |
| 3716 | if (ret == -EIOCBQUEUED) |
| 3717 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3718 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3719 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3720 | iov_iter_restore(iter, state); |
| 3721 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3722 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3723 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3724 | out_free: |
| 3725 | /* it's faster to check here then delegate to kfree */ |
| 3726 | if (iovec) |
| 3727 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3728 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3731 | 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] | 3732 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3733 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3734 | return -EBADF; |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3735 | return io_prep_rw(req, sqe, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3736 | } |
| 3737 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3738 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3739 | { |
| 3740 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3741 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3742 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3743 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3744 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3745 | struct iov_iter_state __state, *state; |
| 3746 | ssize_t ret, ret2; |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3747 | loff_t *ppos; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3748 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3749 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3750 | iter = &rw->iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3751 | state = &rw->iter_state; |
| 3752 | iov_iter_restore(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3753 | iovec = NULL; |
| 3754 | } else { |
| 3755 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3756 | if (ret < 0) |
| 3757 | return ret; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3758 | state = &__state; |
| 3759 | iov_iter_save_state(iter, state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3760 | } |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3761 | req->result = iov_iter_count(iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3762 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3763 | /* Ensure we clear previously set non-block flag */ |
| 3764 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3765 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3766 | else |
| 3767 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3768 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3769 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 3770 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3771 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3772 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3773 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3774 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3775 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3776 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3777 | |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3778 | ppos = io_kiocb_update_pos(req); |
Dylan Yudaken | ff8a070 | 2022-02-22 02:55:02 -0800 | [diff] [blame] | 3779 | |
Dylan Yudaken | 05d69b3 | 2022-02-22 02:55:03 -0800 | [diff] [blame] | 3780 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3781 | if (unlikely(ret)) |
| 3782 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3783 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3784 | /* |
| 3785 | * Open-code file_start_write here to grab freeze protection, |
| 3786 | * which will be released by another thread in |
| 3787 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3788 | * released so that it doesn't complain about the held lock when |
| 3789 | * we return to userspace. |
| 3790 | */ |
| 3791 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3792 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3793 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3794 | SB_FREEZE_WRITE); |
| 3795 | } |
| 3796 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3797 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3798 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3799 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3800 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3801 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3802 | else |
| 3803 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3804 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3805 | if (req->flags & REQ_F_REISSUE) { |
| 3806 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3807 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3808 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3809 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3810 | /* |
| 3811 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3812 | * retry them without IOCB_NOWAIT. |
| 3813 | */ |
| 3814 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3815 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3816 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3817 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3818 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3819 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3820 | /* IOPOLL retry should happen for io-wq threads */ |
| 3821 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3822 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3823 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3824 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3825 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3826 | copy_iov: |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3827 | iov_iter_restore(iter, state); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3828 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Jens Axboe | 295219a | 2022-08-25 10:19:08 -0600 | [diff] [blame] | 3829 | if (!ret) { |
| 3830 | if (kiocb->ki_flags & IOCB_WRITE) |
| 3831 | kiocb_end_write(req); |
| 3832 | return -EAGAIN; |
| 3833 | } |
| 3834 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3835 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3836 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3837 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3838 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3839 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3840 | return ret; |
| 3841 | } |
| 3842 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3843 | static int io_renameat_prep(struct io_kiocb *req, |
| 3844 | const struct io_uring_sqe *sqe) |
| 3845 | { |
| 3846 | struct io_rename *ren = &req->rename; |
| 3847 | const char __user *oldf, *newf; |
| 3848 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3849 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3850 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3851 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3852 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3853 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3854 | return -EBADF; |
| 3855 | |
| 3856 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3857 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3858 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3859 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3860 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3861 | |
| 3862 | ren->oldpath = getname(oldf); |
| 3863 | if (IS_ERR(ren->oldpath)) |
| 3864 | return PTR_ERR(ren->oldpath); |
| 3865 | |
| 3866 | ren->newpath = getname(newf); |
| 3867 | if (IS_ERR(ren->newpath)) { |
| 3868 | putname(ren->oldpath); |
| 3869 | return PTR_ERR(ren->newpath); |
| 3870 | } |
| 3871 | |
| 3872 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3876 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3877 | { |
| 3878 | struct io_rename *ren = &req->rename; |
| 3879 | int ret; |
| 3880 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3881 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3882 | return -EAGAIN; |
| 3883 | |
| 3884 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3885 | ren->newpath, ren->flags); |
| 3886 | |
| 3887 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3888 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3889 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3890 | io_req_complete(req, ret); |
| 3891 | return 0; |
| 3892 | } |
| 3893 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3894 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3895 | const struct io_uring_sqe *sqe) |
| 3896 | { |
| 3897 | struct io_unlink *un = &req->unlink; |
| 3898 | const char __user *fname; |
| 3899 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3900 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3901 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3902 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3903 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3904 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3905 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3906 | return -EBADF; |
| 3907 | |
| 3908 | un->dfd = READ_ONCE(sqe->fd); |
| 3909 | |
| 3910 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3911 | if (un->flags & ~AT_REMOVEDIR) |
| 3912 | return -EINVAL; |
| 3913 | |
| 3914 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3915 | un->filename = getname(fname); |
| 3916 | if (IS_ERR(un->filename)) |
| 3917 | return PTR_ERR(un->filename); |
| 3918 | |
| 3919 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3923 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3924 | { |
| 3925 | struct io_unlink *un = &req->unlink; |
| 3926 | int ret; |
| 3927 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3928 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3929 | return -EAGAIN; |
| 3930 | |
| 3931 | if (un->flags & AT_REMOVEDIR) |
| 3932 | ret = do_rmdir(un->dfd, un->filename); |
| 3933 | else |
| 3934 | ret = do_unlinkat(un->dfd, un->filename); |
| 3935 | |
| 3936 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3937 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3938 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3939 | io_req_complete(req, ret); |
| 3940 | return 0; |
| 3941 | } |
| 3942 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3943 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3944 | const struct io_uring_sqe *sqe) |
| 3945 | { |
| 3946 | struct io_mkdir *mkd = &req->mkdir; |
| 3947 | const char __user *fname; |
| 3948 | |
| 3949 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3950 | return -EINVAL; |
| 3951 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3952 | sqe->splice_fd_in) |
| 3953 | return -EINVAL; |
| 3954 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3955 | return -EBADF; |
| 3956 | |
| 3957 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3958 | mkd->mode = READ_ONCE(sqe->len); |
| 3959 | |
| 3960 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3961 | mkd->filename = getname(fname); |
| 3962 | if (IS_ERR(mkd->filename)) |
| 3963 | return PTR_ERR(mkd->filename); |
| 3964 | |
| 3965 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3966 | return 0; |
| 3967 | } |
| 3968 | |
| 3969 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) |
| 3970 | { |
| 3971 | struct io_mkdir *mkd = &req->mkdir; |
| 3972 | int ret; |
| 3973 | |
| 3974 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3975 | return -EAGAIN; |
| 3976 | |
| 3977 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3978 | |
| 3979 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3980 | if (ret < 0) |
| 3981 | req_set_fail(req); |
| 3982 | io_req_complete(req, ret); |
| 3983 | return 0; |
| 3984 | } |
| 3985 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3986 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3987 | const struct io_uring_sqe *sqe) |
| 3988 | { |
| 3989 | struct io_symlink *sl = &req->symlink; |
| 3990 | const char __user *oldpath, *newpath; |
| 3991 | |
| 3992 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3993 | return -EINVAL; |
| 3994 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 3995 | sqe->splice_fd_in) |
| 3996 | return -EINVAL; |
| 3997 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3998 | return -EBADF; |
| 3999 | |
| 4000 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 4001 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4002 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4003 | |
| 4004 | sl->oldpath = getname(oldpath); |
| 4005 | if (IS_ERR(sl->oldpath)) |
| 4006 | return PTR_ERR(sl->oldpath); |
| 4007 | |
| 4008 | sl->newpath = getname(newpath); |
| 4009 | if (IS_ERR(sl->newpath)) { |
| 4010 | putname(sl->oldpath); |
| 4011 | return PTR_ERR(sl->newpath); |
| 4012 | } |
| 4013 | |
| 4014 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4015 | return 0; |
| 4016 | } |
| 4017 | |
| 4018 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) |
| 4019 | { |
| 4020 | struct io_symlink *sl = &req->symlink; |
| 4021 | int ret; |
| 4022 | |
| 4023 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 4024 | return -EAGAIN; |
| 4025 | |
| 4026 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 4027 | |
| 4028 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4029 | if (ret < 0) |
| 4030 | req_set_fail(req); |
| 4031 | io_req_complete(req, ret); |
| 4032 | return 0; |
| 4033 | } |
| 4034 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4035 | static int io_linkat_prep(struct io_kiocb *req, |
| 4036 | const struct io_uring_sqe *sqe) |
| 4037 | { |
| 4038 | struct io_hardlink *lnk = &req->hardlink; |
| 4039 | const char __user *oldf, *newf; |
| 4040 | |
| 4041 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4042 | return -EINVAL; |
Jens Axboe | a2b1d48 | 2023-10-04 08:43:13 -0600 | [diff] [blame] | 4043 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4044 | return -EINVAL; |
| 4045 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 4046 | return -EBADF; |
| 4047 | |
| 4048 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 4049 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 4050 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4051 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4052 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 4053 | |
Charles Mirabile | 35b5d86 | 2023-11-20 05:55:45 -0500 | [diff] [blame] | 4054 | lnk->oldpath = getname_uflags(oldf, lnk->flags); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4055 | if (IS_ERR(lnk->oldpath)) |
| 4056 | return PTR_ERR(lnk->oldpath); |
| 4057 | |
| 4058 | lnk->newpath = getname(newf); |
| 4059 | if (IS_ERR(lnk->newpath)) { |
| 4060 | putname(lnk->oldpath); |
| 4061 | return PTR_ERR(lnk->newpath); |
| 4062 | } |
| 4063 | |
| 4064 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4065 | return 0; |
| 4066 | } |
| 4067 | |
| 4068 | static int io_linkat(struct io_kiocb *req, int issue_flags) |
| 4069 | { |
| 4070 | struct io_hardlink *lnk = &req->hardlink; |
| 4071 | int ret; |
| 4072 | |
| 4073 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 4074 | return -EAGAIN; |
| 4075 | |
| 4076 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 4077 | lnk->newpath, lnk->flags); |
| 4078 | |
| 4079 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4080 | if (ret < 0) |
| 4081 | req_set_fail(req); |
| 4082 | io_req_complete(req, ret); |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4086 | static int io_shutdown_prep(struct io_kiocb *req, |
| 4087 | const struct io_uring_sqe *sqe) |
| 4088 | { |
| 4089 | #if defined(CONFIG_NET) |
| 4090 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4091 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4092 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 4093 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4094 | return -EINVAL; |
| 4095 | |
| 4096 | req->shutdown.how = READ_ONCE(sqe->len); |
| 4097 | return 0; |
| 4098 | #else |
| 4099 | return -EOPNOTSUPP; |
| 4100 | #endif |
| 4101 | } |
| 4102 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4103 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4104 | { |
| 4105 | #if defined(CONFIG_NET) |
| 4106 | struct socket *sock; |
| 4107 | int ret; |
| 4108 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4109 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4110 | return -EAGAIN; |
| 4111 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4112 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4113 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4114 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4115 | |
| 4116 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 4117 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4118 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4119 | io_req_complete(req, ret); |
| 4120 | return 0; |
| 4121 | #else |
| 4122 | return -EOPNOTSUPP; |
| 4123 | #endif |
| 4124 | } |
| 4125 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4126 | static int __io_splice_prep(struct io_kiocb *req, |
| 4127 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4128 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4129 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4130 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4131 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4132 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4133 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4134 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4135 | sp->len = READ_ONCE(sqe->len); |
| 4136 | sp->flags = READ_ONCE(sqe->splice_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4137 | if (unlikely(sp->flags & ~valid_flags)) |
| 4138 | return -EINVAL; |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4139 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4140 | return 0; |
| 4141 | } |
| 4142 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4143 | static int io_tee_prep(struct io_kiocb *req, |
| 4144 | const struct io_uring_sqe *sqe) |
| 4145 | { |
| 4146 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 4147 | return -EINVAL; |
| 4148 | return __io_splice_prep(req, sqe); |
| 4149 | } |
| 4150 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4151 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4152 | { |
| 4153 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4154 | struct file *out = sp->file_out; |
| 4155 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4156 | struct file *in; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4157 | long ret = 0; |
| 4158 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4159 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4160 | return -EAGAIN; |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4161 | |
| 4162 | in = io_file_get(req->ctx, req, sp->splice_fd_in, |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 4163 | (sp->flags & SPLICE_F_FD_IN_FIXED), issue_flags); |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4164 | if (!in) { |
| 4165 | ret = -EBADF; |
| 4166 | goto done; |
| 4167 | } |
| 4168 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4169 | if (sp->len) |
| 4170 | ret = do_tee(in, out, sp->len, flags); |
| 4171 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4172 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4173 | io_put_file(in); |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4174 | done: |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4175 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4176 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4177 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4178 | return 0; |
| 4179 | } |
| 4180 | |
| 4181 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4182 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4183 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4184 | |
| 4185 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 4186 | sp->off_out = READ_ONCE(sqe->off); |
| 4187 | return __io_splice_prep(req, sqe); |
| 4188 | } |
| 4189 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4190 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4191 | { |
| 4192 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4193 | struct file *out = sp->file_out; |
| 4194 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4195 | loff_t *poff_in, *poff_out; |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4196 | struct file *in; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4197 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4198 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4199 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 4200 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4201 | |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4202 | in = io_file_get(req->ctx, req, sp->splice_fd_in, |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 4203 | (sp->flags & SPLICE_F_FD_IN_FIXED), issue_flags); |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4204 | if (!in) { |
| 4205 | ret = -EBADF; |
| 4206 | goto done; |
| 4207 | } |
| 4208 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4209 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4210 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4211 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4212 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4213 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4214 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4215 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4216 | io_put_file(in); |
Jens Axboe | ae6cba3 | 2022-03-29 10:59:20 -0600 | [diff] [blame] | 4217 | done: |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4218 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4219 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4220 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4221 | return 0; |
| 4222 | } |
| 4223 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4224 | /* |
| 4225 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4226 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4227 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4228 | { |
| 4229 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4230 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4231 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4232 | return -EINVAL; |
| 4233 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4234 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4235 | return 0; |
| 4236 | } |
| 4237 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4238 | 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] | 4239 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4240 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4241 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4242 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4243 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4244 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4245 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4246 | return -EINVAL; |
| 4247 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4248 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4249 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4250 | return -EINVAL; |
| 4251 | |
| 4252 | req->sync.off = READ_ONCE(sqe->off); |
| 4253 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4254 | return 0; |
| 4255 | } |
| 4256 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4257 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4258 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4259 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4260 | int ret; |
| 4261 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4262 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4263 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4264 | return -EAGAIN; |
| 4265 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4266 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4267 | end > 0 ? end : LLONG_MAX, |
| 4268 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4269 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4270 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4271 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4272 | return 0; |
| 4273 | } |
| 4274 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4275 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4276 | const struct io_uring_sqe *sqe) |
| 4277 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4278 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4279 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4280 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4281 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4282 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4283 | |
| 4284 | req->sync.off = READ_ONCE(sqe->off); |
| 4285 | req->sync.len = READ_ONCE(sqe->addr); |
| 4286 | req->sync.mode = READ_ONCE(sqe->len); |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4290 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4291 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4292 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4293 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4294 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4295 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4296 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4297 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4298 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4299 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4300 | req_set_fail(req); |
Jens Axboe | df1ec53 | 2022-03-20 13:08:38 -0600 | [diff] [blame] | 4301 | else |
| 4302 | fsnotify_modify(req->file); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4303 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4304 | return 0; |
| 4305 | } |
| 4306 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4307 | 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] | 4308 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4309 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4310 | int ret; |
| 4311 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4312 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4313 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4314 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4315 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4316 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4317 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4318 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4319 | /* open.how should be already initialised */ |
| 4320 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4321 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4322 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4323 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4324 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4325 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4326 | if (IS_ERR(req->open.filename)) { |
| 4327 | ret = PTR_ERR(req->open.filename); |
| 4328 | req->open.filename = NULL; |
| 4329 | return ret; |
| 4330 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4331 | |
| 4332 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4333 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4334 | return -EINVAL; |
| 4335 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4336 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4337 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4338 | return 0; |
| 4339 | } |
| 4340 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4341 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4342 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4343 | u64 mode = READ_ONCE(sqe->len); |
| 4344 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4345 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4346 | req->open.how = build_open_how(flags, mode); |
| 4347 | return __io_openat_prep(req, sqe); |
| 4348 | } |
| 4349 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4350 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4351 | { |
| 4352 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4353 | size_t len; |
| 4354 | int ret; |
| 4355 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4356 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4357 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4358 | if (len < OPEN_HOW_SIZE_VER0) |
| 4359 | return -EINVAL; |
| 4360 | |
| 4361 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4362 | len); |
| 4363 | if (ret) |
| 4364 | return ret; |
| 4365 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4366 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4369 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4370 | { |
| 4371 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4372 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4373 | bool resolve_nonblock, nonblock_set; |
| 4374 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4375 | int ret; |
| 4376 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4377 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4378 | if (ret) |
| 4379 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4380 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4381 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4382 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4383 | /* |
| 4384 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
Aleksa Sarai | a7cedc2 | 2023-08-12 07:19:05 -0600 | [diff] [blame] | 4385 | * it'll always -EAGAIN. Note that we test for __O_TMPFILE |
| 4386 | * because O_TMPFILE includes O_DIRECTORY, which isn't a flag |
| 4387 | * we need to force async for. |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4388 | */ |
Aleksa Sarai | a7cedc2 | 2023-08-12 07:19:05 -0600 | [diff] [blame] | 4389 | if (req->open.how.flags & (O_TRUNC | O_CREAT | __O_TMPFILE)) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4390 | return -EAGAIN; |
| 4391 | op.lookup_flags |= LOOKUP_CACHED; |
| 4392 | op.open_flag |= O_NONBLOCK; |
| 4393 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4394 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4395 | if (!fixed) { |
| 4396 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4397 | if (ret < 0) |
| 4398 | goto err; |
| 4399 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4400 | |
| 4401 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4402 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4403 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4404 | * We could hang on to this 'fd' on retrying, but seems like |
| 4405 | * marginal gain for something that is now known to be a slower |
| 4406 | * 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] | 4407 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4408 | if (!fixed) |
| 4409 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4410 | |
| 4411 | ret = PTR_ERR(file); |
| 4412 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4413 | if (ret == -EAGAIN && |
| 4414 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4415 | return -EAGAIN; |
| 4416 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4417 | } |
| 4418 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4419 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4420 | file->f_flags &= ~O_NONBLOCK; |
| 4421 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4422 | |
| 4423 | if (!fixed) |
| 4424 | fd_install(ret, file); |
| 4425 | else |
| 4426 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4427 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4428 | err: |
| 4429 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4430 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4431 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4432 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4433 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4434 | return 0; |
| 4435 | } |
| 4436 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4437 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4438 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4439 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4442 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4443 | const struct io_uring_sqe *sqe) |
| 4444 | { |
| 4445 | struct io_provide_buf *p = &req->pbuf; |
| 4446 | u64 tmp; |
| 4447 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4448 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4449 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4450 | return -EINVAL; |
| 4451 | |
| 4452 | tmp = READ_ONCE(sqe->fd); |
| 4453 | if (!tmp || tmp > USHRT_MAX) |
| 4454 | return -EINVAL; |
| 4455 | |
| 4456 | memset(p, 0, sizeof(*p)); |
| 4457 | p->nbufs = tmp; |
| 4458 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4459 | return 0; |
| 4460 | } |
| 4461 | |
| 4462 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4463 | int bgid, unsigned nbufs) |
| 4464 | { |
| 4465 | unsigned i = 0; |
| 4466 | |
| 4467 | /* shouldn't happen */ |
| 4468 | if (!nbufs) |
| 4469 | return 0; |
| 4470 | |
| 4471 | /* the head kbuf is the list itself */ |
| 4472 | while (!list_empty(&buf->list)) { |
| 4473 | struct io_buffer *nxt; |
| 4474 | |
| 4475 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4476 | list_del(&nxt->list); |
| 4477 | kfree(nxt); |
| 4478 | if (++i == nbufs) |
| 4479 | return i; |
Ye Bin | 2d447d3 | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 4480 | cond_resched(); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4481 | } |
| 4482 | i++; |
| 4483 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4484 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4485 | |
| 4486 | return i; |
| 4487 | } |
| 4488 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4489 | 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] | 4490 | { |
| 4491 | struct io_provide_buf *p = &req->pbuf; |
| 4492 | struct io_ring_ctx *ctx = req->ctx; |
| 4493 | struct io_buffer *head; |
| 4494 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4495 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4496 | |
| 4497 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4498 | |
| 4499 | lockdep_assert_held(&ctx->uring_lock); |
| 4500 | |
| 4501 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4502 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4503 | if (head) |
| 4504 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4505 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4506 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4507 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4508 | /* complete before unlock, IOPOLL may need the lock */ |
| 4509 | __io_req_complete(req, issue_flags, ret, 0); |
| 4510 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4511 | return 0; |
| 4512 | } |
| 4513 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4514 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4515 | const struct io_uring_sqe *sqe) |
| 4516 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4517 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4518 | struct io_provide_buf *p = &req->pbuf; |
| 4519 | u64 tmp; |
| 4520 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4521 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4522 | return -EINVAL; |
| 4523 | |
| 4524 | tmp = READ_ONCE(sqe->fd); |
| 4525 | if (!tmp || tmp > USHRT_MAX) |
| 4526 | return -E2BIG; |
| 4527 | p->nbufs = tmp; |
| 4528 | p->addr = READ_ONCE(sqe->addr); |
| 4529 | p->len = READ_ONCE(sqe->len); |
| 4530 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4531 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4532 | &size)) |
| 4533 | return -EOVERFLOW; |
| 4534 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4535 | return -EOVERFLOW; |
| 4536 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4537 | size = (unsigned long)p->len * p->nbufs; |
| 4538 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4539 | return -EFAULT; |
| 4540 | |
| 4541 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4542 | tmp = READ_ONCE(sqe->off); |
| 4543 | if (tmp > USHRT_MAX) |
| 4544 | return -E2BIG; |
| 4545 | p->bid = tmp; |
| 4546 | return 0; |
| 4547 | } |
| 4548 | |
| 4549 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4550 | { |
| 4551 | struct io_buffer *buf; |
| 4552 | u64 addr = pbuf->addr; |
| 4553 | int i, bid = pbuf->bid; |
| 4554 | |
| 4555 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4556 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4557 | if (!buf) |
| 4558 | break; |
| 4559 | |
| 4560 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4561 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4562 | buf->bid = bid; |
| 4563 | addr += pbuf->len; |
| 4564 | bid++; |
| 4565 | if (!*head) { |
| 4566 | INIT_LIST_HEAD(&buf->list); |
| 4567 | *head = buf; |
| 4568 | } else { |
| 4569 | list_add_tail(&buf->list, &(*head)->list); |
| 4570 | } |
Eric Dumazet | c718ea4 | 2022-02-14 20:10:03 -0800 | [diff] [blame] | 4571 | cond_resched(); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | return i ? i : -ENOMEM; |
| 4575 | } |
| 4576 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4577 | 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] | 4578 | { |
| 4579 | struct io_provide_buf *p = &req->pbuf; |
| 4580 | struct io_ring_ctx *ctx = req->ctx; |
| 4581 | struct io_buffer *head, *list; |
| 4582 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4583 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4584 | |
| 4585 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4586 | |
| 4587 | lockdep_assert_held(&ctx->uring_lock); |
| 4588 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4589 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4590 | |
| 4591 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4592 | if (ret >= 0 && !list) { |
Pavel Begunkov | fa30406 | 2022-08-04 15:13:46 +0100 | [diff] [blame] | 4593 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, |
| 4594 | GFP_KERNEL_ACCOUNT); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4595 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4596 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4597 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4598 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4599 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4600 | /* complete before unlock, IOPOLL may need the lock */ |
| 4601 | __io_req_complete(req, issue_flags, ret, 0); |
| 4602 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4603 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4606 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4607 | const struct io_uring_sqe *sqe) |
| 4608 | { |
| 4609 | #if defined(CONFIG_EPOLL) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4610 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4611 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4612 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4613 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4614 | |
| 4615 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4616 | req->epoll.op = READ_ONCE(sqe->len); |
| 4617 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4618 | |
| 4619 | if (ep_op_has_event(req->epoll.op)) { |
| 4620 | struct epoll_event __user *ev; |
| 4621 | |
| 4622 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4623 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4624 | return -EFAULT; |
| 4625 | } |
| 4626 | |
| 4627 | return 0; |
| 4628 | #else |
| 4629 | return -EOPNOTSUPP; |
| 4630 | #endif |
| 4631 | } |
| 4632 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4633 | 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] | 4634 | { |
| 4635 | #if defined(CONFIG_EPOLL) |
| 4636 | struct io_epoll *ie = &req->epoll; |
| 4637 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4638 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4639 | |
| 4640 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4641 | if (force_nonblock && ret == -EAGAIN) |
| 4642 | return -EAGAIN; |
| 4643 | |
| 4644 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4645 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4646 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4647 | return 0; |
| 4648 | #else |
| 4649 | return -EOPNOTSUPP; |
| 4650 | #endif |
| 4651 | } |
| 4652 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4653 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4654 | { |
| 4655 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4656 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4657 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4658 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4659 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4660 | |
| 4661 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4662 | req->madvise.len = READ_ONCE(sqe->len); |
| 4663 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4664 | return 0; |
| 4665 | #else |
| 4666 | return -EOPNOTSUPP; |
| 4667 | #endif |
| 4668 | } |
| 4669 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4670 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4671 | { |
| 4672 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4673 | struct io_madvise *ma = &req->madvise; |
| 4674 | int ret; |
| 4675 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4676 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4677 | return -EAGAIN; |
| 4678 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4679 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4680 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4681 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4682 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4683 | return 0; |
| 4684 | #else |
| 4685 | return -EOPNOTSUPP; |
| 4686 | #endif |
| 4687 | } |
| 4688 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4689 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4690 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4691 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4692 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4693 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4694 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4695 | |
| 4696 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4697 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4698 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4702 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4703 | { |
| 4704 | struct io_fadvise *fa = &req->fadvise; |
| 4705 | int ret; |
| 4706 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4707 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4708 | switch (fa->advice) { |
| 4709 | case POSIX_FADV_NORMAL: |
| 4710 | case POSIX_FADV_RANDOM: |
| 4711 | case POSIX_FADV_SEQUENTIAL: |
| 4712 | break; |
| 4713 | default: |
| 4714 | return -EAGAIN; |
| 4715 | } |
| 4716 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4717 | |
| 4718 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4719 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4720 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4721 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4722 | return 0; |
| 4723 | } |
| 4724 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4725 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4726 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4727 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4728 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4729 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4730 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4731 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4732 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4733 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4734 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4735 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4736 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4737 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4738 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4739 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4740 | return 0; |
| 4741 | } |
| 4742 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4743 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4744 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4745 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4746 | int ret; |
| 4747 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4748 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4749 | return -EAGAIN; |
| 4750 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4751 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4752 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4753 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4754 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4755 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4756 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4757 | return 0; |
| 4758 | } |
| 4759 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4760 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4761 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4762 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4763 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4764 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4765 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4766 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4767 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4768 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4769 | |
| 4770 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4771 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4772 | if (req->close.file_slot && req->close.fd) |
| 4773 | return -EINVAL; |
| 4774 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4775 | return 0; |
| 4776 | } |
| 4777 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4778 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4779 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4780 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4781 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4782 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4783 | struct file *file = NULL; |
| 4784 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4785 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4786 | if (req->close.file_slot) { |
| 4787 | ret = io_close_fixed(req, issue_flags); |
| 4788 | goto err; |
| 4789 | } |
| 4790 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4791 | spin_lock(&files->file_lock); |
| 4792 | fdt = files_fdtable(files); |
| 4793 | if (close->fd >= fdt->max_fds) { |
| 4794 | spin_unlock(&files->file_lock); |
| 4795 | goto err; |
| 4796 | } |
| 4797 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4798 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4799 | spin_unlock(&files->file_lock); |
| 4800 | file = NULL; |
| 4801 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4802 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4803 | |
| 4804 | /* 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] | 4805 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4806 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4807 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4808 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4809 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4810 | ret = __close_fd_get_file(close->fd, &file); |
| 4811 | spin_unlock(&files->file_lock); |
| 4812 | if (ret < 0) { |
| 4813 | if (ret == -ENOENT) |
| 4814 | ret = -EBADF; |
| 4815 | goto err; |
| 4816 | } |
| 4817 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4818 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4819 | ret = filp_close(file, current->files); |
| 4820 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4821 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4822 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4823 | if (file) |
| 4824 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4825 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4826 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4827 | } |
| 4828 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4829 | 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] | 4830 | { |
| 4831 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4832 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4833 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4834 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4835 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4836 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4837 | return -EINVAL; |
| 4838 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4839 | req->sync.off = READ_ONCE(sqe->off); |
| 4840 | req->sync.len = READ_ONCE(sqe->len); |
| 4841 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4842 | return 0; |
| 4843 | } |
| 4844 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4845 | 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] | 4846 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4847 | int ret; |
| 4848 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4849 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4850 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4851 | return -EAGAIN; |
| 4852 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4853 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4854 | req->sync.flags); |
| 4855 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4856 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4857 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4858 | return 0; |
| 4859 | } |
| 4860 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4861 | #if defined(CONFIG_NET) |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4862 | static bool io_net_retry(struct socket *sock, int flags) |
| 4863 | { |
| 4864 | if (!(flags & MSG_WAITALL)) |
| 4865 | return false; |
| 4866 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; |
| 4867 | } |
| 4868 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4869 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4870 | struct io_async_msghdr *kmsg) |
| 4871 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4872 | struct io_async_msghdr *async_msg = req->async_data; |
| 4873 | |
| 4874 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4875 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4876 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4877 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4878 | return -ENOMEM; |
| 4879 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4880 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4881 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4882 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | f9dc33f | 2022-09-29 22:23:18 +0100 | [diff] [blame] | 4883 | if (async_msg->msg.msg_name) |
| 4884 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4885 | /* if were using fast_iov, set it to the new one */ |
Stefan Metzmacher | 2e4c95a | 2022-09-29 09:39:10 +0200 | [diff] [blame] | 4886 | if (!kmsg->free_iov) { |
| 4887 | size_t fast_idx = kmsg->msg.msg_iter.iov - kmsg->fast_iov; |
| 4888 | async_msg->msg.msg_iter.iov = &async_msg->fast_iov[fast_idx]; |
| 4889 | } |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4890 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4891 | return -EAGAIN; |
| 4892 | } |
| 4893 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4894 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4895 | struct io_async_msghdr *iomsg) |
| 4896 | { |
Jens Axboe | 34a7e50 | 2023-06-23 07:38:14 -0600 | [diff] [blame] | 4897 | struct io_sr_msg *sr = &req->sr_msg; |
| 4898 | int ret; |
| 4899 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4900 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4901 | iomsg->free_iov = iomsg->fast_iov; |
Jens Axboe | 34a7e50 | 2023-06-23 07:38:14 -0600 | [diff] [blame] | 4902 | ret = sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4903 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Jens Axboe | 34a7e50 | 2023-06-23 07:38:14 -0600 | [diff] [blame] | 4904 | /* save msg_control as sys_sendmsg() overwrites it */ |
| 4905 | sr->msg_control = iomsg->msg.msg_control; |
| 4906 | return ret; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4907 | } |
| 4908 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4909 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4910 | { |
| 4911 | int ret; |
| 4912 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4913 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4914 | if (!ret) |
| 4915 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4916 | return ret; |
| 4917 | } |
| 4918 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4919 | 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] | 4920 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4921 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4922 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4923 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4924 | return -EINVAL; |
Jens Axboe | 79c10cb | 2022-04-26 19:34:11 -0600 | [diff] [blame] | 4925 | if (unlikely(sqe->addr2 || sqe->file_index)) |
| 4926 | return -EINVAL; |
Jens Axboe | 50fefe5 | 2022-06-30 14:42:05 -0600 | [diff] [blame] | 4927 | if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio)) |
| 4928 | return -EINVAL; |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4929 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4930 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4931 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4932 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4933 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4934 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4935 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4936 | #ifdef CONFIG_COMPAT |
| 4937 | if (req->ctx->compat) |
| 4938 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4939 | #endif |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4940 | sr->done_io = 0; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4941 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4942 | } |
| 4943 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4944 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4945 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4946 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4947 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4948 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4949 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4950 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4951 | int ret; |
| 4952 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4953 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4954 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4955 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4956 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4957 | kmsg = req->async_data; |
| 4958 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4959 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4960 | if (ret) |
| 4961 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4962 | kmsg = &iomsg; |
Jens Axboe | 34a7e50 | 2023-06-23 07:38:14 -0600 | [diff] [blame] | 4963 | } else { |
| 4964 | kmsg->msg.msg_control = sr->msg_control; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4967 | flags = req->sr_msg.msg_flags; |
| 4968 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4969 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4970 | if (flags & MSG_WAITALL) |
| 4971 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4972 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4973 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4974 | |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4975 | if (ret < min_ret) { |
| 4976 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 4977 | return io_setup_async_msg(req, kmsg); |
| 4978 | if (ret == -ERESTARTSYS) |
| 4979 | ret = -EINTR; |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4980 | if (ret > 0 && io_net_retry(sock, flags)) { |
Jens Axboe | 25a543c | 2023-06-23 07:39:42 -0600 | [diff] [blame] | 4981 | kmsg->msg.msg_controllen = 0; |
| 4982 | kmsg->msg.msg_control = NULL; |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4983 | sr->done_io += ret; |
| 4984 | req->flags |= REQ_F_PARTIAL_IO; |
| 4985 | return io_setup_async_msg(req, kmsg); |
| 4986 | } |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4987 | req_set_fail(req); |
| 4988 | } |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4989 | /* fast path, check for non-NULL to avoid function call */ |
| 4990 | if (kmsg->free_iov) |
| 4991 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4992 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 4993 | if (ret >= 0) |
| 4994 | ret += sr->done_io; |
| 4995 | else if (sr->done_io) |
| 4996 | ret = sr->done_io; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4997 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4998 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4999 | } |
| 5000 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5001 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5002 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5003 | struct io_sr_msg *sr = &req->sr_msg; |
| 5004 | struct msghdr msg; |
| 5005 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5006 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5007 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5008 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5009 | int ret; |
| 5010 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5011 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5012 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5013 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5014 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5015 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 5016 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 5017 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5018 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5019 | msg.msg_name = NULL; |
| 5020 | msg.msg_control = NULL; |
| 5021 | msg.msg_controllen = 0; |
| 5022 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5023 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5024 | flags = req->sr_msg.msg_flags; |
| 5025 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5026 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5027 | if (flags & MSG_WAITALL) |
| 5028 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5029 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5030 | msg.msg_flags = flags; |
| 5031 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5032 | if (ret < min_ret) { |
| 5033 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 5034 | return -EAGAIN; |
| 5035 | if (ret == -ERESTARTSYS) |
| 5036 | ret = -EINTR; |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 5037 | if (ret > 0 && io_net_retry(sock, flags)) { |
| 5038 | sr->len -= ret; |
| 5039 | sr->buf += ret; |
| 5040 | sr->done_io += ret; |
| 5041 | req->flags |= REQ_F_PARTIAL_IO; |
| 5042 | return -EAGAIN; |
| 5043 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5044 | req_set_fail(req); |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5045 | } |
Jens Axboe | 3c1a3d0 | 2022-04-20 19:21:36 -0600 | [diff] [blame] | 5046 | if (ret >= 0) |
| 5047 | ret += sr->done_io; |
| 5048 | else if (sr->done_io) |
| 5049 | ret = sr->done_io; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5050 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5051 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5052 | } |
| 5053 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5054 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 5055 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5056 | { |
| 5057 | struct io_sr_msg *sr = &req->sr_msg; |
| 5058 | struct iovec __user *uiov; |
| 5059 | size_t iov_len; |
| 5060 | int ret; |
| 5061 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5062 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 5063 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5064 | if (ret) |
| 5065 | return ret; |
| 5066 | |
| 5067 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 5068 | if (iov_len > 1) |
| 5069 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5070 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5071 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5072 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5073 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5074 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5075 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5076 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5077 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5078 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5079 | if (ret > 0) |
| 5080 | ret = 0; |
| 5081 | } |
| 5082 | |
| 5083 | return ret; |
| 5084 | } |
| 5085 | |
| 5086 | #ifdef CONFIG_COMPAT |
| 5087 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5088 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5089 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5090 | struct io_sr_msg *sr = &req->sr_msg; |
| 5091 | struct compat_iovec __user *uiov; |
| 5092 | compat_uptr_t ptr; |
| 5093 | compat_size_t len; |
| 5094 | int ret; |
| 5095 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 5096 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 5097 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5098 | if (ret) |
| 5099 | return ret; |
| 5100 | |
| 5101 | uiov = compat_ptr(ptr); |
| 5102 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 5103 | compat_ssize_t clen; |
| 5104 | |
| 5105 | if (len > 1) |
| 5106 | return -EINVAL; |
| 5107 | if (!access_ok(uiov, sizeof(*uiov))) |
| 5108 | return -EFAULT; |
| 5109 | if (__get_user(clen, &uiov->iov_len)) |
| 5110 | return -EFAULT; |
| 5111 | if (clen < 0) |
| 5112 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 5113 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5114 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5115 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5116 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5117 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5118 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5119 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5120 | if (ret < 0) |
| 5121 | return ret; |
| 5122 | } |
| 5123 | |
| 5124 | return 0; |
| 5125 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5126 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5127 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5128 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 5129 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5130 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5131 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5132 | |
| 5133 | #ifdef CONFIG_COMPAT |
| 5134 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5135 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5136 | #endif |
| 5137 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5138 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5141 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5142 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5143 | { |
| 5144 | struct io_sr_msg *sr = &req->sr_msg; |
| 5145 | struct io_buffer *kbuf; |
| 5146 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5147 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 5148 | if (IS_ERR(kbuf)) |
| 5149 | return kbuf; |
| 5150 | |
| 5151 | sr->kbuf = kbuf; |
| 5152 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5153 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5154 | } |
| 5155 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5156 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 5157 | { |
| 5158 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 5159 | } |
| 5160 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5161 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5162 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5163 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 5164 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5165 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 5166 | if (!ret) |
| 5167 | req->flags |= REQ_F_NEED_CLEANUP; |
| 5168 | return ret; |
| 5169 | } |
| 5170 | |
| 5171 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5172 | { |
| 5173 | struct io_sr_msg *sr = &req->sr_msg; |
| 5174 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 5175 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5176 | return -EINVAL; |
Jens Axboe | 37811e4 | 2022-04-26 19:34:57 -0600 | [diff] [blame] | 5177 | if (unlikely(sqe->addr2 || sqe->file_index)) |
| 5178 | return -EINVAL; |
Jens Axboe | 50fefe5 | 2022-06-30 14:42:05 -0600 | [diff] [blame] | 5179 | if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio)) |
| 5180 | return -EINVAL; |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 5181 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 5182 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 5183 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5184 | sr->bgid = READ_ONCE(sqe->buf_group); |
David Lamparter | 7e8cd20 | 2023-03-06 13:23:06 -0700 | [diff] [blame] | 5185 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5186 | if (sr->msg_flags & MSG_DONTWAIT) |
| 5187 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5188 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 5189 | #ifdef CONFIG_COMPAT |
| 5190 | if (req->ctx->compat) |
| 5191 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 5192 | #endif |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5193 | sr->done_io = 0; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5194 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5197 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5198 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5199 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5200 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5201 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5202 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5203 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5204 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5205 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5206 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5207 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5208 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5209 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5210 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5211 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5212 | kmsg = req->async_data; |
| 5213 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5214 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 5215 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 5216 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5217 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5218 | } |
| 5219 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5220 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5221 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5222 | if (IS_ERR(kbuf)) |
| 5223 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5224 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5225 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 5226 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5227 | 1, req->sr_msg.len); |
| 5228 | } |
| 5229 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5230 | flags = req->sr_msg.msg_flags; |
| 5231 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5232 | flags |= MSG_DONTWAIT; |
Jens Axboe | 96987c3 | 2023-06-23 07:41:10 -0600 | [diff] [blame] | 5233 | if (flags & MSG_WAITALL && !kmsg->msg.msg_controllen) |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5234 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 5235 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5236 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 5237 | kmsg->uaddr, flags); |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5238 | if (ret < min_ret) { |
| 5239 | if (ret == -EAGAIN && force_nonblock) |
| 5240 | return io_setup_async_msg(req, kmsg); |
| 5241 | if (ret == -ERESTARTSYS) |
| 5242 | ret = -EINTR; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5243 | if (ret > 0 && io_net_retry(sock, flags)) { |
| 5244 | sr->done_io += ret; |
Jens Axboe | 390b881 | 2022-03-23 09:30:05 -0600 | [diff] [blame] | 5245 | req->flags |= REQ_F_PARTIAL_IO; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5246 | return io_setup_async_msg(req, kmsg); |
| 5247 | } |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5248 | req_set_fail(req); |
| 5249 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
| 5250 | req_set_fail(req); |
| 5251 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5252 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5253 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5254 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5255 | /* fast path, check for non-NULL to avoid function call */ |
| 5256 | if (kmsg->free_iov) |
| 5257 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5258 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5259 | if (ret >= 0) |
| 5260 | ret += sr->done_io; |
| 5261 | else if (sr->done_io) |
| 5262 | ret = sr->done_io; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5263 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5264 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5265 | } |
| 5266 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5267 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5268 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5269 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5270 | struct io_sr_msg *sr = &req->sr_msg; |
| 5271 | struct msghdr msg; |
| 5272 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5273 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5274 | struct iovec iov; |
| 5275 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5276 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5277 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5278 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5279 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5280 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5281 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5282 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5283 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5284 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5285 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5286 | if (IS_ERR(kbuf)) |
| 5287 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5288 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5291 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5292 | if (unlikely(ret)) |
| 5293 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5294 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5295 | msg.msg_name = NULL; |
| 5296 | msg.msg_control = NULL; |
| 5297 | msg.msg_controllen = 0; |
| 5298 | msg.msg_namelen = 0; |
| 5299 | msg.msg_iocb = NULL; |
| 5300 | msg.msg_flags = 0; |
| 5301 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5302 | flags = req->sr_msg.msg_flags; |
| 5303 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5304 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5305 | if (flags & MSG_WAITALL) |
| 5306 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5307 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5308 | ret = sock_recvmsg(sock, &msg, flags); |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5309 | if (ret < min_ret) { |
| 5310 | if (ret == -EAGAIN && force_nonblock) |
| 5311 | return -EAGAIN; |
| 5312 | if (ret == -ERESTARTSYS) |
| 5313 | ret = -EINTR; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5314 | if (ret > 0 && io_net_retry(sock, flags)) { |
| 5315 | sr->len -= ret; |
| 5316 | sr->buf += ret; |
| 5317 | sr->done_io += ret; |
Jens Axboe | 390b881 | 2022-03-23 09:30:05 -0600 | [diff] [blame] | 5318 | req->flags |= REQ_F_PARTIAL_IO; |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5319 | return -EAGAIN; |
| 5320 | } |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5321 | req_set_fail(req); |
| 5322 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
Alviro Iskandar Setiawan | e944f1e | 2022-02-07 21:05:33 +0700 | [diff] [blame] | 5323 | out_free: |
Pavel Begunkov | cdc68e7 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5324 | req_set_fail(req); |
| 5325 | } |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5326 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5327 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | 9b7b0f2 | 2023-01-21 10:21:22 -0700 | [diff] [blame] | 5328 | if (ret >= 0) |
| 5329 | ret += sr->done_io; |
| 5330 | else if (sr->done_io) |
| 5331 | ret = sr->done_io; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5332 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5333 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5334 | } |
| 5335 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5336 | 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] | 5337 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5338 | struct io_accept *accept = &req->accept; |
| 5339 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5340 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5341 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5342 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5343 | return -EINVAL; |
| 5344 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5345 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5346 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5347 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5348 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5349 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5350 | accept->file_slot = READ_ONCE(sqe->file_index); |
Jens Axboe | 1323976 | 2022-03-14 17:26:19 -0600 | [diff] [blame] | 5351 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5352 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5353 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5354 | return -EINVAL; |
| 5355 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5356 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5357 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5358 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5359 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5360 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5361 | { |
| 5362 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5363 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5364 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5365 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5366 | struct file *file; |
| 5367 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5368 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5369 | if (!fixed) { |
| 5370 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5371 | if (unlikely(fd < 0)) |
| 5372 | return fd; |
| 5373 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5374 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5375 | accept->flags); |
| 5376 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5377 | if (!fixed) |
| 5378 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5379 | ret = PTR_ERR(file); |
Dylan Yudaken | 30b9068 | 2023-01-21 09:13:12 -0700 | [diff] [blame] | 5380 | /* safe to retry */ |
| 5381 | req->flags |= REQ_F_PARTIAL_IO; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5382 | if (ret == -EAGAIN && force_nonblock) |
| 5383 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5384 | if (ret == -ERESTARTSYS) |
| 5385 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5386 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5387 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5388 | fd_install(fd, file); |
| 5389 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5390 | } else { |
| 5391 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5392 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5393 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5394 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5395 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5396 | } |
| 5397 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5398 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5399 | { |
| 5400 | struct io_async_connect *io = req->async_data; |
| 5401 | struct io_connect *conn = &req->connect; |
| 5402 | |
| 5403 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5404 | } |
| 5405 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5406 | 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] | 5407 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5408 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5409 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5410 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5411 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5412 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5413 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5414 | return -EINVAL; |
| 5415 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5416 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5417 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5418 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5421 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5422 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5423 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5424 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5425 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5426 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5427 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5428 | if (req->async_data) { |
| 5429 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5430 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5431 | ret = move_addr_to_kernel(req->connect.addr, |
| 5432 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5433 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5434 | if (ret) |
| 5435 | goto out; |
| 5436 | io = &__io; |
| 5437 | } |
| 5438 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5439 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5440 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5441 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5442 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5443 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5444 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5445 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5446 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5447 | ret = -ENOMEM; |
| 5448 | goto out; |
| 5449 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5450 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5451 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5452 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5453 | if (ret == -ERESTARTSYS) |
| 5454 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5455 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5456 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5457 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5458 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5459 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5460 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5461 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5462 | #define IO_NETOP_FN(op) \ |
| 5463 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5464 | { \ |
| 5465 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5466 | } |
| 5467 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5468 | #define IO_NETOP_PREP(op) \ |
| 5469 | IO_NETOP_FN(op) \ |
| 5470 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5471 | { \ |
| 5472 | return -EOPNOTSUPP; \ |
| 5473 | } \ |
| 5474 | |
| 5475 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5476 | IO_NETOP_PREP(op) \ |
| 5477 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5478 | { \ |
| 5479 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5480 | } |
| 5481 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5482 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5483 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5484 | IO_NETOP_PREP_ASYNC(connect); |
| 5485 | IO_NETOP_PREP(accept); |
| 5486 | IO_NETOP_FN(send); |
| 5487 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5488 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5489 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5490 | struct io_poll_table { |
| 5491 | struct poll_table_struct pt; |
| 5492 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5493 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5494 | int error; |
| 5495 | }; |
| 5496 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5497 | #define IO_POLL_CANCEL_FLAG BIT(31) |
Pavel Begunkov | 4b702b7 | 2022-12-02 14:27:14 +0000 | [diff] [blame] | 5498 | #define IO_POLL_RETRY_FLAG BIT(30) |
| 5499 | #define IO_POLL_REF_MASK GENMASK(29, 0) |
| 5500 | |
| 5501 | /* |
| 5502 | * We usually have 1-2 refs taken, 128 is more than enough and we want to |
| 5503 | * maximise the margin between this amount and the moment when it overflows. |
| 5504 | */ |
| 5505 | #define IO_POLL_REF_BIAS 128 |
| 5506 | |
| 5507 | static bool io_poll_get_ownership_slowpath(struct io_kiocb *req) |
| 5508 | { |
| 5509 | int v; |
| 5510 | |
| 5511 | /* |
| 5512 | * poll_refs are already elevated and we don't have much hope for |
| 5513 | * grabbing the ownership. Instead of incrementing set a retry flag |
| 5514 | * to notify the loop that there might have been some change. |
| 5515 | */ |
| 5516 | v = atomic_fetch_or(IO_POLL_RETRY_FLAG, &req->poll_refs); |
| 5517 | if (v & IO_POLL_REF_MASK) |
| 5518 | return false; |
| 5519 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); |
| 5520 | } |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5521 | |
| 5522 | /* |
| 5523 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can |
| 5524 | * bump it and acquire ownership. It's disallowed to modify requests while not |
| 5525 | * owning it, that prevents from races for enqueueing task_work's and b/w |
| 5526 | * arming poll and wakeups. |
| 5527 | */ |
| 5528 | static inline bool io_poll_get_ownership(struct io_kiocb *req) |
| 5529 | { |
Pavel Begunkov | 4b702b7 | 2022-12-02 14:27:14 +0000 | [diff] [blame] | 5530 | if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS)) |
| 5531 | return io_poll_get_ownership_slowpath(req); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5532 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); |
| 5533 | } |
| 5534 | |
| 5535 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
| 5536 | { |
| 5537 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
| 5538 | } |
| 5539 | |
Pavel Begunkov | a85d7ac | 2022-08-29 14:30:15 +0100 | [diff] [blame] | 5540 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
| 5541 | { |
| 5542 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
| 5543 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5544 | return req->async_data; |
| 5545 | return req->apoll->double_poll; |
| 5546 | } |
| 5547 | |
| 5548 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5549 | { |
| 5550 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5551 | return &req->poll; |
| 5552 | return &req->apoll->poll; |
| 5553 | } |
| 5554 | |
| 5555 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5556 | { |
| 5557 | struct io_ring_ctx *ctx = req->ctx; |
| 5558 | struct hlist_head *list; |
| 5559 | |
| 5560 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5561 | hlist_add_head(&req->hash_node, list); |
| 5562 | } |
| 5563 | |
| 5564 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5565 | wait_queue_func_t wake_func) |
| 5566 | { |
| 5567 | poll->head = NULL; |
Pavel Begunkov | a85d7ac | 2022-08-29 14:30:15 +0100 | [diff] [blame] | 5568 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5569 | /* mask in events that we always want/need */ |
| 5570 | poll->events = events | IO_POLL_UNMASK; |
| 5571 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5572 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5573 | } |
| 5574 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5575 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5576 | { |
Pavel Begunkov | e9d7ca0 | 2022-08-29 14:30:24 +0100 | [diff] [blame] | 5577 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5578 | |
Pavel Begunkov | e9d7ca0 | 2022-08-29 14:30:24 +0100 | [diff] [blame] | 5579 | if (head) { |
| 5580 | spin_lock_irq(&head->lock); |
| 5581 | list_del_init(&poll->wait.entry); |
| 5582 | poll->head = NULL; |
| 5583 | spin_unlock_irq(&head->lock); |
| 5584 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5585 | } |
| 5586 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5587 | static void io_poll_remove_entries(struct io_kiocb *req) |
| 5588 | { |
| 5589 | struct io_poll_iocb *poll = io_poll_get_single(req); |
| 5590 | struct io_poll_iocb *poll_double = io_poll_get_double(req); |
| 5591 | |
Pavel Begunkov | e9d7ca0 | 2022-08-29 14:30:24 +0100 | [diff] [blame] | 5592 | /* |
| 5593 | * While we hold the waitqueue lock and the waitqueue is nonempty, |
| 5594 | * wake_up_pollfree() will wait for us. However, taking the waitqueue |
| 5595 | * lock in the first place can race with the waitqueue being freed. |
| 5596 | * |
| 5597 | * We solve this as eventpoll does: by taking advantage of the fact that |
| 5598 | * all users of wake_up_pollfree() will RCU-delay the actual free. If |
| 5599 | * we enter rcu_read_lock() and see that the pointer to the queue is |
| 5600 | * non-NULL, we can then lock it without the memory being freed out from |
| 5601 | * under us. |
| 5602 | * |
| 5603 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in |
| 5604 | * case the caller deletes the entry from the queue, leaving it empty. |
| 5605 | * In that case, only RCU prevents the queue memory from being freed. |
| 5606 | */ |
| 5607 | rcu_read_lock(); |
| 5608 | io_poll_remove_entry(poll); |
| 5609 | if (poll_double) |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5610 | io_poll_remove_entry(poll_double); |
Pavel Begunkov | e9d7ca0 | 2022-08-29 14:30:24 +0100 | [diff] [blame] | 5611 | rcu_read_unlock(); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | /* |
| 5615 | * All poll tw should go through this. Checks for poll events, manages |
| 5616 | * references, does rewait, etc. |
| 5617 | * |
| 5618 | * Returns a negative error on failure. >0 when no action require, which is |
| 5619 | * either spurious wakeup or multishot CQE is served. 0 when it's done with |
| 5620 | * the request, then the mask is stored in req->result. |
| 5621 | */ |
| 5622 | static int io_poll_check_events(struct io_kiocb *req) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5623 | { |
| 5624 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5625 | struct io_poll_iocb *poll = io_poll_get_single(req); |
| 5626 | int v; |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5627 | |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 5628 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5629 | if (unlikely(req->task->flags & PF_EXITING)) |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5630 | io_poll_mark_cancelled(req); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5631 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5632 | do { |
| 5633 | v = atomic_read(&req->poll_refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5634 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5635 | /* tw handler should be the owner, and so have some references */ |
| 5636 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) |
| 5637 | return 0; |
| 5638 | if (v & IO_POLL_CANCEL_FLAG) |
| 5639 | return -ECANCELED; |
Pavel Begunkov | cd1981a | 2022-12-02 14:27:12 +0000 | [diff] [blame] | 5640 | /* |
| 5641 | * cqe.res contains only events of the first wake up |
| 5642 | * and all others are be lost. Redo vfs_poll() to get |
| 5643 | * up to date state. |
| 5644 | */ |
| 5645 | if ((v & IO_POLL_REF_MASK) != 1) |
| 5646 | req->result = 0; |
Pavel Begunkov | 4b702b7 | 2022-12-02 14:27:14 +0000 | [diff] [blame] | 5647 | if (v & IO_POLL_RETRY_FLAG) { |
| 5648 | req->result = 0; |
| 5649 | /* |
| 5650 | * We won't find new events that came in between |
| 5651 | * vfs_poll and the ref put unless we clear the |
| 5652 | * flag in advance. |
| 5653 | */ |
| 5654 | atomic_andnot(IO_POLL_RETRY_FLAG, &req->poll_refs); |
| 5655 | v &= ~IO_POLL_RETRY_FLAG; |
| 5656 | } |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5657 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5658 | if (!req->result) { |
| 5659 | struct poll_table_struct pt = { ._key = poll->events }; |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5660 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5661 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5662 | } |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5663 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5664 | /* multishot, just fill an CQE and proceed */ |
| 5665 | if (req->result && !(poll->events & EPOLLONESHOT)) { |
| 5666 | __poll_t mask = mangle_poll(req->result & poll->events); |
| 5667 | bool filled; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5668 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5669 | spin_lock(&ctx->completion_lock); |
| 5670 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, |
| 5671 | IORING_CQE_F_MORE); |
| 5672 | io_commit_cqring(ctx); |
| 5673 | spin_unlock(&ctx->completion_lock); |
| 5674 | if (unlikely(!filled)) |
| 5675 | return -ECANCELED; |
| 5676 | io_cqring_ev_posted(ctx); |
| 5677 | } else if (req->result) { |
| 5678 | return 0; |
| 5679 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5680 | |
Pavel Begunkov | 62321dc | 2022-12-02 14:27:11 +0000 | [diff] [blame] | 5681 | /* force the next iteration to vfs_poll() */ |
| 5682 | req->result = 0; |
| 5683 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5684 | /* |
| 5685 | * Release all references, retry if someone tried to restart |
| 5686 | * task_work while we were executing it. |
| 5687 | */ |
Lin Ma | df4b177 | 2022-12-02 14:27:15 +0000 | [diff] [blame] | 5688 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs) & |
| 5689 | IO_POLL_REF_MASK); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5690 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5691 | return 1; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5692 | } |
| 5693 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5694 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5695 | { |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5696 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5697 | int ret; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5698 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5699 | ret = io_poll_check_events(req); |
| 5700 | if (ret > 0) |
| 5701 | return; |
| 5702 | |
| 5703 | if (!ret) { |
| 5704 | req->result = mangle_poll(req->result & req->poll.events); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5705 | } else { |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5706 | req->result = ret; |
| 5707 | req_set_fail(req); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5708 | } |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5709 | |
| 5710 | io_poll_remove_entries(req); |
| 5711 | spin_lock(&ctx->completion_lock); |
| 5712 | hash_del(&req->hash_node); |
| 5713 | spin_unlock(&ctx->completion_lock); |
| 5714 | io_req_complete_post(req, req->result, 0); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5715 | } |
| 5716 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5717 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
| 5718 | { |
| 5719 | struct io_ring_ctx *ctx = req->ctx; |
| 5720 | int ret; |
| 5721 | |
| 5722 | ret = io_poll_check_events(req); |
| 5723 | if (ret > 0) |
| 5724 | return; |
| 5725 | |
Pavel Begunkov | 1a0aba2 | 2023-09-12 15:01:59 +0100 | [diff] [blame] | 5726 | io_tw_lock(req->ctx, locked); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5727 | io_poll_remove_entries(req); |
| 5728 | spin_lock(&ctx->completion_lock); |
| 5729 | hash_del(&req->hash_node); |
| 5730 | spin_unlock(&ctx->completion_lock); |
| 5731 | |
| 5732 | if (!ret) |
| 5733 | io_req_task_submit(req, locked); |
| 5734 | else |
| 5735 | io_req_complete_failed(req, ret); |
| 5736 | } |
| 5737 | |
| 5738 | static void __io_poll_execute(struct io_kiocb *req, int mask) |
| 5739 | { |
| 5740 | req->result = mask; |
| 5741 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5742 | req->io_task_work.func = io_poll_task_func; |
| 5743 | else |
| 5744 | req->io_task_work.func = io_apoll_task_func; |
| 5745 | |
| 5746 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5747 | io_req_task_work_add(req); |
| 5748 | } |
| 5749 | |
| 5750 | static inline void io_poll_execute(struct io_kiocb *req, int res) |
| 5751 | { |
| 5752 | if (io_poll_get_ownership(req)) |
| 5753 | __io_poll_execute(req, res); |
| 5754 | } |
| 5755 | |
| 5756 | static void io_poll_cancel_req(struct io_kiocb *req) |
| 5757 | { |
| 5758 | io_poll_mark_cancelled(req); |
| 5759 | /* kick tw, which should complete the request */ |
| 5760 | io_poll_execute(req, 0); |
| 5761 | } |
| 5762 | |
| 5763 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5764 | void *key) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5765 | { |
| 5766 | struct io_kiocb *req = wait->private; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5767 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
| 5768 | wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5769 | __poll_t mask = key_to_poll(key); |
| 5770 | |
Pavel Begunkov | e9d7ca0 | 2022-08-29 14:30:24 +0100 | [diff] [blame] | 5771 | if (unlikely(mask & POLLFREE)) { |
| 5772 | io_poll_mark_cancelled(req); |
| 5773 | /* we have to kick tw in case it's not already */ |
| 5774 | io_poll_execute(req, 0); |
| 5775 | |
| 5776 | /* |
| 5777 | * If the waitqueue is being freed early but someone is already |
| 5778 | * holds ownership over it, we have to tear down the request as |
| 5779 | * best we can. That means immediately removing the request from |
| 5780 | * its waitqueue and preventing all further accesses to the |
| 5781 | * waitqueue via the request. |
| 5782 | */ |
| 5783 | list_del_init(&poll->wait.entry); |
| 5784 | |
| 5785 | /* |
| 5786 | * Careful: this *must* be the last step, since as soon |
| 5787 | * as req->head is NULL'ed out, the request can be |
| 5788 | * completed and freed, since aio_poll_complete_work() |
| 5789 | * will no longer need to take the waitqueue lock. |
| 5790 | */ |
| 5791 | smp_store_release(&poll->head, NULL); |
| 5792 | return 1; |
| 5793 | } |
| 5794 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5795 | /* for instances that support it check for an event match first */ |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5796 | if (mask && !(mask & poll->events)) |
| 5797 | return 0; |
| 5798 | |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 5799 | if (io_poll_get_ownership(req)) { |
| 5800 | /* |
| 5801 | * If we trigger a multishot poll off our own wakeup path, |
| 5802 | * disable multishot as there is a circular dependency between |
| 5803 | * CQ posting and triggering the event. |
| 5804 | */ |
| 5805 | if (mask & EPOLL_URING_WAKE) |
| 5806 | poll->events |= EPOLLONESHOT; |
| 5807 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5808 | __io_poll_execute(req, mask); |
Jens Axboe | ccf06b5 | 2022-12-23 07:04:49 -0700 | [diff] [blame] | 5809 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5810 | return 1; |
| 5811 | } |
| 5812 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5813 | 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] | 5814 | struct wait_queue_head *head, |
| 5815 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5816 | { |
| 5817 | struct io_kiocb *req = pt->req; |
| 5818 | |
| 5819 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5820 | * The file being polled uses multiple waitqueues for poll handling |
| 5821 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5822 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5823 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5824 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5825 | struct io_poll_iocb *first = poll; |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5826 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5827 | /* double add on the same waitqueue head, ignore */ |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5828 | if (first->head == head) |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5829 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5830 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5831 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5832 | if ((*poll_ptr)->head == head) |
| 5833 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5834 | pt->error = -EINVAL; |
| 5835 | return; |
| 5836 | } |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5837 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5838 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5839 | if (!poll) { |
| 5840 | pt->error = -ENOMEM; |
| 5841 | return; |
| 5842 | } |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5843 | io_init_poll_iocb(poll, first->events, first->wait.func); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5844 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5845 | } |
| 5846 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5847 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5848 | poll->head = head; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5849 | poll->wait.private = req; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5850 | |
| 5851 | if (poll->events & EPOLLEXCLUSIVE) |
| 5852 | add_wait_queue_exclusive(head, &poll->wait); |
| 5853 | else |
| 5854 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5855 | } |
| 5856 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5857 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5858 | struct poll_table_struct *p) |
| 5859 | { |
| 5860 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5861 | |
| 5862 | __io_queue_proc(&pt->req->poll, pt, head, |
| 5863 | (struct io_poll_iocb **) &pt->req->async_data); |
| 5864 | } |
| 5865 | |
| 5866 | static int __io_arm_poll_handler(struct io_kiocb *req, |
| 5867 | struct io_poll_iocb *poll, |
| 5868 | struct io_poll_table *ipt, __poll_t mask) |
| 5869 | { |
| 5870 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5871 | |
| 5872 | INIT_HLIST_NODE(&req->hash_node); |
| 5873 | io_init_poll_iocb(poll, mask, io_poll_wake); |
| 5874 | poll->file = req->file; |
| 5875 | poll->wait.private = req; |
| 5876 | |
| 5877 | ipt->pt._key = mask; |
| 5878 | ipt->req = req; |
| 5879 | ipt->error = 0; |
| 5880 | ipt->nr_entries = 0; |
| 5881 | |
| 5882 | /* |
| 5883 | * Take the ownership to delay any tw execution up until we're done |
| 5884 | * with poll arming. see io_poll_get_ownership(). |
| 5885 | */ |
| 5886 | atomic_set(&req->poll_refs, 1); |
| 5887 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5888 | |
| 5889 | if (mask && (poll->events & EPOLLONESHOT)) { |
| 5890 | io_poll_remove_entries(req); |
| 5891 | /* no one else has access to the req, forget about the ref */ |
| 5892 | return mask; |
| 5893 | } |
| 5894 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { |
| 5895 | io_poll_remove_entries(req); |
| 5896 | if (!ipt->error) |
| 5897 | ipt->error = -EINVAL; |
| 5898 | return 0; |
| 5899 | } |
| 5900 | |
| 5901 | spin_lock(&ctx->completion_lock); |
| 5902 | io_poll_req_insert(req); |
| 5903 | spin_unlock(&ctx->completion_lock); |
| 5904 | |
| 5905 | if (mask) { |
| 5906 | /* can't multishot if failed, just queue the event we've got */ |
Pavel Begunkov | 182dc3a | 2022-08-29 14:30:23 +0100 | [diff] [blame] | 5907 | if (unlikely(ipt->error || !ipt->nr_entries)) { |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5908 | poll->events |= EPOLLONESHOT; |
Pavel Begunkov | 182dc3a | 2022-08-29 14:30:23 +0100 | [diff] [blame] | 5909 | ipt->error = 0; |
| 5910 | } |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5911 | __io_poll_execute(req, mask); |
| 5912 | return 0; |
| 5913 | } |
| 5914 | |
| 5915 | /* |
Pavel Begunkov | 1d58849 | 2022-12-02 14:27:13 +0000 | [diff] [blame] | 5916 | * Try to release ownership. If we see a change of state, e.g. |
| 5917 | * poll was waken up, queue up a tw, it'll deal with it. |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5918 | */ |
Pavel Begunkov | 1d58849 | 2022-12-02 14:27:13 +0000 | [diff] [blame] | 5919 | if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1) |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5920 | __io_poll_execute(req, 0); |
| 5921 | return 0; |
| 5922 | } |
| 5923 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5924 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5925 | struct poll_table_struct *p) |
| 5926 | { |
| 5927 | 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] | 5928 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5929 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5930 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5931 | } |
| 5932 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5933 | enum { |
| 5934 | IO_APOLL_OK, |
| 5935 | IO_APOLL_ABORTED, |
| 5936 | IO_APOLL_READY |
| 5937 | }; |
| 5938 | |
Jens Axboe | 345fb36 | 2023-03-06 13:28:57 -0700 | [diff] [blame] | 5939 | /* |
| 5940 | * We can't reliably detect loops in repeated poll triggers and issue |
| 5941 | * subsequently failing. But rather than fail these immediately, allow a |
| 5942 | * certain amount of retries before we give up. Given that this condition |
| 5943 | * should _rarely_ trigger even once, we should be fine with a larger value. |
| 5944 | */ |
| 5945 | #define APOLL_MAX_RETRY 128 |
| 5946 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5947 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5948 | { |
| 5949 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5950 | struct io_ring_ctx *ctx = req->ctx; |
| 5951 | struct async_poll *apoll; |
| 5952 | struct io_poll_table ipt; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5953 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
| 5954 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5955 | |
| 5956 | if (!req->file || !file_can_poll(req->file)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5957 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5958 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5959 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5960 | |
| 5961 | if (def->pollin) { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5962 | mask |= POLLIN | POLLRDNORM; |
| 5963 | |
| 5964 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5965 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5966 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5967 | mask &= ~POLLIN; |
| 5968 | } else { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5969 | mask |= POLLOUT | POLLWRNORM; |
| 5970 | } |
| 5971 | |
Pavel Begunkov | 124fb13 | 2023-01-22 10:24:20 -0700 | [diff] [blame] | 5972 | if (req->flags & REQ_F_POLLED) { |
Jens Axboe | a79b13f | 2023-01-21 10:39:22 -0700 | [diff] [blame] | 5973 | apoll = req->apoll; |
Pavel Begunkov | 124fb13 | 2023-01-22 10:24:20 -0700 | [diff] [blame] | 5974 | kfree(apoll->double_poll); |
Jens Axboe | 345fb36 | 2023-03-06 13:28:57 -0700 | [diff] [blame] | 5975 | if (unlikely(!--apoll->poll.retries)) { |
| 5976 | apoll->double_poll = NULL; |
| 5977 | return IO_APOLL_ABORTED; |
| 5978 | } |
Pavel Begunkov | 124fb13 | 2023-01-22 10:24:20 -0700 | [diff] [blame] | 5979 | } else { |
Jens Axboe | a79b13f | 2023-01-21 10:39:22 -0700 | [diff] [blame] | 5980 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
Fedor Pchelkin | f4ba554 | 2023-03-16 21:56:16 +0300 | [diff] [blame] | 5981 | if (unlikely(!apoll)) |
| 5982 | return IO_APOLL_ABORTED; |
Jens Axboe | 345fb36 | 2023-03-06 13:28:57 -0700 | [diff] [blame] | 5983 | apoll->poll.retries = APOLL_MAX_RETRY; |
Pavel Begunkov | 124fb13 | 2023-01-22 10:24:20 -0700 | [diff] [blame] | 5984 | } |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5985 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5986 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5987 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5988 | ipt.pt._qproc = io_async_queue_proc; |
| 5989 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 5990 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5991 | if (ret || ipt.error) |
| 5992 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5993 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5994 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5995 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5996 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5997 | } |
| 5998 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5999 | /* |
| 6000 | * Returns true if we found and killed one or more poll requests |
| 6001 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 6002 | 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] | 6003 | bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6004 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6005 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6006 | struct io_kiocb *req; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6007 | bool found = false; |
| 6008 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6009 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6010 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6011 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 6012 | struct hlist_head *list; |
| 6013 | |
| 6014 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 6015 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6016 | if (io_match_task_safe(req, tsk, cancel_all)) { |
Jens Axboe | 7524ec5 | 2022-08-29 14:30:20 +0100 | [diff] [blame] | 6017 | hlist_del_init(&req->hash_node); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6018 | io_poll_cancel_req(req); |
| 6019 | found = true; |
| 6020 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 6021 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6022 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6023 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6024 | return found; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6025 | } |
| 6026 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 6027 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 6028 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 6029 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6030 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6031 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6032 | struct io_kiocb *req; |
| 6033 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6034 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 6035 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6036 | if (sqe_addr != req->user_data) |
| 6037 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 6038 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 6039 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 6040 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6041 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 6042 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6043 | } |
| 6044 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6045 | static bool io_poll_disarm(struct io_kiocb *req) |
| 6046 | __must_hold(&ctx->completion_lock) |
| 6047 | { |
| 6048 | if (!io_poll_get_ownership(req)) |
| 6049 | return false; |
| 6050 | io_poll_remove_entries(req); |
| 6051 | hash_del(&req->hash_node); |
| 6052 | return true; |
| 6053 | } |
| 6054 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 6055 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 6056 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 6057 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 6058 | { |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6059 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 6060 | |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 6061 | if (!req) |
| 6062 | return -ENOENT; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6063 | io_poll_cancel_req(req); |
| 6064 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6065 | } |
| 6066 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 6067 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 6068 | unsigned int flags) |
| 6069 | { |
| 6070 | u32 events; |
| 6071 | |
| 6072 | events = READ_ONCE(sqe->poll32_events); |
| 6073 | #ifdef __BIG_ENDIAN |
| 6074 | events = swahw32(events); |
| 6075 | #endif |
| 6076 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 6077 | events |= EPOLLONESHOT; |
| 6078 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 6079 | } |
| 6080 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6081 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6082 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6083 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6084 | struct io_poll_update *upd = &req->poll_update; |
| 6085 | u32 flags; |
| 6086 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6087 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6088 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6089 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6090 | return -EINVAL; |
| 6091 | flags = READ_ONCE(sqe->len); |
| 6092 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 6093 | IORING_POLL_ADD_MULTI)) |
| 6094 | return -EINVAL; |
| 6095 | /* meaningless without update */ |
| 6096 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6097 | return -EINVAL; |
| 6098 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6099 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 6100 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 6101 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6102 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6103 | upd->new_user_data = READ_ONCE(sqe->off); |
| 6104 | if (!upd->update_user_data && upd->new_user_data) |
| 6105 | return -EINVAL; |
| 6106 | if (upd->update_events) |
| 6107 | upd->events = io_poll_parse_events(sqe, flags); |
| 6108 | else if (sqe->poll32_events) |
| 6109 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6110 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6111 | return 0; |
| 6112 | } |
| 6113 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6114 | 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] | 6115 | { |
| 6116 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6117 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6118 | |
| 6119 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6120 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6121 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 6122 | return -EINVAL; |
| 6123 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6124 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6125 | return -EINVAL; |
| 6126 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6127 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6128 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6129 | return 0; |
| 6130 | } |
| 6131 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6132 | 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] | 6133 | { |
| 6134 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6135 | struct io_poll_table ipt; |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6136 | int ret; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6137 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6138 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 6139 | |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6140 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
Pavel Begunkov | 6c7259c | 2022-08-29 14:30:22 +0100 | [diff] [blame] | 6141 | if (!ret && ipt.error) |
| 6142 | req_set_fail(req); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6143 | ret = ret ?: ipt.error; |
| 6144 | if (ret) |
| 6145 | __io_req_complete(req, issue_flags, ret, 0); |
| 6146 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6147 | } |
| 6148 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6149 | 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] | 6150 | { |
| 6151 | struct io_ring_ctx *ctx = req->ctx; |
| 6152 | struct io_kiocb *preq; |
Pavel Begunkov | 040e58f | 2022-08-29 14:30:14 +0100 | [diff] [blame] | 6153 | int ret2, ret = 0; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6154 | |
Jens Axboe | 0e388fc | 2023-06-16 21:12:06 -0600 | [diff] [blame] | 6155 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 6156 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6157 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 6158 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6159 | if (!preq || !io_poll_disarm(preq)) { |
Pavel Begunkov | 040e58f | 2022-08-29 14:30:14 +0100 | [diff] [blame] | 6160 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | f770fba | 2022-08-29 14:30:18 +0100 | [diff] [blame] | 6161 | ret = preq ? -EALREADY : -ENOENT; |
Pavel Begunkov | 040e58f | 2022-08-29 14:30:14 +0100 | [diff] [blame] | 6162 | goto out; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6163 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6164 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 6165 | |
Pavel Begunkov | 040e58f | 2022-08-29 14:30:14 +0100 | [diff] [blame] | 6166 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
| 6167 | /* only mask one event flags, keep behavior flags */ |
| 6168 | if (req->poll_update.update_events) { |
| 6169 | preq->poll.events &= ~0xffff; |
| 6170 | preq->poll.events |= req->poll_update.events & 0xffff; |
| 6171 | preq->poll.events |= IO_POLL_UNMASK; |
| 6172 | } |
| 6173 | if (req->poll_update.update_user_data) |
| 6174 | preq->user_data = req->poll_update.new_user_data; |
| 6175 | |
| 6176 | ret2 = io_poll_add(preq, issue_flags); |
| 6177 | /* successfully updated, don't complete poll request */ |
| 6178 | if (!ret2) |
| 6179 | goto out; |
| 6180 | } |
| 6181 | req_set_fail(preq); |
| 6182 | io_req_complete(preq, -ECANCELED); |
| 6183 | out: |
| 6184 | if (ret < 0) |
| 6185 | req_set_fail(req); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6186 | /* complete update request, we're done with it */ |
| 6187 | io_req_complete(req, ret); |
Jens Axboe | 0e388fc | 2023-06-16 21:12:06 -0600 | [diff] [blame] | 6188 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6189 | return 0; |
| 6190 | } |
| 6191 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6192 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6193 | { |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6194 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6195 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6196 | } |
| 6197 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6198 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 6199 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6200 | struct io_timeout_data *data = container_of(timer, |
| 6201 | struct io_timeout_data, timer); |
| 6202 | struct io_kiocb *req = data->req; |
| 6203 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6204 | unsigned long flags; |
| 6205 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6206 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 6207 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 6208 | atomic_set(&req->ctx->cq_timeouts, |
| 6209 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6210 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 6211 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6212 | req->io_task_work.func = io_req_task_timeout; |
| 6213 | io_req_task_work_add(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6214 | return HRTIMER_NORESTART; |
| 6215 | } |
| 6216 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6217 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 6218 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6219 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6220 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6221 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6222 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6223 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6224 | |
| 6225 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6226 | found = user_data == req->user_data; |
| 6227 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6228 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6229 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6230 | if (!found) |
| 6231 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6232 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6233 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6234 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6235 | return ERR_PTR(-EALREADY); |
| 6236 | list_del_init(&req->timeout.list); |
| 6237 | return req; |
| 6238 | } |
| 6239 | |
| 6240 | 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] | 6241 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6242 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6243 | { |
| 6244 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6245 | |
| 6246 | if (IS_ERR(req)) |
| 6247 | return PTR_ERR(req); |
| 6248 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6249 | req_set_fail(req); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 6250 | io_fill_cqe_req(req, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 6251 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6252 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6253 | } |
| 6254 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6255 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 6256 | { |
| 6257 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 6258 | case IORING_TIMEOUT_BOOTTIME: |
| 6259 | return CLOCK_BOOTTIME; |
| 6260 | case IORING_TIMEOUT_REALTIME: |
| 6261 | return CLOCK_REALTIME; |
| 6262 | default: |
| 6263 | /* can't happen, vetted at prep time */ |
| 6264 | WARN_ON_ONCE(1); |
| 6265 | fallthrough; |
| 6266 | case 0: |
| 6267 | return CLOCK_MONOTONIC; |
| 6268 | } |
| 6269 | } |
| 6270 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6271 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6272 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 6273 | __must_hold(&ctx->timeout_lock) |
| 6274 | { |
| 6275 | struct io_timeout_data *io; |
| 6276 | struct io_kiocb *req; |
| 6277 | bool found = false; |
| 6278 | |
| 6279 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 6280 | found = user_data == req->user_data; |
| 6281 | if (found) |
| 6282 | break; |
| 6283 | } |
| 6284 | if (!found) |
| 6285 | return -ENOENT; |
| 6286 | |
| 6287 | io = req->async_data; |
| 6288 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 6289 | return -EALREADY; |
| 6290 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 6291 | io->timer.function = io_link_timeout_fn; |
| 6292 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6296 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6297 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6298 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6299 | { |
| 6300 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6301 | struct io_timeout_data *data; |
| 6302 | |
| 6303 | if (IS_ERR(req)) |
| 6304 | return PTR_ERR(req); |
| 6305 | |
| 6306 | req->timeout.off = 0; /* noseq */ |
| 6307 | data = req->async_data; |
| 6308 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6309 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6310 | data->timer.function = io_timeout_fn; |
| 6311 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 6312 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6313 | } |
| 6314 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6315 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 6316 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6317 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6318 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 6319 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6320 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6321 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6322 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6323 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6324 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6325 | return -EINVAL; |
| 6326 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6327 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6328 | tr->addr = READ_ONCE(sqe->addr); |
| 6329 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6330 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6331 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6332 | return -EINVAL; |
| 6333 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6334 | tr->ltimeout = true; |
| 6335 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6336 | return -EINVAL; |
| 6337 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6338 | return -EFAULT; |
| 6339 | } else if (tr->flags) { |
| 6340 | /* timeout removal doesn't support flags */ |
| 6341 | return -EINVAL; |
| 6342 | } |
| 6343 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6344 | return 0; |
| 6345 | } |
| 6346 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6347 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6348 | { |
| 6349 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6350 | : HRTIMER_MODE_REL; |
| 6351 | } |
| 6352 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6353 | /* |
| 6354 | * Remove or update an existing timeout command |
| 6355 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6356 | 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] | 6357 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6358 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6359 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6360 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6361 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6362 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6363 | spin_lock(&ctx->completion_lock); |
| 6364 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6365 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6366 | spin_unlock_irq(&ctx->timeout_lock); |
| 6367 | spin_unlock(&ctx->completion_lock); |
| 6368 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6369 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6370 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6371 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6372 | if (tr->ltimeout) |
| 6373 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6374 | else |
| 6375 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6376 | spin_unlock_irq(&ctx->timeout_lock); |
| 6377 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6378 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6379 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6380 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6381 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6382 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6383 | } |
| 6384 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6385 | 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] | 6386 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6387 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6388 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6389 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6390 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6391 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6392 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6393 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6394 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6395 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6396 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6397 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6398 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6399 | flags = READ_ONCE(sqe->timeout_flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6400 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
| 6401 | return -EINVAL; |
| 6402 | /* more than one clock specified is invalid, obviously */ |
| 6403 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6404 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6405 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6406 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6407 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6408 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6409 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6410 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6411 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6412 | return -ENOMEM; |
| 6413 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6414 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6415 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6416 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6417 | |
| 6418 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6419 | return -EFAULT; |
| 6420 | |
Jens Axboe | ba7261a | 2022-04-08 11:08:58 -0600 | [diff] [blame] | 6421 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6422 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6423 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6424 | |
| 6425 | if (is_timeout_link) { |
| 6426 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6427 | |
| 6428 | if (!link->head) |
| 6429 | return -EINVAL; |
| 6430 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6431 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6432 | req->timeout.head = link->last; |
| 6433 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6434 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6435 | return 0; |
| 6436 | } |
| 6437 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6438 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6439 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6440 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6441 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6442 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6443 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6444 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6445 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6446 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6447 | /* |
| 6448 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6449 | * timeout event to be satisfied. If it isn't set, then this is |
| 6450 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6451 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6452 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6453 | entry = ctx->timeout_list.prev; |
| 6454 | goto add; |
| 6455 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6456 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6457 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6458 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6459 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6460 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6461 | * This is safe because ->completion_lock is held, and submissions |
| 6462 | * and completions are never mixed in the same ->completion_lock section. |
| 6463 | */ |
| 6464 | ctx->cq_last_tm_flush = tail; |
| 6465 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6466 | /* |
| 6467 | * Insertion sort, ensuring the first entry in the list is always |
| 6468 | * the one we need first. |
| 6469 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6470 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6471 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6472 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6473 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6474 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6475 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6476 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6477 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6478 | break; |
| 6479 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6480 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6481 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6482 | data->timer.function = io_timeout_fn; |
| 6483 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6484 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6485 | return 0; |
| 6486 | } |
| 6487 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6488 | struct io_cancel_data { |
| 6489 | struct io_ring_ctx *ctx; |
| 6490 | u64 user_data; |
| 6491 | }; |
| 6492 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6493 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6494 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6495 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6496 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6497 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6498 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6499 | } |
| 6500 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6501 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6502 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6503 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6504 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6505 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6506 | int ret = 0; |
| 6507 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6508 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6509 | return -ENOENT; |
| 6510 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6511 | 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] | 6512 | switch (cancel_ret) { |
| 6513 | case IO_WQ_CANCEL_OK: |
| 6514 | ret = 0; |
| 6515 | break; |
| 6516 | case IO_WQ_CANCEL_RUNNING: |
| 6517 | ret = -EALREADY; |
| 6518 | break; |
| 6519 | case IO_WQ_CANCEL_NOTFOUND: |
| 6520 | ret = -ENOENT; |
| 6521 | break; |
| 6522 | } |
| 6523 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6524 | return ret; |
| 6525 | } |
| 6526 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6527 | 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] | 6528 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6529 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6530 | int ret; |
| 6531 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6532 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6533 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6534 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6535 | if (ret != -ENOENT) |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6536 | return ret; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6537 | |
| 6538 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6539 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6540 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6541 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6542 | if (ret != -ENOENT) |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6543 | goto out; |
| 6544 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6545 | out: |
| 6546 | spin_unlock(&ctx->completion_lock); |
| 6547 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6548 | } |
| 6549 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6550 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6551 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6552 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6553 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6554 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6555 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6556 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6557 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6558 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6559 | return -EINVAL; |
| 6560 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6561 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6562 | return 0; |
| 6563 | } |
| 6564 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6565 | 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] | 6566 | { |
| 6567 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6568 | u64 sqe_addr = req->cancel.addr; |
| 6569 | struct io_tctx_node *node; |
| 6570 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6571 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6572 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6573 | if (ret != -ENOENT) |
| 6574 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6575 | |
| 6576 | /* slow path, try all io-wq's */ |
| 6577 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 6578 | ret = -ENOENT; |
| 6579 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6580 | struct io_uring_task *tctx = node->task->io_uring; |
| 6581 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6582 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6583 | if (ret != -ENOENT) |
| 6584 | break; |
| 6585 | } |
| 6586 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6587 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6588 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6589 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6590 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6591 | return 0; |
| 6592 | } |
| 6593 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6594 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6595 | const struct io_uring_sqe *sqe) |
| 6596 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6597 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6598 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6599 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6600 | return -EINVAL; |
| 6601 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6602 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6603 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6604 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6605 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6606 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6607 | return 0; |
| 6608 | } |
| 6609 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6610 | 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] | 6611 | { |
| 6612 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6613 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6614 | int ret; |
| 6615 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6616 | up.offset = req->rsrc_update.offset; |
| 6617 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6618 | up.nr = 0; |
| 6619 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6620 | up.resv = 0; |
Dylan Yudaken | 7a7c9f9 | 2022-04-12 09:30:40 -0700 | [diff] [blame] | 6621 | up.resv2 = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6622 | |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6623 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6624 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6625 | &up, req->rsrc_update.nr_args); |
Jens Axboe | cdb31c2 | 2021-09-24 08:43:54 -0600 | [diff] [blame] | 6626 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6627 | |
| 6628 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6629 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6630 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6631 | return 0; |
| 6632 | } |
| 6633 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6634 | 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] | 6635 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6636 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6637 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6638 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6639 | case IORING_OP_READV: |
| 6640 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6641 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6642 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6643 | case IORING_OP_WRITEV: |
| 6644 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6645 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6646 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6647 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6648 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6649 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6650 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6651 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6652 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6653 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6654 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6655 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6656 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6657 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6658 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6659 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6660 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6661 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6662 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6663 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6664 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6665 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6666 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6667 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6668 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6669 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6670 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6671 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6672 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6673 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6674 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6675 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6676 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6677 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6678 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6679 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6680 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6681 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6682 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6683 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6684 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6685 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6686 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6687 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6688 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6689 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6690 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6691 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6692 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6693 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6694 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6695 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6696 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6697 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6698 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6699 | case IORING_OP_SHUTDOWN: |
| 6700 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6701 | case IORING_OP_RENAMEAT: |
| 6702 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6703 | case IORING_OP_UNLINKAT: |
| 6704 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6705 | case IORING_OP_MKDIRAT: |
| 6706 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6707 | case IORING_OP_SYMLINKAT: |
| 6708 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6709 | case IORING_OP_LINKAT: |
| 6710 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6711 | } |
| 6712 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6713 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6714 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6715 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6716 | } |
| 6717 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6718 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6719 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6720 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6721 | return 0; |
| 6722 | if (WARN_ON_ONCE(req->async_data)) |
| 6723 | return -EFAULT; |
| 6724 | if (io_alloc_async_data(req)) |
| 6725 | return -EAGAIN; |
| 6726 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6727 | switch (req->opcode) { |
| 6728 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6729 | return io_rw_prep_async(req, READ); |
| 6730 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6731 | return io_rw_prep_async(req, WRITE); |
| 6732 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6733 | return io_sendmsg_prep_async(req); |
| 6734 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6735 | return io_recvmsg_prep_async(req); |
| 6736 | case IORING_OP_CONNECT: |
| 6737 | return io_connect_prep_async(req); |
| 6738 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6739 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6740 | req->opcode); |
| 6741 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6742 | } |
| 6743 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6744 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6745 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6746 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6747 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6748 | /* need original cached_sq_head, but it was increased for each req */ |
| 6749 | io_for_each_link(req, req) |
| 6750 | seq--; |
| 6751 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6752 | } |
| 6753 | |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6754 | static bool io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6755 | { |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6756 | struct io_kiocb *pos; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6757 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6758 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6759 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6760 | u32 seq; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6761 | |
Pavel Begunkov | b8ce1b9 | 2021-08-31 14:13:11 +0100 | [diff] [blame] | 6762 | if (req->flags & REQ_F_FAIL) { |
| 6763 | io_req_complete_fail_submit(req); |
| 6764 | return true; |
| 6765 | } |
| 6766 | |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6767 | /* |
| 6768 | * If we need to drain a request in the middle of a link, drain the |
| 6769 | * head request and the next request/link after the current link. |
| 6770 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be |
| 6771 | * maintained for every request of our link. |
| 6772 | */ |
| 6773 | if (ctx->drain_next) { |
| 6774 | req->flags |= REQ_F_IO_DRAIN; |
| 6775 | ctx->drain_next = false; |
| 6776 | } |
| 6777 | /* not interested in head, start from the first linked */ |
| 6778 | io_for_each_link(pos, req->link) { |
| 6779 | if (pos->flags & REQ_F_IO_DRAIN) { |
| 6780 | ctx->drain_next = true; |
| 6781 | req->flags |= REQ_F_IO_DRAIN; |
| 6782 | break; |
| 6783 | } |
| 6784 | } |
| 6785 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6786 | /* Still need defer if there is pending req in defer list. */ |
Hao Xu | 1bd12b7 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6787 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6788 | if (likely(list_empty_careful(&ctx->defer_list) && |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6789 | !(req->flags & REQ_F_IO_DRAIN))) { |
Hao Xu | 1bd12b7 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6790 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6791 | ctx->drain_active = false; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6792 | return false; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6793 | } |
Hao Xu | 1bd12b7 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6794 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6795 | |
| 6796 | seq = io_get_sequence(req); |
| 6797 | /* Still a chance to pass the sequence check */ |
| 6798 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6799 | return false; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6800 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6801 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6802 | if (ret) |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6803 | goto fail; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6804 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6805 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6806 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6807 | ret = -ENOMEM; |
| 6808 | fail: |
| 6809 | io_req_complete_failed(req, ret); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6810 | return true; |
| 6811 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6812 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6813 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6814 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6815 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6816 | kfree(de); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6817 | io_queue_async_work(req, NULL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6818 | return true; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6822 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6823 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6824 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6825 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6826 | return true; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6827 | } |
| 6828 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6829 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6830 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6831 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6832 | switch (req->opcode) { |
| 6833 | case IORING_OP_READV: |
| 6834 | case IORING_OP_READ_FIXED: |
| 6835 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6836 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6837 | break; |
| 6838 | case IORING_OP_RECVMSG: |
| 6839 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6840 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6841 | break; |
| 6842 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6843 | } |
| 6844 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6845 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6846 | switch (req->opcode) { |
| 6847 | case IORING_OP_READV: |
| 6848 | case IORING_OP_READ_FIXED: |
| 6849 | case IORING_OP_READ: |
| 6850 | case IORING_OP_WRITEV: |
| 6851 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6852 | case IORING_OP_WRITE: { |
| 6853 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6854 | |
| 6855 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6856 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6857 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6858 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6859 | case IORING_OP_SENDMSG: { |
| 6860 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6861 | |
| 6862 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6863 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6864 | } |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6865 | case IORING_OP_OPENAT: |
| 6866 | case IORING_OP_OPENAT2: |
| 6867 | if (req->open.filename) |
| 6868 | putname(req->open.filename); |
| 6869 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6870 | case IORING_OP_RENAMEAT: |
| 6871 | putname(req->rename.oldpath); |
| 6872 | putname(req->rename.newpath); |
| 6873 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6874 | case IORING_OP_UNLINKAT: |
| 6875 | putname(req->unlink.filename); |
| 6876 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6877 | case IORING_OP_MKDIRAT: |
| 6878 | putname(req->mkdir.filename); |
| 6879 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6880 | case IORING_OP_SYMLINKAT: |
| 6881 | putname(req->symlink.oldpath); |
| 6882 | putname(req->symlink.newpath); |
| 6883 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6884 | case IORING_OP_LINKAT: |
| 6885 | putname(req->hardlink.oldpath); |
| 6886 | putname(req->hardlink.newpath); |
| 6887 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6888 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6889 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6890 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6891 | kfree(req->apoll->double_poll); |
| 6892 | kfree(req->apoll); |
| 6893 | req->apoll = NULL; |
| 6894 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6895 | if (req->flags & REQ_F_INFLIGHT) { |
| 6896 | struct io_uring_task *tctx = req->task->io_uring; |
| 6897 | |
| 6898 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6899 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6900 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6901 | put_cred(req->creds); |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6902 | |
| 6903 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6904 | } |
| 6905 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6906 | 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] | 6907 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6908 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6909 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6910 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6911 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6912 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6913 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6914 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6915 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6916 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6917 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6918 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6919 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6920 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6921 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6922 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6923 | break; |
| 6924 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6925 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6926 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6927 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6928 | break; |
| 6929 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6930 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6931 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6932 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6933 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6934 | break; |
| 6935 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6936 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6937 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6938 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6939 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6940 | break; |
| 6941 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6942 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6943 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6944 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6945 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6946 | break; |
| 6947 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6948 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6949 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6950 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6951 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6952 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6953 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6954 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6955 | break; |
| 6956 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6957 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6958 | break; |
| 6959 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6960 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6961 | break; |
| 6962 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6963 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6964 | break; |
| 6965 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6966 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6967 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6968 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6969 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6970 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6971 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6972 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6973 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6974 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6975 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6976 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6977 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6978 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6979 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6980 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6981 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6982 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6983 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6984 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6985 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6986 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6987 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6988 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6989 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6990 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6991 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6992 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6993 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6994 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6995 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6996 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6997 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6998 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6999 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 7000 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 7001 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 7002 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7003 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 7004 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 7005 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 7006 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 7007 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 7008 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 7009 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 7010 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 7011 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 7012 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 7013 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 7014 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 7015 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 7016 | case IORING_OP_MKDIRAT: |
| 7017 | ret = io_mkdirat(req, issue_flags); |
| 7018 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 7019 | case IORING_OP_SYMLINKAT: |
| 7020 | ret = io_symlinkat(req, issue_flags); |
| 7021 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 7022 | case IORING_OP_LINKAT: |
| 7023 | ret = io_linkat(req, issue_flags); |
| 7024 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7025 | default: |
| 7026 | ret = -EINVAL; |
| 7027 | break; |
| 7028 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 7029 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 7030 | if (creds) |
| 7031 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7032 | if (ret) |
| 7033 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 7034 | /* 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] | 7035 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
| 7036 | io_iopoll_req_issued(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7037 | |
| 7038 | return 0; |
| 7039 | } |
| 7040 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 7041 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 7042 | { |
| 7043 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7044 | |
| 7045 | req = io_put_req_find_next(req); |
| 7046 | return req ? &req->work : NULL; |
| 7047 | } |
| 7048 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7049 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 7050 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7051 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 7052 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7053 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7054 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 7055 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 7056 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 7057 | __io_req_set_refcount(req, 2); |
| 7058 | else |
| 7059 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 7060 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 7061 | timeout = io_prep_linked_timeout(req); |
| 7062 | if (timeout) |
| 7063 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 7064 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 7065 | /* 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] | 7066 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7067 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 7068 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7069 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7070 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 7071 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7072 | /* |
| 7073 | * We can get EAGAIN for polled IO even though we're |
| 7074 | * forcing a sync submission from here, since we can't |
| 7075 | * wait for request slots on the block side. |
| 7076 | */ |
Pavel Begunkov | 51ebf1b | 2022-05-13 11:24:56 +0100 | [diff] [blame] | 7077 | if (ret != -EAGAIN || !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7078 | break; |
Pavel Begunkov | 0def123 | 2023-09-12 15:02:00 +0100 | [diff] [blame] | 7079 | if (io_wq_worker_stopped()) |
| 7080 | break; |
Jens Axboe | 3359fdf | 2023-07-20 13:16:53 -0600 | [diff] [blame] | 7081 | /* |
| 7082 | * If REQ_F_NOWAIT is set, then don't wait or retry with |
| 7083 | * poll. -EAGAIN is final for that case. |
| 7084 | */ |
| 7085 | if (req->flags & REQ_F_NOWAIT) |
| 7086 | break; |
| 7087 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7088 | cond_resched(); |
| 7089 | } while (1); |
| 7090 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 7091 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 7092 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 7093 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 7094 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 7095 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7096 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7097 | 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] | 7098 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 7099 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7100 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 7101 | } |
| 7102 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 7103 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 7104 | int index) |
| 7105 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7106 | 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] | 7107 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7108 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7109 | } |
| 7110 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7111 | 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] | 7112 | { |
| 7113 | unsigned long file_ptr = (unsigned long) file; |
| 7114 | |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 7115 | if (__io_file_supports_nowait(file, READ)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 7116 | file_ptr |= FFS_ASYNC_READ; |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 7117 | if (__io_file_supports_nowait(file, WRITE)) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 7118 | file_ptr |= FFS_ASYNC_WRITE; |
| 7119 | if (S_ISREG(file_inode(file)->i_mode)) |
| 7120 | file_ptr |= FFS_ISREG; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7121 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 7122 | } |
| 7123 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7124 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7125 | struct io_kiocb *req, int fd, |
| 7126 | unsigned int issue_flags) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7127 | { |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7128 | struct file *file = NULL; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7129 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7130 | |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7131 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 7132 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7133 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7134 | goto out; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7135 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 7136 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 7137 | file = (struct file *) (file_ptr & FFS_MASK); |
| 7138 | file_ptr &= ~FFS_MASK; |
| 7139 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | b191e2d | 2021-08-09 13:04:03 +0100 | [diff] [blame] | 7140 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7141 | io_req_set_rsrc_node(req); |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7142 | out: |
| 7143 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 7144 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7145 | } |
| 7146 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7147 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7148 | struct io_kiocb *req, int fd) |
| 7149 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7150 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7151 | |
| 7152 | trace_io_uring_file_get(ctx, fd); |
| 7153 | |
| 7154 | /* we don't allow fixed io_uring files */ |
| 7155 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 7156 | io_req_track_inflight(req); |
| 7157 | return file; |
| 7158 | } |
| 7159 | |
| 7160 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7161 | struct io_kiocb *req, int fd, bool fixed, |
| 7162 | unsigned int issue_flags) |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7163 | { |
| 7164 | if (fixed) |
Bing-Jhong Billy Jheng | cf7f9cd | 2023-03-02 21:00:06 +0800 | [diff] [blame] | 7165 | return io_file_get_fixed(ctx, req, fd, issue_flags); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7166 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7167 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7168 | } |
| 7169 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7170 | 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] | 7171 | { |
| 7172 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 3d2a1e6 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 7173 | int ret = -ENOENT; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7174 | |
| 7175 | if (prev) { |
Pavel Begunkov | 3d2a1e6 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 7176 | if (!(req->task->flags & PF_EXITING)) |
| 7177 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 7178 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7179 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7180 | } else { |
| 7181 | io_req_complete_post(req, -ETIME, 0); |
| 7182 | } |
| 7183 | } |
| 7184 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7185 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 7186 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 7187 | struct io_timeout_data *data = container_of(timer, |
| 7188 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7189 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7190 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7191 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7192 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7193 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7194 | prev = req->timeout.head; |
| 7195 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7196 | |
| 7197 | /* |
| 7198 | * We don't expect the list to be empty, that will only happen if we |
| 7199 | * race with the completion of the linked work. |
| 7200 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 7201 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 7202 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 7203 | if (!req_ref_inc_not_zero(prev)) |
| 7204 | prev = NULL; |
| 7205 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 7206 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7207 | req->timeout.prev = prev; |
| 7208 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7209 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7210 | req->io_task_work.func = io_req_task_link_timeout; |
| 7211 | io_req_task_work_add(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7212 | return HRTIMER_NORESTART; |
| 7213 | } |
| 7214 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 7215 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7216 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 7217 | struct io_ring_ctx *ctx = req->ctx; |
| 7218 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7219 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7220 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 7221 | * If the back reference is NULL, then our linked request finished |
| 7222 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7223 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7224 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 7225 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 7226 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 7227 | data->timer.function = io_link_timeout_fn; |
| 7228 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 7229 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 7230 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7231 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7232 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7233 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7234 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7235 | } |
| 7236 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7237 | static void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7238 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7239 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7240 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 7241 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7242 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 7243 | issue_sqe: |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7244 | 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] | 7245 | |
| 7246 | /* |
| 7247 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 7248 | * doesn't support non-blocking read/write attempts |
| 7249 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7250 | if (likely(!ret)) { |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 7251 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7252 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 7253 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 7254 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 7255 | state->compl_reqs[state->compl_nr++] = req; |
| 7256 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) |
Pavel Begunkov | 2a2758f | 2021-06-17 18:14:00 +0100 | [diff] [blame] | 7257 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7258 | return; |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 7259 | } |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7260 | |
| 7261 | linked_timeout = io_prep_linked_timeout(req); |
| 7262 | if (linked_timeout) |
| 7263 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7264 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7265 | linked_timeout = io_prep_linked_timeout(req); |
| 7266 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 7267 | switch (io_arm_poll_handler(req)) { |
| 7268 | case IO_APOLL_READY: |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7269 | if (linked_timeout) |
Pavel Begunkov | 4ea672a | 2021-10-20 09:53:02 +0100 | [diff] [blame] | 7270 | io_queue_linked_timeout(linked_timeout); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 7271 | goto issue_sqe; |
| 7272 | case IO_APOLL_ABORTED: |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7273 | /* |
| 7274 | * Queued up for async execution, worker will release |
| 7275 | * submit reference when the iocb is actually submitted. |
| 7276 | */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7277 | io_queue_async_work(req, NULL); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 7278 | break; |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7279 | } |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7280 | |
| 7281 | if (linked_timeout) |
| 7282 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 7283 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 7284 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7285 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7286 | } |
| 7287 | |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7288 | static inline void io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7289 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7290 | { |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 7291 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7292 | return; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7293 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7294 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7295 | __io_queue_sqe(req); |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7296 | } else if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 7297 | io_req_complete_fail_submit(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7298 | } else { |
| 7299 | int ret = io_req_prep_async(req); |
| 7300 | |
| 7301 | if (unlikely(ret)) |
| 7302 | io_req_complete_failed(req, ret); |
| 7303 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7304 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 7305 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7306 | } |
| 7307 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7308 | /* |
| 7309 | * Check SQE restrictions (opcode and flags). |
| 7310 | * |
| 7311 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 7312 | */ |
| 7313 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 7314 | struct io_kiocb *req, |
| 7315 | unsigned int sqe_flags) |
| 7316 | { |
Pavel Begunkov | 4cfb25b | 2021-06-26 21:40:47 +0100 | [diff] [blame] | 7317 | if (likely(!ctx->restricted)) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7318 | return true; |
| 7319 | |
| 7320 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 7321 | return false; |
| 7322 | |
| 7323 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 7324 | ctx->restrictions.sqe_flags_required) |
| 7325 | return false; |
| 7326 | |
| 7327 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 7328 | ctx->restrictions.sqe_flags_required)) |
| 7329 | return false; |
| 7330 | |
| 7331 | return true; |
| 7332 | } |
| 7333 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7334 | 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] | 7335 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7336 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7337 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7338 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7339 | unsigned int sqe_flags; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7340 | int personality, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7341 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 7342 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7343 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7344 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 7345 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7346 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7347 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7348 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7349 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7350 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7351 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 7352 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7353 | return -EINVAL; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7354 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 7355 | return -EINVAL; |
Pavel Begunkov | 4cfb25b | 2021-06-26 21:40:47 +0100 | [diff] [blame] | 7356 | if (!io_check_restriction(ctx, req, sqe_flags)) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7357 | return -EACCES; |
| 7358 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7359 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 7360 | !io_op_defs[req->opcode].buffer_select) |
| 7361 | return -EOPNOTSUPP; |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 7362 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
| 7363 | ctx->drain_active = true; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7364 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7365 | personality = READ_ONCE(sqe->personality); |
| 7366 | if (personality) { |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7367 | req->creds = xa_load(&ctx->personalities, personality); |
| 7368 | if (!req->creds) |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7369 | return -EINVAL; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 7370 | get_cred(req->creds); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 7371 | req->flags |= REQ_F_CREDS; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 7372 | } |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7373 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7374 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7375 | /* |
| 7376 | * Plug now if we have more than 1 IO left after this, and the target |
| 7377 | * is potentially a read/write to block based storage. |
| 7378 | */ |
| 7379 | if (!state->plug_started && state->ios_left > 1 && |
| 7380 | io_op_defs[req->opcode].plug) { |
| 7381 | blk_start_plug(&state->plug); |
| 7382 | state->plug_started = true; |
| 7383 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7384 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7385 | if (io_op_defs[req->opcode].needs_file) { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7386 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Jens Axboe | 937c15e | 2023-03-03 06:49:57 -0700 | [diff] [blame] | 7387 | (sqe_flags & IOSQE_FIXED_FILE), |
| 7388 | IO_URING_F_NONBLOCK); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7389 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7390 | ret = -EBADF; |
| 7391 | } |
| 7392 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 7393 | state->ios_left--; |
| 7394 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7395 | } |
| 7396 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7397 | 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] | 7398 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7399 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7400 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7401 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7402 | int ret; |
| 7403 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7404 | ret = io_init_req(ctx, req, sqe); |
| 7405 | if (unlikely(ret)) { |
| 7406 | fail_req: |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7407 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7408 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7409 | /* |
| 7410 | * we can judge a link req is failed or cancelled by if |
| 7411 | * REQ_F_FAIL is set, but the head is an exception since |
| 7412 | * it may be set REQ_F_FAIL because of other req's failure |
| 7413 | * so let's leverage req->result to distinguish if a head |
| 7414 | * is set REQ_F_FAIL because of its failure or other req's |
| 7415 | * failure so that we can set the correct ret code for it. |
| 7416 | * init result here to avoid affecting the normal path. |
| 7417 | */ |
| 7418 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7419 | req_fail_link_node(link->head, -ECANCELED); |
| 7420 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7421 | /* |
| 7422 | * the current req is a normal req, we should return |
| 7423 | * error and thus break the submittion loop. |
| 7424 | */ |
| 7425 | io_req_complete_failed(req, ret); |
| 7426 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7427 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7428 | req_fail_link_node(req, ret); |
| 7429 | } else { |
| 7430 | ret = io_req_prep(req, sqe); |
| 7431 | if (unlikely(ret)) |
| 7432 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7433 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7434 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7435 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7436 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7437 | req->flags, true, |
| 7438 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7439 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7440 | /* |
| 7441 | * If we already have a head request, queue this one for async |
| 7442 | * submittal once the head completes. If we don't have a head but |
| 7443 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7444 | * submitted sync once the chain is complete. If none of those |
| 7445 | * conditions are true (normal request), then just queue it. |
| 7446 | */ |
| 7447 | if (link->head) { |
| 7448 | struct io_kiocb *head = link->head; |
| 7449 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7450 | if (!(req->flags & REQ_F_FAIL)) { |
| 7451 | ret = io_req_prep_async(req); |
| 7452 | if (unlikely(ret)) { |
| 7453 | req_fail_link_node(req, ret); |
| 7454 | if (!(head->flags & REQ_F_FAIL)) |
| 7455 | req_fail_link_node(head, -ECANCELED); |
| 7456 | } |
| 7457 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7458 | trace_io_uring_link(ctx, req, head); |
| 7459 | link->last->link = req; |
| 7460 | link->last = req; |
| 7461 | |
| 7462 | /* last request of a link, enqueue the link */ |
| 7463 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7464 | link->head = NULL; |
Pavel Begunkov | 5e15920 | 2021-06-14 23:37:26 +0100 | [diff] [blame] | 7465 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7466 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7467 | } else { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7468 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7469 | link->head = req; |
| 7470 | link->last = req; |
| 7471 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7472 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7473 | } |
| 7474 | } |
| 7475 | |
| 7476 | return 0; |
| 7477 | } |
| 7478 | |
| 7479 | /* |
| 7480 | * Batched submission is done, ensure local IO is flushed out. |
| 7481 | */ |
| 7482 | static void io_submit_state_end(struct io_submit_state *state, |
| 7483 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 7484 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7485 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7486 | io_queue_sqe(state->link.head); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 7487 | if (state->compl_nr) |
Pavel Begunkov | 2a2758f | 2021-06-17 18:14:00 +0100 | [diff] [blame] | 7488 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7489 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7490 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7491 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7492 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7493 | /* |
| 7494 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7495 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7496 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7497 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7498 | { |
| 7499 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7500 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7501 | /* set only head, no need to init link_last in advance */ |
| 7502 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7503 | } |
| 7504 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7505 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7506 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7507 | struct io_rings *rings = ctx->rings; |
| 7508 | |
| 7509 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7510 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7511 | * since once we write the new head, the application could |
| 7512 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7513 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7514 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7515 | } |
| 7516 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7517 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7518 | * 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] | 7519 | * that is mapped by userspace. This means that care needs to be taken to |
| 7520 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7521 | * being a good citizen. If members of the sqe are validated and then later |
| 7522 | * 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] | 7523 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7524 | */ |
| 7525 | 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] | 7526 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7527 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7528 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7529 | |
| 7530 | /* |
| 7531 | * The cached sq head (or cq tail) serves two purposes: |
| 7532 | * |
| 7533 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7534 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7535 | * 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] | 7536 | * though the application is the one updating it. |
| 7537 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7538 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7539 | if (likely(head < ctx->sq_entries)) |
| 7540 | return &ctx->sq_sqes[head]; |
| 7541 | |
| 7542 | /* drop invalid entries */ |
Pavel Begunkov | cc6b096 | 2023-08-09 13:21:41 +0100 | [diff] [blame] | 7543 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7544 | ctx->cq_extra--; |
Pavel Begunkov | cc6b096 | 2023-08-09 13:21:41 +0100 | [diff] [blame] | 7545 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7546 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7547 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7548 | return NULL; |
| 7549 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7550 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7551 | 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] | 7552 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7553 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7554 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7555 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7556 | /* make sure SQ entry isn't read before tail */ |
| 7557 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 7558 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 7559 | return -EAGAIN; |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7560 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7561 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7562 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7563 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7564 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7565 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7566 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 7567 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7568 | if (unlikely(!req)) { |
| 7569 | if (!submitted) |
| 7570 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7571 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7572 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7573 | sqe = io_get_sqe(ctx); |
| 7574 | if (unlikely(!sqe)) { |
Hao Xu | 0c6e1d7 | 2021-08-26 01:58:56 +0800 | [diff] [blame] | 7575 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7576 | break; |
| 7577 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7578 | /* will complete beyond this point, count as submitted */ |
| 7579 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7580 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7581 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7582 | } |
| 7583 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7584 | if (unlikely(submitted != nr)) { |
| 7585 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7586 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7587 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7588 | current->io_uring->cached_refs += unused; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7589 | percpu_ref_put_many(&ctx->refs, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7590 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7591 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7592 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7593 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7594 | io_commit_sqring(ctx); |
| 7595 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7596 | return submitted; |
| 7597 | } |
| 7598 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7599 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7600 | { |
| 7601 | return READ_ONCE(sqd->state); |
| 7602 | } |
| 7603 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7604 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7605 | { |
| 7606 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7607 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7608 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7609 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7610 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7611 | } |
| 7612 | |
| 7613 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7614 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7615 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7616 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7617 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7618 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7619 | } |
| 7620 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7621 | 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] | 7622 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7623 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7624 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7625 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7626 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7627 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7628 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7629 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7630 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7631 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 7632 | unsigned nr_events = 0; |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7633 | const struct cred *creds = NULL; |
| 7634 | |
| 7635 | if (ctx->sq_creds != current_cred()) |
| 7636 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7637 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7638 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7639 | if (!list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | a8576af | 2021-08-15 10:40:21 +0100 | [diff] [blame] | 7640 | io_do_iopoll(ctx, &nr_events, 0); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7641 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7642 | /* |
| 7643 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7644 | * but also it is relied upon by io_ring_exit_work() |
| 7645 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7646 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7647 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7648 | ret = io_submit_sqes(ctx, to_submit); |
| 7649 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7650 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7651 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7652 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7653 | if (creds) |
| 7654 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7655 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7656 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7657 | return ret; |
| 7658 | } |
| 7659 | |
| 7660 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7661 | { |
| 7662 | struct io_ring_ctx *ctx; |
| 7663 | unsigned sq_thread_idle = 0; |
| 7664 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7665 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7666 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7667 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7668 | } |
| 7669 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7670 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7671 | { |
| 7672 | bool did_sig = false; |
| 7673 | struct ksignal ksig; |
| 7674 | |
| 7675 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7676 | signal_pending(current)) { |
| 7677 | mutex_unlock(&sqd->lock); |
| 7678 | if (signal_pending(current)) |
| 7679 | did_sig = get_signal(&ksig); |
| 7680 | cond_resched(); |
| 7681 | mutex_lock(&sqd->lock); |
| 7682 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7683 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7684 | } |
| 7685 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7686 | static int io_sq_thread(void *data) |
| 7687 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7688 | struct io_sq_data *sqd = data; |
| 7689 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7690 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7691 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7692 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7693 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7694 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7695 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7696 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7697 | if (sqd->sq_cpu != -1) |
| 7698 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7699 | else |
| 7700 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7701 | current->flags |= PF_NO_SETAFFINITY; |
| 7702 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7703 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7704 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7705 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7706 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7707 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7708 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7709 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7710 | timeout = jiffies + sqd->sq_thread_idle; |
| 7711 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7712 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7713 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7714 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7715 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7716 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7717 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7718 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7719 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7720 | if (io_run_task_work()) |
| 7721 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7722 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7723 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7724 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7725 | if (sqt_spin) |
| 7726 | timeout = jiffies + sqd->sq_thread_idle; |
| 7727 | continue; |
| 7728 | } |
| 7729 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7730 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7731 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7732 | bool needs_sched = true; |
| 7733 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7734 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7735 | io_ring_set_wakeup_flag(ctx); |
| 7736 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7737 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7738 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7739 | needs_sched = false; |
| 7740 | break; |
| 7741 | } |
| 7742 | if (io_sqring_entries(ctx)) { |
| 7743 | needs_sched = false; |
| 7744 | break; |
| 7745 | } |
| 7746 | } |
| 7747 | |
| 7748 | if (needs_sched) { |
| 7749 | mutex_unlock(&sqd->lock); |
| 7750 | schedule(); |
| 7751 | mutex_lock(&sqd->lock); |
| 7752 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7753 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7754 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7755 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7756 | |
| 7757 | finish_wait(&sqd->wait, &wait); |
| 7758 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7759 | } |
| 7760 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7761 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7762 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7763 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7764 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7765 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7766 | mutex_unlock(&sqd->lock); |
| 7767 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7768 | complete(&sqd->exited); |
| 7769 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7770 | } |
| 7771 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7772 | struct io_wait_queue { |
| 7773 | struct wait_queue_entry wq; |
| 7774 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7775 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7776 | unsigned nr_timeouts; |
| 7777 | }; |
| 7778 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7779 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7780 | { |
| 7781 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7782 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7783 | |
| 7784 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7785 | * 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] | 7786 | * started waiting. For timeouts, we always want to return to userspace, |
| 7787 | * regardless of event count. |
| 7788 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7789 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7793 | int wake_flags, void *key) |
| 7794 | { |
| 7795 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7796 | wq); |
| 7797 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7798 | /* |
| 7799 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7800 | * the task, and the next invocation will do it. |
| 7801 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7802 | 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] | 7803 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7804 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7805 | } |
| 7806 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7807 | static int io_run_task_work_sig(void) |
| 7808 | { |
| 7809 | if (io_run_task_work()) |
| 7810 | return 1; |
| 7811 | if (!signal_pending(current)) |
| 7812 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7813 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7814 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7815 | return -EINTR; |
| 7816 | } |
| 7817 | |
Jens Axboe | c12fa4a | 2023-08-01 08:39:47 -0600 | [diff] [blame] | 7818 | static bool current_pending_io(void) |
| 7819 | { |
| 7820 | struct io_uring_task *tctx = current->io_uring; |
| 7821 | |
| 7822 | if (!tctx) |
| 7823 | return false; |
| 7824 | return percpu_counter_read_positive(&tctx->inflight); |
| 7825 | } |
| 7826 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7827 | /* when returns >0, the caller should retry */ |
| 7828 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7829 | struct io_wait_queue *iowq, |
Pavel Begunkov | c3222fd | 2023-01-05 10:49:15 +0000 | [diff] [blame] | 7830 | ktime_t *timeout) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7831 | { |
Jens Axboe | c12fa4a | 2023-08-01 08:39:47 -0600 | [diff] [blame] | 7832 | int io_wait, ret; |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7833 | |
| 7834 | /* make sure we run task_work before checking for signals */ |
| 7835 | ret = io_run_task_work_sig(); |
| 7836 | if (ret || io_should_wake(iowq)) |
| 7837 | return ret; |
| 7838 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7839 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7840 | return 1; |
| 7841 | |
Andres Freund | f8307d8 | 2023-07-16 12:07:03 -0600 | [diff] [blame] | 7842 | /* |
Jens Axboe | c12fa4a | 2023-08-01 08:39:47 -0600 | [diff] [blame] | 7843 | * Mark us as being in io_wait if we have pending requests, so cpufreq |
| 7844 | * can take into account that the task is waiting for IO - turns out |
| 7845 | * to be important for low QD IO. |
Andres Freund | f8307d8 | 2023-07-16 12:07:03 -0600 | [diff] [blame] | 7846 | */ |
Jens Axboe | c12fa4a | 2023-08-01 08:39:47 -0600 | [diff] [blame] | 7847 | io_wait = current->in_iowait; |
| 7848 | if (current_pending_io()) |
| 7849 | current->in_iowait = 1; |
Andres Freund | f8307d8 | 2023-07-16 12:07:03 -0600 | [diff] [blame] | 7850 | ret = 1; |
Pavel Begunkov | c3222fd | 2023-01-05 10:49:15 +0000 | [diff] [blame] | 7851 | if (!schedule_hrtimeout(timeout, HRTIMER_MODE_ABS)) |
Andres Freund | f8307d8 | 2023-07-16 12:07:03 -0600 | [diff] [blame] | 7852 | ret = -ETIME; |
Jens Axboe | c12fa4a | 2023-08-01 08:39:47 -0600 | [diff] [blame] | 7853 | current->in_iowait = io_wait; |
Andres Freund | f8307d8 | 2023-07-16 12:07:03 -0600 | [diff] [blame] | 7854 | return ret; |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7855 | } |
| 7856 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7857 | /* |
| 7858 | * Wait until events become available, if we don't already have some. The |
| 7859 | * application must reap them itself, as they reside on the shared cq ring. |
| 7860 | */ |
| 7861 | 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] | 7862 | const sigset_t __user *sig, size_t sigsz, |
| 7863 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7864 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7865 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7866 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 7c83437 | 2022-02-21 05:49:30 -0700 | [diff] [blame] | 7867 | ktime_t timeout = KTIME_MAX; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7868 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7869 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7870 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7871 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7872 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7873 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7874 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7875 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7876 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7877 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7878 | if (uts) { |
| 7879 | struct timespec64 ts; |
| 7880 | |
| 7881 | if (get_timespec64(&ts, uts)) |
| 7882 | return -EFAULT; |
Jens Axboe | 7c83437 | 2022-02-21 05:49:30 -0700 | [diff] [blame] | 7883 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7884 | } |
| 7885 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7886 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7887 | #ifdef CONFIG_COMPAT |
| 7888 | if (in_compat_syscall()) |
| 7889 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7890 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7891 | else |
| 7892 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7893 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7894 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7895 | if (ret) |
| 7896 | return ret; |
| 7897 | } |
| 7898 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7899 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7900 | iowq.wq.private = current; |
| 7901 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7902 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7903 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7904 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7905 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7906 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7907 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7908 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7909 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7910 | ret = -EBUSY; |
| 7911 | break; |
| 7912 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7913 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7914 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | c3222fd | 2023-01-05 10:49:15 +0000 | [diff] [blame] | 7915 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7916 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7917 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7918 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7919 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7920 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7921 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7922 | 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] | 7923 | } |
| 7924 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7925 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7926 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7927 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7928 | |
| 7929 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7930 | kfree(table[i]); |
| 7931 | kfree(table); |
| 7932 | } |
| 7933 | |
| 7934 | static void **io_alloc_page_table(size_t size) |
| 7935 | { |
| 7936 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7937 | size_t init_size = size; |
| 7938 | void **table; |
| 7939 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7940 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7941 | if (!table) |
| 7942 | return NULL; |
| 7943 | |
| 7944 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7945 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7946 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7947 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7948 | if (!table[i]) { |
| 7949 | io_free_page_table(table, init_size); |
| 7950 | return NULL; |
| 7951 | } |
| 7952 | size -= this_size; |
| 7953 | } |
| 7954 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7955 | } |
| 7956 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7957 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7958 | { |
| 7959 | percpu_ref_exit(&ref_node->refs); |
| 7960 | kfree(ref_node); |
| 7961 | } |
| 7962 | |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7963 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
| 7964 | { |
| 7965 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7966 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7967 | unsigned long flags; |
| 7968 | bool first_add = false; |
Dylan Yudaken | 82cc338 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7969 | unsigned long delay = HZ; |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7970 | |
| 7971 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7972 | node->done = true; |
| 7973 | |
Dylan Yudaken | 82cc338 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7974 | /* if we are mid-quiesce then do not delay */ |
| 7975 | if (node->rsrc_data->quiesce) |
| 7976 | delay = 0; |
| 7977 | |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7978 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7979 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7980 | struct io_rsrc_node, node); |
| 7981 | /* recycle ref nodes in order */ |
| 7982 | if (!node->done) |
| 7983 | break; |
| 7984 | list_del(&node->node); |
| 7985 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7986 | } |
| 7987 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7988 | |
| 7989 | if (first_add) |
Dylan Yudaken | 82cc338 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7990 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7991 | } |
| 7992 | |
| 7993 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
| 7994 | { |
| 7995 | struct io_rsrc_node *ref_node; |
| 7996 | |
| 7997 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7998 | if (!ref_node) |
| 7999 | return NULL; |
| 8000 | |
| 8001 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 8002 | 0, GFP_KERNEL)) { |
| 8003 | kfree(ref_node); |
| 8004 | return NULL; |
| 8005 | } |
| 8006 | INIT_LIST_HEAD(&ref_node->node); |
| 8007 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 8008 | ref_node->done = false; |
| 8009 | return ref_node; |
| 8010 | } |
| 8011 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8012 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 8013 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 8014 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8015 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 8016 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 8017 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8018 | if (data_to_kill) { |
| 8019 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 8020 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8021 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 8022 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8023 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 8024 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 8025 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8026 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8027 | percpu_ref_kill(&rsrc_node->refs); |
| 8028 | ctx->rsrc_node = NULL; |
| 8029 | } |
| 8030 | |
| 8031 | if (!ctx->rsrc_node) { |
| 8032 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 8033 | ctx->rsrc_backup_node = NULL; |
| 8034 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8037 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8038 | { |
| 8039 | if (ctx->rsrc_backup_node) |
| 8040 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8041 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8042 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 8043 | } |
| 8044 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8045 | 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] | 8046 | { |
| 8047 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8048 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 8049 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8050 | if (data->quiesce) |
| 8051 | return -ENXIO; |
| 8052 | |
| 8053 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 8054 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8055 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8056 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 8057 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8058 | io_rsrc_node_switch(ctx, data); |
| 8059 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8060 | /* kill initial ref, already quiesced if zero */ |
| 8061 | if (atomic_dec_and_test(&data->refs)) |
| 8062 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 8063 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8064 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 8065 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 8066 | if (!ret) { |
| 8067 | mutex_lock(&ctx->uring_lock); |
Dylan Yudaken | 0d773aa | 2022-02-22 08:17:51 -0800 | [diff] [blame] | 8068 | if (atomic_read(&data->refs) > 0) { |
| 8069 | /* |
| 8070 | * it has been revived by another thread while |
| 8071 | * we were unlocked |
| 8072 | */ |
| 8073 | mutex_unlock(&ctx->uring_lock); |
| 8074 | } else { |
| 8075 | break; |
| 8076 | } |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 8077 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8078 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8079 | atomic_inc(&data->refs); |
| 8080 | /* wait for all works potentially completing data->done */ |
| 8081 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 8082 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8083 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8084 | ret = io_run_task_work_sig(); |
| 8085 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 8086 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8087 | data->quiesce = false; |
| 8088 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8089 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8090 | } |
| 8091 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8092 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 8093 | { |
| 8094 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 8095 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 8096 | |
| 8097 | return &data->tags[table_idx][off]; |
| 8098 | } |
| 8099 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8100 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 8101 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8102 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 8103 | |
| 8104 | if (data->tags) |
| 8105 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8106 | kfree(data); |
| 8107 | } |
| 8108 | |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8109 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 8110 | u64 __user *utags, unsigned nr, |
| 8111 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8112 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8113 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8114 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8115 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8116 | |
| 8117 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 8118 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8119 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8120 | 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] | 8121 | if (!data->tags) { |
| 8122 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8123 | return -ENOMEM; |
| 8124 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8125 | |
| 8126 | data->nr = nr; |
| 8127 | data->ctx = ctx; |
| 8128 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8129 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8130 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8131 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 8132 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 8133 | |
| 8134 | if (copy_from_user(tag_slot, &utags[i], |
| 8135 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8136 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8137 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8138 | } |
| 8139 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8140 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8141 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8142 | *pdata = data; |
| 8143 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8144 | fail: |
| 8145 | io_rsrc_data_free(data); |
| 8146 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8147 | } |
| 8148 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8149 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 8150 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 8151 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 8152 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8153 | return !!table->files; |
| 8154 | } |
| 8155 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8156 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8157 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8158 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8159 | table->files = NULL; |
| 8160 | } |
| 8161 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8162 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 8163 | { |
| 8164 | #if defined(CONFIG_UNIX) |
| 8165 | if (ctx->ring_sock) { |
| 8166 | struct sock *sock = ctx->ring_sock->sk; |
| 8167 | struct sk_buff *skb; |
| 8168 | |
| 8169 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 8170 | kfree_skb(skb); |
| 8171 | } |
| 8172 | #else |
| 8173 | int i; |
| 8174 | |
| 8175 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8176 | struct file *file; |
| 8177 | |
| 8178 | file = io_file_from_index(ctx, i); |
| 8179 | if (file) |
| 8180 | fput(file); |
| 8181 | } |
| 8182 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8183 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8184 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 8185 | ctx->file_data = NULL; |
| 8186 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8187 | } |
| 8188 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8189 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 8190 | { |
Pavel Begunkov | b1e7cad | 2022-06-13 06:32:44 +0100 | [diff] [blame] | 8191 | unsigned nr = ctx->nr_user_files; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8192 | int ret; |
| 8193 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8194 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8195 | return -ENXIO; |
Pavel Begunkov | b1e7cad | 2022-06-13 06:32:44 +0100 | [diff] [blame] | 8196 | |
| 8197 | /* |
| 8198 | * Quiesce may unlock ->uring_lock, and while it's not held |
| 8199 | * prevent new requests using the table. |
| 8200 | */ |
| 8201 | ctx->nr_user_files = 0; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8202 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
Pavel Begunkov | b1e7cad | 2022-06-13 06:32:44 +0100 | [diff] [blame] | 8203 | ctx->nr_user_files = nr; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8204 | if (!ret) |
| 8205 | __io_sqe_files_unregister(ctx); |
| 8206 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8207 | } |
| 8208 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8209 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8210 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8211 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8212 | WARN_ON_ONCE(sqd->thread == current); |
| 8213 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8214 | /* |
| 8215 | * Do the dance but not conditional clear_bit() because it'd race with |
| 8216 | * other threads incrementing park_pending and setting the bit. |
| 8217 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8218 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8219 | if (atomic_dec_return(&sqd->park_pending)) |
| 8220 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8221 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8222 | } |
| 8223 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 8224 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8225 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8226 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8227 | WARN_ON_ONCE(sqd->thread == current); |
| 8228 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8229 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8230 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8231 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8232 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 8233 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8234 | } |
| 8235 | |
| 8236 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 8237 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8238 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 8239 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8240 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8241 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 8242 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8243 | if (sqd->thread) |
| 8244 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8245 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8246 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8247 | } |
| 8248 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8249 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8250 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8251 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8252 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 8253 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8254 | io_sq_thread_stop(sqd); |
| 8255 | kfree(sqd); |
| 8256 | } |
| 8257 | } |
| 8258 | |
| 8259 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 8260 | { |
| 8261 | struct io_sq_data *sqd = ctx->sq_data; |
| 8262 | |
| 8263 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8264 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8265 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8266 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8267 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8268 | |
| 8269 | io_put_sq_data(sqd); |
| 8270 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8271 | } |
| 8272 | } |
| 8273 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8274 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 8275 | { |
| 8276 | struct io_ring_ctx *ctx_attach; |
| 8277 | struct io_sq_data *sqd; |
| 8278 | struct fd f; |
| 8279 | |
| 8280 | f = fdget(p->wq_fd); |
| 8281 | if (!f.file) |
| 8282 | return ERR_PTR(-ENXIO); |
| 8283 | if (f.file->f_op != &io_uring_fops) { |
| 8284 | fdput(f); |
| 8285 | return ERR_PTR(-EINVAL); |
| 8286 | } |
| 8287 | |
| 8288 | ctx_attach = f.file->private_data; |
| 8289 | sqd = ctx_attach->sq_data; |
| 8290 | if (!sqd) { |
| 8291 | fdput(f); |
| 8292 | return ERR_PTR(-EINVAL); |
| 8293 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8294 | if (sqd->task_tgid != current->tgid) { |
| 8295 | fdput(f); |
| 8296 | return ERR_PTR(-EPERM); |
| 8297 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8298 | |
| 8299 | refcount_inc(&sqd->refs); |
| 8300 | fdput(f); |
| 8301 | return sqd; |
| 8302 | } |
| 8303 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8304 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 8305 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8306 | { |
| 8307 | struct io_sq_data *sqd; |
| 8308 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8309 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8310 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 8311 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8312 | if (!IS_ERR(sqd)) { |
| 8313 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8314 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8315 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8316 | /* fall through for EPERM case, setup new sqd/task */ |
| 8317 | if (PTR_ERR(sqd) != -EPERM) |
| 8318 | return sqd; |
| 8319 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8320 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8321 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 8322 | if (!sqd) |
| 8323 | return ERR_PTR(-ENOMEM); |
| 8324 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8325 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8326 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8327 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8328 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8329 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8330 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8331 | return sqd; |
| 8332 | } |
| 8333 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8334 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8335 | /* |
| 8336 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 8337 | * the io_uring can be safely unregistered on process exit, even if we have |
| 8338 | * loops in the file referencing. |
| 8339 | */ |
| 8340 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 8341 | { |
| 8342 | struct sock *sk = ctx->ring_sock->sk; |
| 8343 | struct scm_fp_list *fpl; |
| 8344 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8345 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8346 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8347 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 8348 | if (!fpl) |
| 8349 | return -ENOMEM; |
| 8350 | |
| 8351 | skb = alloc_skb(0, GFP_KERNEL); |
| 8352 | if (!skb) { |
| 8353 | kfree(fpl); |
| 8354 | return -ENOMEM; |
| 8355 | } |
| 8356 | |
| 8357 | skb->sk = sk; |
Pavel Begunkov | 813d8fe | 2022-10-16 22:42:54 +0100 | [diff] [blame] | 8358 | skb->scm_io_uring = 1; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8359 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8360 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8361 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8362 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8363 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8364 | |
| 8365 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8366 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8367 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8368 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 8369 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8370 | } |
| 8371 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8372 | if (nr_files) { |
| 8373 | fpl->max = SCM_MAX_FD; |
| 8374 | fpl->count = nr_files; |
| 8375 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8376 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8377 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 8378 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8379 | |
Pavel Begunkov | 285f5d7 | 2022-04-06 12:43:58 +0100 | [diff] [blame] | 8380 | for (i = 0; i < nr; i++) { |
| 8381 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8382 | |
| 8383 | if (file) |
| 8384 | fput(file); |
| 8385 | } |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8386 | } else { |
| 8387 | kfree_skb(skb); |
Pavel Begunkov | 0853bd6 | 2022-03-25 16:36:31 +0000 | [diff] [blame] | 8388 | free_uid(fpl->user); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8389 | kfree(fpl); |
| 8390 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8391 | |
| 8392 | return 0; |
| 8393 | } |
| 8394 | |
| 8395 | /* |
| 8396 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8397 | * causes regular reference counting to break down. We rely on the UNIX |
| 8398 | * garbage collection to take care of this problem for us. |
| 8399 | */ |
| 8400 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8401 | { |
| 8402 | unsigned left, total; |
| 8403 | int ret = 0; |
| 8404 | |
| 8405 | total = 0; |
| 8406 | left = ctx->nr_user_files; |
| 8407 | while (left) { |
| 8408 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8409 | |
| 8410 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8411 | if (ret) |
| 8412 | break; |
| 8413 | left -= this_files; |
| 8414 | total += this_files; |
| 8415 | } |
| 8416 | |
| 8417 | if (!ret) |
| 8418 | return 0; |
| 8419 | |
| 8420 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8421 | struct file *file = io_file_from_index(ctx, total); |
| 8422 | |
| 8423 | if (file) |
| 8424 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8425 | total++; |
| 8426 | } |
| 8427 | |
| 8428 | return ret; |
| 8429 | } |
| 8430 | #else |
| 8431 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8432 | { |
| 8433 | return 0; |
| 8434 | } |
| 8435 | #endif |
| 8436 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8437 | 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] | 8438 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8439 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8440 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8441 | struct sock *sock = ctx->ring_sock->sk; |
| 8442 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8443 | struct sk_buff *skb; |
| 8444 | int i; |
| 8445 | |
| 8446 | __skb_queue_head_init(&list); |
| 8447 | |
| 8448 | /* |
| 8449 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8450 | * remove this entry and rearrange the file array. |
| 8451 | */ |
| 8452 | skb = skb_dequeue(head); |
| 8453 | while (skb) { |
| 8454 | struct scm_fp_list *fp; |
| 8455 | |
| 8456 | fp = UNIXCB(skb).fp; |
| 8457 | for (i = 0; i < fp->count; i++) { |
| 8458 | int left; |
| 8459 | |
| 8460 | if (fp->fp[i] != file) |
| 8461 | continue; |
| 8462 | |
| 8463 | unix_notinflight(fp->user, fp->fp[i]); |
| 8464 | left = fp->count - 1 - i; |
| 8465 | if (left) { |
| 8466 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8467 | left * sizeof(struct file *)); |
| 8468 | } |
| 8469 | fp->count--; |
| 8470 | if (!fp->count) { |
| 8471 | kfree_skb(skb); |
| 8472 | skb = NULL; |
| 8473 | } else { |
| 8474 | __skb_queue_tail(&list, skb); |
| 8475 | } |
| 8476 | fput(file); |
| 8477 | file = NULL; |
| 8478 | break; |
| 8479 | } |
| 8480 | |
| 8481 | if (!file) |
| 8482 | break; |
| 8483 | |
| 8484 | __skb_queue_tail(&list, skb); |
| 8485 | |
| 8486 | skb = skb_dequeue(head); |
| 8487 | } |
| 8488 | |
| 8489 | if (skb_peek(&list)) { |
| 8490 | spin_lock_irq(&head->lock); |
| 8491 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8492 | __skb_queue_tail(head, skb); |
| 8493 | spin_unlock_irq(&head->lock); |
| 8494 | } |
| 8495 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8496 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8497 | #endif |
| 8498 | } |
| 8499 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8500 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8501 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8502 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8503 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8504 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8505 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8506 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8507 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8508 | |
| 8509 | if (prsrc->tag) { |
| 8510 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8511 | |
| 8512 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8513 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b850d6d | 2022-08-29 14:30:13 +0100 | [diff] [blame] | 8514 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8515 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8516 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8517 | io_cqring_ev_posted(ctx); |
| 8518 | io_ring_submit_unlock(ctx, lock_ring); |
| 8519 | } |
| 8520 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8521 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8522 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8523 | } |
| 8524 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8525 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8526 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8527 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8528 | } |
| 8529 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8530 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8531 | { |
| 8532 | struct io_ring_ctx *ctx; |
| 8533 | struct llist_node *node; |
| 8534 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8535 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8536 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8537 | |
| 8538 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8539 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8540 | struct llist_node *next = node->next; |
| 8541 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8542 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8543 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8544 | node = next; |
| 8545 | } |
| 8546 | } |
| 8547 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8548 | 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] | 8549 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8550 | { |
| 8551 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8552 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8553 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8554 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8555 | |
| 8556 | if (ctx->file_data) |
| 8557 | return -EBUSY; |
| 8558 | if (!nr_args) |
| 8559 | return -EINVAL; |
| 8560 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8561 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8562 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8563 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8564 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8565 | if (ret) |
| 8566 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8567 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8568 | &ctx->file_data); |
| 8569 | if (ret) |
| 8570 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8571 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8572 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8573 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8574 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8575 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8576 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8577 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8578 | ret = -EFAULT; |
| 8579 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8580 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8581 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8582 | if (fd == -1) { |
| 8583 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8584 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8585 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8586 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8587 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8588 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8589 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8590 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8591 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8592 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8593 | |
| 8594 | /* |
| 8595 | * Don't allow io_uring instances to be registered. If UNIX |
| 8596 | * isn't enabled, then this causes a reference cycle and this |
| 8597 | * instance can never get freed. If UNIX is enabled we'll |
| 8598 | * handle it just fine, but there's still no point in allowing |
| 8599 | * a ring fd as it doesn't support regular read/write anyway. |
| 8600 | */ |
| 8601 | if (file->f_op == &io_uring_fops) { |
| 8602 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8603 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8604 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8605 | 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] | 8606 | } |
| 8607 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8608 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8609 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8610 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8611 | return ret; |
| 8612 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8613 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8614 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8615 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8616 | out_fput: |
| 8617 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8618 | file = io_file_from_index(ctx, i); |
| 8619 | if (file) |
| 8620 | fput(file); |
| 8621 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8622 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8623 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8624 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8625 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8626 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8627 | return ret; |
| 8628 | } |
| 8629 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8630 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8631 | int index) |
| 8632 | { |
| 8633 | #if defined(CONFIG_UNIX) |
| 8634 | struct sock *sock = ctx->ring_sock->sk; |
| 8635 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8636 | struct sk_buff *skb; |
| 8637 | |
| 8638 | /* |
| 8639 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8640 | * file set. If there's no room, fall back to allocating a new skb |
| 8641 | * and filling it in. |
| 8642 | */ |
| 8643 | spin_lock_irq(&head->lock); |
| 8644 | skb = skb_peek(head); |
| 8645 | if (skb) { |
| 8646 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8647 | |
| 8648 | if (fpl->count < SCM_MAX_FD) { |
| 8649 | __skb_unlink(skb, head); |
| 8650 | spin_unlock_irq(&head->lock); |
| 8651 | fpl->fp[fpl->count] = get_file(file); |
| 8652 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8653 | fpl->count++; |
| 8654 | spin_lock_irq(&head->lock); |
| 8655 | __skb_queue_head(head, skb); |
| 8656 | } else { |
| 8657 | skb = NULL; |
| 8658 | } |
| 8659 | } |
| 8660 | spin_unlock_irq(&head->lock); |
| 8661 | |
| 8662 | if (skb) { |
| 8663 | fput(file); |
| 8664 | return 0; |
| 8665 | } |
| 8666 | |
| 8667 | return __io_sqe_files_scm(ctx, 1, index); |
| 8668 | #else |
| 8669 | return 0; |
| 8670 | #endif |
| 8671 | } |
| 8672 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8673 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8674 | struct io_rsrc_node *node, void *rsrc) |
| 8675 | { |
Pavel Begunkov | 5218d5c | 2022-04-07 14:05:04 +0100 | [diff] [blame] | 8676 | u64 *tag_slot = io_get_tag_slot(data, idx); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8677 | struct io_rsrc_put *prsrc; |
| 8678 | |
| 8679 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8680 | if (!prsrc) |
| 8681 | return -ENOMEM; |
| 8682 | |
Pavel Begunkov | 5218d5c | 2022-04-07 14:05:04 +0100 | [diff] [blame] | 8683 | prsrc->tag = *tag_slot; |
| 8684 | *tag_slot = 0; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8685 | prsrc->rsrc = rsrc; |
| 8686 | list_add(&prsrc->list, &node->rsrc_list); |
| 8687 | return 0; |
| 8688 | } |
| 8689 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8690 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8691 | unsigned int issue_flags, u32 slot_index) |
| 8692 | { |
| 8693 | struct io_ring_ctx *ctx = req->ctx; |
| 8694 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8695 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8696 | struct io_fixed_file *file_slot; |
| 8697 | int ret = -EBADF; |
| 8698 | |
| 8699 | io_ring_submit_lock(ctx, !force_nonblock); |
| 8700 | if (file->f_op == &io_uring_fops) |
| 8701 | goto err; |
| 8702 | ret = -ENXIO; |
| 8703 | if (!ctx->file_data) |
| 8704 | goto err; |
| 8705 | ret = -EINVAL; |
| 8706 | if (slot_index >= ctx->nr_user_files) |
| 8707 | goto err; |
| 8708 | |
| 8709 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8710 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8711 | |
| 8712 | if (file_slot->file_ptr) { |
| 8713 | struct file *old_file; |
| 8714 | |
| 8715 | ret = io_rsrc_node_switch_start(ctx); |
| 8716 | if (ret) |
| 8717 | goto err; |
| 8718 | |
| 8719 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8720 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8721 | ctx->rsrc_node, old_file); |
| 8722 | if (ret) |
| 8723 | goto err; |
| 8724 | file_slot->file_ptr = 0; |
| 8725 | needs_switch = true; |
| 8726 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8727 | |
| 8728 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8729 | io_fixed_file_set(file_slot, file); |
| 8730 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8731 | if (ret) { |
| 8732 | file_slot->file_ptr = 0; |
| 8733 | goto err; |
| 8734 | } |
| 8735 | |
| 8736 | ret = 0; |
| 8737 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8738 | if (needs_switch) |
| 8739 | io_rsrc_node_switch(ctx, ctx->file_data); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8740 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 8741 | if (ret) |
| 8742 | fput(file); |
| 8743 | return ret; |
| 8744 | } |
| 8745 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8746 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8747 | { |
| 8748 | unsigned int offset = req->close.file_slot - 1; |
| 8749 | struct io_ring_ctx *ctx = req->ctx; |
| 8750 | struct io_fixed_file *file_slot; |
| 8751 | struct file *file; |
Pavel Begunkov | b8ed0f7 | 2022-04-07 14:05:05 +0100 | [diff] [blame] | 8752 | int ret; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8753 | |
| 8754 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8755 | ret = -ENXIO; |
| 8756 | if (unlikely(!ctx->file_data)) |
| 8757 | goto out; |
| 8758 | ret = -EINVAL; |
| 8759 | if (offset >= ctx->nr_user_files) |
| 8760 | goto out; |
| 8761 | ret = io_rsrc_node_switch_start(ctx); |
| 8762 | if (ret) |
| 8763 | goto out; |
| 8764 | |
Pavel Begunkov | b8ed0f7 | 2022-04-07 14:05:05 +0100 | [diff] [blame] | 8765 | offset = array_index_nospec(offset, ctx->nr_user_files); |
| 8766 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8767 | ret = -EBADF; |
| 8768 | if (!file_slot->file_ptr) |
| 8769 | goto out; |
| 8770 | |
| 8771 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8772 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8773 | if (ret) |
| 8774 | goto out; |
| 8775 | |
| 8776 | file_slot->file_ptr = 0; |
| 8777 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8778 | ret = 0; |
| 8779 | out: |
| 8780 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 8781 | return ret; |
| 8782 | } |
| 8783 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8784 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8785 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8786 | unsigned nr_args) |
| 8787 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8788 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8789 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8790 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8791 | struct io_fixed_file *file_slot; |
| 8792 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8793 | int fd, i, err = 0; |
| 8794 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8795 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8796 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8797 | if (!ctx->file_data) |
| 8798 | return -ENXIO; |
| 8799 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8800 | return -EINVAL; |
| 8801 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8802 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8803 | u64 tag = 0; |
| 8804 | |
| 8805 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8806 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8807 | err = -EFAULT; |
| 8808 | break; |
| 8809 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8810 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8811 | err = -EINVAL; |
| 8812 | break; |
| 8813 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8814 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8815 | continue; |
| 8816 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8817 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8818 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8819 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8820 | if (file_slot->file_ptr) { |
| 8821 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b8ed0f7 | 2022-04-07 14:05:05 +0100 | [diff] [blame] | 8822 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8823 | if (err) |
| 8824 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8825 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8826 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8827 | } |
| 8828 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8829 | file = fget(fd); |
| 8830 | if (!file) { |
| 8831 | err = -EBADF; |
| 8832 | break; |
| 8833 | } |
| 8834 | /* |
| 8835 | * Don't allow io_uring instances to be registered. If |
| 8836 | * UNIX isn't enabled, then this causes a reference |
| 8837 | * cycle and this instance can never get freed. If UNIX |
| 8838 | * is enabled we'll handle it just fine, but there's |
| 8839 | * still no point in allowing a ring fd as it doesn't |
| 8840 | * support regular read/write anyway. |
| 8841 | */ |
| 8842 | if (file->f_op == &io_uring_fops) { |
| 8843 | fput(file); |
| 8844 | err = -EBADF; |
| 8845 | break; |
| 8846 | } |
Pavel Begunkov | 50c981b | 2022-04-06 12:43:57 +0100 | [diff] [blame] | 8847 | *io_get_tag_slot(data, i) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8848 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8849 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8850 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8851 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8852 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8853 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8854 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8855 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8856 | } |
| 8857 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8858 | if (needs_switch) |
| 8859 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8860 | return done ? done : err; |
| 8861 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8862 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8863 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8864 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8865 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8866 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8867 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8868 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8869 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8870 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8871 | hash = ctx->hash_map; |
| 8872 | if (!hash) { |
| 8873 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8874 | if (!hash) { |
| 8875 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8876 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8877 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8878 | refcount_set(&hash->refs, 1); |
| 8879 | init_waitqueue_head(&hash->wait); |
| 8880 | ctx->hash_map = hash; |
| 8881 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8882 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8883 | |
| 8884 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8885 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8886 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8887 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8888 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8889 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8890 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8891 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8892 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8893 | } |
| 8894 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8895 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 8896 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8897 | { |
| 8898 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8899 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8900 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8901 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8902 | if (unlikely(!tctx)) |
| 8903 | return -ENOMEM; |
| 8904 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8905 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8906 | if (unlikely(ret)) { |
| 8907 | kfree(tctx); |
| 8908 | return ret; |
| 8909 | } |
| 8910 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8911 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8912 | if (IS_ERR(tctx->io_wq)) { |
| 8913 | ret = PTR_ERR(tctx->io_wq); |
| 8914 | percpu_counter_destroy(&tctx->inflight); |
| 8915 | kfree(tctx); |
| 8916 | return ret; |
| 8917 | } |
| 8918 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8919 | xa_init(&tctx->xa); |
| 8920 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8921 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8922 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8923 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8924 | spin_lock_init(&tctx->task_lock); |
| 8925 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8926 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8927 | return 0; |
| 8928 | } |
| 8929 | |
| 8930 | void __io_uring_free(struct task_struct *tsk) |
| 8931 | { |
| 8932 | struct io_uring_task *tctx = tsk->io_uring; |
| 8933 | |
| 8934 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8935 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8936 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8937 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8938 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8939 | kfree(tctx); |
| 8940 | tsk->io_uring = NULL; |
| 8941 | } |
| 8942 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8943 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8944 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8945 | { |
| 8946 | int ret; |
| 8947 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8948 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8949 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8950 | IORING_SETUP_ATTACH_WQ) { |
| 8951 | struct fd f; |
| 8952 | |
| 8953 | f = fdget(p->wq_fd); |
| 8954 | if (!f.file) |
| 8955 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8956 | if (f.file->f_op != &io_uring_fops) { |
| 8957 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8958 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8959 | } |
| 8960 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8961 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8962 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8963 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8964 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8965 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8966 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8967 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8968 | if (IS_ERR(sqd)) { |
| 8969 | ret = PTR_ERR(sqd); |
| 8970 | goto err; |
| 8971 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8972 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8973 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8974 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8975 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8976 | if (!ctx->sq_thread_idle) |
| 8977 | ctx->sq_thread_idle = HZ; |
| 8978 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8979 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8980 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8981 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8982 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8983 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8984 | io_sq_thread_unpark(sqd); |
| 8985 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8986 | if (ret < 0) |
| 8987 | goto err; |
| 8988 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8989 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8990 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8991 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8992 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8993 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8994 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8995 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8996 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8997 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8998 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8999 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9000 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9001 | |
| 9002 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 9003 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 9004 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 9005 | if (IS_ERR(tsk)) { |
| 9006 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 9007 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9008 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 9009 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 9010 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 9011 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 9012 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9013 | if (ret) |
| 9014 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9015 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 9016 | /* Can't have SQ_AFF without SQPOLL */ |
| 9017 | ret = -EINVAL; |
| 9018 | goto err; |
| 9019 | } |
| 9020 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9021 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 9022 | err_sqpoll: |
| 9023 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9024 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9025 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9026 | return ret; |
| 9027 | } |
| 9028 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9029 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 9030 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9031 | { |
| 9032 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 9033 | } |
| 9034 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9035 | static inline int __io_account_mem(struct user_struct *user, |
| 9036 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9037 | { |
| 9038 | unsigned long page_limit, cur_pages, new_pages; |
| 9039 | |
| 9040 | /* Don't allow more pages than we can safely lock */ |
| 9041 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 9042 | |
| 9043 | do { |
| 9044 | cur_pages = atomic_long_read(&user->locked_vm); |
| 9045 | new_pages = cur_pages + nr_pages; |
| 9046 | if (new_pages > page_limit) |
| 9047 | return -ENOMEM; |
| 9048 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 9049 | new_pages) != cur_pages); |
| 9050 | |
| 9051 | return 0; |
| 9052 | } |
| 9053 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9054 | 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] | 9055 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9056 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9057 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9058 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9059 | if (ctx->mm_account) |
| 9060 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9061 | } |
| 9062 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9063 | 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] | 9064 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9065 | int ret; |
| 9066 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9067 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9068 | ret = __io_account_mem(ctx->user, nr_pages); |
| 9069 | if (ret) |
| 9070 | return ret; |
| 9071 | } |
| 9072 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9073 | if (ctx->mm_account) |
| 9074 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9075 | |
| 9076 | return 0; |
| 9077 | } |
| 9078 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9079 | static void io_mem_free(void *ptr) |
| 9080 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 9081 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9082 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 9083 | if (!ptr) |
| 9084 | return; |
| 9085 | |
| 9086 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9087 | if (put_page_testzero(page)) |
| 9088 | free_compound_page(page); |
| 9089 | } |
| 9090 | |
| 9091 | static void *io_mem_alloc(size_t size) |
| 9092 | { |
Shakeel Butt | 246dfbc | 2022-01-24 21:17:36 -0800 | [diff] [blame] | 9093 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9094 | |
Shakeel Butt | 246dfbc | 2022-01-24 21:17:36 -0800 | [diff] [blame] | 9095 | return (void *) __get_free_pages(gfp, get_order(size)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9096 | } |
| 9097 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9098 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 9099 | size_t *sq_offset) |
| 9100 | { |
| 9101 | struct io_rings *rings; |
| 9102 | size_t off, sq_array_size; |
| 9103 | |
| 9104 | off = struct_size(rings, cqes, cq_entries); |
| 9105 | if (off == SIZE_MAX) |
| 9106 | return SIZE_MAX; |
| 9107 | |
| 9108 | #ifdef CONFIG_SMP |
| 9109 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 9110 | if (off == 0) |
| 9111 | return SIZE_MAX; |
| 9112 | #endif |
| 9113 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 9114 | if (sq_offset) |
| 9115 | *sq_offset = off; |
| 9116 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9117 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 9118 | if (sq_array_size == SIZE_MAX) |
| 9119 | return SIZE_MAX; |
| 9120 | |
| 9121 | if (check_add_overflow(off, sq_array_size, &off)) |
| 9122 | return SIZE_MAX; |
| 9123 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9124 | return off; |
| 9125 | } |
| 9126 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9127 | 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] | 9128 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9129 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 9130 | unsigned int i; |
| 9131 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9132 | if (imu != ctx->dummy_ubuf) { |
| 9133 | for (i = 0; i < imu->nr_bvecs; i++) |
| 9134 | unpin_user_page(imu->bvec[i].bv_page); |
| 9135 | if (imu->acct_pages) |
| 9136 | io_unaccount_mem(ctx, imu->acct_pages); |
| 9137 | kvfree(imu); |
| 9138 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9139 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 9140 | } |
| 9141 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9142 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 9143 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9144 | io_buffer_unmap(ctx, &prsrc->buf); |
| 9145 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9146 | } |
| 9147 | |
| 9148 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9149 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 9150 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9151 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 9152 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 9153 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9154 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9155 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9156 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9157 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9158 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9159 | } |
| 9160 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9161 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 9162 | { |
Pavel Begunkov | 91f5a60 | 2022-06-13 06:30:06 +0100 | [diff] [blame] | 9163 | unsigned nr = ctx->nr_user_bufs; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9164 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9165 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9166 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9167 | return -ENXIO; |
| 9168 | |
Pavel Begunkov | 91f5a60 | 2022-06-13 06:30:06 +0100 | [diff] [blame] | 9169 | /* |
| 9170 | * Quiesce may unlock ->uring_lock, and while it's not held |
| 9171 | * prevent new requests using the table. |
| 9172 | */ |
| 9173 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9174 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
Pavel Begunkov | 91f5a60 | 2022-06-13 06:30:06 +0100 | [diff] [blame] | 9175 | ctx->nr_user_bufs = nr; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9176 | if (!ret) |
| 9177 | __io_sqe_buffers_unregister(ctx); |
| 9178 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9179 | } |
| 9180 | |
| 9181 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 9182 | void __user *arg, unsigned index) |
| 9183 | { |
| 9184 | struct iovec __user *src; |
| 9185 | |
| 9186 | #ifdef CONFIG_COMPAT |
| 9187 | if (ctx->compat) { |
| 9188 | struct compat_iovec __user *ciovs; |
| 9189 | struct compat_iovec ciov; |
| 9190 | |
| 9191 | ciovs = (struct compat_iovec __user *) arg; |
| 9192 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 9193 | return -EFAULT; |
| 9194 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 9195 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9196 | dst->iov_len = ciov.iov_len; |
| 9197 | return 0; |
| 9198 | } |
| 9199 | #endif |
| 9200 | src = (struct iovec __user *) arg; |
| 9201 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 9202 | return -EFAULT; |
| 9203 | return 0; |
| 9204 | } |
| 9205 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9206 | /* |
| 9207 | * Not super efficient, but this is just a registration time. And we do cache |
| 9208 | * the last compound head, so generally we'll only do a full search if we don't |
| 9209 | * match that one. |
| 9210 | * |
| 9211 | * We check if the given compound head page has already been accounted, to |
| 9212 | * avoid double accounting it. This allows us to account the full size of the |
| 9213 | * page, not just the constituent pages of a huge page. |
| 9214 | */ |
| 9215 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 9216 | int nr_pages, struct page *hpage) |
| 9217 | { |
| 9218 | int i, j; |
| 9219 | |
| 9220 | /* check current page array */ |
| 9221 | for (i = 0; i < nr_pages; i++) { |
| 9222 | if (!PageCompound(pages[i])) |
| 9223 | continue; |
| 9224 | if (compound_head(pages[i]) == hpage) |
| 9225 | return true; |
| 9226 | } |
| 9227 | |
| 9228 | /* check previously registered pages */ |
| 9229 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9230 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9231 | |
| 9232 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 9233 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 9234 | continue; |
| 9235 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 9236 | return true; |
| 9237 | } |
| 9238 | } |
| 9239 | |
| 9240 | return false; |
| 9241 | } |
| 9242 | |
| 9243 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 9244 | int nr_pages, struct io_mapped_ubuf *imu, |
| 9245 | struct page **last_hpage) |
| 9246 | { |
| 9247 | int i, ret; |
| 9248 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 9249 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9250 | for (i = 0; i < nr_pages; i++) { |
| 9251 | if (!PageCompound(pages[i])) { |
| 9252 | imu->acct_pages++; |
| 9253 | } else { |
| 9254 | struct page *hpage; |
| 9255 | |
| 9256 | hpage = compound_head(pages[i]); |
| 9257 | if (hpage == *last_hpage) |
| 9258 | continue; |
| 9259 | *last_hpage = hpage; |
| 9260 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 9261 | continue; |
| 9262 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 9263 | } |
| 9264 | } |
| 9265 | |
| 9266 | if (!imu->acct_pages) |
| 9267 | return 0; |
| 9268 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9269 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9270 | if (ret) |
| 9271 | imu->acct_pages = 0; |
| 9272 | return ret; |
| 9273 | } |
| 9274 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9275 | 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] | 9276 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9277 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9278 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9279 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9280 | struct vm_area_struct **vmas = NULL; |
| 9281 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9282 | unsigned long off, start, end, ubuf; |
| 9283 | size_t size; |
| 9284 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9285 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9286 | if (!iov->iov_base) { |
| 9287 | *pimu = ctx->dummy_ubuf; |
| 9288 | return 0; |
| 9289 | } |
| 9290 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9291 | ubuf = (unsigned long) iov->iov_base; |
| 9292 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 9293 | start = ubuf >> PAGE_SHIFT; |
| 9294 | nr_pages = end - start; |
| 9295 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9296 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9297 | ret = -ENOMEM; |
| 9298 | |
| 9299 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 9300 | if (!pages) |
| 9301 | goto done; |
| 9302 | |
| 9303 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 9304 | GFP_KERNEL); |
| 9305 | if (!vmas) |
| 9306 | goto done; |
| 9307 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9308 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 9309 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9310 | goto done; |
| 9311 | |
| 9312 | ret = 0; |
| 9313 | mmap_read_lock(current->mm); |
| 9314 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 9315 | pages, vmas); |
| 9316 | if (pret == nr_pages) { |
Pavel Begunkov | dde0d0d | 2023-03-06 13:21:40 -0700 | [diff] [blame] | 9317 | struct file *file = vmas[0]->vm_file; |
| 9318 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9319 | /* don't support file backed memory */ |
| 9320 | for (i = 0; i < nr_pages; i++) { |
Pavel Begunkov | dde0d0d | 2023-03-06 13:21:40 -0700 | [diff] [blame] | 9321 | if (vmas[i]->vm_file != file) { |
| 9322 | ret = -EINVAL; |
| 9323 | break; |
| 9324 | } |
| 9325 | if (!file) |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 9326 | continue; |
Pavel Begunkov | dde0d0d | 2023-03-06 13:21:40 -0700 | [diff] [blame] | 9327 | if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file)) { |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9328 | ret = -EOPNOTSUPP; |
| 9329 | break; |
| 9330 | } |
| 9331 | } |
| 9332 | } else { |
| 9333 | ret = pret < 0 ? pret : -EFAULT; |
| 9334 | } |
| 9335 | mmap_read_unlock(current->mm); |
| 9336 | if (ret) { |
| 9337 | /* |
| 9338 | * if we did partial map, or found file backed vmas, |
| 9339 | * release any pages we did get |
| 9340 | */ |
| 9341 | if (pret > 0) |
| 9342 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9343 | goto done; |
| 9344 | } |
| 9345 | |
| 9346 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 9347 | if (ret) { |
| 9348 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9349 | goto done; |
| 9350 | } |
| 9351 | |
| 9352 | off = ubuf & ~PAGE_MASK; |
| 9353 | size = iov->iov_len; |
| 9354 | for (i = 0; i < nr_pages; i++) { |
| 9355 | size_t vec_len; |
| 9356 | |
| 9357 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 9358 | imu->bvec[i].bv_page = pages[i]; |
| 9359 | imu->bvec[i].bv_len = vec_len; |
| 9360 | imu->bvec[i].bv_offset = off; |
| 9361 | off = 0; |
| 9362 | size -= vec_len; |
| 9363 | } |
| 9364 | /* store original address for later verification */ |
| 9365 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9366 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9367 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9368 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9369 | ret = 0; |
| 9370 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9371 | if (ret) |
| 9372 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9373 | kvfree(pages); |
| 9374 | kvfree(vmas); |
| 9375 | return ret; |
| 9376 | } |
| 9377 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9378 | 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] | 9379 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9380 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 9381 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9382 | } |
| 9383 | |
| 9384 | static int io_buffer_validate(struct iovec *iov) |
| 9385 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9386 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 9387 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9388 | /* |
| 9389 | * Don't impose further limits on the size and buffer |
| 9390 | * constraints here, we'll -EINVAL later when IO is |
| 9391 | * submitted if they are wrong. |
| 9392 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9393 | if (!iov->iov_base) |
| 9394 | return iov->iov_len ? -EFAULT : 0; |
| 9395 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9396 | return -EFAULT; |
| 9397 | |
| 9398 | /* arbitrary limit, but we need something */ |
| 9399 | if (iov->iov_len > SZ_1G) |
| 9400 | return -EFAULT; |
| 9401 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9402 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9403 | return -EOVERFLOW; |
| 9404 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9405 | return 0; |
| 9406 | } |
| 9407 | |
| 9408 | 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] | 9409 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9410 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9411 | struct page *last_hpage = NULL; |
| 9412 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9413 | int i, ret; |
| 9414 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9415 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9416 | if (ctx->user_bufs) |
| 9417 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9418 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9419 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9420 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9421 | if (ret) |
| 9422 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9423 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9424 | if (ret) |
| 9425 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9426 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9427 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9428 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9429 | return ret; |
| 9430 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9431 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9432 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9433 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9434 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9435 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9436 | ret = io_buffer_validate(&iov); |
| 9437 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9438 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9439 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9440 | ret = -EINVAL; |
| 9441 | break; |
| 9442 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9443 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9444 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9445 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9446 | if (ret) |
| 9447 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9448 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9449 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9450 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9451 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9452 | ctx->buf_data = data; |
| 9453 | if (ret) |
| 9454 | __io_sqe_buffers_unregister(ctx); |
| 9455 | else |
| 9456 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9457 | return ret; |
| 9458 | } |
| 9459 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9460 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9461 | struct io_uring_rsrc_update2 *up, |
| 9462 | unsigned int nr_args) |
| 9463 | { |
| 9464 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9465 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9466 | struct page *last_hpage = NULL; |
| 9467 | bool needs_switch = false; |
| 9468 | __u32 done; |
| 9469 | int i, err; |
| 9470 | |
| 9471 | if (!ctx->buf_data) |
| 9472 | return -ENXIO; |
| 9473 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9474 | return -EINVAL; |
| 9475 | |
| 9476 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9477 | struct io_mapped_ubuf *imu; |
| 9478 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9479 | u64 tag = 0; |
| 9480 | |
| 9481 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9482 | if (err) |
| 9483 | break; |
| 9484 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9485 | err = -EFAULT; |
| 9486 | break; |
| 9487 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9488 | err = io_buffer_validate(&iov); |
| 9489 | if (err) |
| 9490 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9491 | if (!iov.iov_base && tag) { |
| 9492 | err = -EINVAL; |
| 9493 | break; |
| 9494 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9495 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9496 | if (err) |
| 9497 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9498 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9499 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9500 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | b8ed0f7 | 2022-04-07 14:05:05 +0100 | [diff] [blame] | 9501 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9502 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9503 | if (unlikely(err)) { |
| 9504 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9505 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9506 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9507 | ctx->user_bufs[i] = NULL; |
| 9508 | needs_switch = true; |
| 9509 | } |
| 9510 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9511 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9512 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9513 | } |
| 9514 | |
| 9515 | if (needs_switch) |
| 9516 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9517 | return done ? done : err; |
| 9518 | } |
| 9519 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9520 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9521 | { |
| 9522 | __s32 __user *fds = arg; |
| 9523 | int fd; |
| 9524 | |
| 9525 | if (ctx->cq_ev_fd) |
| 9526 | return -EBUSY; |
| 9527 | |
| 9528 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9529 | return -EFAULT; |
| 9530 | |
| 9531 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9532 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9533 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9534 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9535 | ctx->cq_ev_fd = NULL; |
| 9536 | return ret; |
| 9537 | } |
| 9538 | |
| 9539 | return 0; |
| 9540 | } |
| 9541 | |
| 9542 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9543 | { |
| 9544 | if (ctx->cq_ev_fd) { |
| 9545 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9546 | ctx->cq_ev_fd = NULL; |
| 9547 | return 0; |
| 9548 | } |
| 9549 | |
| 9550 | return -ENXIO; |
| 9551 | } |
| 9552 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9553 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9554 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9555 | struct io_buffer *buf; |
| 9556 | unsigned long index; |
| 9557 | |
Ye Bin | 2d447d3 | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 9558 | xa_for_each(&ctx->io_buffers, index, buf) |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9559 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9560 | } |
| 9561 | |
Pavel Begunkov | 7255834 | 2021-08-09 20:18:09 +0100 | [diff] [blame] | 9562 | static void io_req_cache_free(struct list_head *list) |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9563 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 9564 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9565 | |
Pavel Begunkov | bb943b8 | 2021-08-09 20:18:10 +0100 | [diff] [blame] | 9566 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
| 9567 | list_del(&req->inflight_entry); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 9568 | kmem_cache_free(req_cachep, req); |
| 9569 | } |
| 9570 | } |
| 9571 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9572 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9573 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9574 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9575 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9576 | mutex_lock(&ctx->uring_lock); |
| 9577 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9578 | if (state->free_reqs) { |
| 9579 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
| 9580 | state->free_reqs = 0; |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 9581 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9582 | |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9583 | io_flush_cached_locked_reqs(ctx, state); |
| 9584 | io_req_cache_free(&state->free_list); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9585 | mutex_unlock(&ctx->uring_lock); |
| 9586 | } |
| 9587 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9588 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9589 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9590 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9591 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9592 | } |
| 9593 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9594 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 9595 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9596 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9597 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9598 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9599 | io_wait_rsrc_data(ctx->buf_data); |
| 9600 | io_wait_rsrc_data(ctx->file_data); |
| 9601 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9602 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9603 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9604 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9605 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9606 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9607 | if (ctx->rings) |
| 9608 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9609 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9610 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9611 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9612 | if (ctx->sq_creds) |
| 9613 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9614 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9615 | /* there are no registered resources left, nobody uses it */ |
| 9616 | if (ctx->rsrc_node) |
| 9617 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9618 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9619 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9620 | flush_delayed_work(&ctx->rsrc_put_work); |
| 9621 | |
| 9622 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9623 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9624 | |
| 9625 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9626 | if (ctx->ring_sock) { |
| 9627 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9628 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9629 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9630 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9631 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9632 | |
Pavel Begunkov | cd148d4 | 2022-10-16 22:42:55 +0100 | [diff] [blame] | 9633 | if (ctx->mm_account) { |
| 9634 | mmdrop(ctx->mm_account); |
| 9635 | ctx->mm_account = NULL; |
| 9636 | } |
| 9637 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9638 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9639 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9640 | |
| 9641 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9642 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9643 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9644 | if (ctx->hash_map) |
| 9645 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9646 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9647 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9648 | kfree(ctx); |
| 9649 | } |
| 9650 | |
| 9651 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9652 | { |
| 9653 | struct io_ring_ctx *ctx = file->private_data; |
| 9654 | __poll_t mask = 0; |
| 9655 | |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 9656 | poll_wait(file, &ctx->poll_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9657 | /* |
| 9658 | * synchronizes with barrier from wq_has_sleeper call in |
| 9659 | * io_commit_cqring |
| 9660 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9661 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9662 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9663 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9664 | |
| 9665 | /* |
| 9666 | * Don't flush cqring overflow list here, just do a simple check. |
| 9667 | * Otherwise there could possible be ABBA deadlock: |
| 9668 | * CPU0 CPU1 |
| 9669 | * ---- ---- |
| 9670 | * lock(&ctx->uring_lock); |
| 9671 | * lock(&ep->mtx); |
| 9672 | * lock(&ctx->uring_lock); |
| 9673 | * lock(&ep->mtx); |
| 9674 | * |
| 9675 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9676 | * pushs them to do the flush. |
| 9677 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9678 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9679 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9680 | |
| 9681 | return mask; |
| 9682 | } |
| 9683 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9684 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9685 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9686 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9687 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9688 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9689 | if (creds) { |
| 9690 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9691 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9692 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9693 | |
| 9694 | return -EINVAL; |
| 9695 | } |
| 9696 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9697 | struct io_tctx_exit { |
| 9698 | struct callback_head task_work; |
| 9699 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9700 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9701 | }; |
| 9702 | |
| 9703 | static void io_tctx_exit_cb(struct callback_head *cb) |
| 9704 | { |
| 9705 | struct io_uring_task *tctx = current->io_uring; |
| 9706 | struct io_tctx_exit *work; |
| 9707 | |
| 9708 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9709 | /* |
| 9710 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9711 | * node. It'll be removed by the end of cancellation, just ignore it. |
Harshit Mogalapalli | f895511 | 2022-12-06 01:38:32 -0800 | [diff] [blame] | 9712 | * tctx can be NULL if the queueing of this task_work raced with |
| 9713 | * work cancelation off the exec path. |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9714 | */ |
Harshit Mogalapalli | f895511 | 2022-12-06 01:38:32 -0800 | [diff] [blame] | 9715 | if (tctx && !atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9716 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9717 | complete(&work->completion); |
| 9718 | } |
| 9719 | |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9720 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 9721 | { |
| 9722 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9723 | |
| 9724 | return req->ctx == data; |
| 9725 | } |
| 9726 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9727 | static void io_ring_exit_work(struct work_struct *work) |
| 9728 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9729 | 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] | 9730 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9731 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9732 | struct io_tctx_exit exit; |
| 9733 | struct io_tctx_node *node; |
| 9734 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9735 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9736 | /* |
| 9737 | * If we're doing polled IO and end up having requests being |
| 9738 | * submitted async (out-of-line), then completions can come in while |
| 9739 | * we're waiting for refs to drop. We need to reap these manually, |
| 9740 | * as nobody else will be looking for them. |
| 9741 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9742 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9743 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9744 | if (ctx->sq_data) { |
| 9745 | struct io_sq_data *sqd = ctx->sq_data; |
| 9746 | struct task_struct *tsk; |
| 9747 | |
| 9748 | io_sq_thread_park(sqd); |
| 9749 | tsk = sqd->thread; |
| 9750 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9751 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9752 | io_cancel_ctx_cb, ctx, true); |
| 9753 | io_sq_thread_unpark(sqd); |
| 9754 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9755 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9756 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9757 | /* there is little hope left, don't run it too often */ |
| 9758 | interval = HZ * 60; |
| 9759 | } |
Jens Axboe | 8e29835 | 2023-06-11 21:14:09 -0600 | [diff] [blame] | 9760 | /* |
| 9761 | * This is really an uninterruptible wait, as it has to be |
| 9762 | * complete. But it's also run from a kworker, which doesn't |
| 9763 | * take signals, so it's fine to make it interruptible. This |
| 9764 | * avoids scenarios where we knowingly can wait much longer |
| 9765 | * on completions, for example if someone does a SIGSTOP on |
| 9766 | * a task that needs to finish task_work to make this loop |
| 9767 | * complete. That's a synthetic situation that should not |
| 9768 | * cause a stuck task backtrace, and hence a potential panic |
| 9769 | * on stuck tasks if that is enabled. |
| 9770 | */ |
| 9771 | } while (!wait_for_completion_interruptible_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9772 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9773 | init_completion(&exit.completion); |
| 9774 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9775 | exit.ctx = ctx; |
Pavel Begunkov | ea3291c | 2023-12-03 15:37:53 +0000 | [diff] [blame^] | 9776 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9777 | mutex_lock(&ctx->uring_lock); |
| 9778 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9779 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9780 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9781 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9782 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9783 | /* don't spin on a single task if cancellation failed */ |
| 9784 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9785 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9786 | if (WARN_ON_ONCE(ret)) |
| 9787 | continue; |
| 9788 | wake_up_process(node->task); |
| 9789 | |
| 9790 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 8e29835 | 2023-06-11 21:14:09 -0600 | [diff] [blame] | 9791 | /* |
| 9792 | * See comment above for |
| 9793 | * wait_for_completion_interruptible_timeout() on why this |
| 9794 | * wait is marked as interruptible. |
| 9795 | */ |
| 9796 | wait_for_completion_interruptible(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9797 | mutex_lock(&ctx->uring_lock); |
| 9798 | } |
| 9799 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9800 | spin_lock(&ctx->completion_lock); |
| 9801 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9802 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9803 | io_ring_ctx_free(ctx); |
| 9804 | } |
| 9805 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9806 | /* Returns true if we found and killed one or more timeouts */ |
| 9807 | 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] | 9808 | bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9809 | { |
| 9810 | struct io_kiocb *req, *tmp; |
| 9811 | int canceled = 0; |
| 9812 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9813 | spin_lock(&ctx->completion_lock); |
| 9814 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9815 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9816 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9817 | io_kill_timeout(req, -ECANCELED); |
| 9818 | canceled++; |
| 9819 | } |
| 9820 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9821 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9822 | if (canceled != 0) |
| 9823 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9824 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9825 | if (canceled != 0) |
| 9826 | io_cqring_ev_posted(ctx); |
| 9827 | return canceled != 0; |
| 9828 | } |
| 9829 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9830 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 9831 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9832 | unsigned long index; |
| 9833 | struct creds *creds; |
| 9834 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9835 | mutex_lock(&ctx->uring_lock); |
| 9836 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9837 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9838 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9839 | xa_for_each(&ctx->personalities, index, creds) |
| 9840 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9841 | mutex_unlock(&ctx->uring_lock); |
| 9842 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9843 | io_kill_timeouts(ctx, NULL, true); |
| 9844 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9845 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9846 | /* 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] | 9847 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9848 | |
Jens Axboe | 86e2d69 | 2023-01-21 12:36:08 -0700 | [diff] [blame] | 9849 | /* drop cached put refs after potentially doing completions */ |
| 9850 | if (current->io_uring) |
| 9851 | io_uring_drop_tctx_refs(current); |
| 9852 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9853 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9854 | /* |
| 9855 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9856 | * if we're exiting a ton of rings at the same time. It just adds |
| 9857 | * noise and overhead, there's no discernable change in runtime |
| 9858 | * over using system_wq. |
| 9859 | */ |
| 9860 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9861 | } |
| 9862 | |
| 9863 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9864 | { |
| 9865 | struct io_ring_ctx *ctx = file->private_data; |
| 9866 | |
| 9867 | file->private_data = NULL; |
| 9868 | io_ring_ctx_wait_and_kill(ctx); |
| 9869 | return 0; |
| 9870 | } |
| 9871 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9872 | struct io_task_cancel { |
| 9873 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9874 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9875 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9876 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9877 | 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] | 9878 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9879 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9880 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9881 | |
Pavel Begunkov | 1c939a5 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9882 | return io_match_task_safe(req, cancel->task, cancel->all); |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9883 | } |
| 9884 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9885 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9886 | struct task_struct *task, bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9887 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9888 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9889 | LIST_HEAD(list); |
| 9890 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9891 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9892 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 1c939a5 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9893 | if (io_match_task_safe(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9894 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9895 | break; |
| 9896 | } |
| 9897 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9898 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9899 | if (list_empty(&list)) |
| 9900 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9901 | |
| 9902 | while (!list_empty(&list)) { |
| 9903 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9904 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9905 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9906 | kfree(de); |
| 9907 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9908 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9909 | } |
| 9910 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9911 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
| 9912 | { |
| 9913 | struct io_tctx_node *node; |
| 9914 | enum io_wq_cancel cret; |
| 9915 | bool ret = false; |
| 9916 | |
| 9917 | mutex_lock(&ctx->uring_lock); |
| 9918 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9919 | struct io_uring_task *tctx = node->task->io_uring; |
| 9920 | |
| 9921 | /* |
| 9922 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9923 | * killed after ctx nodes, which requires to take the lock. |
| 9924 | */ |
| 9925 | if (!tctx || !tctx->io_wq) |
| 9926 | continue; |
| 9927 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9928 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9929 | } |
| 9930 | mutex_unlock(&ctx->uring_lock); |
| 9931 | |
| 9932 | return ret; |
| 9933 | } |
| 9934 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9935 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9936 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9937 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9938 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9939 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9940 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9941 | |
| 9942 | while (1) { |
| 9943 | enum io_wq_cancel cret; |
| 9944 | bool ret = false; |
| 9945 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9946 | if (!task) { |
| 9947 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9948 | } else if (tctx && tctx->io_wq) { |
| 9949 | /* |
| 9950 | * Cancels requests of all rings, not only @ctx, but |
| 9951 | * it's fine as the task is in exit/exec. |
| 9952 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9953 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9954 | &cancel, true); |
| 9955 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9956 | } |
| 9957 | |
| 9958 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9959 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9960 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9961 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 9962 | io_iopoll_try_reap_events(ctx); |
| 9963 | ret = true; |
Jens Axboe | abd54d8 | 2023-03-06 13:18:27 -0700 | [diff] [blame] | 9964 | cond_resched(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9965 | } |
| 9966 | } |
| 9967 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9968 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9969 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9970 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9971 | if (task) |
| 9972 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9973 | if (!ret) |
| 9974 | break; |
| 9975 | cond_resched(); |
| 9976 | } |
| 9977 | } |
| 9978 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9979 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9980 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9981 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9982 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9983 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9984 | |
| 9985 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9986 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9987 | if (unlikely(ret)) |
| 9988 | return ret; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9989 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9990 | tctx = current->io_uring; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9991 | if (ctx->iowq_limits_set) { |
| 9992 | unsigned int limits[2] = { ctx->iowq_limits[0], |
| 9993 | ctx->iowq_limits[1], }; |
| 9994 | |
| 9995 | ret = io_wq_max_workers(tctx->io_wq, limits); |
| 9996 | if (ret) |
| 9997 | return ret; |
| 9998 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9999 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10000 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 10001 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 10002 | if (!node) |
| 10003 | return -ENOMEM; |
| 10004 | node->ctx = ctx; |
| 10005 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10006 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10007 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 10008 | node, GFP_KERNEL)); |
| 10009 | if (ret) { |
| 10010 | kfree(node); |
| 10011 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10012 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10013 | |
| 10014 | mutex_lock(&ctx->uring_lock); |
| 10015 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 10016 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10017 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10018 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10019 | return 0; |
| 10020 | } |
| 10021 | |
| 10022 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10023 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 10024 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10025 | 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] | 10026 | { |
| 10027 | struct io_uring_task *tctx = current->io_uring; |
| 10028 | |
| 10029 | if (likely(tctx && tctx->last == ctx)) |
| 10030 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10031 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 10032 | } |
| 10033 | |
| 10034 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10035 | * Remove this io_uring_file -> task mapping. |
| 10036 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10037 | static void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10038 | { |
| 10039 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 10040 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 10041 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 10042 | if (!tctx) |
| 10043 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 10044 | node = xa_erase(&tctx->xa, index); |
| 10045 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 10046 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10047 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 10048 | WARN_ON_ONCE(current != node->task); |
| 10049 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 10050 | |
| 10051 | mutex_lock(&node->ctx->uring_lock); |
| 10052 | list_del(&node->ctx_node); |
| 10053 | mutex_unlock(&node->ctx->uring_lock); |
| 10054 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 10055 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10056 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 10057 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10058 | } |
| 10059 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 10060 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 10061 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 10062 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 10063 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 10064 | unsigned long index; |
| 10065 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 10066 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10067 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 10068 | cond_resched(); |
| 10069 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 10070 | if (wq) { |
| 10071 | /* |
| 10072 | * Must be after io_uring_del_task_file() (removes nodes under |
| 10073 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 10074 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 10075 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 10076 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 10077 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 10078 | } |
| 10079 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 10080 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 10081 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 10082 | if (tracked) |
| 10083 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 10084 | return percpu_counter_sum(&tctx->inflight); |
| 10085 | } |
| 10086 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10087 | /* |
| 10088 | * Find any io_uring ctx that this task has registered or done IO on, and cancel |
Jens Axboe | 8e12976 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 10089 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10090 | */ |
| 10091 | 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] | 10092 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 10093 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 10094 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 10095 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 10096 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 10097 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10098 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 10099 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 10100 | if (!current->io_uring) |
| 10101 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 10102 | if (tctx->io_wq) |
| 10103 | io_wq_exit_start(tctx->io_wq); |
| 10104 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 10105 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 10106 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 10107 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10108 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 10109 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 10110 | if (!inflight) |
| 10111 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10112 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10113 | if (!sqd) { |
| 10114 | struct io_tctx_node *node; |
| 10115 | unsigned long index; |
| 10116 | |
| 10117 | xa_for_each(&tctx->xa, index, node) { |
| 10118 | /* sqpoll task will cancel all its requests */ |
| 10119 | if (node->ctx->sq_data) |
| 10120 | continue; |
| 10121 | io_uring_try_cancel_requests(node->ctx, current, |
| 10122 | cancel_all); |
| 10123 | } |
| 10124 | } else { |
| 10125 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 10126 | io_uring_try_cancel_requests(ctx, current, |
| 10127 | cancel_all); |
| 10128 | } |
| 10129 | |
Jens Axboe | 8e12976 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 10130 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
| 10131 | io_run_task_work(); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 10132 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 8e12976 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 10133 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10134 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 10135 | * If we've seen completions, retry without waiting. This |
| 10136 | * avoids a race where a completion comes in before we did |
| 10137 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10138 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 10139 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 10140 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 10141 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 10142 | } while (1); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 10143 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 10144 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 10145 | if (cancel_all) { |
Pavel Begunkov | b168b1a | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 10146 | /* |
| 10147 | * We shouldn't run task_works after cancel, so just leave |
| 10148 | * ->in_idle set for normal exit. |
| 10149 | */ |
| 10150 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 10151 | /* for exec all current's requests should be gone, kill tctx */ |
| 10152 | __io_uring_free(current); |
| 10153 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 10154 | } |
| 10155 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 10156 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10157 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 10158 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 10159 | } |
| 10160 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10161 | static void *io_uring_validate_mmap_request(struct file *file, |
| 10162 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10163 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10164 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10165 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10166 | struct page *page; |
| 10167 | void *ptr; |
| 10168 | |
| 10169 | switch (offset) { |
| 10170 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10171 | case IORING_OFF_CQ_RING: |
| 10172 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10173 | break; |
| 10174 | case IORING_OFF_SQES: |
| 10175 | ptr = ctx->sq_sqes; |
| 10176 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10177 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10178 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10179 | } |
| 10180 | |
| 10181 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 10182 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10183 | return ERR_PTR(-EINVAL); |
| 10184 | |
| 10185 | return ptr; |
| 10186 | } |
| 10187 | |
| 10188 | #ifdef CONFIG_MMU |
| 10189 | |
| 10190 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 10191 | { |
| 10192 | size_t sz = vma->vm_end - vma->vm_start; |
| 10193 | unsigned long pfn; |
| 10194 | void *ptr; |
| 10195 | |
| 10196 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 10197 | if (IS_ERR(ptr)) |
| 10198 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10199 | |
| 10200 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 10201 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 10202 | } |
| 10203 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10204 | #else /* !CONFIG_MMU */ |
| 10205 | |
| 10206 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 10207 | { |
| 10208 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 10209 | } |
| 10210 | |
| 10211 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 10212 | { |
| 10213 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 10214 | } |
| 10215 | |
| 10216 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 10217 | unsigned long addr, unsigned long len, |
| 10218 | unsigned long pgoff, unsigned long flags) |
| 10219 | { |
| 10220 | void *ptr; |
| 10221 | |
| 10222 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 10223 | if (IS_ERR(ptr)) |
| 10224 | return PTR_ERR(ptr); |
| 10225 | |
| 10226 | return (unsigned long) ptr; |
| 10227 | } |
| 10228 | |
| 10229 | #endif /* !CONFIG_MMU */ |
| 10230 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10231 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10232 | { |
| 10233 | DEFINE_WAIT(wait); |
| 10234 | |
| 10235 | do { |
| 10236 | if (!io_sqring_full(ctx)) |
| 10237 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10238 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 10239 | |
| 10240 | if (!io_sqring_full(ctx)) |
| 10241 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10242 | schedule(); |
| 10243 | } while (!signal_pending(current)); |
| 10244 | |
| 10245 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 10246 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10247 | } |
| 10248 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10249 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 10250 | struct __kernel_timespec __user **ts, |
| 10251 | const sigset_t __user **sig) |
| 10252 | { |
| 10253 | struct io_uring_getevents_arg arg; |
| 10254 | |
| 10255 | /* |
| 10256 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 10257 | * is just a pointer to the sigset_t. |
| 10258 | */ |
| 10259 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 10260 | *sig = (const sigset_t __user *) argp; |
| 10261 | *ts = NULL; |
| 10262 | return 0; |
| 10263 | } |
| 10264 | |
| 10265 | /* |
| 10266 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 10267 | * timespec and sigset_t pointers if good. |
| 10268 | */ |
| 10269 | if (*argsz != sizeof(arg)) |
| 10270 | return -EINVAL; |
| 10271 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 10272 | return -EFAULT; |
Dylan Yudaken | 9947548 | 2022-04-12 09:30:42 -0700 | [diff] [blame] | 10273 | if (arg.pad) |
| 10274 | return -EINVAL; |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10275 | *sig = u64_to_user_ptr(arg.sigmask); |
| 10276 | *argsz = arg.sigmask_sz; |
| 10277 | *ts = u64_to_user_ptr(arg.ts); |
| 10278 | return 0; |
| 10279 | } |
| 10280 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10281 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10282 | u32, min_complete, u32, flags, const void __user *, argp, |
| 10283 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10284 | { |
| 10285 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10286 | int submitted = 0; |
| 10287 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10288 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10289 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 10290 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 10291 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10292 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 10293 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10294 | return -EINVAL; |
| 10295 | |
| 10296 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10297 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10298 | return -EBADF; |
| 10299 | |
| 10300 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10301 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10302 | goto out_fput; |
| 10303 | |
| 10304 | ret = -ENXIO; |
| 10305 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10306 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10307 | goto out_fput; |
| 10308 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10309 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10310 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10311 | goto out; |
| 10312 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10313 | /* |
| 10314 | * For SQ polling, the thread will do all submissions and completions. |
| 10315 | * Just return the requested submit count, and wake the thread if |
| 10316 | * we were asked to. |
| 10317 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10318 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10319 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 10320 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 10321 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10322 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 10323 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 10324 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10325 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10326 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 10327 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10328 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 10329 | ret = io_sqpoll_wait_sq(ctx); |
| 10330 | if (ret) |
| 10331 | goto out; |
| 10332 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10333 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10334 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10335 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10336 | if (unlikely(ret)) |
| 10337 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10338 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10339 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10340 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10341 | |
| 10342 | if (submitted != to_submit) |
| 10343 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10344 | } |
| 10345 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10346 | const sigset_t __user *sig; |
| 10347 | struct __kernel_timespec __user *ts; |
| 10348 | |
| 10349 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 10350 | if (unlikely(ret)) |
| 10351 | goto out; |
| 10352 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10353 | min_complete = min(min_complete, ctx->cq_entries); |
| 10354 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 10355 | /* |
| 10356 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 10357 | * space applications don't need to do io completion events |
| 10358 | * polling again, they can rely on io_sq_thread to do polling |
| 10359 | * work, which can reduce cpu usage and uring_lock contention. |
| 10360 | */ |
| 10361 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 10362 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 10363 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10364 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10365 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10366 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10367 | } |
| 10368 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10369 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 10370 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10371 | out_fput: |
| 10372 | fdput(f); |
| 10373 | return submitted ? submitted : ret; |
| 10374 | } |
| 10375 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10376 | #ifdef CONFIG_PROC_FS |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10377 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
| 10378 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10379 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10380 | struct user_namespace *uns = seq_user_ns(m); |
| 10381 | struct group_info *gi; |
| 10382 | kernel_cap_t cap; |
| 10383 | unsigned __capi; |
| 10384 | int g; |
| 10385 | |
| 10386 | seq_printf(m, "%5d\n", id); |
| 10387 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 10388 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 10389 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 10390 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 10391 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 10392 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 10393 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 10394 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 10395 | seq_puts(m, "\n\tGroups:\t"); |
| 10396 | gi = cred->group_info; |
| 10397 | for (g = 0; g < gi->ngroups; g++) { |
| 10398 | seq_put_decimal_ull(m, g ? " " : "", |
| 10399 | from_kgid_munged(uns, gi->gid[g])); |
| 10400 | } |
| 10401 | seq_puts(m, "\n\tCapEff:\t"); |
| 10402 | cap = cred->cap_effective; |
| 10403 | CAP_FOR_EACH_U32(__capi) |
| 10404 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 10405 | seq_putc(m, '\n'); |
| 10406 | return 0; |
| 10407 | } |
| 10408 | |
| 10409 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 10410 | { |
Jens Axboe | 3d79127 | 2023-10-21 12:30:29 -0600 | [diff] [blame] | 10411 | int sq_pid = -1, sq_cpu = -1; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10412 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10413 | int i; |
| 10414 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10415 | /* |
| 10416 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10417 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10418 | * cases. If we fail to get the lock, we just don't iterate any |
| 10419 | * structures that could be going away outside the io_uring mutex. |
| 10420 | */ |
| 10421 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10422 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10423 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Jens Axboe | 3d79127 | 2023-10-21 12:30:29 -0600 | [diff] [blame] | 10424 | struct io_sq_data *sq = ctx->sq_data; |
| 10425 | |
| 10426 | if (mutex_trylock(&sq->lock)) { |
| 10427 | if (sq->thread) { |
| 10428 | sq_pid = task_pid_nr(sq->thread); |
| 10429 | sq_cpu = task_cpu(sq->thread); |
| 10430 | } |
| 10431 | mutex_unlock(&sq->lock); |
| 10432 | } |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10433 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10434 | |
Jens Axboe | 3d79127 | 2023-10-21 12:30:29 -0600 | [diff] [blame] | 10435 | seq_printf(m, "SqThread:\t%d\n", sq_pid); |
| 10436 | seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10437 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10438 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10439 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10440 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10441 | if (f) |
| 10442 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10443 | else |
| 10444 | seq_printf(m, "%5u: <none>\n", i); |
| 10445 | } |
| 10446 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10447 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10448 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10449 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10450 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10451 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10452 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10453 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10454 | unsigned long index; |
| 10455 | const struct cred *cred; |
| 10456 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10457 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10458 | xa_for_each(&ctx->personalities, index, cred) |
| 10459 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10460 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10461 | seq_printf(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10462 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10463 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10464 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10465 | struct io_kiocb *req; |
| 10466 | |
| 10467 | hlist_for_each_entry(req, list, hash_node) |
| 10468 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10469 | req->task->task_works != NULL); |
| 10470 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10471 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10472 | if (has_lock) |
| 10473 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10474 | } |
| 10475 | |
| 10476 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 10477 | { |
| 10478 | struct io_ring_ctx *ctx = f->private_data; |
| 10479 | |
| 10480 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10481 | __io_uring_show_fdinfo(ctx, m); |
| 10482 | percpu_ref_put(&ctx->refs); |
| 10483 | } |
| 10484 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10485 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10486 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10487 | static const struct file_operations io_uring_fops = { |
| 10488 | .release = io_uring_release, |
| 10489 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10490 | #ifndef CONFIG_MMU |
| 10491 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10492 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10493 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10494 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10495 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10496 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10497 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10498 | }; |
| 10499 | |
| 10500 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10501 | struct io_uring_params *p) |
| 10502 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10503 | struct io_rings *rings; |
| 10504 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10505 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10506 | /* make sure these are sane, as we already accounted them */ |
| 10507 | ctx->sq_entries = p->sq_entries; |
| 10508 | ctx->cq_entries = p->cq_entries; |
| 10509 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10510 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10511 | if (size == SIZE_MAX) |
| 10512 | return -EOVERFLOW; |
| 10513 | |
| 10514 | rings = io_mem_alloc(size); |
| 10515 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10516 | return -ENOMEM; |
| 10517 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10518 | ctx->rings = rings; |
| 10519 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10520 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10521 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10522 | rings->sq_ring_entries = p->sq_entries; |
| 10523 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10524 | |
| 10525 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10526 | if (size == SIZE_MAX) { |
| 10527 | io_mem_free(ctx->rings); |
| 10528 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10529 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10530 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10531 | |
| 10532 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10533 | if (!ctx->sq_sqes) { |
| 10534 | io_mem_free(ctx->rings); |
| 10535 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10536 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10537 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10538 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10539 | return 0; |
| 10540 | } |
| 10541 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10542 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10543 | { |
| 10544 | int ret, fd; |
| 10545 | |
| 10546 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10547 | if (fd < 0) |
| 10548 | return fd; |
| 10549 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10550 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10551 | if (ret) { |
| 10552 | put_unused_fd(fd); |
| 10553 | return ret; |
| 10554 | } |
| 10555 | fd_install(fd, file); |
| 10556 | return fd; |
| 10557 | } |
| 10558 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10559 | /* |
| 10560 | * Allocate an anonymous fd, this is what constitutes the application |
| 10561 | * visible backing of an io_uring instance. The application mmaps this |
| 10562 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10563 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10564 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10565 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10566 | { |
| 10567 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10568 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10569 | int ret; |
| 10570 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10571 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10572 | &ctx->ring_sock); |
| 10573 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10574 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10575 | #endif |
| 10576 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10577 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 10578 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10579 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10580 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10581 | sock_release(ctx->ring_sock); |
| 10582 | ctx->ring_sock = NULL; |
| 10583 | } else { |
| 10584 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10585 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10586 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10587 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10588 | } |
| 10589 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10590 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10591 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10592 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10593 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10594 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10595 | int ret; |
| 10596 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10597 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10598 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10599 | if (entries > IORING_MAX_ENTRIES) { |
| 10600 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10601 | return -EINVAL; |
| 10602 | entries = IORING_MAX_ENTRIES; |
| 10603 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10604 | |
| 10605 | /* |
| 10606 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10607 | * application to drive a higher depth than the size of the SQ ring, |
| 10608 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10609 | * some flexibility in overcommitting a bit. If the application has |
| 10610 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10611 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10612 | */ |
| 10613 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10614 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10615 | /* |
| 10616 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10617 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10618 | * any cq vs sq ring sizing. |
| 10619 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10620 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10621 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10622 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10623 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10624 | return -EINVAL; |
| 10625 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10626 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10627 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10628 | if (p->cq_entries < p->sq_entries) |
| 10629 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10630 | } else { |
| 10631 | p->cq_entries = 2 * p->sq_entries; |
| 10632 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10633 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10634 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10635 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10636 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10637 | ctx->compat = in_compat_syscall(); |
Ondrej Mosnacek | 21d063d | 2023-07-18 13:56:07 +0200 | [diff] [blame] | 10638 | if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK)) |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10639 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10640 | |
| 10641 | /* |
| 10642 | * This is just grabbed for accounting purposes. When a process exits, |
| 10643 | * the mm is exited and dropped before the files, hence we need to hang |
| 10644 | * on to this mm purely for the purposes of being able to unaccount |
| 10645 | * memory (locked/pinned vm). It's not used for anything else. |
| 10646 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10647 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10648 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10649 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10650 | ret = io_allocate_scq_urings(ctx, p); |
| 10651 | if (ret) |
| 10652 | goto err; |
| 10653 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10654 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10655 | if (ret) |
| 10656 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10657 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10658 | ret = io_rsrc_node_switch_start(ctx); |
| 10659 | if (ret) |
| 10660 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10661 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10662 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10663 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10664 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10665 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10666 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10667 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10668 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10669 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10670 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10671 | |
| 10672 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10673 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10674 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10675 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10676 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10677 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10678 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10679 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10680 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10681 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10682 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10683 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10684 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10685 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
| 10686 | IORING_FEAT_RSRC_TAGS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10687 | |
| 10688 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10689 | ret = -EFAULT; |
| 10690 | goto err; |
| 10691 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10692 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10693 | file = io_uring_get_file(ctx); |
| 10694 | if (IS_ERR(file)) { |
| 10695 | ret = PTR_ERR(file); |
| 10696 | goto err; |
| 10697 | } |
| 10698 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10699 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10700 | * Install ring fd as the very last thing, so we don't risk someone |
| 10701 | * having closed it before we finish setup |
| 10702 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10703 | ret = io_uring_install_fd(ctx, file); |
| 10704 | if (ret < 0) { |
| 10705 | /* fput will clean it up */ |
| 10706 | fput(file); |
| 10707 | return ret; |
| 10708 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10709 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10710 | 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] | 10711 | return ret; |
| 10712 | err: |
| 10713 | io_ring_ctx_wait_and_kill(ctx); |
| 10714 | return ret; |
| 10715 | } |
| 10716 | |
| 10717 | /* |
| 10718 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10719 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10720 | * params structure passed in. |
| 10721 | */ |
| 10722 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10723 | { |
| 10724 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10725 | int i; |
| 10726 | |
| 10727 | if (copy_from_user(&p, params, sizeof(p))) |
| 10728 | return -EFAULT; |
| 10729 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10730 | if (p.resv[i]) |
| 10731 | return -EINVAL; |
| 10732 | } |
| 10733 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10734 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10735 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10736 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10737 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10738 | return -EINVAL; |
| 10739 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10740 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10741 | } |
| 10742 | |
| 10743 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10744 | struct io_uring_params __user *, params) |
| 10745 | { |
| 10746 | return io_uring_setup(entries, params); |
| 10747 | } |
| 10748 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10749 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 10750 | { |
| 10751 | struct io_uring_probe *p; |
| 10752 | size_t size; |
| 10753 | int i, ret; |
| 10754 | |
| 10755 | size = struct_size(p, ops, nr_args); |
| 10756 | if (size == SIZE_MAX) |
| 10757 | return -EOVERFLOW; |
| 10758 | p = kzalloc(size, GFP_KERNEL); |
| 10759 | if (!p) |
| 10760 | return -ENOMEM; |
| 10761 | |
| 10762 | ret = -EFAULT; |
| 10763 | if (copy_from_user(p, arg, size)) |
| 10764 | goto out; |
| 10765 | ret = -EINVAL; |
| 10766 | if (memchr_inv(p, 0, size)) |
| 10767 | goto out; |
| 10768 | |
| 10769 | p->last_op = IORING_OP_LAST - 1; |
| 10770 | if (nr_args > IORING_OP_LAST) |
| 10771 | nr_args = IORING_OP_LAST; |
| 10772 | |
| 10773 | for (i = 0; i < nr_args; i++) { |
| 10774 | p->ops[i].op = i; |
| 10775 | if (!io_op_defs[i].not_supported) |
| 10776 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10777 | } |
| 10778 | p->ops_len = i; |
| 10779 | |
| 10780 | ret = 0; |
| 10781 | if (copy_to_user(arg, p, size)) |
| 10782 | ret = -EFAULT; |
| 10783 | out: |
| 10784 | kfree(p); |
| 10785 | return ret; |
| 10786 | } |
| 10787 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10788 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10789 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10790 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10791 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10792 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10793 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10794 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10795 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10796 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10797 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10798 | if (ret < 0) { |
| 10799 | put_cred(creds); |
| 10800 | return ret; |
| 10801 | } |
| 10802 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10803 | } |
| 10804 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10805 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 10806 | unsigned int nr_args) |
| 10807 | { |
| 10808 | struct io_uring_restriction *res; |
| 10809 | size_t size; |
| 10810 | int i, ret; |
| 10811 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10812 | /* Restrictions allowed only if rings started disabled */ |
| 10813 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10814 | return -EBADFD; |
| 10815 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10816 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10817 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10818 | return -EBUSY; |
| 10819 | |
| 10820 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10821 | return -EINVAL; |
| 10822 | |
| 10823 | size = array_size(nr_args, sizeof(*res)); |
| 10824 | if (size == SIZE_MAX) |
| 10825 | return -EOVERFLOW; |
| 10826 | |
| 10827 | res = memdup_user(arg, size); |
| 10828 | if (IS_ERR(res)) |
| 10829 | return PTR_ERR(res); |
| 10830 | |
| 10831 | ret = 0; |
| 10832 | |
| 10833 | for (i = 0; i < nr_args; i++) { |
| 10834 | switch (res[i].opcode) { |
| 10835 | case IORING_RESTRICTION_REGISTER_OP: |
| 10836 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10837 | ret = -EINVAL; |
| 10838 | goto out; |
| 10839 | } |
| 10840 | |
| 10841 | __set_bit(res[i].register_op, |
| 10842 | ctx->restrictions.register_op); |
| 10843 | break; |
| 10844 | case IORING_RESTRICTION_SQE_OP: |
| 10845 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10846 | ret = -EINVAL; |
| 10847 | goto out; |
| 10848 | } |
| 10849 | |
| 10850 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10851 | break; |
| 10852 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10853 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10854 | break; |
| 10855 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10856 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10857 | break; |
| 10858 | default: |
| 10859 | ret = -EINVAL; |
| 10860 | goto out; |
| 10861 | } |
| 10862 | } |
| 10863 | |
| 10864 | out: |
| 10865 | /* Reset all restrictions if an error happened */ |
| 10866 | if (ret != 0) |
| 10867 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10868 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10869 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10870 | |
| 10871 | kfree(res); |
| 10872 | return ret; |
| 10873 | } |
| 10874 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10875 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10876 | { |
| 10877 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10878 | return -EBADFD; |
| 10879 | |
| 10880 | if (ctx->restrictions.registered) |
| 10881 | ctx->restricted = 1; |
| 10882 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10883 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10884 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10885 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10886 | return 0; |
| 10887 | } |
| 10888 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10889 | 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] | 10890 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10891 | unsigned nr_args) |
| 10892 | { |
| 10893 | __u32 tmp; |
| 10894 | int err; |
| 10895 | |
| 10896 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10897 | return -EOVERFLOW; |
| 10898 | err = io_rsrc_node_switch_start(ctx); |
| 10899 | if (err) |
| 10900 | return err; |
| 10901 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10902 | switch (type) { |
| 10903 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10904 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10905 | case IORING_RSRC_BUFFER: |
| 10906 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10907 | } |
| 10908 | return -EINVAL; |
| 10909 | } |
| 10910 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10911 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10912 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10913 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10914 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10915 | |
| 10916 | if (!nr_args) |
| 10917 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10918 | memset(&up, 0, sizeof(up)); |
| 10919 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10920 | return -EFAULT; |
Dylan Yudaken | 7a7c9f9 | 2022-04-12 09:30:40 -0700 | [diff] [blame] | 10921 | if (up.resv || up.resv2) |
Dylan Yudaken | 22aa159 | 2022-04-12 09:30:39 -0700 | [diff] [blame] | 10922 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10923 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10924 | } |
| 10925 | |
| 10926 | 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] | 10927 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10928 | { |
| 10929 | struct io_uring_rsrc_update2 up; |
| 10930 | |
| 10931 | if (size != sizeof(up)) |
| 10932 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10933 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10934 | return -EFAULT; |
Dylan Yudaken | 7a7c9f9 | 2022-04-12 09:30:40 -0700 | [diff] [blame] | 10935 | if (!up.nr || up.resv || up.resv2) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10936 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10937 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10938 | } |
| 10939 | |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10940 | 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] | 10941 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10942 | { |
| 10943 | struct io_uring_rsrc_register rr; |
| 10944 | |
| 10945 | /* keep it extendible */ |
| 10946 | if (size != sizeof(rr)) |
| 10947 | return -EINVAL; |
| 10948 | |
| 10949 | memset(&rr, 0, sizeof(rr)); |
| 10950 | if (copy_from_user(&rr, arg, size)) |
| 10951 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10952 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10953 | return -EINVAL; |
| 10954 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10955 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10956 | case IORING_RSRC_FILE: |
| 10957 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10958 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10959 | case IORING_RSRC_BUFFER: |
| 10960 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10961 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10962 | } |
| 10963 | return -EINVAL; |
| 10964 | } |
| 10965 | |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10966 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
| 10967 | unsigned len) |
| 10968 | { |
| 10969 | struct io_uring_task *tctx = current->io_uring; |
| 10970 | cpumask_var_t new_mask; |
| 10971 | int ret; |
| 10972 | |
| 10973 | if (!tctx || !tctx->io_wq) |
| 10974 | return -EINVAL; |
| 10975 | |
| 10976 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10977 | return -ENOMEM; |
| 10978 | |
| 10979 | cpumask_clear(new_mask); |
| 10980 | if (len > cpumask_size()) |
| 10981 | len = cpumask_size(); |
| 10982 | |
Eugene Syromiatnikov | fe223dd | 2022-04-06 13:55:33 +0200 | [diff] [blame] | 10983 | if (in_compat_syscall()) { |
| 10984 | ret = compat_get_bitmap(cpumask_bits(new_mask), |
| 10985 | (const compat_ulong_t __user *)arg, |
| 10986 | len * 8 /* CHAR_BIT */); |
| 10987 | } else { |
| 10988 | ret = copy_from_user(new_mask, arg, len); |
| 10989 | } |
| 10990 | |
| 10991 | if (ret) { |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10992 | free_cpumask_var(new_mask); |
| 10993 | return -EFAULT; |
| 10994 | } |
| 10995 | |
| 10996 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10997 | free_cpumask_var(new_mask); |
| 10998 | return ret; |
| 10999 | } |
| 11000 | |
| 11001 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
| 11002 | { |
| 11003 | struct io_uring_task *tctx = current->io_uring; |
| 11004 | |
| 11005 | if (!tctx || !tctx->io_wq) |
| 11006 | return -EINVAL; |
| 11007 | |
| 11008 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 11009 | } |
| 11010 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11011 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 11012 | void __user *arg) |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 11013 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11014 | { |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 11015 | struct io_tctx_node *node; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11016 | struct io_uring_task *tctx = NULL; |
| 11017 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11018 | __u32 new_count[2]; |
| 11019 | int i, ret; |
| 11020 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11021 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 11022 | return -EFAULT; |
| 11023 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 11024 | if (new_count[i] > INT_MAX) |
| 11025 | return -EINVAL; |
| 11026 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11027 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 11028 | sqd = ctx->sq_data; |
| 11029 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 11030 | /* |
| 11031 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 11032 | * ordering. Fine to drop uring_lock here, we hold |
| 11033 | * a ref to the ctx. |
| 11034 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11035 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 11036 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11037 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 11038 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11039 | if (sqd->thread) |
| 11040 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11041 | } |
| 11042 | } else { |
| 11043 | tctx = current->io_uring; |
| 11044 | } |
| 11045 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 11046 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11047 | |
Pavel Begunkov | 4cac487 | 2021-11-08 15:10:03 +0000 | [diff] [blame] | 11048 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 11049 | if (new_count[i]) |
| 11050 | ctx->iowq_limits[i] = new_count[i]; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 11051 | ctx->iowq_limits_set = true; |
| 11052 | |
| 11053 | ret = -EINVAL; |
| 11054 | if (tctx && tctx->io_wq) { |
| 11055 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 11056 | if (ret) |
| 11057 | goto err; |
| 11058 | } else { |
| 11059 | memset(new_count, 0, sizeof(new_count)); |
| 11060 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11061 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11062 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11063 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11064 | io_put_sq_data(sqd); |
| 11065 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11066 | |
| 11067 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 11068 | return -EFAULT; |
| 11069 | |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 11070 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
| 11071 | if (sqd) |
| 11072 | return 0; |
| 11073 | |
| 11074 | /* now propagate the restriction to all registered users */ |
| 11075 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 11076 | struct io_uring_task *tctx = node->task->io_uring; |
| 11077 | |
| 11078 | if (WARN_ON_ONCE(!tctx->io_wq)) |
| 11079 | continue; |
| 11080 | |
| 11081 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 11082 | new_count[i] = ctx->iowq_limits[i]; |
| 11083 | /* ignore errors, it always returns zero anyway */ |
| 11084 | (void)io_wq_max_workers(tctx->io_wq, new_count); |
| 11085 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11086 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11087 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11088 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11089 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 11090 | io_put_sq_data(sqd); |
| 11091 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 11092 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11093 | } |
| 11094 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11095 | static bool io_register_op_must_quiesce(int op) |
| 11096 | { |
| 11097 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 11098 | case IORING_REGISTER_BUFFERS: |
| 11099 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 11100 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11101 | case IORING_UNREGISTER_FILES: |
| 11102 | case IORING_REGISTER_FILES_UPDATE: |
| 11103 | case IORING_REGISTER_PROBE: |
| 11104 | case IORING_REGISTER_PERSONALITY: |
| 11105 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11106 | case IORING_REGISTER_FILES2: |
| 11107 | case IORING_REGISTER_FILES_UPDATE2: |
| 11108 | case IORING_REGISTER_BUFFERS2: |
| 11109 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 11110 | case IORING_REGISTER_IOWQ_AFF: |
| 11111 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11112 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11113 | return false; |
| 11114 | default: |
| 11115 | return true; |
| 11116 | } |
| 11117 | } |
| 11118 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 11119 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
| 11120 | { |
| 11121 | long ret; |
| 11122 | |
| 11123 | percpu_ref_kill(&ctx->refs); |
| 11124 | |
| 11125 | /* |
| 11126 | * Drop uring mutex before waiting for references to exit. If another |
| 11127 | * thread is currently inside io_uring_enter() it might need to grab the |
| 11128 | * uring_lock to make progress. If we hold it here across the drain |
| 11129 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 11130 | * no new references will come in after we've killed the percpu ref. |
| 11131 | */ |
| 11132 | mutex_unlock(&ctx->uring_lock); |
| 11133 | do { |
| 11134 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 11135 | if (!ret) |
| 11136 | break; |
| 11137 | ret = io_run_task_work_sig(); |
| 11138 | } while (ret >= 0); |
| 11139 | mutex_lock(&ctx->uring_lock); |
| 11140 | |
| 11141 | if (ret) |
| 11142 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 11143 | return ret; |
| 11144 | } |
| 11145 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11146 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 11147 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 11148 | __releases(ctx->uring_lock) |
| 11149 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11150 | { |
| 11151 | int ret; |
| 11152 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 11153 | /* |
| 11154 | * We're inside the ring mutex, if the ref is already dying, then |
| 11155 | * someone else killed the ctx or is already going through |
| 11156 | * io_uring_register(). |
| 11157 | */ |
| 11158 | if (percpu_ref_is_dying(&ctx->refs)) |
| 11159 | return -ENXIO; |
| 11160 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 11161 | if (ctx->restricted) { |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 11162 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 11163 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 11164 | return -EACCES; |
| 11165 | } |
| 11166 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11167 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 11168 | ret = io_ctx_quiesce(ctx); |
| 11169 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 11170 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11171 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11172 | |
| 11173 | switch (opcode) { |
| 11174 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 11175 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11176 | break; |
| 11177 | case IORING_UNREGISTER_BUFFERS: |
| 11178 | ret = -EINVAL; |
| 11179 | if (arg || nr_args) |
| 11180 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 11181 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11182 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 11183 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11184 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 11185 | break; |
| 11186 | case IORING_UNREGISTER_FILES: |
| 11187 | ret = -EINVAL; |
| 11188 | if (arg || nr_args) |
| 11189 | break; |
| 11190 | ret = io_sqe_files_unregister(ctx); |
| 11191 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 11192 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11193 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 11194 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11195 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 11196 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11197 | ret = -EINVAL; |
| 11198 | if (nr_args != 1) |
| 11199 | break; |
| 11200 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 11201 | if (ret) |
| 11202 | break; |
| 11203 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 11204 | ctx->eventfd_async = 1; |
| 11205 | else |
| 11206 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11207 | break; |
| 11208 | case IORING_UNREGISTER_EVENTFD: |
| 11209 | ret = -EINVAL; |
| 11210 | if (arg || nr_args) |
| 11211 | break; |
| 11212 | ret = io_eventfd_unregister(ctx); |
| 11213 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 11214 | case IORING_REGISTER_PROBE: |
| 11215 | ret = -EINVAL; |
| 11216 | if (!arg || nr_args > 256) |
| 11217 | break; |
| 11218 | ret = io_probe(ctx, arg, nr_args); |
| 11219 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11220 | case IORING_REGISTER_PERSONALITY: |
| 11221 | ret = -EINVAL; |
| 11222 | if (arg || nr_args) |
| 11223 | break; |
| 11224 | ret = io_register_personality(ctx); |
| 11225 | break; |
| 11226 | case IORING_UNREGISTER_PERSONALITY: |
| 11227 | ret = -EINVAL; |
| 11228 | if (arg) |
| 11229 | break; |
| 11230 | ret = io_unregister_personality(ctx, nr_args); |
| 11231 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 11232 | case IORING_REGISTER_ENABLE_RINGS: |
| 11233 | ret = -EINVAL; |
| 11234 | if (arg || nr_args) |
| 11235 | break; |
| 11236 | ret = io_register_enable_rings(ctx); |
| 11237 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 11238 | case IORING_REGISTER_RESTRICTIONS: |
| 11239 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 11240 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11241 | case IORING_REGISTER_FILES2: |
| 11242 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11243 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11244 | case IORING_REGISTER_FILES_UPDATE2: |
| 11245 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11246 | IORING_RSRC_FILE); |
| 11247 | break; |
| 11248 | case IORING_REGISTER_BUFFERS2: |
| 11249 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 11250 | break; |
| 11251 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 11252 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11253 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11254 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 11255 | case IORING_REGISTER_IOWQ_AFF: |
| 11256 | ret = -EINVAL; |
| 11257 | if (!arg || !nr_args) |
| 11258 | break; |
| 11259 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 11260 | break; |
| 11261 | case IORING_UNREGISTER_IOWQ_AFF: |
| 11262 | ret = -EINVAL; |
| 11263 | if (arg || nr_args) |
| 11264 | break; |
| 11265 | ret = io_unregister_iowq_aff(ctx); |
| 11266 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11267 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 11268 | ret = -EINVAL; |
| 11269 | if (!arg || nr_args != 2) |
| 11270 | break; |
| 11271 | ret = io_register_iowq_max_workers(ctx, arg); |
| 11272 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11273 | default: |
| 11274 | ret = -EINVAL; |
| 11275 | break; |
| 11276 | } |
| 11277 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11278 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11279 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11280 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 11281 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11282 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11283 | return ret; |
| 11284 | } |
| 11285 | |
| 11286 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 11287 | void __user *, arg, unsigned int, nr_args) |
| 11288 | { |
| 11289 | struct io_ring_ctx *ctx; |
| 11290 | long ret = -EBADF; |
| 11291 | struct fd f; |
| 11292 | |
Jens Axboe | f9309dc | 2022-12-23 06:37:08 -0700 | [diff] [blame] | 11293 | if (opcode >= IORING_REGISTER_LAST) |
| 11294 | return -EINVAL; |
| 11295 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11296 | f = fdget(fd); |
| 11297 | if (!f.file) |
| 11298 | return -EBADF; |
| 11299 | |
| 11300 | ret = -EOPNOTSUPP; |
| 11301 | if (f.file->f_op != &io_uring_fops) |
| 11302 | goto out_fput; |
| 11303 | |
| 11304 | ctx = f.file->private_data; |
| 11305 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 11306 | io_run_task_work(); |
| 11307 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11308 | mutex_lock(&ctx->uring_lock); |
| 11309 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 11310 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 11311 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 11312 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11313 | out_fput: |
| 11314 | fdput(f); |
| 11315 | return ret; |
| 11316 | } |
| 11317 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11318 | static int __init io_uring_init(void) |
| 11319 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11320 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 11321 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 11322 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 11323 | } while (0) |
| 11324 | |
| 11325 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 11326 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 11327 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 11328 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 11329 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 11330 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 11331 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 11332 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 11333 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 11334 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11335 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11336 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 11337 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 11338 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 11339 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 11340 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 11341 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 11342 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11343 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 11344 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 11345 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 11346 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 11347 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 11348 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 11349 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 11350 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11351 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11352 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 11353 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11354 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11355 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11356 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 11357 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11358 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11359 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 11360 | sizeof(struct io_uring_rsrc_update)); |
| 11361 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 11362 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 11363 | |
| 11364 | /* ->buf_index is u16 */ |
| 11365 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 11366 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11367 | /* should fit into one byte */ |
| 11368 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
| 11369 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 11370 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 11371 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11372 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 11373 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 11374 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11375 | return 0; |
| 11376 | }; |
| 11377 | __initcall(io_uring_init); |