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> |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 82 | #include <linux/audit.h> |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 83 | #include <linux/security.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 85 | #define CREATE_TRACE_POINTS |
| 86 | #include <trace/events/io_uring.h> |
| 87 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 88 | #include <uapi/linux/io_uring.h> |
| 89 | |
| 90 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 91 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 92 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 93 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 96 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 97 | /* only define max */ |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 100 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 101 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
| 104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) |
| 105 | |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
| 107 | |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 110 | |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
| 112 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 113 | |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 114 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 115 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
| 116 | REQ_F_ASYNC_DATA) |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 117 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 118 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
| 119 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 120 | struct io_uring { |
| 121 | u32 head ____cacheline_aligned_in_smp; |
| 122 | u32 tail ____cacheline_aligned_in_smp; |
| 123 | }; |
| 124 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 126 | * This data is shared with the application through the mmap at offsets |
| 127 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 128 | * |
| 129 | * The offsets to the member fields are published through struct |
| 130 | * io_sqring_offsets when calling io_uring_setup. |
| 131 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 132 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 133 | /* |
| 134 | * Head and tail offsets into the ring; the offsets need to be |
| 135 | * masked to get valid indices. |
| 136 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 137 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 138 | * and the application controls tail of the sq ring and the head of the |
| 139 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 140 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 141 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 142 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 144 | * ring_entries - 1) |
| 145 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 146 | u32 sq_ring_mask, cq_ring_mask; |
| 147 | /* Ring sizes (constant, power of 2) */ |
| 148 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Number of invalid entries dropped by the kernel due to |
| 151 | * invalid index stored in array |
| 152 | * |
| 153 | * Written by the kernel, shouldn't be modified by the |
| 154 | * application (i.e. get number of "new events" by comparing to |
| 155 | * cached value). |
| 156 | * |
| 157 | * After a new SQ head value was read by the application this |
| 158 | * counter includes all submissions that were dropped reaching |
| 159 | * the new SQ head (and possibly more). |
| 160 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 161 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 163 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 164 | * |
| 165 | * Written by the kernel, shouldn't be modified by the |
| 166 | * application. |
| 167 | * |
| 168 | * The application needs a full memory barrier before checking |
| 169 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 170 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 171 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 172 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 173 | * Runtime CQ flags |
| 174 | * |
| 175 | * Written by the application, shouldn't be modified by the |
| 176 | * kernel. |
| 177 | */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 178 | u32 cq_flags; |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 179 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 180 | * Number of completion events lost because the queue was full; |
| 181 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 182 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 183 | * the completion queue. |
| 184 | * |
| 185 | * Written by the kernel, shouldn't be modified by the |
| 186 | * application (i.e. get number of "new events" by comparing to |
| 187 | * cached value). |
| 188 | * |
| 189 | * As completion events come in out of order this counter is not |
| 190 | * ordered with any other data. |
| 191 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 192 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 193 | /* |
| 194 | * Ring buffer of completion events. |
| 195 | * |
| 196 | * The kernel writes completion events fresh every time they are |
| 197 | * produced, so the application is allowed to modify pending |
| 198 | * entries. |
| 199 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 200 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 203 | enum io_uring_cmd_flags { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 204 | IO_URING_F_COMPLETE_DEFER = 1, |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 205 | IO_URING_F_UNLOCKED = 2, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 206 | /* int's last bit, sign checks are usually faster than a bit test */ |
| 207 | IO_URING_F_NONBLOCK = INT_MIN, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 210 | struct io_mapped_ubuf { |
| 211 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 212 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 213 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 214 | unsigned long acct_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 215 | struct bio_vec bvec[]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 218 | struct io_ring_ctx; |
| 219 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 220 | struct io_overflow_cqe { |
| 221 | struct io_uring_cqe cqe; |
| 222 | struct list_head list; |
| 223 | }; |
| 224 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 225 | struct io_fixed_file { |
| 226 | /* file * with additional FFS_* flags */ |
| 227 | unsigned long file_ptr; |
| 228 | }; |
| 229 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 230 | struct io_rsrc_put { |
| 231 | struct list_head list; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 232 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 233 | union { |
| 234 | void *rsrc; |
| 235 | struct file *file; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 236 | struct io_mapped_ubuf *buf; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 237 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 240 | struct io_file_table { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 241 | struct io_fixed_file *files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 244 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 245 | struct percpu_ref refs; |
| 246 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 247 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 248 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 249 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 250 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 251 | }; |
| 252 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 253 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 254 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 255 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 256 | struct io_ring_ctx *ctx; |
| 257 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 258 | u64 **tags; |
| 259 | unsigned int nr; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 260 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 261 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 262 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 263 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 266 | struct io_buffer { |
| 267 | struct list_head list; |
| 268 | __u64 addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 269 | __u32 len; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 270 | __u16 bid; |
| 271 | }; |
| 272 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 273 | struct io_restriction { |
| 274 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 275 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 276 | u8 sqe_flags_allowed; |
| 277 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 278 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 279 | }; |
| 280 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 281 | enum { |
| 282 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 283 | IO_SQ_THREAD_SHOULD_PARK, |
| 284 | }; |
| 285 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 286 | struct io_sq_data { |
| 287 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 288 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 289 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 290 | |
| 291 | /* ctx's that are using this sqd */ |
| 292 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 293 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 294 | struct task_struct *thread; |
| 295 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 296 | |
| 297 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 298 | int sq_cpu; |
| 299 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 300 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 301 | |
| 302 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 303 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 304 | }; |
| 305 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 306 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 307 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 308 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 309 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 310 | struct io_submit_link { |
| 311 | struct io_kiocb *head; |
| 312 | struct io_kiocb *last; |
| 313 | }; |
| 314 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 315 | struct io_submit_state { |
Pavel Begunkov | 5a158c6 | 2021-10-06 16:06:48 +0100 | [diff] [blame] | 316 | /* inline/task_work completion list, under ->uring_lock */ |
| 317 | struct io_wq_work_node free_list; |
| 318 | /* batch completion logic */ |
| 319 | struct io_wq_work_list compl_reqs; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 320 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 321 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 322 | bool plug_started; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 323 | bool need_plug; |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 324 | bool flush_cqes; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 325 | unsigned short submit_nr; |
Pavel Begunkov | 5a158c6 | 2021-10-06 16:06:48 +0100 | [diff] [blame] | 326 | struct blk_plug plug; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 329 | struct io_ring_ctx { |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 330 | /* const or read-mostly hot data */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 331 | struct { |
| 332 | struct percpu_ref refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 333 | |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 334 | struct io_rings *rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 335 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 336 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 337 | unsigned int drain_next: 1; |
| 338 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 339 | unsigned int restricted: 1; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 340 | unsigned int off_timeout_used: 1; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 341 | unsigned int drain_active: 1; |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 342 | unsigned int drain_disabled: 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; |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 371 | int rsrc_cached_refs; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 372 | struct io_file_table file_table; |
| 373 | unsigned nr_user_files; |
| 374 | unsigned nr_user_bufs; |
| 375 | struct io_mapped_ubuf **user_bufs; |
| 376 | |
| 377 | struct io_submit_state submit_state; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 378 | struct list_head timeout_list; |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 379 | struct list_head ltimeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 380 | struct list_head cq_overflow_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 381 | struct xarray io_buffers; |
| 382 | struct xarray personalities; |
| 383 | u32 pers_next; |
| 384 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 385 | } ____cacheline_aligned_in_smp; |
| 386 | |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 387 | /* IRQ completion list, under ->completion_lock */ |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 388 | struct io_wq_work_list locked_free_list; |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 389 | unsigned int locked_free_nr; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 390 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 391 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 392 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 393 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 394 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 395 | struct list_head sqd_list; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 396 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 397 | unsigned long check_cq_overflow; |
| 398 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 399 | struct { |
| 400 | unsigned cached_cq_tail; |
| 401 | unsigned cq_entries; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 402 | struct eventfd_ctx *cq_ev_fd; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 403 | struct wait_queue_head 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 | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 420 | struct io_wq_work_list 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 | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 489 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 490 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 491 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 492 | }; |
| 493 | |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 494 | struct io_poll_update { |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 495 | struct file *file; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 496 | u64 old_user_data; |
| 497 | u64 new_user_data; |
| 498 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 499 | bool update_events; |
| 500 | bool update_user_data; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 501 | }; |
| 502 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 503 | struct io_close { |
| 504 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 505 | int fd; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 506 | u32 file_slot; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 509 | struct io_timeout_data { |
| 510 | struct io_kiocb *req; |
| 511 | struct hrtimer timer; |
| 512 | struct timespec64 ts; |
| 513 | enum hrtimer_mode mode; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 514 | u32 flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 517 | struct io_accept { |
| 518 | struct file *file; |
| 519 | struct sockaddr __user *addr; |
| 520 | int __user *addr_len; |
| 521 | int flags; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 522 | u32 file_slot; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 523 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | struct io_sync { |
| 527 | struct file *file; |
| 528 | loff_t len; |
| 529 | loff_t off; |
| 530 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 531 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 532 | }; |
| 533 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 534 | struct io_cancel { |
| 535 | struct file *file; |
| 536 | u64 addr; |
| 537 | }; |
| 538 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 539 | struct io_timeout { |
| 540 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 541 | u32 off; |
| 542 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 543 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 544 | /* head of the link, used by linked timeouts only */ |
| 545 | struct io_kiocb *head; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 546 | /* for linked completions */ |
| 547 | struct io_kiocb *prev; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 550 | struct io_timeout_rem { |
| 551 | struct file *file; |
| 552 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 553 | |
| 554 | /* timeout update */ |
| 555 | struct timespec64 ts; |
| 556 | u32 flags; |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 557 | bool ltimeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 558 | }; |
| 559 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 560 | struct io_rw { |
| 561 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 562 | struct kiocb kiocb; |
| 563 | u64 addr; |
| 564 | u64 len; |
| 565 | }; |
| 566 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 567 | struct io_connect { |
| 568 | struct file *file; |
| 569 | struct sockaddr __user *addr; |
| 570 | int addr_len; |
| 571 | }; |
| 572 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 573 | struct io_sr_msg { |
| 574 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 575 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 576 | struct compat_msghdr __user *umsg_compat; |
| 577 | struct user_msghdr __user *umsg; |
| 578 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 579 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 580 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 581 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 582 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | struct io_open { |
| 586 | struct file *file; |
| 587 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 588 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 589 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 590 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 591 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 594 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 595 | struct file *file; |
| 596 | u64 arg; |
| 597 | u32 nr_args; |
| 598 | u32 offset; |
| 599 | }; |
| 600 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 601 | struct io_fadvise { |
| 602 | struct file *file; |
| 603 | u64 offset; |
| 604 | u32 len; |
| 605 | u32 advice; |
| 606 | }; |
| 607 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 608 | struct io_madvise { |
| 609 | struct file *file; |
| 610 | u64 addr; |
| 611 | u32 len; |
| 612 | u32 advice; |
| 613 | }; |
| 614 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 615 | struct io_epoll { |
| 616 | struct file *file; |
| 617 | int epfd; |
| 618 | int op; |
| 619 | int fd; |
| 620 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 623 | struct io_splice { |
| 624 | struct file *file_out; |
| 625 | struct file *file_in; |
| 626 | loff_t off_out; |
| 627 | loff_t off_in; |
| 628 | u64 len; |
| 629 | unsigned int flags; |
| 630 | }; |
| 631 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 632 | struct io_provide_buf { |
| 633 | struct file *file; |
| 634 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 635 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 636 | __u32 bgid; |
| 637 | __u16 nbufs; |
| 638 | __u16 bid; |
| 639 | }; |
| 640 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 641 | struct io_statx { |
| 642 | struct file *file; |
| 643 | int dfd; |
| 644 | unsigned int mask; |
| 645 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 646 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 647 | struct statx __user *buffer; |
| 648 | }; |
| 649 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 650 | struct io_shutdown { |
| 651 | struct file *file; |
| 652 | int how; |
| 653 | }; |
| 654 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 655 | struct io_rename { |
| 656 | struct file *file; |
| 657 | int old_dfd; |
| 658 | int new_dfd; |
| 659 | struct filename *oldpath; |
| 660 | struct filename *newpath; |
| 661 | int flags; |
| 662 | }; |
| 663 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 664 | struct io_unlink { |
| 665 | struct file *file; |
| 666 | int dfd; |
| 667 | int flags; |
| 668 | struct filename *filename; |
| 669 | }; |
| 670 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 671 | struct io_mkdir { |
| 672 | struct file *file; |
| 673 | int dfd; |
| 674 | umode_t mode; |
| 675 | struct filename *filename; |
| 676 | }; |
| 677 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 678 | struct io_symlink { |
| 679 | struct file *file; |
| 680 | int new_dfd; |
| 681 | struct filename *oldpath; |
| 682 | struct filename *newpath; |
| 683 | }; |
| 684 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 685 | struct io_hardlink { |
| 686 | struct file *file; |
| 687 | int old_dfd; |
| 688 | int new_dfd; |
| 689 | struct filename *oldpath; |
| 690 | struct filename *newpath; |
| 691 | int flags; |
| 692 | }; |
| 693 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 694 | struct io_async_connect { |
| 695 | struct sockaddr_storage address; |
| 696 | }; |
| 697 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 698 | struct io_async_msghdr { |
| 699 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 700 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 701 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 702 | struct sockaddr __user *uaddr; |
| 703 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 704 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 705 | }; |
| 706 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 707 | struct io_rw_state { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 708 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 709 | struct iov_iter_state iter_state; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 710 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | struct io_async_rw { |
| 714 | struct io_rw_state s; |
| 715 | const struct iovec *free_iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 716 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 717 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 718 | }; |
| 719 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 720 | enum { |
| 721 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 722 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 723 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 724 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 725 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 726 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 727 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 728 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 729 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 730 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 731 | REQ_F_INFLIGHT_BIT, |
| 732 | REQ_F_CUR_POS_BIT, |
| 733 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 734 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 735 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 736 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 737 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 738 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 739 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 740 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 741 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 742 | REQ_F_ARM_LTIMEOUT_BIT, |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 743 | REQ_F_ASYNC_DATA_BIT, |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 744 | REQ_F_SKIP_LINK_CQES_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 745 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 746 | REQ_F_SUPPORT_NOWAIT_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 747 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 748 | |
| 749 | /* not a real bit, just to check we're not overflowing the space */ |
| 750 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 751 | }; |
| 752 | |
| 753 | enum { |
| 754 | /* ctx owns file */ |
| 755 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 756 | /* drain existing IO first */ |
| 757 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 758 | /* linked sqes */ |
| 759 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 760 | /* doesn't sever on completion < 0 */ |
| 761 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 762 | /* IOSQE_ASYNC */ |
| 763 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 764 | /* IOSQE_BUFFER_SELECT */ |
| 765 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 766 | /* IOSQE_CQE_SKIP_SUCCESS */ |
| 767 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_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), |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 789 | /* supports async reads/writes */ |
| 790 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 791 | /* regular file */ |
| 792 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 793 | /* has creds assigned */ |
| 794 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 795 | /* skip refcounting if not set */ |
| 796 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 797 | /* there is a linked timeout that has to be armed */ |
| 798 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 799 | /* ->async_data allocated */ |
| 800 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 801 | /* don't post CQEs while failing linked requests */ |
| 802 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_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; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 859 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 860 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 861 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 862 | /* polled IO has completed */ |
| 863 | u8 iopoll_completed; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 864 | u16 buf_index; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 865 | unsigned int flags; |
| 866 | |
| 867 | u64 user_data; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 868 | u32 result; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 869 | u32 cflags; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 870 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 871 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 872 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 873 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 874 | struct percpu_ref *fixed_rsrc_refs; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 875 | /* store used ubuf, so we can prevent reloading */ |
| 876 | struct io_mapped_ubuf *imu; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 877 | |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 878 | /* used by request caches, completion batching and iopoll */ |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 879 | struct io_wq_work_node comp_list; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 880 | atomic_t refs; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 881 | struct io_kiocb *link; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 882 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 883 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 884 | struct hlist_node hash_node; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 885 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 886 | struct async_poll *apoll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 887 | /* opcode allocated if it needs to store data for async defer */ |
| 888 | void *async_data; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 889 | struct io_wq_work work; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 890 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 891 | const struct cred *creds; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 892 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 893 | struct io_buffer *kbuf; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 894 | }; |
| 895 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 896 | struct io_tctx_node { |
| 897 | struct list_head ctx_node; |
| 898 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 899 | struct io_ring_ctx *ctx; |
| 900 | }; |
| 901 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 902 | struct io_defer_entry { |
| 903 | struct list_head list; |
| 904 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 905 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 906 | }; |
| 907 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 908 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 909 | /* needs req->file assigned */ |
| 910 | unsigned needs_file : 1; |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 911 | /* should block plug */ |
| 912 | unsigned plug : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 913 | /* hash wq insertion if file is a regular file */ |
| 914 | unsigned hash_reg_file : 1; |
| 915 | /* unbound wq insertion if file is a non-regular file */ |
| 916 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 917 | /* set if opcode supports polled "wait" */ |
| 918 | unsigned pollin : 1; |
| 919 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 920 | /* op supports buffer selection */ |
| 921 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 922 | /* do prep async if is going to be punted */ |
| 923 | unsigned needs_async_setup : 1; |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 924 | /* opcode is not supported by this kernel */ |
| 925 | unsigned not_supported : 1; |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 926 | /* skip auditing */ |
| 927 | unsigned audit_skip : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 928 | /* size of async data needed, if any */ |
| 929 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 930 | }; |
| 931 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 932 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 933 | [IORING_OP_NOP] = {}, |
| 934 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 935 | .needs_file = 1, |
| 936 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 937 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 938 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 939 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 940 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 941 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 942 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 943 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 944 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 945 | .needs_file = 1, |
| 946 | .hash_reg_file = 1, |
| 947 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 948 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 949 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 950 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 951 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 952 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 953 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 954 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 955 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 956 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 957 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 958 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 959 | .needs_file = 1, |
| 960 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 961 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 962 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 963 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 964 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 965 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 966 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 967 | .needs_file = 1, |
| 968 | .hash_reg_file = 1, |
| 969 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 970 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 971 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 972 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 973 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 974 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 975 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 976 | .needs_file = 1, |
| 977 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 978 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 979 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 980 | [IORING_OP_POLL_REMOVE] = { |
| 981 | .audit_skip = 1, |
| 982 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 983 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 984 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 985 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 986 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 987 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 988 | .needs_file = 1, |
| 989 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 990 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 991 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 992 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 993 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 994 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 995 | .needs_file = 1, |
| 996 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 997 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 998 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 999 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1000 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1001 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1002 | [IORING_OP_TIMEOUT] = { |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1003 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1004 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1005 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 1006 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 1007 | /* used by timeout updates' prep() */ |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1008 | .audit_skip = 1, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 1009 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1010 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1011 | .needs_file = 1, |
| 1012 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1013 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1014 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1015 | [IORING_OP_ASYNC_CANCEL] = { |
| 1016 | .audit_skip = 1, |
| 1017 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1018 | [IORING_OP_LINK_TIMEOUT] = { |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1019 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1020 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1021 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1022 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1023 | .needs_file = 1, |
| 1024 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1025 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 1026 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1027 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1028 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1029 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1030 | .needs_file = 1, |
| 1031 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1032 | [IORING_OP_OPENAT] = {}, |
| 1033 | [IORING_OP_CLOSE] = {}, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1034 | [IORING_OP_FILES_UPDATE] = { |
| 1035 | .audit_skip = 1, |
| 1036 | }, |
| 1037 | [IORING_OP_STATX] = { |
| 1038 | .audit_skip = 1, |
| 1039 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1040 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1041 | .needs_file = 1, |
| 1042 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1043 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1044 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1045 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1046 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1047 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1048 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1049 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1050 | .needs_file = 1, |
Jens Axboe | 7b3188e | 2021-08-30 19:37:41 -0600 | [diff] [blame] | 1051 | .hash_reg_file = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1052 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1053 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1054 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1055 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1056 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1057 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1058 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1059 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1060 | .audit_skip = 1, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1061 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1062 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1063 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1064 | .needs_file = 1, |
| 1065 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1066 | .pollout = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1067 | .audit_skip = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1068 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1069 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1070 | .needs_file = 1, |
| 1071 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1072 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1073 | .buffer_select = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1074 | .audit_skip = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1075 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1076 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1077 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1078 | [IORING_OP_EPOLL_CTL] = { |
| 1079 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1080 | .audit_skip = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1081 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1082 | [IORING_OP_SPLICE] = { |
| 1083 | .needs_file = 1, |
| 1084 | .hash_reg_file = 1, |
| 1085 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1086 | .audit_skip = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1087 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1088 | [IORING_OP_PROVIDE_BUFFERS] = { |
| 1089 | .audit_skip = 1, |
| 1090 | }, |
| 1091 | [IORING_OP_REMOVE_BUFFERS] = { |
| 1092 | .audit_skip = 1, |
| 1093 | }, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1094 | [IORING_OP_TEE] = { |
| 1095 | .needs_file = 1, |
| 1096 | .hash_reg_file = 1, |
| 1097 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1098 | .audit_skip = 1, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1099 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1100 | [IORING_OP_SHUTDOWN] = { |
| 1101 | .needs_file = 1, |
| 1102 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1103 | [IORING_OP_RENAMEAT] = {}, |
| 1104 | [IORING_OP_UNLINKAT] = {}, |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 1105 | [IORING_OP_MKDIRAT] = {}, |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 1106 | [IORING_OP_SYMLINKAT] = {}, |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 1107 | [IORING_OP_LINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1108 | }; |
| 1109 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1110 | /* requests with any of those set should undergo io_disarm_next() */ |
| 1111 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) |
| 1112 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1113 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 1114 | static void io_uring_del_tctx_node(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1115 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1116 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1117 | bool cancel_all); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 1118 | 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] | 1119 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1120 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
| 1121 | |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1122 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1123 | static void io_put_req_deferred(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1124 | static void io_dismantle_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1125 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 1126 | 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] | 1127 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 1128 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1129 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 1130 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1131 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1132 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1133 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1134 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1135 | static void io_req_task_queue(struct io_kiocb *req); |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1136 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1137 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1138 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1139 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 1140 | unsigned int issue_flags, u32 slot_index); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 1141 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1142 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 1143 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1144 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1145 | static struct kmem_cache *req_cachep; |
| 1146 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1147 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1148 | |
| 1149 | struct sock *io_uring_get_socket(struct file *file) |
| 1150 | { |
| 1151 | #if defined(CONFIG_UNIX) |
| 1152 | if (file->f_op == &io_uring_fops) { |
| 1153 | struct io_ring_ctx *ctx = file->private_data; |
| 1154 | |
| 1155 | return ctx->ring_sock->sk; |
| 1156 | } |
| 1157 | #endif |
| 1158 | return NULL; |
| 1159 | } |
| 1160 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1161 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1162 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
| 1163 | { |
| 1164 | if (!*locked) { |
| 1165 | mutex_lock(&ctx->uring_lock); |
| 1166 | *locked = true; |
| 1167 | } |
| 1168 | } |
| 1169 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1170 | #define io_for_each_link(pos, head) \ |
| 1171 | for (pos = (head); pos; pos = pos->link) |
| 1172 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1173 | /* |
| 1174 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1175 | * see commit f958d7b528b1 for details. |
| 1176 | */ |
| 1177 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1178 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1179 | |
| 1180 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1181 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1182 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1183 | return atomic_inc_not_zero(&req->refs); |
| 1184 | } |
| 1185 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1186 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1187 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1188 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
| 1189 | return true; |
| 1190 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1191 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1192 | return atomic_dec_and_test(&req->refs); |
| 1193 | } |
| 1194 | |
| 1195 | static inline void req_ref_put(struct io_kiocb *req) |
| 1196 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1197 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1198 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
| 1199 | } |
| 1200 | |
| 1201 | static inline void req_ref_get(struct io_kiocb *req) |
| 1202 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1203 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1204 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1205 | atomic_inc(&req->refs); |
| 1206 | } |
| 1207 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1208 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
| 1209 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1210 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1211 | __io_submit_flush_completions(ctx); |
| 1212 | } |
| 1213 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1214 | 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] | 1215 | { |
| 1216 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1217 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1218 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1222 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1223 | { |
| 1224 | __io_req_set_refcount(req, 1); |
| 1225 | } |
| 1226 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1227 | #define IO_RSRC_REF_BATCH 100 |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1228 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1229 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, |
| 1230 | struct io_ring_ctx *ctx) |
| 1231 | __must_hold(&ctx->uring_lock) |
| 1232 | { |
| 1233 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
| 1234 | |
| 1235 | if (ref) { |
| 1236 | if (ref == &ctx->rsrc_node->refs) |
| 1237 | ctx->rsrc_cached_refs++; |
| 1238 | else |
| 1239 | percpu_ref_put(ref); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1244 | { |
| 1245 | if (req->fixed_rsrc_refs) |
| 1246 | percpu_ref_put(req->fixed_rsrc_refs); |
| 1247 | } |
| 1248 | |
| 1249 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) |
| 1250 | __must_hold(&ctx->uring_lock) |
| 1251 | { |
| 1252 | if (ctx->rsrc_cached_refs) { |
| 1253 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); |
| 1254 | ctx->rsrc_cached_refs = 0; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) |
| 1259 | __must_hold(&ctx->uring_lock) |
| 1260 | { |
| 1261 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; |
| 1262 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); |
| 1263 | } |
| 1264 | |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 1265 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
| 1266 | struct io_ring_ctx *ctx) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1267 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1268 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1269 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1270 | ctx->rsrc_cached_refs--; |
| 1271 | if (unlikely(ctx->rsrc_cached_refs < 0)) |
| 1272 | io_rsrc_refs_refill(ctx); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1276 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1277 | { |
| 1278 | bool got = percpu_ref_tryget(ref); |
| 1279 | |
| 1280 | /* already at zero, wait for ->release() */ |
| 1281 | if (!got) |
| 1282 | wait_for_completion(compl); |
| 1283 | percpu_ref_resurrect(ref); |
| 1284 | if (got) |
| 1285 | percpu_ref_put(ref); |
| 1286 | } |
| 1287 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1288 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1289 | bool cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1290 | { |
| 1291 | struct io_kiocb *req; |
| 1292 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1293 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1294 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1295 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1296 | return true; |
| 1297 | |
| 1298 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1299 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1300 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1301 | } |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 1305 | static inline bool req_has_async_data(struct io_kiocb *req) |
| 1306 | { |
| 1307 | return req->flags & REQ_F_ASYNC_DATA; |
| 1308 | } |
| 1309 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1310 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1311 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1312 | req->flags |= REQ_F_FAIL; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1313 | if (req->flags & REQ_F_CQE_SKIP) { |
| 1314 | req->flags &= ~REQ_F_CQE_SKIP; |
| 1315 | req->flags |= REQ_F_SKIP_LINK_CQES; |
| 1316 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1317 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1318 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1319 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1320 | { |
| 1321 | req_set_fail(req); |
| 1322 | req->result = res; |
| 1323 | } |
| 1324 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1325 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1326 | { |
| 1327 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1328 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1329 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1332 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1333 | { |
| 1334 | return !req->timeout.off; |
| 1335 | } |
| 1336 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1337 | static __cold void io_fallback_req_func(struct work_struct *work) |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1338 | { |
| 1339 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1340 | fallback_work.work); |
| 1341 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1342 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1343 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1344 | |
| 1345 | percpu_ref_get(&ctx->refs); |
| 1346 | 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] | 1347 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1348 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1349 | if (locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1350 | io_submit_flush_completions(ctx); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1351 | mutex_unlock(&ctx->uring_lock); |
| 1352 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1353 | percpu_ref_put(&ctx->refs); |
| 1354 | } |
| 1355 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1356 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1357 | { |
| 1358 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1359 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1360 | |
| 1361 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1362 | if (!ctx) |
| 1363 | return NULL; |
| 1364 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1365 | /* |
| 1366 | * Use 5 bits less than the max cq entries, that should give us around |
| 1367 | * 32 entries per hash list if totally full and uniformly spread. |
| 1368 | */ |
| 1369 | hash_bits = ilog2(p->cq_entries); |
| 1370 | hash_bits -= 5; |
| 1371 | if (hash_bits <= 0) |
| 1372 | hash_bits = 1; |
| 1373 | ctx->cancel_hash_bits = hash_bits; |
| 1374 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1375 | GFP_KERNEL); |
| 1376 | if (!ctx->cancel_hash) |
| 1377 | goto err; |
| 1378 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1379 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1380 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1381 | if (!ctx->dummy_ubuf) |
| 1382 | goto err; |
| 1383 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1384 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1385 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1386 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1387 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1388 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1389 | |
| 1390 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1391 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1392 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1393 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1394 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1395 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1396 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1397 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1398 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1399 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1400 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 1401 | INIT_WQ_LIST(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1402 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1403 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1404 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1405 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1406 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1407 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1408 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1409 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1410 | ctx->submit_state.free_list.next = NULL; |
| 1411 | INIT_WQ_LIST(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1412 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1413 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1414 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1415 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1416 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1417 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1418 | kfree(ctx); |
| 1419 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1422 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1423 | { |
| 1424 | struct io_rings *r = ctx->rings; |
| 1425 | |
| 1426 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1427 | ctx->cq_extra--; |
| 1428 | } |
| 1429 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1430 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1431 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1432 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1433 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1434 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1435 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1436 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1437 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1438 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1439 | } |
| 1440 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1441 | #define FFS_NOWAIT 0x1UL |
| 1442 | #define FFS_ISREG 0x2UL |
| 1443 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1444 | |
| 1445 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1446 | { |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1447 | return req->flags & REQ_F_FIXED_FILE; |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1450 | static inline void io_req_track_inflight(struct io_kiocb *req) |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1451 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1452 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1453 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1454 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1458 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1459 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1460 | if (WARN_ON_ONCE(!req->link)) |
| 1461 | return NULL; |
| 1462 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1463 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1464 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1465 | |
| 1466 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1467 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1468 | __io_req_set_refcount(req->link, 2); |
| 1469 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1473 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1474 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1475 | return NULL; |
| 1476 | return __io_prep_linked_timeout(req); |
| 1477 | } |
| 1478 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1479 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1480 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1481 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1482 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1483 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1484 | if (!(req->flags & REQ_F_CREDS)) { |
| 1485 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1486 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1487 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1488 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1489 | req->work.list.next = NULL; |
| 1490 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1491 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1492 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1493 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1494 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1495 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1496 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1497 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1498 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1499 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1500 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1501 | |
| 1502 | switch (req->opcode) { |
| 1503 | case IORING_OP_SPLICE: |
| 1504 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1505 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1506 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1507 | break; |
| 1508 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1509 | } |
| 1510 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1511 | static void io_prep_async_link(struct io_kiocb *req) |
| 1512 | { |
| 1513 | struct io_kiocb *cur; |
| 1514 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1515 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1516 | struct io_ring_ctx *ctx = req->ctx; |
| 1517 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1518 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1519 | io_for_each_link(cur, req) |
| 1520 | io_prep_async_work(cur); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1521 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1522 | } else { |
| 1523 | io_for_each_link(cur, req) |
| 1524 | io_prep_async_work(cur); |
| 1525 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1526 | } |
| 1527 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1528 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
| 1529 | { |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 1530 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1531 | struct io_submit_state *state = &req->ctx->submit_state; |
| 1532 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 1533 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1534 | ctx->submit_state.flush_cqes = true; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1535 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
| 1536 | } |
| 1537 | |
Arnd Bergmann | 0016924 | 2021-10-19 17:34:53 +0200 | [diff] [blame] | 1538 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1539 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1540 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1541 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1542 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1543 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1544 | BUG_ON(!tctx); |
| 1545 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1546 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1547 | /* init ->work of the whole link before punting */ |
| 1548 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1549 | |
| 1550 | /* |
| 1551 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1552 | * happen, catch it here and ensure the request is marked as |
| 1553 | * canceled. That will make io-wq go through the usual work cancel |
| 1554 | * procedure rather than attempt to run this request (or create a new |
| 1555 | * worker for it). |
| 1556 | */ |
| 1557 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1558 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1559 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1560 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1561 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1562 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1563 | if (link) |
| 1564 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1565 | } |
| 1566 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1567 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1568 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1569 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1570 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1571 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1572 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1573 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1574 | if (status) |
| 1575 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1576 | atomic_set(&req->ctx->cq_timeouts, |
| 1577 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1578 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1579 | io_fill_cqe_req(req, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1580 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1584 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1585 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1586 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1587 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1588 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1589 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1590 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1591 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1592 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1593 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1594 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1595 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1596 | } |
| 1597 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1598 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1599 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1600 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1601 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1602 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1603 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1604 | while (!list_empty(&ctx->timeout_list)) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1605 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1606 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1607 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1608 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1609 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1610 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1611 | |
| 1612 | /* |
| 1613 | * Since seq can easily wrap around over time, subtract |
| 1614 | * the last seq at which timeouts were flushed before comparing. |
| 1615 | * Assuming not more than 2^31-1 events have happened since, |
| 1616 | * these subtractions won't have wrapped, so we can check if |
| 1617 | * target is in [last_seq, current_seq] by comparing the two. |
| 1618 | */ |
| 1619 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1620 | events_got = seq - ctx->cq_last_tm_flush; |
| 1621 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1622 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1623 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1624 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1625 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1626 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1627 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1628 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1629 | } |
| 1630 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1631 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1632 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1633 | if (ctx->off_timeout_used) |
| 1634 | io_flush_timeouts(ctx); |
| 1635 | if (ctx->drain_active) |
| 1636 | io_queue_deferred(ctx); |
| 1637 | } |
| 1638 | |
| 1639 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1640 | { |
| 1641 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1642 | __io_commit_cqring_flush(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1643 | /* order cqe stores with ring update */ |
| 1644 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1645 | } |
| 1646 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1647 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1648 | { |
| 1649 | struct io_rings *r = ctx->rings; |
| 1650 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1651 | 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] | 1652 | } |
| 1653 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1654 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1655 | { |
| 1656 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1657 | } |
| 1658 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1659 | 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] | 1660 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1661 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1662 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1663 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1664 | /* |
| 1665 | * writes to the cq entry need to come after reading head; the |
| 1666 | * control dependency is enough as we're using WRITE_ONCE to |
| 1667 | * fill the cq entry |
| 1668 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1669 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1670 | return NULL; |
| 1671 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1672 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1673 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1676 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1677 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1678 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1679 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1680 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1681 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1682 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1685 | /* |
| 1686 | * This should only get called when at least one event has been posted. |
| 1687 | * Some applications rely on the eventfd notification count only changing |
| 1688 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1689 | * 1:1 relationship between how many times this function is called (and |
| 1690 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1691 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1692 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1693 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1694 | /* |
| 1695 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1696 | * io_should_wake() handle the termination of the loop and only |
| 1697 | * wake as many waiters as we need to. |
| 1698 | */ |
| 1699 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1700 | wake_up_all(&ctx->cq_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1701 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1702 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1703 | } |
| 1704 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1705 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1706 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1707 | /* see waitqueue_active() comment */ |
| 1708 | smp_mb(); |
| 1709 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1710 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1711 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1712 | wake_up_all(&ctx->cq_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1713 | } |
| 1714 | if (io_should_trigger_evfd(ctx)) |
| 1715 | eventfd_signal(ctx->cq_ev_fd, 1); |
| 1716 | } |
| 1717 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1718 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1719 | 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] | 1720 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1721 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1722 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1723 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1724 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1725 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1726 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1727 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1728 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1729 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1730 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1731 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1732 | if (!cqe && !force) |
| 1733 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1734 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1735 | struct io_overflow_cqe, list); |
| 1736 | if (cqe) |
| 1737 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1738 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1739 | io_account_cq_overflow(ctx); |
| 1740 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1741 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1742 | list_del(&ocqe->list); |
| 1743 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1746 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1747 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1748 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1749 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1750 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1751 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1752 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1753 | if (posted) |
| 1754 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1755 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1756 | if (posted) |
| 1757 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1758 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1761 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1762 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1763 | bool ret = true; |
| 1764 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1765 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1766 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1767 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1768 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1769 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1770 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1771 | mutex_unlock(&ctx->uring_lock); |
| 1772 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1773 | |
| 1774 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1777 | /* must to be called somewhat shortly after putting a request */ |
| 1778 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1779 | { |
| 1780 | struct io_uring_task *tctx = task->io_uring; |
| 1781 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1782 | if (likely(task == current)) { |
| 1783 | tctx->cached_refs += nr; |
| 1784 | } else { |
| 1785 | percpu_counter_sub(&tctx->inflight, nr); |
| 1786 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1787 | wake_up(&tctx->wait); |
| 1788 | put_task_struct_many(task, nr); |
| 1789 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1790 | } |
| 1791 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1792 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1793 | { |
| 1794 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1795 | |
| 1796 | percpu_counter_add(&tctx->inflight, refill); |
| 1797 | refcount_add(refill, ¤t->usage); |
| 1798 | tctx->cached_refs += refill; |
| 1799 | } |
| 1800 | |
| 1801 | static inline void io_get_task_refs(int nr) |
| 1802 | { |
| 1803 | struct io_uring_task *tctx = current->io_uring; |
| 1804 | |
| 1805 | tctx->cached_refs -= nr; |
| 1806 | if (unlikely(tctx->cached_refs < 0)) |
| 1807 | io_task_refs_refill(tctx); |
| 1808 | } |
| 1809 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1810 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1811 | s32 res, u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1812 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1813 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1814 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1815 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1816 | if (!ocqe) { |
| 1817 | /* |
| 1818 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1819 | * or cannot allocate an overflow entry, then we need to drop it |
| 1820 | * on the floor. |
| 1821 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1822 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1823 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1824 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1825 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1826 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1827 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1828 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1829 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1830 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1831 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1832 | ocqe->cqe.res = res; |
| 1833 | ocqe->cqe.flags = cflags; |
| 1834 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1835 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1838 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
| 1839 | s32 res, u32 cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1840 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1841 | struct io_uring_cqe *cqe; |
| 1842 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1843 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1844 | |
| 1845 | /* |
| 1846 | * If we can't get a cq entry, userspace overflowed the |
| 1847 | * submission (by quite a lot). Increment the overflow count in |
| 1848 | * the ring. |
| 1849 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1850 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1851 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1852 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1853 | WRITE_ONCE(cqe->res, res); |
| 1854 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1855 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1856 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1857 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1860 | 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] | 1861 | { |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1862 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1863 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
| 1867 | s32 res, u32 cflags) |
| 1868 | { |
| 1869 | ctx->cq_extra++; |
| 1870 | return __io_fill_cqe(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1873 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
| 1874 | u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1875 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1876 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1877 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1878 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1879 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1880 | __io_fill_cqe(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1881 | /* |
| 1882 | * If we're the last reference to this request, add to our locked |
| 1883 | * free_list cache. |
| 1884 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1885 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1886 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1887 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1888 | io_disarm_next(req); |
| 1889 | if (req->link) { |
| 1890 | io_req_task_queue(req->link); |
| 1891 | req->link = NULL; |
| 1892 | } |
| 1893 | } |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1894 | io_req_put_rsrc(req, ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1895 | io_dismantle_req(req); |
| 1896 | io_put_task(req->task, 1); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1897 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1898 | ctx->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1899 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1900 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1901 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1902 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1905 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
| 1906 | u32 cflags) |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1907 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1908 | req->result = res; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 1909 | req->cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1910 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1911 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1912 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1913 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1914 | s32 res, u32 cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1915 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1916 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1917 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1918 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1919 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1920 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1921 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1922 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1923 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1924 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1927 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1928 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1929 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1930 | io_req_complete_post(req, res, 0); |
| 1931 | } |
| 1932 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 1933 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 1934 | { |
| 1935 | /* |
| 1936 | * We don't submit, fail them all, for that replace hardlinks with |
| 1937 | * normal links. Extra REQ_F_LINK is tolerated. |
| 1938 | */ |
| 1939 | req->flags &= ~REQ_F_HARDLINK; |
| 1940 | req->flags |= REQ_F_LINK; |
| 1941 | io_req_complete_failed(req, req->result); |
| 1942 | } |
| 1943 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1944 | /* |
| 1945 | * Don't initialise the fields below on every allocation, but do that in |
| 1946 | * advance and keep them valid across allocations. |
| 1947 | */ |
| 1948 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1949 | { |
| 1950 | req->ctx = ctx; |
| 1951 | req->link = NULL; |
| 1952 | req->async_data = NULL; |
| 1953 | /* not necessary, but safer to zero */ |
| 1954 | req->result = 0; |
| 1955 | } |
| 1956 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1957 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1958 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1959 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1960 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1961 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1962 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1963 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1966 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1967 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1968 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1969 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1970 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1971 | /* |
| 1972 | * If we have more than a batch's worth of requests in our IRQ side |
| 1973 | * locked cache, grab the lock and move them over to our submission |
| 1974 | * side cache. |
| 1975 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1976 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1977 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1978 | return !!state->free_list.next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1981 | /* |
| 1982 | * A request might get retired back into the request caches even before opcode |
| 1983 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 1984 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 1985 | * and with extra caution to not get a request that is still worked on. |
| 1986 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1987 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1988 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1989 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1990 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1991 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1992 | void *reqs[IO_REQ_ALLOC_BATCH]; |
| 1993 | struct io_kiocb *req; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1994 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1995 | |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1996 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 1997 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1998 | |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 1999 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2000 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2001 | /* |
| 2002 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2003 | * retry single alloc to be on the safe side. |
| 2004 | */ |
| 2005 | if (unlikely(ret <= 0)) { |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2006 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2007 | if (!reqs[0]) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2008 | return false; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2009 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2010 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2011 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2012 | percpu_ref_get_many(&ctx->refs, ret); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2013 | for (i = 0; i < ret; i++) { |
| 2014 | req = reqs[i]; |
| 2015 | |
| 2016 | io_preinit_req(req, ctx); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2017 | wq_stack_add_head(&req->comp_list, &state->free_list); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2018 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2019 | return true; |
| 2020 | } |
| 2021 | |
| 2022 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) |
| 2023 | { |
| 2024 | if (unlikely(!ctx->submit_state.free_list.next)) |
| 2025 | return __io_alloc_req_refill(ctx); |
| 2026 | return true; |
| 2027 | } |
| 2028 | |
| 2029 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
| 2030 | { |
| 2031 | struct io_wq_work_node *node; |
| 2032 | |
| 2033 | node = wq_stack_extract(&ctx->submit_state.free_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2034 | return container_of(node, struct io_kiocb, comp_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2037 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2038 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2039 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2040 | fput(file); |
| 2041 | } |
| 2042 | |
Pavel Begunkov | 6b63952 | 2021-09-08 16:40:50 +0100 | [diff] [blame] | 2043 | static inline void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2044 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 2045 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2046 | |
Pavel Begunkov | 867f8fa | 2021-10-04 20:02:58 +0100 | [diff] [blame] | 2047 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 2048 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2049 | if (!(flags & REQ_F_FIXED_FILE)) |
| 2050 | io_put_file(req->file); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2051 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2052 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2053 | static __cold void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2054 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2055 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2056 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2057 | io_req_put_rsrc(req, ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2058 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2059 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2060 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2061 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2062 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 2063 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2064 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2065 | } |
| 2066 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2067 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2068 | { |
| 2069 | struct io_kiocb *nxt = req->link; |
| 2070 | |
| 2071 | req->link = nxt->link; |
| 2072 | nxt->link = NULL; |
| 2073 | } |
| 2074 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2075 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 2076 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2077 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2078 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2079 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2080 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 2081 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2082 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2083 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2084 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2085 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2086 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2087 | list_del(&link->timeout.list); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2088 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 2089 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2090 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2091 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2092 | } |
| 2093 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2094 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2095 | } |
| 2096 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2097 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2098 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2099 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2100 | struct io_kiocb *nxt, *link = req->link; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2101 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2102 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2103 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2104 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2105 | long res = -ECANCELED; |
| 2106 | |
| 2107 | if (link->flags & REQ_F_FAIL) |
| 2108 | res = link->result; |
| 2109 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2110 | nxt = link->link; |
| 2111 | link->link = NULL; |
| 2112 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2113 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2114 | if (!ignore_cqes) { |
| 2115 | link->flags &= ~REQ_F_CQE_SKIP; |
| 2116 | io_fill_cqe_req(link, res, 0); |
| 2117 | } |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2118 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2119 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2120 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2121 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2122 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2123 | static bool io_disarm_next(struct io_kiocb *req) |
| 2124 | __must_hold(&req->ctx->completion_lock) |
| 2125 | { |
| 2126 | bool posted = false; |
| 2127 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2128 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2129 | struct io_kiocb *link = req->link; |
| 2130 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2131 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2132 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2133 | io_remove_next_linked(req); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2134 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 2135 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2136 | io_put_req_deferred(link); |
| 2137 | posted = true; |
| 2138 | } |
| 2139 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2140 | struct io_ring_ctx *ctx = req->ctx; |
| 2141 | |
| 2142 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2143 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2144 | spin_unlock_irq(&ctx->timeout_lock); |
| 2145 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2146 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2147 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2148 | posted |= (req->link != NULL); |
| 2149 | io_fail_links(req); |
| 2150 | } |
| 2151 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2152 | } |
| 2153 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2154 | static void __io_req_find_next_prep(struct io_kiocb *req) |
| 2155 | { |
| 2156 | struct io_ring_ctx *ctx = req->ctx; |
| 2157 | bool posted; |
| 2158 | |
| 2159 | spin_lock(&ctx->completion_lock); |
| 2160 | posted = io_disarm_next(req); |
| 2161 | if (posted) |
| 2162 | io_commit_cqring(req->ctx); |
| 2163 | spin_unlock(&ctx->completion_lock); |
| 2164 | if (posted) |
| 2165 | io_cqring_ev_posted(ctx); |
| 2166 | } |
| 2167 | |
| 2168 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2169 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2170 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2171 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2172 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
| 2173 | return NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2174 | /* |
| 2175 | * If LINK is set, we have dependent requests in this chain. If we |
| 2176 | * didn't fail this request, queue the first one up, moving any other |
| 2177 | * dependencies to the next request. In case of failure, fail the rest |
| 2178 | * of the chain. |
| 2179 | */ |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2180 | if (unlikely(req->flags & IO_DISARM_MASK)) |
| 2181 | __io_req_find_next_prep(req); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2182 | nxt = req->link; |
| 2183 | req->link = NULL; |
| 2184 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2185 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2186 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2187 | 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] | 2188 | { |
| 2189 | if (!ctx) |
| 2190 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2191 | if (*locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2192 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2193 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2194 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2195 | } |
| 2196 | percpu_ref_put(&ctx->refs); |
| 2197 | } |
| 2198 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2199 | static void tctx_task_work(struct callback_head *cb) |
| 2200 | { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2201 | bool locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2202 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2203 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2204 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2205 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2206 | while (1) { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2207 | struct io_wq_work_node *node; |
| 2208 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2209 | if (!tctx->task_list.first && locked) |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2210 | io_submit_flush_completions(ctx); |
| 2211 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2212 | spin_lock_irq(&tctx->task_lock); |
Pavel Begunkov | c6538be | 2021-06-17 18:14:08 +0100 | [diff] [blame] | 2213 | node = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2214 | INIT_WQ_LIST(&tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2215 | if (!node) |
| 2216 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2217 | spin_unlock_irq(&tctx->task_lock); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2218 | if (!node) |
| 2219 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2220 | |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2221 | do { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2222 | struct io_wq_work_node *next = node->next; |
| 2223 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2224 | io_task_work.node); |
| 2225 | |
| 2226 | if (req->ctx != ctx) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2227 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2228 | ctx = req->ctx; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2229 | /* if not contended, grab and improve batching */ |
| 2230 | locked = mutex_trylock(&ctx->uring_lock); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2231 | percpu_ref_get(&ctx->refs); |
| 2232 | } |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2233 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2234 | node = next; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2235 | } while (node); |
| 2236 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2237 | cond_resched(); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2238 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2239 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2240 | ctx_flush_and_put(ctx, &locked); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2243 | static void io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2244 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2245 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2246 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2247 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2248 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2249 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2250 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2251 | |
| 2252 | WARN_ON_ONCE(!tctx); |
| 2253 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2254 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2255 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2256 | running = tctx->task_running; |
| 2257 | if (!running) |
| 2258 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2259 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2260 | |
| 2261 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2262 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2263 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2264 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2265 | /* |
| 2266 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2267 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2268 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2269 | * will do the job. |
| 2270 | */ |
| 2271 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | d97ec62 | 2021-09-08 16:40:53 +0100 | [diff] [blame] | 2272 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
| 2273 | if (notify == TWA_NONE) |
| 2274 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2275 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2276 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2277 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2278 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2279 | tctx->task_running = false; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2280 | node = tctx->task_list.first; |
| 2281 | INIT_WQ_LIST(&tctx->task_list); |
| 2282 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2283 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2284 | while (node) { |
| 2285 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2286 | node = node->next; |
| 2287 | if (llist_add(&req->io_task_work.fallback_node, |
| 2288 | &req->ctx->fallback_llist)) |
| 2289 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2290 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2293 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2294 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2295 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2296 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2297 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2298 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2299 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2300 | } |
| 2301 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2302 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2303 | { |
| 2304 | struct io_ring_ctx *ctx = req->ctx; |
| 2305 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2306 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2307 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2308 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2309 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2310 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2311 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2312 | } |
| 2313 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2314 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2315 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2316 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2317 | req->io_task_work.func = io_req_task_cancel; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2318 | io_req_task_work_add(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2321 | static void io_req_task_queue(struct io_kiocb *req) |
| 2322 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2323 | req->io_task_work.func = io_req_task_submit; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2324 | io_req_task_work_add(req); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2327 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2328 | { |
| 2329 | req->io_task_work.func = io_queue_async_work; |
| 2330 | io_req_task_work_add(req); |
| 2331 | } |
| 2332 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2333 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2334 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2335 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2336 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2337 | if (nxt) |
| 2338 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2341 | static void io_free_req(struct io_kiocb *req) |
| 2342 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2343 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2344 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2345 | } |
| 2346 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2347 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2348 | { |
| 2349 | io_free_req(req); |
| 2350 | } |
| 2351 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2352 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2353 | struct io_wq_work_node *node) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2354 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2355 | { |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2356 | struct task_struct *task = NULL; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2357 | int task_refs = 0; |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2358 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2359 | do { |
| 2360 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2361 | comp_list); |
| 2362 | |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2363 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2364 | node = req->comp_list.next; |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2365 | if (!req_ref_put_and_test(req)) |
| 2366 | continue; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2367 | } |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2368 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2369 | io_req_put_rsrc_locked(req, ctx); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2370 | io_queue_next(req); |
| 2371 | io_dismantle_req(req); |
| 2372 | |
| 2373 | if (req->task != task) { |
| 2374 | if (task) |
| 2375 | io_put_task(task, task_refs); |
| 2376 | task = req->task; |
| 2377 | task_refs = 0; |
| 2378 | } |
| 2379 | task_refs++; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2380 | node = req->comp_list.next; |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2381 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2382 | } while (node); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2383 | |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2384 | if (task) |
| 2385 | io_put_task(task, task_refs); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2388 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2389 | __must_hold(&ctx->uring_lock) |
| 2390 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2391 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2392 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2393 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 2394 | if (state->flush_cqes) { |
| 2395 | spin_lock(&ctx->completion_lock); |
| 2396 | wq_list_for_each(node, prev, &state->compl_reqs) { |
| 2397 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2398 | comp_list); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2399 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 2400 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 2401 | __io_fill_cqe(ctx, req->user_data, req->result, |
| 2402 | req->cflags); |
| 2403 | } |
| 2404 | |
| 2405 | io_commit_cqring(ctx); |
| 2406 | spin_unlock(&ctx->completion_lock); |
| 2407 | io_cqring_ev_posted(ctx); |
| 2408 | state->flush_cqes = false; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2409 | } |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2410 | |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2411 | io_free_batch_list(ctx, state->compl_reqs.first); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2412 | INIT_WQ_LIST(&state->compl_reqs); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2413 | } |
| 2414 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2415 | /* |
| 2416 | * Drop reference to request, return next in chain (if there is one) if this |
| 2417 | * was the last reference to this request. |
| 2418 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2419 | 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] | 2420 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2421 | struct io_kiocb *nxt = NULL; |
| 2422 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2423 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2424 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2425 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2426 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2427 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2430 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2431 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2432 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2433 | io_free_req(req); |
| 2434 | } |
| 2435 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2436 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2437 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2438 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2439 | req->io_task_work.func = io_free_req_work; |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2440 | io_req_task_work_add(req); |
| 2441 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2444 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2445 | { |
| 2446 | /* See comment at the top of this file */ |
| 2447 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2448 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2449 | } |
| 2450 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2451 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2452 | { |
| 2453 | struct io_rings *rings = ctx->rings; |
| 2454 | |
| 2455 | /* make sure SQ entry isn't read before tail */ |
| 2456 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2457 | } |
| 2458 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2459 | 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] | 2460 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2461 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2462 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2463 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2464 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2465 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2466 | kfree(kbuf); |
| 2467 | return cflags; |
| 2468 | } |
| 2469 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2470 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2471 | { |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2472 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 2473 | return 0; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 2474 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2475 | } |
| 2476 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2477 | static inline bool io_run_task_work(void) |
| 2478 | { |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2479 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2480 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2481 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2482 | return true; |
| 2483 | } |
| 2484 | |
| 2485 | return false; |
| 2486 | } |
| 2487 | |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2488 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2489 | { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2490 | struct io_wq_work_node *pos, *start, *prev; |
Christoph Hellwig | d729cf9 | 2021-10-12 13:12:20 +0200 | [diff] [blame] | 2491 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2492 | DEFINE_IO_COMP_BATCH(iob); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2493 | int nr_events = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2494 | |
| 2495 | /* |
| 2496 | * Only spin for completions if we don't have multiple devices hanging |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2497 | * off our complete list. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2498 | */ |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2499 | if (ctx->poll_multi_queue || force_nonspin) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2500 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2501 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2502 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
| 2503 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2504 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2505 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2506 | |
| 2507 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2508 | * Move completed and retryable entries to our local lists. |
| 2509 | * If we find a request that requires polling, break out |
| 2510 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2511 | */ |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2512 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2513 | break; |
| 2514 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2515 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2516 | if (unlikely(ret < 0)) |
| 2517 | return ret; |
| 2518 | else if (ret) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2519 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2520 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2521 | /* iopoll may have completed current req */ |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2522 | if (!rq_list_empty(iob.req_list) || |
| 2523 | READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2524 | break; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2527 | if (!rq_list_empty(iob.req_list)) |
| 2528 | iob.complete(&iob); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2529 | else if (!pos) |
| 2530 | return 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2531 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2532 | prev = start; |
| 2533 | wq_list_for_each_resume(pos, prev) { |
| 2534 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2535 | u32 cflags; |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2536 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2537 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
| 2538 | if (!smp_load_acquire(&req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2539 | break; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2540 | cflags = io_put_rw_kbuf(req); |
| 2541 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 2542 | __io_fill_cqe(ctx, req->user_data, req->result, cflags); |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2543 | nr_events++; |
| 2544 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2545 | |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2546 | if (unlikely(!nr_events)) |
| 2547 | return 0; |
| 2548 | |
| 2549 | io_commit_cqring(ctx); |
| 2550 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2551 | pos = start ? start->next : ctx->iopoll_list.first; |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2552 | wq_list_cut(&ctx->iopoll_list, prev, start); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2553 | io_free_batch_list(ctx, pos); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2554 | return nr_events; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2558 | * We can't just wait for polled events to come to us, we have to actively |
| 2559 | * find and complete them. |
| 2560 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2561 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2562 | { |
| 2563 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2564 | return; |
| 2565 | |
| 2566 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2567 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2568 | /* let it sleep and repeat later if can't complete a request */ |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2569 | if (io_do_iopoll(ctx, true) == 0) |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2570 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2571 | /* |
| 2572 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2573 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2574 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2575 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2576 | if (need_resched()) { |
| 2577 | mutex_unlock(&ctx->uring_lock); |
| 2578 | cond_resched(); |
| 2579 | mutex_lock(&ctx->uring_lock); |
| 2580 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2581 | } |
| 2582 | mutex_unlock(&ctx->uring_lock); |
| 2583 | } |
| 2584 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2585 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2586 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2587 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2588 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2589 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2590 | /* |
| 2591 | * We disallow the app entering submit/complete with polling, but we |
| 2592 | * still need to lock the ring to prevent racing with polled issue |
| 2593 | * that got punted to a workqueue. |
| 2594 | */ |
| 2595 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2596 | /* |
| 2597 | * Don't enter poll loop if we already have events pending. |
| 2598 | * If we do, we can potentially be spinning for commands that |
| 2599 | * already triggered a CQE (eg in error). |
| 2600 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2601 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2602 | __io_cqring_overflow_flush(ctx, false); |
| 2603 | if (io_cqring_events(ctx)) |
| 2604 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2605 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2606 | /* |
| 2607 | * If a submit got punted to a workqueue, we can have the |
| 2608 | * application entering polling for a command before it gets |
| 2609 | * issued. That app will hold the uring_lock for the duration |
| 2610 | * of the poll right here, so we need to take a breather every |
| 2611 | * now and then to ensure that the issue has a chance to add |
| 2612 | * the poll to the issued list. Otherwise we can spin here |
| 2613 | * forever, while the workqueue is stuck trying to acquire the |
| 2614 | * very same mutex. |
| 2615 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2616 | if (wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2617 | u32 tail = ctx->cached_cq_tail; |
| 2618 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2619 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2620 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2621 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2622 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2623 | /* some requests don't go through iopoll_list */ |
| 2624 | if (tail != ctx->cached_cq_tail || |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2625 | wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2626 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2627 | } |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2628 | ret = io_do_iopoll(ctx, !min); |
| 2629 | if (ret < 0) |
| 2630 | break; |
| 2631 | nr_events += ret; |
| 2632 | ret = 0; |
| 2633 | } while (nr_events < min && !need_resched()); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2634 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2635 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2636 | return ret; |
| 2637 | } |
| 2638 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2639 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2640 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2641 | /* |
| 2642 | * Tell lockdep we inherited freeze protection from submission |
| 2643 | * thread. |
| 2644 | */ |
| 2645 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2646 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2647 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2648 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2649 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2653 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2654 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2655 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2656 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2657 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2658 | if (!req_has_async_data(req)) |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2659 | return !io_req_prep_async(req); |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 2660 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2661 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2662 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2663 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2664 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2665 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2666 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2667 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2668 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2669 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2670 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2671 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2672 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2673 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2674 | /* |
| 2675 | * If ref is dying, we might be running poll reap from the exit work. |
| 2676 | * Don't attempt to reissue from that path, just let it fail with |
| 2677 | * -EAGAIN. |
| 2678 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2679 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2680 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2681 | /* |
| 2682 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2683 | * not in the original thread group (or in task context). |
| 2684 | */ |
| 2685 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2686 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2687 | return true; |
| 2688 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2689 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2690 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2691 | { |
| 2692 | return false; |
| 2693 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2694 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2695 | { |
| 2696 | return false; |
| 2697 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2698 | #endif |
| 2699 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2700 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2701 | { |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2702 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2703 | kiocb_end_write(req); |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2704 | if (unlikely(res != req->result)) { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2705 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2706 | io_rw_should_reissue(req)) { |
| 2707 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2708 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2709 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2710 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2711 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2712 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2713 | return false; |
| 2714 | } |
| 2715 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2716 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2717 | { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2718 | unsigned int cflags = io_put_rw_kbuf(req); |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 2719 | int res = req->result; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2720 | |
| 2721 | if (*locked) { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2722 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2723 | io_req_add_compl_list(req); |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2724 | } else { |
| 2725 | io_req_complete_post(req, res, cflags); |
| 2726 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2730 | unsigned int issue_flags) |
| 2731 | { |
| 2732 | if (__io_complete_rw_common(req, res)) |
| 2733 | return; |
Pavel Begunkov | 6363785 | 2021-09-02 00:38:22 +0100 | [diff] [blame] | 2734 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2735 | } |
| 2736 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2737 | static void io_complete_rw(struct kiocb *kiocb, long res) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2738 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2739 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2740 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2741 | if (__io_complete_rw_common(req, res)) |
| 2742 | return; |
| 2743 | req->result = res; |
| 2744 | req->io_task_work.func = io_req_task_complete; |
| 2745 | io_req_task_work_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2746 | } |
| 2747 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2748 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2749 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2750 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2751 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2752 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2753 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2754 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2755 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2756 | req->flags |= REQ_F_REISSUE; |
| 2757 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2758 | } |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2759 | req->result = res; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2760 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2761 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2762 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
| 2763 | smp_store_release(&req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2768 | * 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] | 2769 | * 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] | 2770 | * accessing the kiocb cookie. |
| 2771 | */ |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 2772 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2773 | { |
| 2774 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2775 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2776 | |
| 2777 | /* workqueue context doesn't hold uring_lock, grab it now */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2778 | if (unlikely(needs_lock)) |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2779 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2780 | |
| 2781 | /* |
| 2782 | * Track whether we have multiple files in our lists. This will impact |
| 2783 | * how we do polling eventually, not spinning if we're on potentially |
| 2784 | * different devices. |
| 2785 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2786 | if (wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2787 | ctx->poll_multi_queue = false; |
| 2788 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2789 | struct io_kiocb *list_req; |
| 2790 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2791 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
| 2792 | comp_list); |
Christoph Hellwig | 30da1b4 | 2021-10-12 13:12:14 +0200 | [diff] [blame] | 2793 | if (list_req->file != req->file) |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2794 | ctx->poll_multi_queue = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | /* |
| 2798 | * For fast devices, IO may have already completed. If it has, add |
| 2799 | * it to the front so we find it first. |
| 2800 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2801 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2802 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2803 | else |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2804 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2805 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2806 | if (unlikely(needs_lock)) { |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2807 | /* |
| 2808 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2809 | * in sq thread task context or in io worker task context. If |
| 2810 | * current task context is sq thread, we don't need to check |
| 2811 | * whether should wake up sq thread. |
| 2812 | */ |
| 2813 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2814 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2815 | wake_up(&ctx->sq_data->wait); |
| 2816 | |
| 2817 | mutex_unlock(&ctx->uring_lock); |
| 2818 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2821 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2822 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2823 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2824 | } |
| 2825 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2826 | /* |
| 2827 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2828 | * any file. For now, just ensure that anything potentially problematic is done |
| 2829 | * inline. |
| 2830 | */ |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2831 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2832 | { |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2833 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2834 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2835 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2836 | return true; |
| 2837 | return false; |
| 2838 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2839 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2840 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2841 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2842 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2843 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2844 | file->f_op != &io_uring_fops) |
| 2845 | return true; |
| 2846 | return false; |
| 2847 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2848 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2849 | /* any ->read/write should understand O_NONBLOCK */ |
| 2850 | if (file->f_flags & O_NONBLOCK) |
| 2851 | return true; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2852 | return file->f_mode & FMODE_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2855 | /* |
| 2856 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2857 | * any file. For now, just ensure that anything potentially problematic is done |
| 2858 | * inline. |
| 2859 | */ |
| 2860 | static unsigned int io_file_get_flags(struct file *file) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2861 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2862 | umode_t mode = file_inode(file)->i_mode; |
| 2863 | unsigned int res = 0; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2864 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2865 | if (S_ISREG(mode)) |
| 2866 | res |= FFS_ISREG; |
| 2867 | if (__io_file_supports_nowait(file, mode)) |
| 2868 | res |= FFS_NOWAIT; |
| 2869 | return res; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2872 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2873 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2874 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 2877 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2878 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2879 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2880 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2881 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2882 | unsigned ioprio; |
| 2883 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2884 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2885 | if (!io_req_ffs_set(req)) |
| 2886 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2887 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2888 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2889 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2890 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2891 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2892 | } |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 2893 | kiocb->ki_flags = iocb_flags(file); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2894 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2895 | if (unlikely(ret)) |
| 2896 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2897 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2898 | /* |
| 2899 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 2900 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 2901 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 2902 | */ |
| 2903 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2904 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2905 | req->flags |= REQ_F_NOWAIT; |
| 2906 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2907 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 2908 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2909 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2910 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 2911 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2912 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2913 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2914 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2915 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2916 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2917 | kiocb->ki_complete = io_complete_rw; |
| 2918 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2919 | |
Pavel Begunkov | fb27274 | 2021-10-23 12:14:02 +0100 | [diff] [blame] | 2920 | ioprio = READ_ONCE(sqe->ioprio); |
| 2921 | if (ioprio) { |
| 2922 | ret = ioprio_check_cap(ioprio); |
| 2923 | if (ret) |
| 2924 | return ret; |
| 2925 | |
| 2926 | kiocb->ki_ioprio = ioprio; |
| 2927 | } else { |
| 2928 | kiocb->ki_ioprio = get_current_ioprio(); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 2931 | req->imu = NULL; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2932 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2933 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2934 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2935 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2939 | { |
| 2940 | switch (ret) { |
| 2941 | case -EIOCBQUEUED: |
| 2942 | break; |
| 2943 | case -ERESTARTSYS: |
| 2944 | case -ERESTARTNOINTR: |
| 2945 | case -ERESTARTNOHAND: |
| 2946 | case -ERESTART_RESTARTBLOCK: |
| 2947 | /* |
| 2948 | * We can't just restart the syscall, since previously |
| 2949 | * submitted sqes may already be in progress. Just fail this |
| 2950 | * IO with EINTR. |
| 2951 | */ |
| 2952 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2953 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2954 | default: |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2955 | kiocb->ki_complete(kiocb, ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 2959 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2960 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2961 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2962 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2963 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2964 | /* add previously done IO, if any */ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2965 | if (req_has_async_data(req) && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2966 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2967 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2968 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2969 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2970 | } |
| 2971 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2972 | if (req->flags & REQ_F_CUR_POS) |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 2973 | req->file->f_pos = req->rw.kiocb.ki_pos; |
| 2974 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2975 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2976 | else |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 2977 | io_rw_done(&req->rw.kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2978 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2979 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2980 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 2981 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2982 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2983 | } else { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2984 | req_set_fail(req); |
Pavel Begunkov | 06bdea2 | 2021-11-23 00:07:46 +0000 | [diff] [blame] | 2985 | req->result = ret; |
| 2986 | req->io_task_work.func = io_req_task_complete; |
| 2987 | io_req_task_work_add(req); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2988 | } |
| 2989 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2990 | } |
| 2991 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2992 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 2993 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2994 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2995 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2996 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2997 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2998 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2999 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3000 | return -EFAULT; |
| 3001 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 3002 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3003 | return -EFAULT; |
| 3004 | |
| 3005 | /* |
| 3006 | * May not be a start of buffer, set size appropriately |
| 3007 | * and advance us to the beginning. |
| 3008 | */ |
| 3009 | offset = buf_addr - imu->ubuf; |
| 3010 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3011 | |
| 3012 | if (offset) { |
| 3013 | /* |
| 3014 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3015 | * using the latter parts of a big fixed buffer - it iterates |
| 3016 | * over each segment manually. We can cheat a bit here, because |
| 3017 | * we know that: |
| 3018 | * |
| 3019 | * 1) it's a BVEC iter, we set it up |
| 3020 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3021 | * first and last bvec |
| 3022 | * |
| 3023 | * So just find our index, and adjust the iterator afterwards. |
| 3024 | * If the offset is within the first bvec (or the whole first |
| 3025 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3026 | * since we can just skip the first segment, which may not |
| 3027 | * be PAGE_SIZE aligned. |
| 3028 | */ |
| 3029 | const struct bio_vec *bvec = imu->bvec; |
| 3030 | |
| 3031 | if (offset <= bvec->bv_len) { |
| 3032 | iov_iter_advance(iter, offset); |
| 3033 | } else { |
| 3034 | unsigned long seg_skip; |
| 3035 | |
| 3036 | /* skip first vec */ |
| 3037 | offset -= bvec->bv_len; |
| 3038 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3039 | |
| 3040 | iter->bvec = bvec + seg_skip; |
| 3041 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3042 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3043 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3047 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3050 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 3051 | { |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3052 | struct io_mapped_ubuf *imu = req->imu; |
| 3053 | u16 index, buf_index = req->buf_index; |
| 3054 | |
| 3055 | if (likely(!imu)) { |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3056 | struct io_ring_ctx *ctx = req->ctx; |
| 3057 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3058 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3059 | return -EFAULT; |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3060 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3061 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3062 | imu = READ_ONCE(ctx->user_bufs[index]); |
| 3063 | req->imu = imu; |
| 3064 | } |
| 3065 | return __io_import_fixed(req, rw, iter, imu); |
| 3066 | } |
| 3067 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3068 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3069 | { |
| 3070 | if (needs_lock) |
| 3071 | mutex_unlock(&ctx->uring_lock); |
| 3072 | } |
| 3073 | |
| 3074 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3075 | { |
| 3076 | /* |
| 3077 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3078 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3079 | * The only exception is when we've detached the request and issue it |
| 3080 | * from an async worker thread, grab the lock for that case. |
| 3081 | */ |
| 3082 | if (needs_lock) |
| 3083 | mutex_lock(&ctx->uring_lock); |
| 3084 | } |
| 3085 | |
| 3086 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3087 | int bgid, unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3088 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3089 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3090 | struct io_buffer *head; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3091 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3092 | |
| 3093 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3094 | return kbuf; |
| 3095 | |
| 3096 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3097 | |
| 3098 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3099 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3100 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3101 | if (head) { |
| 3102 | if (!list_empty(&head->list)) { |
| 3103 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3104 | list); |
| 3105 | list_del(&kbuf->list); |
| 3106 | } else { |
| 3107 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3108 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3109 | } |
| 3110 | if (*len > kbuf->len) |
| 3111 | *len = kbuf->len; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3112 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3113 | req->kbuf = kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3114 | } else { |
| 3115 | kbuf = ERR_PTR(-ENOBUFS); |
| 3116 | } |
| 3117 | |
| 3118 | io_ring_submit_unlock(req->ctx, needs_lock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3119 | return kbuf; |
| 3120 | } |
| 3121 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3122 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3123 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3124 | { |
| 3125 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3126 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3127 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3128 | bgid = req->buf_index; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3129 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3130 | if (IS_ERR(kbuf)) |
| 3131 | return kbuf; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3132 | return u64_to_user_ptr(kbuf->addr); |
| 3133 | } |
| 3134 | |
| 3135 | #ifdef CONFIG_COMPAT |
| 3136 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3137 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3138 | { |
| 3139 | struct compat_iovec __user *uiov; |
| 3140 | compat_ssize_t clen; |
| 3141 | void __user *buf; |
| 3142 | ssize_t len; |
| 3143 | |
| 3144 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3145 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3146 | return -EFAULT; |
| 3147 | if (__get_user(clen, &uiov->iov_len)) |
| 3148 | return -EFAULT; |
| 3149 | if (clen < 0) |
| 3150 | return -EINVAL; |
| 3151 | |
| 3152 | len = clen; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3153 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3154 | if (IS_ERR(buf)) |
| 3155 | return PTR_ERR(buf); |
| 3156 | iov[0].iov_base = buf; |
| 3157 | iov[0].iov_len = (compat_size_t) len; |
| 3158 | return 0; |
| 3159 | } |
| 3160 | #endif |
| 3161 | |
| 3162 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3163 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3164 | { |
| 3165 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3166 | void __user *buf; |
| 3167 | ssize_t len; |
| 3168 | |
| 3169 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3170 | return -EFAULT; |
| 3171 | |
| 3172 | len = iov[0].iov_len; |
| 3173 | if (len < 0) |
| 3174 | return -EINVAL; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3175 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3176 | if (IS_ERR(buf)) |
| 3177 | return PTR_ERR(buf); |
| 3178 | iov[0].iov_base = buf; |
| 3179 | iov[0].iov_len = len; |
| 3180 | return 0; |
| 3181 | } |
| 3182 | |
| 3183 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3184 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3185 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3186 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3187 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3188 | |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3189 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3190 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3191 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3192 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3193 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3194 | return -EINVAL; |
| 3195 | |
| 3196 | #ifdef CONFIG_COMPAT |
| 3197 | if (req->ctx->compat) |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3198 | return io_compat_import(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3199 | #endif |
| 3200 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3201 | return __io_iov_buffer_select(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3204 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
| 3205 | struct io_rw_state *s, |
| 3206 | unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3207 | { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3208 | struct iov_iter *iter = &s->iter; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3209 | u8 opcode = req->opcode; |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3210 | struct iovec *iovec; |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3211 | void __user *buf; |
| 3212 | size_t sqe_len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3213 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3214 | |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3215 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
| 3216 | ret = io_import_fixed(req, rw, iter); |
| 3217 | if (ret) |
| 3218 | return ERR_PTR(ret); |
| 3219 | return NULL; |
| 3220 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3221 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3222 | /* buffer index only valid with fixed read/write, or buffer select */ |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3223 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3224 | return ERR_PTR(-EINVAL); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3225 | |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3226 | buf = u64_to_user_ptr(req->rw.addr); |
| 3227 | sqe_len = req->rw.len; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3228 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3229 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3230 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3231 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3232 | if (IS_ERR(buf)) |
Changcheng Deng | 898df24 | 2021-10-20 08:49:48 +0000 | [diff] [blame] | 3233 | return ERR_CAST(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3234 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3237 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3238 | if (ret) |
| 3239 | return ERR_PTR(ret); |
| 3240 | return NULL; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3243 | iovec = s->fast_iov; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3244 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3245 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3246 | if (ret) |
| 3247 | return ERR_PTR(ret); |
| 3248 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
| 3249 | return NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3252 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3253 | req->ctx->compat); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3254 | if (unlikely(ret < 0)) |
| 3255 | return ERR_PTR(ret); |
| 3256 | return iovec; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3257 | } |
| 3258 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3259 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
| 3260 | struct iovec **iovec, struct io_rw_state *s, |
| 3261 | unsigned int issue_flags) |
| 3262 | { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3263 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
| 3264 | if (unlikely(IS_ERR(*iovec))) |
| 3265 | return PTR_ERR(*iovec); |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3266 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3267 | iov_iter_save_state(&s->iter, &s->iter_state); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3268 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3269 | } |
| 3270 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3271 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3272 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3273 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3274 | } |
| 3275 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3276 | /* |
| 3277 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3278 | * by looping over ->read() or ->write() manually. |
| 3279 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3280 | 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] | 3281 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3282 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3283 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3284 | ssize_t ret = 0; |
| 3285 | |
| 3286 | /* |
| 3287 | * Don't support polled IO through this interface, and we can't |
| 3288 | * support non-blocking either. For the latter, this just causes |
| 3289 | * the kiocb to be handled from an async context. |
| 3290 | */ |
| 3291 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3292 | return -EOPNOTSUPP; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3293 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
| 3294 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3295 | return -EAGAIN; |
| 3296 | |
| 3297 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3298 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3299 | ssize_t nr; |
| 3300 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3301 | if (!iov_iter_is_bvec(iter)) { |
| 3302 | iovec = iov_iter_iovec(iter); |
| 3303 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3304 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3305 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3306 | } |
| 3307 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3308 | if (rw == READ) { |
| 3309 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3310 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3311 | } else { |
| 3312 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3313 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | if (nr < 0) { |
| 3317 | if (!ret) |
| 3318 | ret = nr; |
| 3319 | break; |
| 3320 | } |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3321 | if (!iov_iter_is_bvec(iter)) { |
| 3322 | iov_iter_advance(iter, nr); |
| 3323 | } else { |
| 3324 | req->rw.len -= nr; |
| 3325 | req->rw.addr += nr; |
| 3326 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3327 | ret += nr; |
| 3328 | if (nr != iovec.iov_len) |
| 3329 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | return ret; |
| 3333 | } |
| 3334 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3335 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3336 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3337 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3338 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3339 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3340 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3341 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3342 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3343 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3344 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3345 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3346 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3347 | unsigned iov_off = 0; |
| 3348 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3349 | rw->s.iter.iov = rw->s.fast_iov; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3350 | if (iter->iov != fast_iov) { |
| 3351 | iov_off = iter->iov - fast_iov; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3352 | rw->s.iter.iov += iov_off; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3353 | } |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3354 | if (rw->s.fast_iov != fast_iov) |
| 3355 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3356 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3357 | } else { |
| 3358 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | |
Hao Xu | 8d4af68 | 2021-09-22 18:15:22 +0800 | [diff] [blame] | 3362 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3363 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3364 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3365 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3366 | if (req->async_data) { |
| 3367 | req->flags |= REQ_F_ASYNC_DATA; |
| 3368 | return false; |
| 3369 | } |
| 3370 | return true; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3371 | } |
| 3372 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3373 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3374 | struct io_rw_state *s, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3375 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3376 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3377 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3378 | if (!req_has_async_data(req)) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3379 | struct io_async_rw *iorw; |
| 3380 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3381 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3382 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3383 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3384 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3385 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3386 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3387 | iorw = req->async_data; |
| 3388 | /* we've copied and mapped the iter, ensure state is saved */ |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3389 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3390 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3391 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3394 | 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] | 3395 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3396 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3397 | struct iovec *iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3398 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3399 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3400 | /* submission path, ->uring_lock should already be taken */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3401 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3402 | if (unlikely(ret < 0)) |
| 3403 | return ret; |
| 3404 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3405 | iorw->bytes_done = 0; |
| 3406 | iorw->free_iovec = iov; |
| 3407 | if (iov) |
| 3408 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3409 | return 0; |
| 3410 | } |
| 3411 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3412 | 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] | 3413 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3414 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3415 | return -EBADF; |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 3416 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3417 | } |
| 3418 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3419 | /* |
Matthew Wilcox (Oracle) | ffdc8da | 2020-12-30 17:58:40 -0500 | [diff] [blame] | 3420 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3421 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3422 | * This gets called when the page is unlocked, and we generally expect that to |
| 3423 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3424 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3425 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3426 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3427 | * slow path. |
| 3428 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3429 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3430 | int sync, void *arg) |
| 3431 | { |
| 3432 | struct wait_page_queue *wpq; |
| 3433 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3434 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3435 | |
| 3436 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3437 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3438 | if (!wake_page_match(wpq, key)) |
| 3439 | return 0; |
| 3440 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3441 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3442 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3443 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3444 | return 1; |
| 3445 | } |
| 3446 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3447 | /* |
| 3448 | * This controls whether a given IO request should be armed for async page |
| 3449 | * based retry. If we return false here, the request is handed to the async |
| 3450 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3451 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3452 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3453 | * will register a callback when the page is unlocked at IO completion. Through |
| 3454 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3455 | * That retry will attempt the buffered read again. The retry will generally |
| 3456 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3457 | * async worker threads for a blocking retry. |
| 3458 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3459 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3460 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3461 | struct io_async_rw *rw = req->async_data; |
| 3462 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3463 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3464 | |
| 3465 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3466 | if (req->flags & REQ_F_NOWAIT) |
| 3467 | return false; |
| 3468 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3469 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3470 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3471 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3472 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3473 | /* |
| 3474 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3475 | * support callback based unlocks |
| 3476 | */ |
| 3477 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3478 | return false; |
| 3479 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3480 | wait->wait.func = io_async_buf_func; |
| 3481 | wait->wait.private = req; |
| 3482 | wait->wait.flags = 0; |
| 3483 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3484 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3485 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3486 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3487 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3488 | } |
| 3489 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3490 | 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] | 3491 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3492 | if (likely(req->file->f_op->read_iter)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3493 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3494 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3495 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3496 | else |
| 3497 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3498 | } |
| 3499 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3500 | static bool need_read_all(struct io_kiocb *req) |
| 3501 | { |
| 3502 | return req->flags & REQ_F_ISREG || |
| 3503 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3504 | } |
| 3505 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3506 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3507 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3508 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3509 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3510 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3511 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3512 | struct io_async_rw *rw; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3513 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3514 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3515 | if (!req_has_async_data(req)) { |
| 3516 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); |
| 3517 | if (unlikely(ret < 0)) |
| 3518 | return ret; |
| 3519 | } else { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3520 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3521 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3522 | /* |
| 3523 | * We come here from an earlier attempt, restore our state to |
| 3524 | * match in case it doesn't. It's cheap enough that we don't |
| 3525 | * need to make this conditional. |
| 3526 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3527 | iov_iter_restore(&s->iter, &s->iter_state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3528 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3529 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3530 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3531 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3532 | if (force_nonblock) { |
| 3533 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3534 | if (unlikely(!io_file_supports_nowait(req))) { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3535 | ret = io_setup_async_rw(req, iovec, s, true); |
| 3536 | return ret ?: -EAGAIN; |
| 3537 | } |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3538 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3539 | } else { |
| 3540 | /* Ensure we clear previously set non-block flag */ |
| 3541 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3542 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3543 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3544 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3545 | if (unlikely(ret)) { |
| 3546 | kfree(iovec); |
| 3547 | return ret; |
| 3548 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3549 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3550 | ret = io_iter_do_read(req, &s->iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3551 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3552 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3553 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3554 | /* IOPOLL retry should happen for io-wq threads */ |
| 3555 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3556 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3557 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3558 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3559 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3560 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3561 | } else if (ret == -EIOCBQUEUED) { |
| 3562 | goto out_free; |
Pavel Begunkov | f80a50a | 2021-10-14 16:10:13 +0100 | [diff] [blame] | 3563 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3564 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3565 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3566 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3567 | } |
| 3568 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3569 | /* |
| 3570 | * Don't depend on the iter state matching what was consumed, or being |
| 3571 | * untouched in case of error. Restore it and we'll advance it |
| 3572 | * manually if we need to. |
| 3573 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3574 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3575 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3576 | ret2 = io_setup_async_rw(req, iovec, s, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3577 | if (ret2) |
| 3578 | return ret2; |
| 3579 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3580 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3581 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3582 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3583 | /* |
| 3584 | * Now use our persistent iterator and state, if we aren't already. |
| 3585 | * We've restored and mapped the iter to match. |
| 3586 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3587 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3588 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3589 | /* |
| 3590 | * We end up here because of a partial read, either from |
| 3591 | * above or inside this loop. Advance the iter by the bytes |
| 3592 | * that were consumed. |
| 3593 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3594 | iov_iter_advance(&s->iter, ret); |
| 3595 | if (!iov_iter_count(&s->iter)) |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3596 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3597 | rw->bytes_done += ret; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3598 | iov_iter_save_state(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3599 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3600 | /* if we can retry, do so with the callbacks armed */ |
| 3601 | if (!io_rw_should_retry(req)) { |
| 3602 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3603 | return -EAGAIN; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3608 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3609 | * desired page gets unlocked. We can also get a partial read |
| 3610 | * here, and if we do, then just retry at the new offset. |
| 3611 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3612 | ret = io_iter_do_read(req, &s->iter); |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3613 | if (ret == -EIOCBQUEUED) |
| 3614 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3615 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3616 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3617 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3618 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3619 | done: |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3620 | kiocb_done(req, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3621 | out_free: |
| 3622 | /* it's faster to check here then delegate to kfree */ |
| 3623 | if (iovec) |
| 3624 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3625 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3628 | 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] | 3629 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3630 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3631 | return -EBADF; |
Jens Axboe | 3884b83 | 2021-10-25 13:45:12 -0600 | [diff] [blame] | 3632 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 3633 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3634 | } |
| 3635 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3636 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3637 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3638 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3639 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3640 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3641 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3642 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3643 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3644 | if (!req_has_async_data(req)) { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3645 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
| 3646 | if (unlikely(ret < 0)) |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3647 | return ret; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3648 | } else { |
| 3649 | struct io_async_rw *rw = req->async_data; |
| 3650 | |
| 3651 | s = &rw->s; |
| 3652 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3653 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3654 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3655 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3656 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3657 | if (force_nonblock) { |
| 3658 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3659 | if (unlikely(!io_file_supports_nowait(req))) |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3660 | goto copy_iov; |
| 3661 | |
| 3662 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3663 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3664 | (req->flags & REQ_F_ISREG)) |
| 3665 | goto copy_iov; |
| 3666 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3667 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3668 | } else { |
| 3669 | /* Ensure we clear previously set non-block flag */ |
| 3670 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3671 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3672 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3673 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3674 | if (unlikely(ret)) |
| 3675 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3676 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3677 | /* |
| 3678 | * Open-code file_start_write here to grab freeze protection, |
| 3679 | * which will be released by another thread in |
| 3680 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3681 | * released so that it doesn't complain about the held lock when |
| 3682 | * we return to userspace. |
| 3683 | */ |
| 3684 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3685 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3686 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3687 | SB_FREEZE_WRITE); |
| 3688 | } |
| 3689 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3690 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3691 | if (likely(req->file->f_op->write_iter)) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3692 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3693 | else if (req->file->f_op->write) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3694 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3695 | else |
| 3696 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3697 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3698 | if (req->flags & REQ_F_REISSUE) { |
| 3699 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3700 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3701 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3702 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3703 | /* |
| 3704 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3705 | * retry them without IOCB_NOWAIT. |
| 3706 | */ |
| 3707 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3708 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3709 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3710 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3711 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3712 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3713 | /* IOPOLL retry should happen for io-wq threads */ |
Noah Goldstein | b10841c | 2021-10-16 20:32:29 -0500 | [diff] [blame] | 3714 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3715 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3716 | done: |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3717 | kiocb_done(req, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3718 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3719 | copy_iov: |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3720 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3721 | ret = io_setup_async_rw(req, iovec, s, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3722 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3723 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3724 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3725 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3726 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3727 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3728 | return ret; |
| 3729 | } |
| 3730 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3731 | static int io_renameat_prep(struct io_kiocb *req, |
| 3732 | const struct io_uring_sqe *sqe) |
| 3733 | { |
| 3734 | struct io_rename *ren = &req->rename; |
| 3735 | const char __user *oldf, *newf; |
| 3736 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3737 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3738 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3739 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3740 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3741 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3742 | return -EBADF; |
| 3743 | |
| 3744 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3745 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3746 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3747 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3748 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3749 | |
| 3750 | ren->oldpath = getname(oldf); |
| 3751 | if (IS_ERR(ren->oldpath)) |
| 3752 | return PTR_ERR(ren->oldpath); |
| 3753 | |
| 3754 | ren->newpath = getname(newf); |
| 3755 | if (IS_ERR(ren->newpath)) { |
| 3756 | putname(ren->oldpath); |
| 3757 | return PTR_ERR(ren->newpath); |
| 3758 | } |
| 3759 | |
| 3760 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3761 | return 0; |
| 3762 | } |
| 3763 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3764 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3765 | { |
| 3766 | struct io_rename *ren = &req->rename; |
| 3767 | int ret; |
| 3768 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3769 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3770 | return -EAGAIN; |
| 3771 | |
| 3772 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3773 | ren->newpath, ren->flags); |
| 3774 | |
| 3775 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3776 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3777 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3778 | io_req_complete(req, ret); |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3782 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3783 | const struct io_uring_sqe *sqe) |
| 3784 | { |
| 3785 | struct io_unlink *un = &req->unlink; |
| 3786 | const char __user *fname; |
| 3787 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3788 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3789 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3790 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3791 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3792 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3793 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3794 | return -EBADF; |
| 3795 | |
| 3796 | un->dfd = READ_ONCE(sqe->fd); |
| 3797 | |
| 3798 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3799 | if (un->flags & ~AT_REMOVEDIR) |
| 3800 | return -EINVAL; |
| 3801 | |
| 3802 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3803 | un->filename = getname(fname); |
| 3804 | if (IS_ERR(un->filename)) |
| 3805 | return PTR_ERR(un->filename); |
| 3806 | |
| 3807 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3811 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3812 | { |
| 3813 | struct io_unlink *un = &req->unlink; |
| 3814 | int ret; |
| 3815 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3816 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3817 | return -EAGAIN; |
| 3818 | |
| 3819 | if (un->flags & AT_REMOVEDIR) |
| 3820 | ret = do_rmdir(un->dfd, un->filename); |
| 3821 | else |
| 3822 | ret = do_unlinkat(un->dfd, un->filename); |
| 3823 | |
| 3824 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3825 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3826 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3827 | io_req_complete(req, ret); |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3831 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3832 | const struct io_uring_sqe *sqe) |
| 3833 | { |
| 3834 | struct io_mkdir *mkd = &req->mkdir; |
| 3835 | const char __user *fname; |
| 3836 | |
| 3837 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3838 | return -EINVAL; |
| 3839 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3840 | sqe->splice_fd_in) |
| 3841 | return -EINVAL; |
| 3842 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3843 | return -EBADF; |
| 3844 | |
| 3845 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3846 | mkd->mode = READ_ONCE(sqe->len); |
| 3847 | |
| 3848 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3849 | mkd->filename = getname(fname); |
| 3850 | if (IS_ERR(mkd->filename)) |
| 3851 | return PTR_ERR(mkd->filename); |
| 3852 | |
| 3853 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3854 | return 0; |
| 3855 | } |
| 3856 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3857 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3858 | { |
| 3859 | struct io_mkdir *mkd = &req->mkdir; |
| 3860 | int ret; |
| 3861 | |
| 3862 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3863 | return -EAGAIN; |
| 3864 | |
| 3865 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3866 | |
| 3867 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3868 | if (ret < 0) |
| 3869 | req_set_fail(req); |
| 3870 | io_req_complete(req, ret); |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3874 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3875 | const struct io_uring_sqe *sqe) |
| 3876 | { |
| 3877 | struct io_symlink *sl = &req->symlink; |
| 3878 | const char __user *oldpath, *newpath; |
| 3879 | |
| 3880 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3881 | return -EINVAL; |
| 3882 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 3883 | sqe->splice_fd_in) |
| 3884 | return -EINVAL; |
| 3885 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3886 | return -EBADF; |
| 3887 | |
| 3888 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 3889 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3890 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3891 | |
| 3892 | sl->oldpath = getname(oldpath); |
| 3893 | if (IS_ERR(sl->oldpath)) |
| 3894 | return PTR_ERR(sl->oldpath); |
| 3895 | |
| 3896 | sl->newpath = getname(newpath); |
| 3897 | if (IS_ERR(sl->newpath)) { |
| 3898 | putname(sl->oldpath); |
| 3899 | return PTR_ERR(sl->newpath); |
| 3900 | } |
| 3901 | |
| 3902 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3903 | return 0; |
| 3904 | } |
| 3905 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3906 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3907 | { |
| 3908 | struct io_symlink *sl = &req->symlink; |
| 3909 | int ret; |
| 3910 | |
| 3911 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3912 | return -EAGAIN; |
| 3913 | |
| 3914 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 3915 | |
| 3916 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3917 | if (ret < 0) |
| 3918 | req_set_fail(req); |
| 3919 | io_req_complete(req, ret); |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3923 | static int io_linkat_prep(struct io_kiocb *req, |
| 3924 | const struct io_uring_sqe *sqe) |
| 3925 | { |
| 3926 | struct io_hardlink *lnk = &req->hardlink; |
| 3927 | const char __user *oldf, *newf; |
| 3928 | |
| 3929 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3930 | return -EINVAL; |
| 3931 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 3932 | return -EINVAL; |
| 3933 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3934 | return -EBADF; |
| 3935 | |
| 3936 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 3937 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 3938 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3939 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3940 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 3941 | |
| 3942 | lnk->oldpath = getname(oldf); |
| 3943 | if (IS_ERR(lnk->oldpath)) |
| 3944 | return PTR_ERR(lnk->oldpath); |
| 3945 | |
| 3946 | lnk->newpath = getname(newf); |
| 3947 | if (IS_ERR(lnk->newpath)) { |
| 3948 | putname(lnk->oldpath); |
| 3949 | return PTR_ERR(lnk->newpath); |
| 3950 | } |
| 3951 | |
| 3952 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3953 | return 0; |
| 3954 | } |
| 3955 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3956 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3957 | { |
| 3958 | struct io_hardlink *lnk = &req->hardlink; |
| 3959 | int ret; |
| 3960 | |
| 3961 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3962 | return -EAGAIN; |
| 3963 | |
| 3964 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 3965 | lnk->newpath, lnk->flags); |
| 3966 | |
| 3967 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3968 | if (ret < 0) |
| 3969 | req_set_fail(req); |
| 3970 | io_req_complete(req, ret); |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3974 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3975 | const struct io_uring_sqe *sqe) |
| 3976 | { |
| 3977 | #if defined(CONFIG_NET) |
| 3978 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3979 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3980 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3981 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3982 | return -EINVAL; |
| 3983 | |
| 3984 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3985 | return 0; |
| 3986 | #else |
| 3987 | return -EOPNOTSUPP; |
| 3988 | #endif |
| 3989 | } |
| 3990 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3991 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3992 | { |
| 3993 | #if defined(CONFIG_NET) |
| 3994 | struct socket *sock; |
| 3995 | int ret; |
| 3996 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3997 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3998 | return -EAGAIN; |
| 3999 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4000 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4001 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4002 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4003 | |
| 4004 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 4005 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4006 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4007 | io_req_complete(req, ret); |
| 4008 | return 0; |
| 4009 | #else |
| 4010 | return -EOPNOTSUPP; |
| 4011 | #endif |
| 4012 | } |
| 4013 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4014 | static int __io_splice_prep(struct io_kiocb *req, |
| 4015 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4016 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4017 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4018 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4019 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4020 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4021 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4022 | |
| 4023 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4024 | sp->len = READ_ONCE(sqe->len); |
| 4025 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 4026 | |
| 4027 | if (unlikely(sp->flags & ~valid_flags)) |
| 4028 | return -EINVAL; |
| 4029 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 4030 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 4031 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 4032 | if (!sp->file_in) |
| 4033 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4034 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4035 | return 0; |
| 4036 | } |
| 4037 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4038 | static int io_tee_prep(struct io_kiocb *req, |
| 4039 | const struct io_uring_sqe *sqe) |
| 4040 | { |
| 4041 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 4042 | return -EINVAL; |
| 4043 | return __io_splice_prep(req, sqe); |
| 4044 | } |
| 4045 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4046 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4047 | { |
| 4048 | struct io_splice *sp = &req->splice; |
| 4049 | struct file *in = sp->file_in; |
| 4050 | struct file *out = sp->file_out; |
| 4051 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4052 | long ret = 0; |
| 4053 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4054 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4055 | return -EAGAIN; |
| 4056 | if (sp->len) |
| 4057 | ret = do_tee(in, out, sp->len, flags); |
| 4058 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4059 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4060 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4061 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4062 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4063 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4064 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4065 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4066 | return 0; |
| 4067 | } |
| 4068 | |
| 4069 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4070 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4071 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4072 | |
| 4073 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 4074 | sp->off_out = READ_ONCE(sqe->off); |
| 4075 | return __io_splice_prep(req, sqe); |
| 4076 | } |
| 4077 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4078 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4079 | { |
| 4080 | struct io_splice *sp = &req->splice; |
| 4081 | struct file *in = sp->file_in; |
| 4082 | struct file *out = sp->file_out; |
| 4083 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4084 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4085 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4086 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4087 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 4088 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4089 | |
| 4090 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4091 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4092 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4093 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4094 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4095 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4096 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4097 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4098 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4099 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4100 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4101 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4102 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4103 | return 0; |
| 4104 | } |
| 4105 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4106 | /* |
| 4107 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4108 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4109 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4110 | { |
| 4111 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4112 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4113 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4114 | return -EINVAL; |
| 4115 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4116 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4117 | return 0; |
| 4118 | } |
| 4119 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4120 | 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] | 4121 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4122 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4123 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4124 | if (!req->file) |
| 4125 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4126 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4127 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4128 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4129 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4130 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4131 | return -EINVAL; |
| 4132 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4133 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4134 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4135 | return -EINVAL; |
| 4136 | |
| 4137 | req->sync.off = READ_ONCE(sqe->off); |
| 4138 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4139 | return 0; |
| 4140 | } |
| 4141 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4142 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4143 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4144 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4145 | int ret; |
| 4146 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4147 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4148 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4149 | return -EAGAIN; |
| 4150 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4151 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4152 | end > 0 ? end : LLONG_MAX, |
| 4153 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4154 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4155 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4156 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4157 | return 0; |
| 4158 | } |
| 4159 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4160 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4161 | const struct io_uring_sqe *sqe) |
| 4162 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4163 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4164 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4165 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4166 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4167 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4168 | |
| 4169 | req->sync.off = READ_ONCE(sqe->off); |
| 4170 | req->sync.len = READ_ONCE(sqe->addr); |
| 4171 | req->sync.mode = READ_ONCE(sqe->len); |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4175 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4176 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4177 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4178 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4179 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4180 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4181 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4182 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4183 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4184 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4185 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4186 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
| 4189 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4190 | 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] | 4191 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4192 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4193 | int ret; |
| 4194 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4195 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4196 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4197 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4198 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4199 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4200 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4201 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4202 | /* open.how should be already initialised */ |
| 4203 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4204 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4205 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4206 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4207 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4208 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4209 | if (IS_ERR(req->open.filename)) { |
| 4210 | ret = PTR_ERR(req->open.filename); |
| 4211 | req->open.filename = NULL; |
| 4212 | return ret; |
| 4213 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4214 | |
| 4215 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4216 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4217 | return -EINVAL; |
| 4218 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4219 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4220 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4221 | return 0; |
| 4222 | } |
| 4223 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4224 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4225 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4226 | u64 mode = READ_ONCE(sqe->len); |
| 4227 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4228 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4229 | req->open.how = build_open_how(flags, mode); |
| 4230 | return __io_openat_prep(req, sqe); |
| 4231 | } |
| 4232 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4233 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4234 | { |
| 4235 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4236 | size_t len; |
| 4237 | int ret; |
| 4238 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4239 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4240 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4241 | if (len < OPEN_HOW_SIZE_VER0) |
| 4242 | return -EINVAL; |
| 4243 | |
| 4244 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4245 | len); |
| 4246 | if (ret) |
| 4247 | return ret; |
| 4248 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4249 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4252 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4253 | { |
| 4254 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4255 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4256 | bool resolve_nonblock, nonblock_set; |
| 4257 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4258 | int ret; |
| 4259 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4260 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4261 | if (ret) |
| 4262 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4263 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4264 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4265 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4266 | /* |
| 4267 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4268 | * it'll always -EAGAIN |
| 4269 | */ |
| 4270 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4271 | return -EAGAIN; |
| 4272 | op.lookup_flags |= LOOKUP_CACHED; |
| 4273 | op.open_flag |= O_NONBLOCK; |
| 4274 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4275 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4276 | if (!fixed) { |
| 4277 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4278 | if (ret < 0) |
| 4279 | goto err; |
| 4280 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4281 | |
| 4282 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4283 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4284 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4285 | * We could hang on to this 'fd' on retrying, but seems like |
| 4286 | * marginal gain for something that is now known to be a slower |
| 4287 | * 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] | 4288 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4289 | if (!fixed) |
| 4290 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4291 | |
| 4292 | ret = PTR_ERR(file); |
| 4293 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4294 | if (ret == -EAGAIN && |
| 4295 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4296 | return -EAGAIN; |
| 4297 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4298 | } |
| 4299 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4300 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4301 | file->f_flags &= ~O_NONBLOCK; |
| 4302 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4303 | |
| 4304 | if (!fixed) |
| 4305 | fd_install(ret, file); |
| 4306 | else |
| 4307 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4308 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4309 | err: |
| 4310 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4311 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4312 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4313 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4314 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4318 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4319 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4320 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4321 | } |
| 4322 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4323 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4324 | const struct io_uring_sqe *sqe) |
| 4325 | { |
| 4326 | struct io_provide_buf *p = &req->pbuf; |
| 4327 | u64 tmp; |
| 4328 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4329 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4330 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4331 | return -EINVAL; |
| 4332 | |
| 4333 | tmp = READ_ONCE(sqe->fd); |
| 4334 | if (!tmp || tmp > USHRT_MAX) |
| 4335 | return -EINVAL; |
| 4336 | |
| 4337 | memset(p, 0, sizeof(*p)); |
| 4338 | p->nbufs = tmp; |
| 4339 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
| 4343 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4344 | int bgid, unsigned nbufs) |
| 4345 | { |
| 4346 | unsigned i = 0; |
| 4347 | |
| 4348 | /* shouldn't happen */ |
| 4349 | if (!nbufs) |
| 4350 | return 0; |
| 4351 | |
| 4352 | /* the head kbuf is the list itself */ |
| 4353 | while (!list_empty(&buf->list)) { |
| 4354 | struct io_buffer *nxt; |
| 4355 | |
| 4356 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4357 | list_del(&nxt->list); |
| 4358 | kfree(nxt); |
| 4359 | if (++i == nbufs) |
| 4360 | return i; |
| 4361 | } |
| 4362 | i++; |
| 4363 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4364 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4365 | |
| 4366 | return i; |
| 4367 | } |
| 4368 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4369 | 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] | 4370 | { |
| 4371 | struct io_provide_buf *p = &req->pbuf; |
| 4372 | struct io_ring_ctx *ctx = req->ctx; |
| 4373 | struct io_buffer *head; |
| 4374 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4375 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4376 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4377 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4378 | |
| 4379 | lockdep_assert_held(&ctx->uring_lock); |
| 4380 | |
| 4381 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4382 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4383 | if (head) |
| 4384 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4385 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4386 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4387 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4388 | /* complete before unlock, IOPOLL may need the lock */ |
| 4389 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4390 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4391 | return 0; |
| 4392 | } |
| 4393 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4394 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4395 | const struct io_uring_sqe *sqe) |
| 4396 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4397 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4398 | struct io_provide_buf *p = &req->pbuf; |
| 4399 | u64 tmp; |
| 4400 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4401 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4402 | return -EINVAL; |
| 4403 | |
| 4404 | tmp = READ_ONCE(sqe->fd); |
| 4405 | if (!tmp || tmp > USHRT_MAX) |
| 4406 | return -E2BIG; |
| 4407 | p->nbufs = tmp; |
| 4408 | p->addr = READ_ONCE(sqe->addr); |
| 4409 | p->len = READ_ONCE(sqe->len); |
| 4410 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4411 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4412 | &size)) |
| 4413 | return -EOVERFLOW; |
| 4414 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4415 | return -EOVERFLOW; |
| 4416 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4417 | size = (unsigned long)p->len * p->nbufs; |
| 4418 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4419 | return -EFAULT; |
| 4420 | |
| 4421 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4422 | tmp = READ_ONCE(sqe->off); |
| 4423 | if (tmp > USHRT_MAX) |
| 4424 | return -E2BIG; |
| 4425 | p->bid = tmp; |
| 4426 | return 0; |
| 4427 | } |
| 4428 | |
| 4429 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4430 | { |
| 4431 | struct io_buffer *buf; |
| 4432 | u64 addr = pbuf->addr; |
| 4433 | int i, bid = pbuf->bid; |
| 4434 | |
| 4435 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4436 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4437 | if (!buf) |
| 4438 | break; |
| 4439 | |
| 4440 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4441 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4442 | buf->bid = bid; |
| 4443 | addr += pbuf->len; |
| 4444 | bid++; |
| 4445 | if (!*head) { |
| 4446 | INIT_LIST_HEAD(&buf->list); |
| 4447 | *head = buf; |
| 4448 | } else { |
| 4449 | list_add_tail(&buf->list, &(*head)->list); |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | return i ? i : -ENOMEM; |
| 4454 | } |
| 4455 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4456 | 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] | 4457 | { |
| 4458 | struct io_provide_buf *p = &req->pbuf; |
| 4459 | struct io_ring_ctx *ctx = req->ctx; |
| 4460 | struct io_buffer *head, *list; |
| 4461 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4462 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4463 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4464 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4465 | |
| 4466 | lockdep_assert_held(&ctx->uring_lock); |
| 4467 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4468 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4469 | |
| 4470 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4471 | if (ret >= 0 && !list) { |
| 4472 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4473 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4474 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4475 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4476 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4477 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4478 | /* complete before unlock, IOPOLL may need the lock */ |
| 4479 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4480 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4481 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4484 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4485 | const struct io_uring_sqe *sqe) |
| 4486 | { |
| 4487 | #if defined(CONFIG_EPOLL) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4488 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4489 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4490 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4491 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4492 | |
| 4493 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4494 | req->epoll.op = READ_ONCE(sqe->len); |
| 4495 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4496 | |
| 4497 | if (ep_op_has_event(req->epoll.op)) { |
| 4498 | struct epoll_event __user *ev; |
| 4499 | |
| 4500 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4501 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4502 | return -EFAULT; |
| 4503 | } |
| 4504 | |
| 4505 | return 0; |
| 4506 | #else |
| 4507 | return -EOPNOTSUPP; |
| 4508 | #endif |
| 4509 | } |
| 4510 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4511 | 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] | 4512 | { |
| 4513 | #if defined(CONFIG_EPOLL) |
| 4514 | struct io_epoll *ie = &req->epoll; |
| 4515 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4516 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4517 | |
| 4518 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4519 | if (force_nonblock && ret == -EAGAIN) |
| 4520 | return -EAGAIN; |
| 4521 | |
| 4522 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4523 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4524 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4525 | return 0; |
| 4526 | #else |
| 4527 | return -EOPNOTSUPP; |
| 4528 | #endif |
| 4529 | } |
| 4530 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4531 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4532 | { |
| 4533 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4534 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4535 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4536 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4537 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4538 | |
| 4539 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4540 | req->madvise.len = READ_ONCE(sqe->len); |
| 4541 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4542 | return 0; |
| 4543 | #else |
| 4544 | return -EOPNOTSUPP; |
| 4545 | #endif |
| 4546 | } |
| 4547 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4548 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4549 | { |
| 4550 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4551 | struct io_madvise *ma = &req->madvise; |
| 4552 | int ret; |
| 4553 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4554 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4555 | return -EAGAIN; |
| 4556 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4557 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4558 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4559 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4560 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4561 | return 0; |
| 4562 | #else |
| 4563 | return -EOPNOTSUPP; |
| 4564 | #endif |
| 4565 | } |
| 4566 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4567 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4568 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4569 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4570 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4571 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4572 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4573 | |
| 4574 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4575 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4576 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4577 | return 0; |
| 4578 | } |
| 4579 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4580 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4581 | { |
| 4582 | struct io_fadvise *fa = &req->fadvise; |
| 4583 | int ret; |
| 4584 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4585 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4586 | switch (fa->advice) { |
| 4587 | case POSIX_FADV_NORMAL: |
| 4588 | case POSIX_FADV_RANDOM: |
| 4589 | case POSIX_FADV_SEQUENTIAL: |
| 4590 | break; |
| 4591 | default: |
| 4592 | return -EAGAIN; |
| 4593 | } |
| 4594 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4595 | |
| 4596 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4597 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4598 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4599 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4600 | return 0; |
| 4601 | } |
| 4602 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4603 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4604 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4605 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4606 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4607 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4608 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4609 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4610 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4611 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4612 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4613 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4614 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4615 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4616 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4617 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4618 | return 0; |
| 4619 | } |
| 4620 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4621 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4622 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4623 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4624 | int ret; |
| 4625 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4626 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4627 | return -EAGAIN; |
| 4628 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4629 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4630 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4631 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4632 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4633 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4634 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4635 | return 0; |
| 4636 | } |
| 4637 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4638 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4639 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4640 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4641 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4642 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4643 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4644 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4645 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4646 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4647 | |
| 4648 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4649 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4650 | if (req->close.file_slot && req->close.fd) |
| 4651 | return -EINVAL; |
| 4652 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4653 | return 0; |
| 4654 | } |
| 4655 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4656 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4657 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4658 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4659 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4660 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4661 | struct file *file = NULL; |
| 4662 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4663 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4664 | if (req->close.file_slot) { |
| 4665 | ret = io_close_fixed(req, issue_flags); |
| 4666 | goto err; |
| 4667 | } |
| 4668 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4669 | spin_lock(&files->file_lock); |
| 4670 | fdt = files_fdtable(files); |
| 4671 | if (close->fd >= fdt->max_fds) { |
| 4672 | spin_unlock(&files->file_lock); |
| 4673 | goto err; |
| 4674 | } |
| 4675 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4676 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4677 | spin_unlock(&files->file_lock); |
| 4678 | file = NULL; |
| 4679 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4680 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4681 | |
| 4682 | /* 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] | 4683 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4684 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4685 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4686 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4687 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4688 | ret = __close_fd_get_file(close->fd, &file); |
| 4689 | spin_unlock(&files->file_lock); |
| 4690 | if (ret < 0) { |
| 4691 | if (ret == -ENOENT) |
| 4692 | ret = -EBADF; |
| 4693 | goto err; |
| 4694 | } |
| 4695 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4696 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4697 | ret = filp_close(file, current->files); |
| 4698 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4699 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4700 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4701 | if (file) |
| 4702 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4703 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4704 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4707 | 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] | 4708 | { |
| 4709 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4710 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4711 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4712 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4713 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4714 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4715 | return -EINVAL; |
| 4716 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4717 | req->sync.off = READ_ONCE(sqe->off); |
| 4718 | req->sync.len = READ_ONCE(sqe->len); |
| 4719 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4720 | return 0; |
| 4721 | } |
| 4722 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4723 | 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] | 4724 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4725 | int ret; |
| 4726 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4727 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4728 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4729 | return -EAGAIN; |
| 4730 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4731 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4732 | req->sync.flags); |
| 4733 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4734 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4735 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4736 | return 0; |
| 4737 | } |
| 4738 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4739 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4740 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4741 | struct io_async_msghdr *kmsg) |
| 4742 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4743 | struct io_async_msghdr *async_msg = req->async_data; |
| 4744 | |
| 4745 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4746 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4747 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4748 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4749 | return -ENOMEM; |
| 4750 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4751 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4752 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4753 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4754 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4755 | /* if were using fast_iov, set it to the new one */ |
| 4756 | if (!async_msg->free_iov) |
| 4757 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4758 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4759 | return -EAGAIN; |
| 4760 | } |
| 4761 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4762 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4763 | struct io_async_msghdr *iomsg) |
| 4764 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4765 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4766 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4767 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4768 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4769 | } |
| 4770 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4771 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4772 | { |
| 4773 | int ret; |
| 4774 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4775 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4776 | if (!ret) |
| 4777 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4778 | return ret; |
| 4779 | } |
| 4780 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4781 | 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] | 4782 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4783 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4784 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4785 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4786 | return -EINVAL; |
| 4787 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4788 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4789 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4790 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4791 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4792 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4793 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4794 | #ifdef CONFIG_COMPAT |
| 4795 | if (req->ctx->compat) |
| 4796 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4797 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4798 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4801 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4802 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4803 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4804 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4805 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4806 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4807 | int ret; |
| 4808 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4809 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4810 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4811 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4812 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 4813 | if (req_has_async_data(req)) { |
| 4814 | kmsg = req->async_data; |
| 4815 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4816 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4817 | if (ret) |
| 4818 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4819 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4822 | flags = req->sr_msg.msg_flags; |
| 4823 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4824 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4825 | if (flags & MSG_WAITALL) |
| 4826 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4827 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4828 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4829 | |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4830 | if (ret < min_ret) { |
| 4831 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 4832 | return io_setup_async_msg(req, kmsg); |
| 4833 | if (ret == -ERESTARTSYS) |
| 4834 | ret = -EINTR; |
| 4835 | req_set_fail(req); |
| 4836 | } |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4837 | /* fast path, check for non-NULL to avoid function call */ |
| 4838 | if (kmsg->free_iov) |
| 4839 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4840 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4841 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4842 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4843 | } |
| 4844 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4845 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4846 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4847 | struct io_sr_msg *sr = &req->sr_msg; |
| 4848 | struct msghdr msg; |
| 4849 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4850 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4851 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4852 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4853 | int ret; |
| 4854 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4855 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4856 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4857 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4858 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4859 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4860 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4861 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4862 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4863 | msg.msg_name = NULL; |
| 4864 | msg.msg_control = NULL; |
| 4865 | msg.msg_controllen = 0; |
| 4866 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4867 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4868 | flags = req->sr_msg.msg_flags; |
| 4869 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4870 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4871 | if (flags & MSG_WAITALL) |
| 4872 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4873 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4874 | msg.msg_flags = flags; |
| 4875 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4876 | if (ret < min_ret) { |
| 4877 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 4878 | return -EAGAIN; |
| 4879 | if (ret == -ERESTARTSYS) |
| 4880 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4881 | req_set_fail(req); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4882 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4883 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4884 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4887 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4888 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4889 | { |
| 4890 | struct io_sr_msg *sr = &req->sr_msg; |
| 4891 | struct iovec __user *uiov; |
| 4892 | size_t iov_len; |
| 4893 | int ret; |
| 4894 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4895 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4896 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4897 | if (ret) |
| 4898 | return ret; |
| 4899 | |
| 4900 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4901 | if (iov_len > 1) |
| 4902 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4903 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4904 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4905 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4906 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4907 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4908 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4909 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4910 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4911 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4912 | if (ret > 0) |
| 4913 | ret = 0; |
| 4914 | } |
| 4915 | |
| 4916 | return ret; |
| 4917 | } |
| 4918 | |
| 4919 | #ifdef CONFIG_COMPAT |
| 4920 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4921 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4922 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4923 | struct io_sr_msg *sr = &req->sr_msg; |
| 4924 | struct compat_iovec __user *uiov; |
| 4925 | compat_uptr_t ptr; |
| 4926 | compat_size_t len; |
| 4927 | int ret; |
| 4928 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4929 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4930 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4931 | if (ret) |
| 4932 | return ret; |
| 4933 | |
| 4934 | uiov = compat_ptr(ptr); |
| 4935 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4936 | compat_ssize_t clen; |
| 4937 | |
| 4938 | if (len > 1) |
| 4939 | return -EINVAL; |
| 4940 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4941 | return -EFAULT; |
| 4942 | if (__get_user(clen, &uiov->iov_len)) |
| 4943 | return -EFAULT; |
| 4944 | if (clen < 0) |
| 4945 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4946 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4947 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4948 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4949 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4950 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4951 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4952 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4953 | if (ret < 0) |
| 4954 | return ret; |
| 4955 | } |
| 4956 | |
| 4957 | return 0; |
| 4958 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4959 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4960 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4961 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4962 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4963 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4964 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4965 | |
| 4966 | #ifdef CONFIG_COMPAT |
| 4967 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4968 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4969 | #endif |
| 4970 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4971 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4974 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 4975 | unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4976 | { |
| 4977 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4978 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 4979 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4980 | } |
| 4981 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4982 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4983 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 4984 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4985 | } |
| 4986 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4987 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4988 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4989 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4990 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4991 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4992 | if (!ret) |
| 4993 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4994 | return ret; |
| 4995 | } |
| 4996 | |
| 4997 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4998 | { |
| 4999 | struct io_sr_msg *sr = &req->sr_msg; |
| 5000 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 5001 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5002 | return -EINVAL; |
| 5003 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 5004 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 5005 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5006 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5007 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 5008 | if (sr->msg_flags & MSG_DONTWAIT) |
| 5009 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5010 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 5011 | #ifdef CONFIG_COMPAT |
| 5012 | if (req->ctx->compat) |
| 5013 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 5014 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5015 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5018 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5019 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5020 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5021 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5022 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5023 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5024 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5025 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5026 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5027 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5028 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5029 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5030 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5031 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5032 | if (req_has_async_data(req)) { |
| 5033 | kmsg = req->async_data; |
| 5034 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5035 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 5036 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 5037 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5038 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5039 | } |
| 5040 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5041 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5042 | kbuf = io_recv_buffer_select(req, issue_flags); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5043 | if (IS_ERR(kbuf)) |
| 5044 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5045 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5046 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 5047 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5048 | 1, req->sr_msg.len); |
| 5049 | } |
| 5050 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5051 | flags = req->sr_msg.msg_flags; |
| 5052 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5053 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5054 | if (flags & MSG_WAITALL) |
| 5055 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 5056 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5057 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 5058 | kmsg->uaddr, flags); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5059 | if (ret < min_ret) { |
| 5060 | if (ret == -EAGAIN && force_nonblock) |
| 5061 | return io_setup_async_msg(req, kmsg); |
| 5062 | if (ret == -ERESTARTSYS) |
| 5063 | ret = -EINTR; |
| 5064 | req_set_fail(req); |
| 5065 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
| 5066 | req_set_fail(req); |
| 5067 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5068 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5069 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5070 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5071 | /* fast path, check for non-NULL to avoid function call */ |
| 5072 | if (kmsg->free_iov) |
| 5073 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5074 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5075 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5076 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5077 | } |
| 5078 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5079 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5080 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5081 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5082 | struct io_sr_msg *sr = &req->sr_msg; |
| 5083 | struct msghdr msg; |
| 5084 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5085 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5086 | struct iovec iov; |
| 5087 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5088 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5089 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5090 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5091 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5092 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5093 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5094 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5095 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5096 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5097 | kbuf = io_recv_buffer_select(req, issue_flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5098 | if (IS_ERR(kbuf)) |
| 5099 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5100 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5103 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5104 | if (unlikely(ret)) |
| 5105 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5106 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5107 | msg.msg_name = NULL; |
| 5108 | msg.msg_control = NULL; |
| 5109 | msg.msg_controllen = 0; |
| 5110 | msg.msg_namelen = 0; |
| 5111 | msg.msg_iocb = NULL; |
| 5112 | msg.msg_flags = 0; |
| 5113 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5114 | flags = req->sr_msg.msg_flags; |
| 5115 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5116 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5117 | if (flags & MSG_WAITALL) |
| 5118 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5119 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5120 | ret = sock_recvmsg(sock, &msg, flags); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5121 | out_free: |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5122 | if (ret < min_ret) { |
| 5123 | if (ret == -EAGAIN && force_nonblock) |
| 5124 | return -EAGAIN; |
| 5125 | if (ret == -ERESTARTSYS) |
| 5126 | ret = -EINTR; |
| 5127 | req_set_fail(req); |
| 5128 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
| 5129 | req_set_fail(req); |
| 5130 | } |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5131 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5132 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5133 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5134 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5135 | } |
| 5136 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5137 | 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] | 5138 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5139 | struct io_accept *accept = &req->accept; |
| 5140 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5141 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5142 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5143 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5144 | return -EINVAL; |
| 5145 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5146 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5147 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5148 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5149 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5150 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5151 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5152 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5153 | (accept->flags & SOCK_CLOEXEC))) |
| 5154 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5155 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5156 | return -EINVAL; |
| 5157 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5158 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5159 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5160 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5161 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5162 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5163 | { |
| 5164 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5165 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5166 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5167 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5168 | struct file *file; |
| 5169 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5170 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5171 | if (req->file->f_flags & O_NONBLOCK) |
| 5172 | req->flags |= REQ_F_NOWAIT; |
| 5173 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5174 | if (!fixed) { |
| 5175 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5176 | if (unlikely(fd < 0)) |
| 5177 | return fd; |
| 5178 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5179 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5180 | accept->flags); |
| 5181 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5182 | if (!fixed) |
| 5183 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5184 | ret = PTR_ERR(file); |
| 5185 | if (ret == -EAGAIN && force_nonblock) |
| 5186 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5187 | if (ret == -ERESTARTSYS) |
| 5188 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5189 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5190 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5191 | fd_install(fd, file); |
| 5192 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5193 | } else { |
| 5194 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5195 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5196 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5197 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5198 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5199 | } |
| 5200 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5201 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5202 | { |
| 5203 | struct io_async_connect *io = req->async_data; |
| 5204 | struct io_connect *conn = &req->connect; |
| 5205 | |
| 5206 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5207 | } |
| 5208 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5209 | 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] | 5210 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5211 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5212 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5213 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5214 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5215 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5216 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5217 | return -EINVAL; |
| 5218 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5219 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5220 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5221 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5222 | } |
| 5223 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5224 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5225 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5226 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5227 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5228 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5229 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5230 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5231 | if (req_has_async_data(req)) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5232 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5233 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5234 | ret = move_addr_to_kernel(req->connect.addr, |
| 5235 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5236 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5237 | if (ret) |
| 5238 | goto out; |
| 5239 | io = &__io; |
| 5240 | } |
| 5241 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5242 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5243 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5244 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5245 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5246 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5247 | if (req_has_async_data(req)) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5248 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5249 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5250 | ret = -ENOMEM; |
| 5251 | goto out; |
| 5252 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5253 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5254 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5255 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5256 | if (ret == -ERESTARTSYS) |
| 5257 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5258 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5259 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5260 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5261 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5262 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5263 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5264 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5265 | #define IO_NETOP_FN(op) \ |
| 5266 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5267 | { \ |
| 5268 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5271 | #define IO_NETOP_PREP(op) \ |
| 5272 | IO_NETOP_FN(op) \ |
| 5273 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5274 | { \ |
| 5275 | return -EOPNOTSUPP; \ |
| 5276 | } \ |
| 5277 | |
| 5278 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5279 | IO_NETOP_PREP(op) \ |
| 5280 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5281 | { \ |
| 5282 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5283 | } |
| 5284 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5285 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5286 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5287 | IO_NETOP_PREP_ASYNC(connect); |
| 5288 | IO_NETOP_PREP(accept); |
| 5289 | IO_NETOP_FN(send); |
| 5290 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5291 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5292 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5293 | struct io_poll_table { |
| 5294 | struct poll_table_struct pt; |
| 5295 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5296 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5297 | int error; |
| 5298 | }; |
| 5299 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5300 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5301 | __poll_t mask, io_req_tw_func_t func) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5302 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5303 | /* for instances that support it check for an event match first: */ |
| 5304 | if (mask && !(mask & poll->events)) |
| 5305 | return 0; |
| 5306 | |
| 5307 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5308 | |
| 5309 | list_del_init(&poll->wait.entry); |
| 5310 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5311 | req->result = mask; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5312 | req->io_task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5313 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5314 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5315 | * If this fails, then the task is exiting. When a task exits, the |
| 5316 | * work gets canceled, so just cancel this request as well instead |
| 5317 | * of executing it. We can't safely execute it anyway, as we may not |
| 5318 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5319 | */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5320 | io_req_task_work_add(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5321 | return 1; |
| 5322 | } |
| 5323 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5324 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5325 | __acquires(&req->ctx->completion_lock) |
| 5326 | { |
| 5327 | struct io_ring_ctx *ctx = req->ctx; |
| 5328 | |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 5329 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5330 | if (unlikely(req->task->flags & PF_EXITING)) |
| 5331 | WRITE_ONCE(poll->canceled, true); |
| 5332 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5333 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5334 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5335 | |
| 5336 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5337 | } |
| 5338 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5339 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5340 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5341 | add_wait_queue(poll->head, &poll->wait); |
| 5342 | return true; |
| 5343 | } |
| 5344 | |
| 5345 | return false; |
| 5346 | } |
| 5347 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5348 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5349 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5350 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5351 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5352 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5353 | return req->apoll->double_poll; |
| 5354 | } |
| 5355 | |
| 5356 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5357 | { |
| 5358 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5359 | return &req->poll; |
| 5360 | return &req->apoll->poll; |
| 5361 | } |
| 5362 | |
| 5363 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5364 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5365 | { |
| 5366 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5367 | |
| 5368 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5369 | |
| 5370 | if (poll && poll->head) { |
| 5371 | struct wait_queue_head *head = poll->head; |
| 5372 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5373 | spin_lock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5374 | list_del_init(&poll->wait.entry); |
| 5375 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5376 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5377 | poll->head = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5378 | spin_unlock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5382 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5383 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5384 | { |
| 5385 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5386 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5387 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5388 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5389 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 5390 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5391 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5392 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5393 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5394 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5395 | if (req->poll.events & EPOLLONESHOT) |
| 5396 | flags = 0; |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 5397 | |
| 5398 | if (!(flags & IORING_CQE_F_MORE)) { |
| 5399 | io_fill_cqe_req(req, error, flags); |
| 5400 | } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) { |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5401 | req->poll.events |= EPOLLONESHOT; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5402 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5403 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5404 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5405 | } |
| 5406 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5407 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5408 | { |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5409 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5410 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5411 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5412 | if (io_poll_rewait(req, &req->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5413 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5414 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5415 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5416 | |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5417 | if (req->poll.done) { |
| 5418 | spin_unlock(&ctx->completion_lock); |
| 5419 | return; |
| 5420 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5421 | done = __io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5422 | if (done) { |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5423 | io_poll_remove_double(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5424 | hash_del(&req->hash_node); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5425 | req->poll.done = true; |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5426 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5427 | req->result = 0; |
| 5428 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 5429 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5430 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5431 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5432 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5433 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5434 | if (done) { |
| 5435 | nxt = io_put_req_find_next(req); |
| 5436 | if (nxt) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5437 | io_req_task_submit(nxt, locked); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5438 | } |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 5439 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5443 | int sync, void *key) |
| 5444 | { |
| 5445 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5446 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5447 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5448 | unsigned long flags; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5449 | |
| 5450 | /* for instances that support it check for an event match first: */ |
| 5451 | if (mask && !(mask & poll->events)) |
| 5452 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5453 | if (!(poll->events & EPOLLONESHOT)) |
| 5454 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5455 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5456 | list_del_init(&wait->entry); |
| 5457 | |
Jens Axboe | 9ce85ef | 2021-07-09 08:20:28 -0600 | [diff] [blame] | 5458 | if (poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5459 | bool done; |
| 5460 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5461 | spin_lock_irqsave(&poll->head->lock, flags); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5462 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5463 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5464 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5465 | /* make sure double remove sees this as being gone */ |
| 5466 | wait->private = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5467 | spin_unlock_irqrestore(&poll->head->lock, flags); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5468 | if (!done) { |
| 5469 | /* use wait func handler, so it matches the rq type */ |
| 5470 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5471 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5472 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5473 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5474 | return 1; |
| 5475 | } |
| 5476 | |
| 5477 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5478 | wait_queue_func_t wake_func) |
| 5479 | { |
| 5480 | poll->head = NULL; |
| 5481 | poll->done = false; |
| 5482 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 5483 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5484 | /* mask in events that we always want/need */ |
| 5485 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5486 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5487 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5488 | } |
| 5489 | |
| 5490 | 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] | 5491 | struct wait_queue_head *head, |
| 5492 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5493 | { |
| 5494 | struct io_kiocb *req = pt->req; |
| 5495 | |
| 5496 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5497 | * The file being polled uses multiple waitqueues for poll handling |
| 5498 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5499 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5500 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5501 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5502 | struct io_poll_iocb *poll_one = poll; |
| 5503 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5504 | /* double add on the same waitqueue head, ignore */ |
| 5505 | if (poll_one->head == head) |
| 5506 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5507 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5508 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5509 | if ((*poll_ptr)->head == head) |
| 5510 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5511 | pt->error = -EINVAL; |
| 5512 | return; |
| 5513 | } |
Jens Axboe | ea6a693d | 2021-04-15 09:47:13 -0600 | [diff] [blame] | 5514 | /* |
| 5515 | * Can't handle multishot for double wait for now, turn it |
| 5516 | * into one-shot mode. |
| 5517 | */ |
Pavel Begunkov | 7a27472 | 2021-05-17 12:43:34 +0100 | [diff] [blame] | 5518 | if (!(poll_one->events & EPOLLONESHOT)) |
| 5519 | poll_one->events |= EPOLLONESHOT; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5520 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5521 | if (!poll) { |
| 5522 | pt->error = -ENOMEM; |
| 5523 | return; |
| 5524 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5525 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5526 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5527 | poll->wait.private = req; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5528 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5529 | *poll_ptr = poll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5530 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5531 | req->flags |= REQ_F_ASYNC_DATA; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5532 | } |
| 5533 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5534 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5535 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5536 | |
| 5537 | if (poll->events & EPOLLEXCLUSIVE) |
| 5538 | add_wait_queue_exclusive(head, &poll->wait); |
| 5539 | else |
| 5540 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5541 | } |
| 5542 | |
| 5543 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5544 | struct poll_table_struct *p) |
| 5545 | { |
| 5546 | 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] | 5547 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5548 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5549 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5550 | } |
| 5551 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5552 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5553 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5554 | struct async_poll *apoll = req->apoll; |
| 5555 | struct io_ring_ctx *ctx = req->ctx; |
| 5556 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5557 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5558 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5559 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5560 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5561 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5562 | } |
| 5563 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5564 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5565 | io_poll_remove_double(req); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5566 | apoll->poll.done = true; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5567 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5568 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5569 | if (!READ_ONCE(apoll->poll.canceled)) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5570 | io_req_task_submit(req, locked); |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5571 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5572 | io_req_complete_failed(req, -ECANCELED); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5576 | void *key) |
| 5577 | { |
| 5578 | struct io_kiocb *req = wait->private; |
| 5579 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5580 | |
| 5581 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5582 | key_to_poll(key)); |
| 5583 | |
| 5584 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5585 | } |
| 5586 | |
| 5587 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5588 | { |
| 5589 | struct io_ring_ctx *ctx = req->ctx; |
| 5590 | struct hlist_head *list; |
| 5591 | |
| 5592 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5593 | hlist_add_head(&req->hash_node, list); |
| 5594 | } |
| 5595 | |
| 5596 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5597 | struct io_poll_iocb *poll, |
| 5598 | struct io_poll_table *ipt, __poll_t mask, |
| 5599 | wait_queue_func_t wake_func) |
| 5600 | __acquires(&ctx->completion_lock) |
| 5601 | { |
| 5602 | struct io_ring_ctx *ctx = req->ctx; |
| 5603 | bool cancel = false; |
| 5604 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5605 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5606 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5607 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5608 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5609 | |
| 5610 | ipt->pt._key = mask; |
| 5611 | ipt->req = req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5612 | ipt->error = 0; |
| 5613 | ipt->nr_entries = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5614 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5615 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5616 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
| 5617 | ipt->error = -EINVAL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5618 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5619 | spin_lock(&ctx->completion_lock); |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5620 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
Pavel Begunkov | 46fee9a | 2021-07-20 10:50:44 +0100 | [diff] [blame] | 5621 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5622 | if (likely(poll->head)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5623 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5624 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5625 | if (ipt->error) |
| 5626 | cancel = true; |
| 5627 | ipt->error = 0; |
| 5628 | mask = 0; |
| 5629 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5630 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5631 | list_del_init(&poll->wait.entry); |
| 5632 | else if (cancel) |
| 5633 | WRITE_ONCE(poll->canceled, true); |
| 5634 | else if (!poll->done) /* actually waiting for an event */ |
| 5635 | io_poll_req_insert(req); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5636 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | return mask; |
| 5640 | } |
| 5641 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5642 | enum { |
| 5643 | IO_APOLL_OK, |
| 5644 | IO_APOLL_ABORTED, |
| 5645 | IO_APOLL_READY |
| 5646 | }; |
| 5647 | |
| 5648 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5649 | { |
| 5650 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5651 | struct io_ring_ctx *ctx = req->ctx; |
| 5652 | struct async_poll *apoll; |
| 5653 | struct io_poll_table ipt; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5654 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5655 | |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5656 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5657 | return IO_APOLL_ABORTED; |
Pavel Begunkov | 658d0a4 | 2021-10-23 12:13:58 +0100 | [diff] [blame] | 5658 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
| 5659 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5660 | |
| 5661 | if (def->pollin) { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5662 | mask |= POLLIN | POLLRDNORM; |
| 5663 | |
| 5664 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5665 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5666 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5667 | mask &= ~POLLIN; |
| 5668 | } else { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5669 | mask |= POLLOUT | POLLWRNORM; |
| 5670 | } |
| 5671 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5672 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5673 | if (unlikely(!apoll)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5674 | return IO_APOLL_ABORTED; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5675 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5676 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5677 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5678 | ipt.pt._qproc = io_async_queue_proc; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5679 | io_req_set_refcount(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5680 | |
| 5681 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5682 | io_async_wake); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5683 | spin_unlock(&ctx->completion_lock); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5684 | if (ret || ipt.error) |
| 5685 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5686 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5687 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5688 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5689 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5693 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5694 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5695 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5696 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5697 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5698 | if (!poll->head) |
| 5699 | return false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5700 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5701 | if (do_cancel) |
| 5702 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5703 | if (!list_empty(&poll->wait.entry)) { |
| 5704 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5705 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5706 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5707 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5708 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5709 | return do_complete; |
| 5710 | } |
| 5711 | |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5712 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5713 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5714 | { |
| 5715 | bool do_complete; |
| 5716 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5717 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5718 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5719 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5720 | if (do_complete) { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5721 | req_set_fail(req); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 5722 | io_fill_cqe_req(req, -ECANCELED, 0); |
| 5723 | io_commit_cqring(req->ctx); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5724 | io_put_req_deferred(req); |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5725 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5726 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5727 | } |
| 5728 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5729 | /* |
| 5730 | * Returns true if we found and killed one or more poll requests |
| 5731 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 5732 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
| 5733 | struct task_struct *tsk, bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5734 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5735 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5736 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5737 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5738 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5739 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5740 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5741 | struct hlist_head *list; |
| 5742 | |
| 5743 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5744 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 5745 | if (io_match_task(req, tsk, cancel_all)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5746 | posted += io_poll_remove_one(req); |
| 5747 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5748 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5749 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5750 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5751 | if (posted) |
| 5752 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5753 | |
| 5754 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5755 | } |
| 5756 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5757 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5758 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5759 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5760 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5761 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5762 | struct io_kiocb *req; |
| 5763 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5764 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5765 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5766 | if (sqe_addr != req->user_data) |
| 5767 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5768 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5769 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5770 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5771 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5772 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5773 | } |
| 5774 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5775 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5776 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5777 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5778 | { |
| 5779 | struct io_kiocb *req; |
| 5780 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5781 | req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5782 | if (!req) |
| 5783 | return -ENOENT; |
| 5784 | if (io_poll_remove_one(req)) |
| 5785 | return 0; |
| 5786 | |
| 5787 | return -EALREADY; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5790 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5791 | unsigned int flags) |
| 5792 | { |
| 5793 | u32 events; |
| 5794 | |
| 5795 | events = READ_ONCE(sqe->poll32_events); |
| 5796 | #ifdef __BIG_ENDIAN |
| 5797 | events = swahw32(events); |
| 5798 | #endif |
| 5799 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5800 | events |= EPOLLONESHOT; |
| 5801 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5802 | } |
| 5803 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5804 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5805 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5806 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5807 | struct io_poll_update *upd = &req->poll_update; |
| 5808 | u32 flags; |
| 5809 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5810 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5811 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5812 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5813 | return -EINVAL; |
| 5814 | flags = READ_ONCE(sqe->len); |
| 5815 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5816 | IORING_POLL_ADD_MULTI)) |
| 5817 | return -EINVAL; |
| 5818 | /* meaningless without update */ |
| 5819 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5820 | return -EINVAL; |
| 5821 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5822 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5823 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5824 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5825 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5826 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5827 | if (!upd->update_user_data && upd->new_user_data) |
| 5828 | return -EINVAL; |
| 5829 | if (upd->update_events) |
| 5830 | upd->events = io_poll_parse_events(sqe, flags); |
| 5831 | else if (sqe->poll32_events) |
| 5832 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5833 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5834 | return 0; |
| 5835 | } |
| 5836 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5837 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5838 | void *key) |
| 5839 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5840 | struct io_kiocb *req = wait->private; |
| 5841 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5842 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5843 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5844 | } |
| 5845 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5846 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5847 | struct poll_table_struct *p) |
| 5848 | { |
| 5849 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5850 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5851 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5852 | } |
| 5853 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5854 | 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] | 5855 | { |
| 5856 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5857 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5858 | |
| 5859 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5860 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5861 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5862 | return -EINVAL; |
| 5863 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5864 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5865 | return -EINVAL; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 5866 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
| 5867 | return -EINVAL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5868 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5869 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5870 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5871 | return 0; |
| 5872 | } |
| 5873 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5874 | 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] | 5875 | { |
| 5876 | struct io_poll_iocb *poll = &req->poll; |
| 5877 | struct io_ring_ctx *ctx = req->ctx; |
| 5878 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5879 | __poll_t mask; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5880 | bool done; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5881 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5882 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5883 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5884 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5885 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5886 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5887 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5888 | ipt.error = 0; |
Pavel Begunkov | eb6e6f0 | 2021-10-04 20:02:59 +0100 | [diff] [blame] | 5889 | done = __io_poll_complete(req, mask); |
| 5890 | io_commit_cqring(req->ctx); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5891 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5892 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5893 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5894 | if (mask) { |
| 5895 | io_cqring_ev_posted(ctx); |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5896 | if (done) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5897 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5898 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5899 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5902 | 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] | 5903 | { |
| 5904 | struct io_ring_ctx *ctx = req->ctx; |
| 5905 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5906 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5907 | int ret; |
| 5908 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5909 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5910 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5911 | if (!preq) { |
| 5912 | ret = -ENOENT; |
| 5913 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5914 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5915 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5916 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
| 5917 | completing = true; |
| 5918 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; |
| 5919 | goto err; |
| 5920 | } |
| 5921 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5922 | /* |
| 5923 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5924 | * update those. For multishot, if we're racing with completion, just |
| 5925 | * let completion re-add it. |
| 5926 | */ |
| 5927 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5928 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5929 | ret = -EALREADY; |
| 5930 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5931 | } |
| 5932 | /* we now have a detached poll request. reissue. */ |
| 5933 | ret = 0; |
| 5934 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5935 | if (ret < 0) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5936 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5937 | req_set_fail(req); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5938 | io_req_complete(req, ret); |
| 5939 | return 0; |
| 5940 | } |
| 5941 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5942 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5943 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5944 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5945 | preq->poll.events |= IO_POLL_UNMASK; |
| 5946 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5947 | if (req->poll_update.update_user_data) |
| 5948 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5949 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5950 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5951 | /* complete update request, we're done with it */ |
| 5952 | io_req_complete(req, ret); |
| 5953 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5954 | if (!completing) { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5955 | ret = io_poll_add(preq, issue_flags); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5956 | if (ret < 0) { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5957 | req_set_fail(preq); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5958 | io_req_complete(preq, ret); |
| 5959 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5960 | } |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5964 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5965 | { |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 5966 | struct io_timeout_data *data = req->async_data; |
| 5967 | |
| 5968 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
| 5969 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 5970 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5971 | } |
| 5972 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5973 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5974 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5975 | struct io_timeout_data *data = container_of(timer, |
| 5976 | struct io_timeout_data, timer); |
| 5977 | struct io_kiocb *req = data->req; |
| 5978 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5979 | unsigned long flags; |
| 5980 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5981 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5982 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5983 | atomic_set(&req->ctx->cq_timeouts, |
| 5984 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5985 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5986 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5987 | req->io_task_work.func = io_req_task_timeout; |
| 5988 | io_req_task_work_add(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5989 | return HRTIMER_NORESTART; |
| 5990 | } |
| 5991 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5992 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5993 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5994 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5995 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5996 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5997 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5998 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5999 | |
| 6000 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6001 | found = user_data == req->user_data; |
| 6002 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6003 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6004 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6005 | if (!found) |
| 6006 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6007 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6008 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6009 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6010 | return ERR_PTR(-EALREADY); |
| 6011 | list_del_init(&req->timeout.list); |
| 6012 | return req; |
| 6013 | } |
| 6014 | |
| 6015 | 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] | 6016 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6017 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6018 | { |
| 6019 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6020 | |
| 6021 | if (IS_ERR(req)) |
| 6022 | return PTR_ERR(req); |
| 6023 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6024 | req_set_fail(req); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 6025 | io_fill_cqe_req(req, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 6026 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6027 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6028 | } |
| 6029 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6030 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 6031 | { |
| 6032 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 6033 | case IORING_TIMEOUT_BOOTTIME: |
| 6034 | return CLOCK_BOOTTIME; |
| 6035 | case IORING_TIMEOUT_REALTIME: |
| 6036 | return CLOCK_REALTIME; |
| 6037 | default: |
| 6038 | /* can't happen, vetted at prep time */ |
| 6039 | WARN_ON_ONCE(1); |
| 6040 | fallthrough; |
| 6041 | case 0: |
| 6042 | return CLOCK_MONOTONIC; |
| 6043 | } |
| 6044 | } |
| 6045 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6046 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6047 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 6048 | __must_hold(&ctx->timeout_lock) |
| 6049 | { |
| 6050 | struct io_timeout_data *io; |
| 6051 | struct io_kiocb *req; |
| 6052 | bool found = false; |
| 6053 | |
| 6054 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 6055 | found = user_data == req->user_data; |
| 6056 | if (found) |
| 6057 | break; |
| 6058 | } |
| 6059 | if (!found) |
| 6060 | return -ENOENT; |
| 6061 | |
| 6062 | io = req->async_data; |
| 6063 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 6064 | return -EALREADY; |
| 6065 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 6066 | io->timer.function = io_link_timeout_fn; |
| 6067 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6071 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6072 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6073 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6074 | { |
| 6075 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6076 | struct io_timeout_data *data; |
| 6077 | |
| 6078 | if (IS_ERR(req)) |
| 6079 | return PTR_ERR(req); |
| 6080 | |
| 6081 | req->timeout.off = 0; /* noseq */ |
| 6082 | data = req->async_data; |
| 6083 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6084 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6085 | data->timer.function = io_timeout_fn; |
| 6086 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 6087 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6088 | } |
| 6089 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6090 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 6091 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6092 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6093 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 6094 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6095 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6096 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6097 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6098 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6099 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6100 | return -EINVAL; |
| 6101 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6102 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6103 | tr->addr = READ_ONCE(sqe->addr); |
| 6104 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6105 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6106 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6107 | return -EINVAL; |
| 6108 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6109 | tr->ltimeout = true; |
| 6110 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6111 | return -EINVAL; |
| 6112 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6113 | return -EFAULT; |
| 6114 | } else if (tr->flags) { |
| 6115 | /* timeout removal doesn't support flags */ |
| 6116 | return -EINVAL; |
| 6117 | } |
| 6118 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6119 | return 0; |
| 6120 | } |
| 6121 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6122 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6123 | { |
| 6124 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6125 | : HRTIMER_MODE_REL; |
| 6126 | } |
| 6127 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6128 | /* |
| 6129 | * Remove or update an existing timeout command |
| 6130 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6131 | 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] | 6132 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6133 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6134 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6135 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6136 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6137 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6138 | spin_lock(&ctx->completion_lock); |
| 6139 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6140 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6141 | spin_unlock_irq(&ctx->timeout_lock); |
| 6142 | spin_unlock(&ctx->completion_lock); |
| 6143 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6144 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6145 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6146 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6147 | if (tr->ltimeout) |
| 6148 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6149 | else |
| 6150 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6151 | spin_unlock_irq(&ctx->timeout_lock); |
| 6152 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6153 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6154 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6155 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6156 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6157 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6158 | } |
| 6159 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6160 | 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] | 6161 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6162 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6163 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6164 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6165 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6166 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6168 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6169 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6170 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6171 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6172 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6173 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6174 | flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 6175 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
| 6176 | IORING_TIMEOUT_ETIME_SUCCESS)) |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6177 | return -EINVAL; |
| 6178 | /* more than one clock specified is invalid, obviously */ |
| 6179 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6180 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6181 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6182 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6183 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6184 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6185 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6186 | |
Pavel Begunkov | d6a644a | 2021-10-23 12:14:00 +0100 | [diff] [blame] | 6187 | if (WARN_ON_ONCE(req_has_async_data(req))) |
| 6188 | return -EFAULT; |
| 6189 | if (io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6190 | return -ENOMEM; |
| 6191 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6192 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6193 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6194 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6195 | |
| 6196 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6197 | return -EFAULT; |
| 6198 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6199 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6200 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6201 | |
| 6202 | if (is_timeout_link) { |
| 6203 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6204 | |
| 6205 | if (!link->head) |
| 6206 | return -EINVAL; |
| 6207 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6208 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6209 | req->timeout.head = link->last; |
| 6210 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6211 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6212 | return 0; |
| 6213 | } |
| 6214 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6215 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6216 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6217 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6218 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6219 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6220 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6221 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6222 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6223 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6224 | /* |
| 6225 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6226 | * timeout event to be satisfied. If it isn't set, then this is |
| 6227 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6228 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6229 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6230 | entry = ctx->timeout_list.prev; |
| 6231 | goto add; |
| 6232 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6233 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6234 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6235 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6236 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6237 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6238 | * This is safe because ->completion_lock is held, and submissions |
| 6239 | * and completions are never mixed in the same ->completion_lock section. |
| 6240 | */ |
| 6241 | ctx->cq_last_tm_flush = tail; |
| 6242 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6243 | /* |
| 6244 | * Insertion sort, ensuring the first entry in the list is always |
| 6245 | * the one we need first. |
| 6246 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6247 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6248 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6249 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6250 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6251 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6252 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6253 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6254 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6255 | break; |
| 6256 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6257 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6258 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6259 | data->timer.function = io_timeout_fn; |
| 6260 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6261 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6262 | return 0; |
| 6263 | } |
| 6264 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6265 | struct io_cancel_data { |
| 6266 | struct io_ring_ctx *ctx; |
| 6267 | u64 user_data; |
| 6268 | }; |
| 6269 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6270 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6271 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6272 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6273 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6274 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6275 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6276 | } |
| 6277 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6278 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6279 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6280 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6281 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6282 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6283 | int ret = 0; |
| 6284 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6285 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6286 | return -ENOENT; |
| 6287 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6288 | 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] | 6289 | switch (cancel_ret) { |
| 6290 | case IO_WQ_CANCEL_OK: |
| 6291 | ret = 0; |
| 6292 | break; |
| 6293 | case IO_WQ_CANCEL_RUNNING: |
| 6294 | ret = -EALREADY; |
| 6295 | break; |
| 6296 | case IO_WQ_CANCEL_NOTFOUND: |
| 6297 | ret = -ENOENT; |
| 6298 | break; |
| 6299 | } |
| 6300 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6301 | return ret; |
| 6302 | } |
| 6303 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6304 | 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] | 6305 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6306 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6307 | int ret; |
| 6308 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6309 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6310 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6311 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6312 | if (ret != -ENOENT) |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6313 | return ret; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6314 | |
| 6315 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6316 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6317 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6318 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6319 | if (ret != -ENOENT) |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6320 | goto out; |
| 6321 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6322 | out: |
| 6323 | spin_unlock(&ctx->completion_lock); |
| 6324 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6325 | } |
| 6326 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6327 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6328 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6329 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6330 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6331 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6332 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6333 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6334 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6335 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6336 | return -EINVAL; |
| 6337 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6338 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6339 | return 0; |
| 6340 | } |
| 6341 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6342 | 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] | 6343 | { |
| 6344 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6345 | u64 sqe_addr = req->cancel.addr; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6346 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6347 | struct io_tctx_node *node; |
| 6348 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6349 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6350 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6351 | if (ret != -ENOENT) |
| 6352 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6353 | |
| 6354 | /* slow path, try all io-wq's */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6355 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6356 | ret = -ENOENT; |
| 6357 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6358 | struct io_uring_task *tctx = node->task->io_uring; |
| 6359 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6360 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6361 | if (ret != -ENOENT) |
| 6362 | break; |
| 6363 | } |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6364 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6365 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6366 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6367 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6368 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6369 | return 0; |
| 6370 | } |
| 6371 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6372 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6373 | const struct io_uring_sqe *sqe) |
| 6374 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6375 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6376 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6377 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6378 | return -EINVAL; |
| 6379 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6380 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6381 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6382 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6383 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6384 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6385 | return 0; |
| 6386 | } |
| 6387 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6388 | 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] | 6389 | { |
| 6390 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6391 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6392 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6393 | int ret; |
| 6394 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6395 | up.offset = req->rsrc_update.offset; |
| 6396 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6397 | up.nr = 0; |
| 6398 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6399 | up.resv = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6400 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6401 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6402 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6403 | &up, req->rsrc_update.nr_args); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6404 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6405 | |
| 6406 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6407 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6408 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6409 | return 0; |
| 6410 | } |
| 6411 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6412 | 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] | 6413 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6414 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6415 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6416 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6417 | case IORING_OP_READV: |
| 6418 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6419 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6420 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6421 | case IORING_OP_WRITEV: |
| 6422 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6423 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6424 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6425 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6426 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6427 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6428 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6429 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6430 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6431 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6432 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6433 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6434 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6435 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6436 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6437 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6438 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6439 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6440 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6441 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6442 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6443 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6444 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6445 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6446 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6447 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6448 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6449 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6450 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6451 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6452 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6453 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6454 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6455 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6456 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6457 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6458 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6459 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6460 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6461 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6462 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6463 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6464 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6465 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6466 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6467 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6468 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6469 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6470 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6471 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6472 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6473 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6474 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6475 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6476 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6477 | case IORING_OP_SHUTDOWN: |
| 6478 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6479 | case IORING_OP_RENAMEAT: |
| 6480 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6481 | case IORING_OP_UNLINKAT: |
| 6482 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6483 | case IORING_OP_MKDIRAT: |
| 6484 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6485 | case IORING_OP_SYMLINKAT: |
| 6486 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6487 | case IORING_OP_LINKAT: |
| 6488 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6489 | } |
| 6490 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6491 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6492 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6493 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6494 | } |
| 6495 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6496 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6497 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6498 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6499 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6500 | if (WARN_ON_ONCE(req_has_async_data(req))) |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6501 | return -EFAULT; |
| 6502 | if (io_alloc_async_data(req)) |
| 6503 | return -EAGAIN; |
| 6504 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6505 | switch (req->opcode) { |
| 6506 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6507 | return io_rw_prep_async(req, READ); |
| 6508 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6509 | return io_rw_prep_async(req, WRITE); |
| 6510 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6511 | return io_sendmsg_prep_async(req); |
| 6512 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6513 | return io_recvmsg_prep_async(req); |
| 6514 | case IORING_OP_CONNECT: |
| 6515 | return io_connect_prep_async(req); |
| 6516 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6517 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6518 | req->opcode); |
| 6519 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6520 | } |
| 6521 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6522 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6523 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6524 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6525 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6526 | /* need original cached_sq_head, but it was increased for each req */ |
| 6527 | io_for_each_link(req, req) |
| 6528 | seq--; |
| 6529 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6530 | } |
| 6531 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 6532 | static __cold void io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6533 | { |
| 6534 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6535 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6536 | int ret; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6537 | u32 seq = io_get_sequence(req); |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6538 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6539 | /* Still need defer if there is pending req in defer list. */ |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6540 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6541 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6542 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6543 | queue: |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6544 | ctx->drain_active = false; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6545 | io_req_task_queue(req); |
| 6546 | return; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6547 | } |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6548 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6549 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6550 | ret = io_req_prep_async(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6551 | if (ret) { |
| 6552 | fail: |
| 6553 | io_req_complete_failed(req, ret); |
| 6554 | return; |
| 6555 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6556 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6557 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6558 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6559 | ret = -ENOMEM; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6560 | goto fail; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6561 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6562 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6563 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6564 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6565 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6566 | kfree(de); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6567 | goto queue; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6571 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6572 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6573 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6574 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6575 | } |
| 6576 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6577 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6578 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6579 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 6580 | kfree(req->kbuf); |
| 6581 | req->kbuf = NULL; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6582 | } |
| 6583 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6584 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6585 | switch (req->opcode) { |
| 6586 | case IORING_OP_READV: |
| 6587 | case IORING_OP_READ_FIXED: |
| 6588 | case IORING_OP_READ: |
| 6589 | case IORING_OP_WRITEV: |
| 6590 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6591 | case IORING_OP_WRITE: { |
| 6592 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6593 | |
| 6594 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6595 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6596 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6597 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6598 | case IORING_OP_SENDMSG: { |
| 6599 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6600 | |
| 6601 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6602 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6603 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6604 | case IORING_OP_SPLICE: |
| 6605 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6606 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6607 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6608 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6609 | case IORING_OP_OPENAT: |
| 6610 | case IORING_OP_OPENAT2: |
| 6611 | if (req->open.filename) |
| 6612 | putname(req->open.filename); |
| 6613 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6614 | case IORING_OP_RENAMEAT: |
| 6615 | putname(req->rename.oldpath); |
| 6616 | putname(req->rename.newpath); |
| 6617 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6618 | case IORING_OP_UNLINKAT: |
| 6619 | putname(req->unlink.filename); |
| 6620 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6621 | case IORING_OP_MKDIRAT: |
| 6622 | putname(req->mkdir.filename); |
| 6623 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6624 | case IORING_OP_SYMLINKAT: |
| 6625 | putname(req->symlink.oldpath); |
| 6626 | putname(req->symlink.newpath); |
| 6627 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6628 | case IORING_OP_LINKAT: |
| 6629 | putname(req->hardlink.oldpath); |
| 6630 | putname(req->hardlink.newpath); |
| 6631 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6632 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6633 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6634 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6635 | kfree(req->apoll->double_poll); |
| 6636 | kfree(req->apoll); |
| 6637 | req->apoll = NULL; |
| 6638 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6639 | if (req->flags & REQ_F_INFLIGHT) { |
| 6640 | struct io_uring_task *tctx = req->task->io_uring; |
| 6641 | |
| 6642 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6643 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6644 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6645 | put_cred(req->creds); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6646 | if (req->flags & REQ_F_ASYNC_DATA) { |
| 6647 | kfree(req->async_data); |
| 6648 | req->async_data = NULL; |
| 6649 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6650 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6651 | } |
| 6652 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6653 | 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] | 6654 | { |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6655 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6656 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6657 | |
Pavel Begunkov | 6878b40 | 2021-09-24 21:59:41 +0100 | [diff] [blame] | 6658 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6659 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6660 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6661 | if (!io_op_defs[req->opcode].audit_skip) |
| 6662 | audit_uring_entry(req->opcode); |
| 6663 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6664 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6665 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6666 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6667 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6668 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6669 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6670 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6671 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6672 | break; |
| 6673 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6674 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6675 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6676 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6677 | break; |
| 6678 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6679 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6680 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6681 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6682 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6683 | break; |
| 6684 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6685 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6686 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6687 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6688 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6689 | break; |
| 6690 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6691 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6692 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6693 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6694 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6695 | break; |
| 6696 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6697 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6698 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6699 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6700 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6701 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6702 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6703 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6704 | break; |
| 6705 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6706 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6707 | break; |
| 6708 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6709 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6710 | break; |
| 6711 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6712 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6713 | break; |
| 6714 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6715 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6716 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6717 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6718 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6719 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6720 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6721 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6722 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6723 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6724 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6725 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6726 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6727 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6728 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6729 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6730 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6731 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6732 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6733 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6734 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6735 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6736 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6737 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6738 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6739 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6740 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6741 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6742 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6743 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6744 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6745 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6746 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6747 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6748 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6749 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6750 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6751 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6752 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6753 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6754 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6755 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6756 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6757 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6758 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6759 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6760 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6761 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6762 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6763 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6764 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6765 | case IORING_OP_MKDIRAT: |
| 6766 | ret = io_mkdirat(req, issue_flags); |
| 6767 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6768 | case IORING_OP_SYMLINKAT: |
| 6769 | ret = io_symlinkat(req, issue_flags); |
| 6770 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6771 | case IORING_OP_LINKAT: |
| 6772 | ret = io_linkat(req, issue_flags); |
| 6773 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6774 | default: |
| 6775 | ret = -EINVAL; |
| 6776 | break; |
| 6777 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6778 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6779 | if (!io_op_defs[req->opcode].audit_skip) |
| 6780 | audit_uring_exit(!ret, ret); |
| 6781 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6782 | if (creds) |
| 6783 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6784 | if (ret) |
| 6785 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6786 | /* If the op doesn't have a file, we're not polling for it */ |
Pavel Begunkov | 9983028 | 2021-10-15 17:09:11 +0100 | [diff] [blame] | 6787 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 6788 | io_iopoll_req_issued(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6789 | |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 6793 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 6794 | { |
| 6795 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6796 | |
| 6797 | req = io_put_req_find_next(req); |
| 6798 | return req ? &req->work : NULL; |
| 6799 | } |
| 6800 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6801 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6802 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6803 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6804 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
| 6805 | bool needs_poll = false; |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6806 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6807 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6808 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6809 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 6810 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 6811 | __io_req_set_refcount(req, 2); |
| 6812 | else |
| 6813 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6814 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6815 | timeout = io_prep_linked_timeout(req); |
| 6816 | if (timeout) |
| 6817 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6818 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6819 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6820 | if (work->flags & IO_WQ_WORK_CANCEL) { |
| 6821 | io_req_task_queue_fail(req, -ECANCELED); |
| 6822 | return; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6823 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6824 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6825 | if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6826 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 6827 | bool opcode_poll = def->pollin || def->pollout; |
| 6828 | |
| 6829 | if (opcode_poll && file_can_poll(req->file)) { |
| 6830 | needs_poll = true; |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6831 | issue_flags |= IO_URING_F_NONBLOCK; |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6832 | } |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6833 | } |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6834 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6835 | do { |
| 6836 | ret = io_issue_sqe(req, issue_flags); |
| 6837 | if (ret != -EAGAIN) |
| 6838 | break; |
| 6839 | /* |
| 6840 | * We can get EAGAIN for iopolled IO even though we're |
| 6841 | * forcing a sync submission from here, since we can't |
| 6842 | * wait for request slots on the block side. |
| 6843 | */ |
| 6844 | if (!needs_poll) { |
| 6845 | cond_resched(); |
| 6846 | continue; |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6847 | } |
| 6848 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6849 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
| 6850 | return; |
| 6851 | /* aborted or ready, in either case retry blocking */ |
| 6852 | needs_poll = false; |
| 6853 | issue_flags &= ~IO_URING_F_NONBLOCK; |
| 6854 | } while (1); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6855 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6856 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6857 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6858 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6859 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6860 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6861 | 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] | 6862 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6863 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6864 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6867 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6868 | int index) |
| 6869 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6870 | 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] | 6871 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6872 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6873 | } |
| 6874 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6875 | 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] | 6876 | { |
| 6877 | unsigned long file_ptr = (unsigned long) file; |
| 6878 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 6879 | file_ptr |= io_file_get_flags(file); |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6880 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6881 | } |
| 6882 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6883 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
| 6884 | struct io_kiocb *req, int fd) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6885 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6886 | struct file *file; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6887 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6888 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6889 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
| 6890 | return NULL; |
| 6891 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6892 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 6893 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6894 | file_ptr &= ~FFS_MASK; |
| 6895 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 6896 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 6897 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6898 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6899 | } |
| 6900 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6901 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6902 | struct io_kiocb *req, int fd) |
| 6903 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6904 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6905 | |
| 6906 | trace_io_uring_file_get(ctx, fd); |
| 6907 | |
| 6908 | /* we don't allow fixed io_uring files */ |
| 6909 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6910 | io_req_track_inflight(req); |
| 6911 | return file; |
| 6912 | } |
| 6913 | |
| 6914 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6915 | struct io_kiocb *req, int fd, bool fixed) |
| 6916 | { |
| 6917 | if (fixed) |
| 6918 | return io_file_get_fixed(ctx, req, fd); |
| 6919 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6920 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6923 | 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] | 6924 | { |
| 6925 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6926 | int ret; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6927 | |
| 6928 | if (prev) { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6929 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6930 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6931 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6932 | } else { |
| 6933 | io_req_complete_post(req, -ETIME, 0); |
| 6934 | } |
| 6935 | } |
| 6936 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6937 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6938 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6939 | struct io_timeout_data *data = container_of(timer, |
| 6940 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6941 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6942 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6943 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6944 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6945 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6946 | prev = req->timeout.head; |
| 6947 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6948 | |
| 6949 | /* |
| 6950 | * We don't expect the list to be empty, that will only happen if we |
| 6951 | * race with the completion of the linked work. |
| 6952 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6953 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6954 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6955 | if (!req_ref_inc_not_zero(prev)) |
| 6956 | prev = NULL; |
| 6957 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6958 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6959 | req->timeout.prev = prev; |
| 6960 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6961 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6962 | req->io_task_work.func = io_req_task_link_timeout; |
| 6963 | io_req_task_work_add(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6964 | return HRTIMER_NORESTART; |
| 6965 | } |
| 6966 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6967 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6968 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6969 | struct io_ring_ctx *ctx = req->ctx; |
| 6970 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6971 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6972 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6973 | * If the back reference is NULL, then our linked request finished |
| 6974 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6975 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6976 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6977 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6978 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6979 | data->timer.function = io_link_timeout_fn; |
| 6980 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6981 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6982 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6983 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6984 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6985 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6986 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6987 | } |
| 6988 | |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6989 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
| 6990 | __must_hold(&req->ctx->uring_lock) |
| 6991 | { |
| 6992 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 6993 | |
| 6994 | switch (io_arm_poll_handler(req)) { |
| 6995 | case IO_APOLL_READY: |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6996 | io_req_task_queue(req); |
| 6997 | break; |
| 6998 | case IO_APOLL_ABORTED: |
| 6999 | /* |
| 7000 | * Queued up for async execution, worker will release |
| 7001 | * submit reference when the iocb is actually submitted. |
| 7002 | */ |
| 7003 | io_queue_async_work(req, NULL); |
| 7004 | break; |
| 7005 | } |
| 7006 | |
| 7007 | if (linked_timeout) |
| 7008 | io_queue_linked_timeout(linked_timeout); |
| 7009 | } |
| 7010 | |
| 7011 | static inline void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7012 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7013 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7014 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 7015 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7016 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7017 | 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] | 7018 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7019 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
| 7020 | io_req_add_compl_list(req); |
Pavel Begunkov | d9f9d28 | 2021-09-24 22:00:00 +0100 | [diff] [blame] | 7021 | return; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7022 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 7023 | /* |
| 7024 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 7025 | * doesn't support non-blocking read/write attempts |
| 7026 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7027 | if (likely(!ret)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7028 | linked_timeout = io_prep_linked_timeout(req); |
| 7029 | if (linked_timeout) |
| 7030 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7031 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 7032 | io_queue_sqe_arm_apoll(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 7033 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 7034 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7035 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7036 | } |
| 7037 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7038 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7039 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7040 | { |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7041 | if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 7042 | io_req_complete_fail_submit(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 7043 | } else if (unlikely(req->ctx->drain_active)) { |
| 7044 | io_drain_req(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7045 | } else { |
| 7046 | int ret = io_req_prep_async(req); |
| 7047 | |
| 7048 | if (unlikely(ret)) |
| 7049 | io_req_complete_failed(req, ret); |
| 7050 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7051 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 7052 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7053 | } |
| 7054 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7055 | static inline void io_queue_sqe(struct io_kiocb *req) |
| 7056 | __must_hold(&req->ctx->uring_lock) |
| 7057 | { |
| 7058 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) |
| 7059 | __io_queue_sqe(req); |
| 7060 | else |
| 7061 | io_queue_sqe_fallback(req); |
| 7062 | } |
| 7063 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7064 | /* |
| 7065 | * Check SQE restrictions (opcode and flags). |
| 7066 | * |
| 7067 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 7068 | */ |
| 7069 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 7070 | struct io_kiocb *req, |
| 7071 | unsigned int sqe_flags) |
| 7072 | { |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7073 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 7074 | return false; |
| 7075 | |
| 7076 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 7077 | ctx->restrictions.sqe_flags_required) |
| 7078 | return false; |
| 7079 | |
| 7080 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 7081 | ctx->restrictions.sqe_flags_required)) |
| 7082 | return false; |
| 7083 | |
| 7084 | return true; |
| 7085 | } |
| 7086 | |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7087 | static void io_init_req_drain(struct io_kiocb *req) |
| 7088 | { |
| 7089 | struct io_ring_ctx *ctx = req->ctx; |
| 7090 | struct io_kiocb *head = ctx->submit_state.link.head; |
| 7091 | |
| 7092 | ctx->drain_active = true; |
| 7093 | if (head) { |
| 7094 | /* |
| 7095 | * If we need to drain a request in the middle of a link, drain |
| 7096 | * the head request and the next request/link after the current |
| 7097 | * link. Considering sequential execution of links, |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame^] | 7098 | * REQ_F_IO_DRAIN will be maintained for every request of our |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7099 | * link. |
| 7100 | */ |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame^] | 7101 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7102 | ctx->drain_next = true; |
| 7103 | } |
| 7104 | } |
| 7105 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7106 | 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] | 7107 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7108 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7109 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7110 | unsigned int sqe_flags; |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7111 | int personality; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7112 | u8 opcode; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7113 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 7114 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7115 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7116 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 7117 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7118 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7119 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7120 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7121 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7122 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7123 | if (unlikely(opcode >= IORING_OP_LAST)) { |
| 7124 | req->opcode = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7125 | return -EINVAL; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7126 | } |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7127 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
| 7128 | /* enforce forwards compatibility on users */ |
| 7129 | if (sqe_flags & ~SQE_VALID_FLAGS) |
| 7130 | return -EINVAL; |
| 7131 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7132 | !io_op_defs[opcode].buffer_select) |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7133 | return -EOPNOTSUPP; |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 7134 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
| 7135 | ctx->drain_disabled = true; |
| 7136 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 7137 | if (ctx->drain_disabled) |
| 7138 | return -EOPNOTSUPP; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7139 | io_init_req_drain(req); |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 7140 | } |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7141 | } |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7142 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { |
| 7143 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) |
| 7144 | return -EACCES; |
| 7145 | /* knock it to the slow queue path, will be drained there */ |
| 7146 | if (ctx->drain_active) |
| 7147 | req->flags |= REQ_F_FORCE_ASYNC; |
| 7148 | /* if there is no link, we're at "next" request and need to drain */ |
| 7149 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { |
| 7150 | ctx->drain_next = false; |
| 7151 | ctx->drain_active = true; |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame^] | 7152 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7153 | } |
| 7154 | } |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7155 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7156 | if (io_op_defs[opcode].needs_file) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7157 | struct io_submit_state *state = &ctx->submit_state; |
| 7158 | |
| 7159 | /* |
| 7160 | * Plug now if we have more than 2 IO left after this, and the |
| 7161 | * target is potentially a read/write to block based storage. |
| 7162 | */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7163 | if (state->need_plug && io_op_defs[opcode].plug) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7164 | state->plug_started = true; |
| 7165 | state->need_plug = false; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7166 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7167 | } |
| 7168 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7169 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7170 | (sqe_flags & IOSQE_FIXED_FILE)); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7171 | if (unlikely(!req->file)) |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7172 | return -EBADF; |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7173 | } |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 7174 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7175 | personality = READ_ONCE(sqe->personality); |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7176 | if (personality) { |
Linus Torvalds | cdab10b | 2021-11-01 21:06:18 -0700 | [diff] [blame] | 7177 | int ret; |
| 7178 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7179 | req->creds = xa_load(&ctx->personalities, personality); |
| 7180 | if (!req->creds) |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7181 | return -EINVAL; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7182 | get_cred(req->creds); |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 7183 | ret = security_uring_override_creds(req->creds); |
| 7184 | if (ret) { |
| 7185 | put_cred(req->creds); |
| 7186 | return ret; |
| 7187 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7188 | req->flags |= REQ_F_CREDS; |
| 7189 | } |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7190 | |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7191 | return io_req_prep(req, sqe); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7192 | } |
| 7193 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7194 | 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] | 7195 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7196 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7197 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7198 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7199 | int ret; |
| 7200 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7201 | ret = io_init_req(ctx, req, sqe); |
| 7202 | if (unlikely(ret)) { |
Jens Axboe | a87acfd | 2021-09-11 16:04:50 -0600 | [diff] [blame] | 7203 | trace_io_uring_req_failed(sqe, ret); |
| 7204 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7205 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7206 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7207 | /* |
| 7208 | * we can judge a link req is failed or cancelled by if |
| 7209 | * REQ_F_FAIL is set, but the head is an exception since |
| 7210 | * it may be set REQ_F_FAIL because of other req's failure |
| 7211 | * so let's leverage req->result to distinguish if a head |
| 7212 | * is set REQ_F_FAIL because of its failure or other req's |
| 7213 | * failure so that we can set the correct ret code for it. |
| 7214 | * init result here to avoid affecting the normal path. |
| 7215 | */ |
| 7216 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7217 | req_fail_link_node(link->head, -ECANCELED); |
| 7218 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7219 | /* |
| 7220 | * the current req is a normal req, we should return |
| 7221 | * error and thus break the submittion loop. |
| 7222 | */ |
| 7223 | io_req_complete_failed(req, ret); |
| 7224 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7225 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7226 | req_fail_link_node(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7227 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7228 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7229 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7230 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7231 | req->flags, true, |
| 7232 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7233 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7234 | /* |
| 7235 | * If we already have a head request, queue this one for async |
| 7236 | * submittal once the head completes. If we don't have a head but |
| 7237 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7238 | * submitted sync once the chain is complete. If none of those |
| 7239 | * conditions are true (normal request), then just queue it. |
| 7240 | */ |
| 7241 | if (link->head) { |
| 7242 | struct io_kiocb *head = link->head; |
| 7243 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7244 | if (!(req->flags & REQ_F_FAIL)) { |
| 7245 | ret = io_req_prep_async(req); |
| 7246 | if (unlikely(ret)) { |
| 7247 | req_fail_link_node(req, ret); |
| 7248 | if (!(head->flags & REQ_F_FAIL)) |
| 7249 | req_fail_link_node(head, -ECANCELED); |
| 7250 | } |
| 7251 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7252 | trace_io_uring_link(ctx, req, head); |
| 7253 | link->last->link = req; |
| 7254 | link->last = req; |
| 7255 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7256 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
| 7257 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7258 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7259 | link->head = NULL; |
| 7260 | req = head; |
| 7261 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 7262 | link->head = req; |
| 7263 | link->last = req; |
| 7264 | return 0; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7265 | } |
| 7266 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7267 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7268 | return 0; |
| 7269 | } |
| 7270 | |
| 7271 | /* |
| 7272 | * Batched submission is done, ensure local IO is flushed out. |
| 7273 | */ |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7274 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7275 | { |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7276 | struct io_submit_state *state = &ctx->submit_state; |
| 7277 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7278 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7279 | io_queue_sqe(state->link.head); |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7280 | /* flush only after queuing links as they can generate completions */ |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 7281 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7282 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7283 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7284 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7285 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7286 | /* |
| 7287 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7288 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7289 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7290 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7291 | { |
| 7292 | state->plug_started = false; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7293 | state->need_plug = max_ios > 2; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7294 | state->submit_nr = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7295 | /* set only head, no need to init link_last in advance */ |
| 7296 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7297 | } |
| 7298 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7299 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7300 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7301 | struct io_rings *rings = ctx->rings; |
| 7302 | |
| 7303 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7304 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7305 | * since once we write the new head, the application could |
| 7306 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7307 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7308 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7309 | } |
| 7310 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7311 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7312 | * 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] | 7313 | * that is mapped by userspace. This means that care needs to be taken to |
| 7314 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7315 | * being a good citizen. If members of the sqe are validated and then later |
| 7316 | * 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] | 7317 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7318 | */ |
| 7319 | 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] | 7320 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7321 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7322 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7323 | |
| 7324 | /* |
| 7325 | * The cached sq head (or cq tail) serves two purposes: |
| 7326 | * |
| 7327 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7328 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7329 | * 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] | 7330 | * though the application is the one updating it. |
| 7331 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7332 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7333 | if (likely(head < ctx->sq_entries)) |
| 7334 | return &ctx->sq_sqes[head]; |
| 7335 | |
| 7336 | /* drop invalid entries */ |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7337 | ctx->cq_extra--; |
| 7338 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7339 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7340 | return NULL; |
| 7341 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7342 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7343 | 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] | 7344 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7345 | { |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7346 | unsigned int entries = io_sqring_entries(ctx); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7347 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7348 | |
Pavel Begunkov | 51d48da | 2021-10-04 20:02:47 +0100 | [diff] [blame] | 7349 | if (unlikely(!entries)) |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7350 | return 0; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7351 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7352 | nr = min3(nr, ctx->sq_entries, entries); |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7353 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7354 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7355 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7356 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7357 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7358 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7359 | |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7360 | if (unlikely(!io_alloc_req_refill(ctx))) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7361 | if (!submitted) |
| 7362 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7363 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7364 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7365 | req = io_alloc_req(ctx); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7366 | sqe = io_get_sqe(ctx); |
| 7367 | if (unlikely(!sqe)) { |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 7368 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7369 | break; |
| 7370 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7371 | /* will complete beyond this point, count as submitted */ |
| 7372 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7373 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7374 | break; |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7375 | } while (submitted < nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7376 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7377 | if (unlikely(submitted != nr)) { |
| 7378 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7379 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7380 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7381 | current->io_uring->cached_refs += unused; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7382 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7383 | |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7384 | io_submit_state_end(ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7385 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7386 | io_commit_sqring(ctx); |
| 7387 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7388 | return submitted; |
| 7389 | } |
| 7390 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7391 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7392 | { |
| 7393 | return READ_ONCE(sqd->state); |
| 7394 | } |
| 7395 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7396 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7397 | { |
| 7398 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7399 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7400 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7401 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7402 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7406 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7407 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7408 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7409 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7410 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7411 | } |
| 7412 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7413 | 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] | 7414 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7415 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7416 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7417 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7418 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7419 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7420 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7421 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7422 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7423 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7424 | const struct cred *creds = NULL; |
| 7425 | |
| 7426 | if (ctx->sq_creds != current_cred()) |
| 7427 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7428 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7429 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7430 | if (!wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 7431 | io_do_iopoll(ctx, true); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7432 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7433 | /* |
| 7434 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7435 | * but also it is relied upon by io_ring_exit_work() |
| 7436 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7437 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7438 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7439 | ret = io_submit_sqes(ctx, to_submit); |
| 7440 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7441 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7442 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7443 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7444 | if (creds) |
| 7445 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7446 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7447 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7448 | return ret; |
| 7449 | } |
| 7450 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7451 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7452 | { |
| 7453 | struct io_ring_ctx *ctx; |
| 7454 | unsigned sq_thread_idle = 0; |
| 7455 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7456 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7457 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7458 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7459 | } |
| 7460 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7461 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7462 | { |
| 7463 | bool did_sig = false; |
| 7464 | struct ksignal ksig; |
| 7465 | |
| 7466 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7467 | signal_pending(current)) { |
| 7468 | mutex_unlock(&sqd->lock); |
| 7469 | if (signal_pending(current)) |
| 7470 | did_sig = get_signal(&ksig); |
| 7471 | cond_resched(); |
| 7472 | mutex_lock(&sqd->lock); |
| 7473 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7474 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7475 | } |
| 7476 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7477 | static int io_sq_thread(void *data) |
| 7478 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7479 | struct io_sq_data *sqd = data; |
| 7480 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7481 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7482 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7483 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7484 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7485 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7486 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7487 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7488 | if (sqd->sq_cpu != -1) |
| 7489 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7490 | else |
| 7491 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7492 | current->flags |= PF_NO_SETAFFINITY; |
| 7493 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7494 | audit_alloc_kernel(current); |
| 7495 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7496 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7497 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7498 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7499 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7500 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7501 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7502 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7503 | timeout = jiffies + sqd->sq_thread_idle; |
| 7504 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7505 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7506 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7507 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7508 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7509 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7510 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7511 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7512 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7513 | if (io_run_task_work()) |
| 7514 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7515 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7516 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7517 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7518 | if (sqt_spin) |
| 7519 | timeout = jiffies + sqd->sq_thread_idle; |
| 7520 | continue; |
| 7521 | } |
| 7522 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7523 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7524 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7525 | bool needs_sched = true; |
| 7526 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7527 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7528 | io_ring_set_wakeup_flag(ctx); |
| 7529 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7530 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7531 | !wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7532 | needs_sched = false; |
| 7533 | break; |
| 7534 | } |
| 7535 | if (io_sqring_entries(ctx)) { |
| 7536 | needs_sched = false; |
| 7537 | break; |
| 7538 | } |
| 7539 | } |
| 7540 | |
| 7541 | if (needs_sched) { |
| 7542 | mutex_unlock(&sqd->lock); |
| 7543 | schedule(); |
| 7544 | mutex_lock(&sqd->lock); |
| 7545 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7546 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7547 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7548 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7549 | |
| 7550 | finish_wait(&sqd->wait, &wait); |
| 7551 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7552 | } |
| 7553 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7554 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7555 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7556 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7557 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7558 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7559 | mutex_unlock(&sqd->lock); |
| 7560 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7561 | audit_free(current); |
| 7562 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7563 | complete(&sqd->exited); |
| 7564 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7565 | } |
| 7566 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7567 | struct io_wait_queue { |
| 7568 | struct wait_queue_entry wq; |
| 7569 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7570 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7571 | unsigned nr_timeouts; |
| 7572 | }; |
| 7573 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7574 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7575 | { |
| 7576 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7577 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7578 | |
| 7579 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7580 | * 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] | 7581 | * started waiting. For timeouts, we always want to return to userspace, |
| 7582 | * regardless of event count. |
| 7583 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7584 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7588 | int wake_flags, void *key) |
| 7589 | { |
| 7590 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7591 | wq); |
| 7592 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7593 | /* |
| 7594 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7595 | * the task, and the next invocation will do it. |
| 7596 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7597 | 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] | 7598 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7599 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7600 | } |
| 7601 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7602 | static int io_run_task_work_sig(void) |
| 7603 | { |
| 7604 | if (io_run_task_work()) |
| 7605 | return 1; |
| 7606 | if (!signal_pending(current)) |
| 7607 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7608 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7609 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7610 | return -EINTR; |
| 7611 | } |
| 7612 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7613 | /* when returns >0, the caller should retry */ |
| 7614 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7615 | struct io_wait_queue *iowq, |
| 7616 | signed long *timeout) |
| 7617 | { |
| 7618 | int ret; |
| 7619 | |
| 7620 | /* make sure we run task_work before checking for signals */ |
| 7621 | ret = io_run_task_work_sig(); |
| 7622 | if (ret || io_should_wake(iowq)) |
| 7623 | return ret; |
| 7624 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7625 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7626 | return 1; |
| 7627 | |
| 7628 | *timeout = schedule_timeout(*timeout); |
| 7629 | return !*timeout ? -ETIME : 1; |
| 7630 | } |
| 7631 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7632 | /* |
| 7633 | * Wait until events become available, if we don't already have some. The |
| 7634 | * application must reap them itself, as they reside on the shared cq ring. |
| 7635 | */ |
| 7636 | 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] | 7637 | const sigset_t __user *sig, size_t sigsz, |
| 7638 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7639 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7640 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7641 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7642 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7643 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7644 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7645 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7646 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7647 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7648 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7649 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7650 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7651 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7652 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7653 | if (uts) { |
| 7654 | struct timespec64 ts; |
| 7655 | |
| 7656 | if (get_timespec64(&ts, uts)) |
| 7657 | return -EFAULT; |
| 7658 | timeout = timespec64_to_jiffies(&ts); |
| 7659 | } |
| 7660 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7661 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7662 | #ifdef CONFIG_COMPAT |
| 7663 | if (in_compat_syscall()) |
| 7664 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7665 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7666 | else |
| 7667 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7668 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7669 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7670 | if (ret) |
| 7671 | return ret; |
| 7672 | } |
| 7673 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7674 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7675 | iowq.wq.private = current; |
| 7676 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7677 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7678 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7679 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7680 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7681 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7682 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7683 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7684 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7685 | ret = -EBUSY; |
| 7686 | break; |
| 7687 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7688 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7689 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7690 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7691 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7692 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7693 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7694 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7695 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7696 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7697 | 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] | 7698 | } |
| 7699 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7700 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7701 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7702 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7703 | |
| 7704 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7705 | kfree(table[i]); |
| 7706 | kfree(table); |
| 7707 | } |
| 7708 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7709 | static __cold void **io_alloc_page_table(size_t size) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7710 | { |
| 7711 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7712 | size_t init_size = size; |
| 7713 | void **table; |
| 7714 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7715 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7716 | if (!table) |
| 7717 | return NULL; |
| 7718 | |
| 7719 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7720 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7721 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7722 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7723 | if (!table[i]) { |
| 7724 | io_free_page_table(table, init_size); |
| 7725 | return NULL; |
| 7726 | } |
| 7727 | size -= this_size; |
| 7728 | } |
| 7729 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7730 | } |
| 7731 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7732 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7733 | { |
| 7734 | percpu_ref_exit(&ref_node->refs); |
| 7735 | kfree(ref_node); |
| 7736 | } |
| 7737 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7738 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7739 | { |
| 7740 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7741 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7742 | unsigned long flags; |
| 7743 | bool first_add = false; |
| 7744 | |
| 7745 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7746 | node->done = true; |
| 7747 | |
| 7748 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7749 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7750 | struct io_rsrc_node, node); |
| 7751 | /* recycle ref nodes in order */ |
| 7752 | if (!node->done) |
| 7753 | break; |
| 7754 | list_del(&node->node); |
| 7755 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7756 | } |
| 7757 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7758 | |
| 7759 | if (first_add) |
| 7760 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
| 7761 | } |
| 7762 | |
| 7763 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
| 7764 | { |
| 7765 | struct io_rsrc_node *ref_node; |
| 7766 | |
| 7767 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7768 | if (!ref_node) |
| 7769 | return NULL; |
| 7770 | |
| 7771 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 7772 | 0, GFP_KERNEL)) { |
| 7773 | kfree(ref_node); |
| 7774 | return NULL; |
| 7775 | } |
| 7776 | INIT_LIST_HEAD(&ref_node->node); |
| 7777 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 7778 | ref_node->done = false; |
| 7779 | return ref_node; |
| 7780 | } |
| 7781 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7782 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7783 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7784 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7785 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7786 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7787 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7788 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7789 | io_rsrc_refs_drop(ctx); |
| 7790 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7791 | if (data_to_kill) { |
| 7792 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7793 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7794 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7795 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7796 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7797 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7798 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7799 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7800 | percpu_ref_kill(&rsrc_node->refs); |
| 7801 | ctx->rsrc_node = NULL; |
| 7802 | } |
| 7803 | |
| 7804 | if (!ctx->rsrc_node) { |
| 7805 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7806 | ctx->rsrc_backup_node = NULL; |
| 7807 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7810 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7811 | { |
| 7812 | if (ctx->rsrc_backup_node) |
| 7813 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7814 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7815 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7816 | } |
| 7817 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7818 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
| 7819 | struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7820 | { |
| 7821 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7822 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7823 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7824 | if (data->quiesce) |
| 7825 | return -ENXIO; |
| 7826 | |
| 7827 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7828 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7829 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7830 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7831 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7832 | io_rsrc_node_switch(ctx, data); |
| 7833 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7834 | /* kill initial ref, already quiesced if zero */ |
| 7835 | if (atomic_dec_and_test(&data->refs)) |
| 7836 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7837 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7838 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7839 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7840 | if (!ret) { |
| 7841 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7842 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7843 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7844 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7845 | atomic_inc(&data->refs); |
| 7846 | /* wait for all works potentially completing data->done */ |
| 7847 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7848 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7849 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7850 | ret = io_run_task_work_sig(); |
| 7851 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7852 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7853 | data->quiesce = false; |
| 7854 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7855 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7856 | } |
| 7857 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7858 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 7859 | { |
| 7860 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 7861 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 7862 | |
| 7863 | return &data->tags[table_idx][off]; |
| 7864 | } |
| 7865 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7866 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7867 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7868 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 7869 | |
| 7870 | if (data->tags) |
| 7871 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7872 | kfree(data); |
| 7873 | } |
| 7874 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7875 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 7876 | u64 __user *utags, unsigned nr, |
| 7877 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7878 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7879 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7880 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7881 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7882 | |
| 7883 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7884 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7885 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7886 | 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] | 7887 | if (!data->tags) { |
| 7888 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7889 | return -ENOMEM; |
| 7890 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7891 | |
| 7892 | data->nr = nr; |
| 7893 | data->ctx = ctx; |
| 7894 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7895 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7896 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7897 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 7898 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 7899 | |
| 7900 | if (copy_from_user(tag_slot, &utags[i], |
| 7901 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7902 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7903 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7904 | } |
| 7905 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7906 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7907 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7908 | *pdata = data; |
| 7909 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7910 | fail: |
| 7911 | io_rsrc_data_free(data); |
| 7912 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7913 | } |
| 7914 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7915 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 7916 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7917 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 7918 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7919 | return !!table->files; |
| 7920 | } |
| 7921 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7922 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7923 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7924 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7925 | table->files = NULL; |
| 7926 | } |
| 7927 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7928 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7929 | { |
| 7930 | #if defined(CONFIG_UNIX) |
| 7931 | if (ctx->ring_sock) { |
| 7932 | struct sock *sock = ctx->ring_sock->sk; |
| 7933 | struct sk_buff *skb; |
| 7934 | |
| 7935 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7936 | kfree_skb(skb); |
| 7937 | } |
| 7938 | #else |
| 7939 | int i; |
| 7940 | |
| 7941 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7942 | struct file *file; |
| 7943 | |
| 7944 | file = io_file_from_index(ctx, i); |
| 7945 | if (file) |
| 7946 | fput(file); |
| 7947 | } |
| 7948 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7949 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7950 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7951 | ctx->file_data = NULL; |
| 7952 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7953 | } |
| 7954 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7955 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7956 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7957 | int ret; |
| 7958 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7959 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7960 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7961 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7962 | if (!ret) |
| 7963 | __io_sqe_files_unregister(ctx); |
| 7964 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7965 | } |
| 7966 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7967 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7968 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7969 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7970 | WARN_ON_ONCE(sqd->thread == current); |
| 7971 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7972 | /* |
| 7973 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7974 | * other threads incrementing park_pending and setting the bit. |
| 7975 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7976 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7977 | if (atomic_dec_return(&sqd->park_pending)) |
| 7978 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7979 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7980 | } |
| 7981 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7982 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7983 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7984 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7985 | WARN_ON_ONCE(sqd->thread == current); |
| 7986 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7987 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7988 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7989 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7990 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7991 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7995 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7996 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7997 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7998 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7999 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 8000 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8001 | if (sqd->thread) |
| 8002 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8003 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8004 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8005 | } |
| 8006 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8007 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8008 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8009 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8010 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 8011 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8012 | io_sq_thread_stop(sqd); |
| 8013 | kfree(sqd); |
| 8014 | } |
| 8015 | } |
| 8016 | |
| 8017 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 8018 | { |
| 8019 | struct io_sq_data *sqd = ctx->sq_data; |
| 8020 | |
| 8021 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8022 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8023 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8024 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8025 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8026 | |
| 8027 | io_put_sq_data(sqd); |
| 8028 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8029 | } |
| 8030 | } |
| 8031 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8032 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 8033 | { |
| 8034 | struct io_ring_ctx *ctx_attach; |
| 8035 | struct io_sq_data *sqd; |
| 8036 | struct fd f; |
| 8037 | |
| 8038 | f = fdget(p->wq_fd); |
| 8039 | if (!f.file) |
| 8040 | return ERR_PTR(-ENXIO); |
| 8041 | if (f.file->f_op != &io_uring_fops) { |
| 8042 | fdput(f); |
| 8043 | return ERR_PTR(-EINVAL); |
| 8044 | } |
| 8045 | |
| 8046 | ctx_attach = f.file->private_data; |
| 8047 | sqd = ctx_attach->sq_data; |
| 8048 | if (!sqd) { |
| 8049 | fdput(f); |
| 8050 | return ERR_PTR(-EINVAL); |
| 8051 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8052 | if (sqd->task_tgid != current->tgid) { |
| 8053 | fdput(f); |
| 8054 | return ERR_PTR(-EPERM); |
| 8055 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8056 | |
| 8057 | refcount_inc(&sqd->refs); |
| 8058 | fdput(f); |
| 8059 | return sqd; |
| 8060 | } |
| 8061 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8062 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 8063 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8064 | { |
| 8065 | struct io_sq_data *sqd; |
| 8066 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8067 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8068 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 8069 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8070 | if (!IS_ERR(sqd)) { |
| 8071 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8072 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8073 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8074 | /* fall through for EPERM case, setup new sqd/task */ |
| 8075 | if (PTR_ERR(sqd) != -EPERM) |
| 8076 | return sqd; |
| 8077 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8078 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8079 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 8080 | if (!sqd) |
| 8081 | return ERR_PTR(-ENOMEM); |
| 8082 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8083 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8084 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8085 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8086 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8087 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8088 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8089 | return sqd; |
| 8090 | } |
| 8091 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8092 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8093 | /* |
| 8094 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 8095 | * the io_uring can be safely unregistered on process exit, even if we have |
| 8096 | * loops in the file referencing. |
| 8097 | */ |
| 8098 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 8099 | { |
| 8100 | struct sock *sk = ctx->ring_sock->sk; |
| 8101 | struct scm_fp_list *fpl; |
| 8102 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8103 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8104 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8105 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 8106 | if (!fpl) |
| 8107 | return -ENOMEM; |
| 8108 | |
| 8109 | skb = alloc_skb(0, GFP_KERNEL); |
| 8110 | if (!skb) { |
| 8111 | kfree(fpl); |
| 8112 | return -ENOMEM; |
| 8113 | } |
| 8114 | |
| 8115 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8116 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8117 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8118 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8119 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8120 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8121 | |
| 8122 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8123 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8124 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8125 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 8126 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8127 | } |
| 8128 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8129 | if (nr_files) { |
| 8130 | fpl->max = SCM_MAX_FD; |
| 8131 | fpl->count = nr_files; |
| 8132 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8133 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8134 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 8135 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8136 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8137 | for (i = 0; i < nr_files; i++) |
| 8138 | fput(fpl->fp[i]); |
| 8139 | } else { |
| 8140 | kfree_skb(skb); |
| 8141 | kfree(fpl); |
| 8142 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8143 | |
| 8144 | return 0; |
| 8145 | } |
| 8146 | |
| 8147 | /* |
| 8148 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8149 | * causes regular reference counting to break down. We rely on the UNIX |
| 8150 | * garbage collection to take care of this problem for us. |
| 8151 | */ |
| 8152 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8153 | { |
| 8154 | unsigned left, total; |
| 8155 | int ret = 0; |
| 8156 | |
| 8157 | total = 0; |
| 8158 | left = ctx->nr_user_files; |
| 8159 | while (left) { |
| 8160 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8161 | |
| 8162 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8163 | if (ret) |
| 8164 | break; |
| 8165 | left -= this_files; |
| 8166 | total += this_files; |
| 8167 | } |
| 8168 | |
| 8169 | if (!ret) |
| 8170 | return 0; |
| 8171 | |
| 8172 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8173 | struct file *file = io_file_from_index(ctx, total); |
| 8174 | |
| 8175 | if (file) |
| 8176 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8177 | total++; |
| 8178 | } |
| 8179 | |
| 8180 | return ret; |
| 8181 | } |
| 8182 | #else |
| 8183 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8184 | { |
| 8185 | return 0; |
| 8186 | } |
| 8187 | #endif |
| 8188 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8189 | 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] | 8190 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8191 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8192 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8193 | struct sock *sock = ctx->ring_sock->sk; |
| 8194 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8195 | struct sk_buff *skb; |
| 8196 | int i; |
| 8197 | |
| 8198 | __skb_queue_head_init(&list); |
| 8199 | |
| 8200 | /* |
| 8201 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8202 | * remove this entry and rearrange the file array. |
| 8203 | */ |
| 8204 | skb = skb_dequeue(head); |
| 8205 | while (skb) { |
| 8206 | struct scm_fp_list *fp; |
| 8207 | |
| 8208 | fp = UNIXCB(skb).fp; |
| 8209 | for (i = 0; i < fp->count; i++) { |
| 8210 | int left; |
| 8211 | |
| 8212 | if (fp->fp[i] != file) |
| 8213 | continue; |
| 8214 | |
| 8215 | unix_notinflight(fp->user, fp->fp[i]); |
| 8216 | left = fp->count - 1 - i; |
| 8217 | if (left) { |
| 8218 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8219 | left * sizeof(struct file *)); |
| 8220 | } |
| 8221 | fp->count--; |
| 8222 | if (!fp->count) { |
| 8223 | kfree_skb(skb); |
| 8224 | skb = NULL; |
| 8225 | } else { |
| 8226 | __skb_queue_tail(&list, skb); |
| 8227 | } |
| 8228 | fput(file); |
| 8229 | file = NULL; |
| 8230 | break; |
| 8231 | } |
| 8232 | |
| 8233 | if (!file) |
| 8234 | break; |
| 8235 | |
| 8236 | __skb_queue_tail(&list, skb); |
| 8237 | |
| 8238 | skb = skb_dequeue(head); |
| 8239 | } |
| 8240 | |
| 8241 | if (skb_peek(&list)) { |
| 8242 | spin_lock_irq(&head->lock); |
| 8243 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8244 | __skb_queue_tail(head, skb); |
| 8245 | spin_unlock_irq(&head->lock); |
| 8246 | } |
| 8247 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8248 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8249 | #endif |
| 8250 | } |
| 8251 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8252 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8253 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8254 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8255 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8256 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8257 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8258 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8259 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8260 | |
| 8261 | if (prsrc->tag) { |
| 8262 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8263 | |
| 8264 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8265 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 8266 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8267 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8268 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8269 | io_cqring_ev_posted(ctx); |
| 8270 | io_ring_submit_unlock(ctx, lock_ring); |
| 8271 | } |
| 8272 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8273 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8274 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8275 | } |
| 8276 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8277 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8278 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8279 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8280 | } |
| 8281 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8282 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8283 | { |
| 8284 | struct io_ring_ctx *ctx; |
| 8285 | struct llist_node *node; |
| 8286 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8287 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8288 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8289 | |
| 8290 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8291 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8292 | struct llist_node *next = node->next; |
| 8293 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8294 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8295 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8296 | node = next; |
| 8297 | } |
| 8298 | } |
| 8299 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8300 | 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] | 8301 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8302 | { |
| 8303 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8304 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8305 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8306 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8307 | |
| 8308 | if (ctx->file_data) |
| 8309 | return -EBUSY; |
| 8310 | if (!nr_args) |
| 8311 | return -EINVAL; |
| 8312 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8313 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8314 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8315 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8316 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8317 | if (ret) |
| 8318 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8319 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8320 | &ctx->file_data); |
| 8321 | if (ret) |
| 8322 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8323 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8324 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8325 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8326 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8327 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8328 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8329 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8330 | ret = -EFAULT; |
| 8331 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8332 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8333 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8334 | if (fd == -1) { |
| 8335 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8336 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8337 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8338 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8339 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8340 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8341 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8342 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8343 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8344 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8345 | |
| 8346 | /* |
| 8347 | * Don't allow io_uring instances to be registered. If UNIX |
| 8348 | * isn't enabled, then this causes a reference cycle and this |
| 8349 | * instance can never get freed. If UNIX is enabled we'll |
| 8350 | * handle it just fine, but there's still no point in allowing |
| 8351 | * a ring fd as it doesn't support regular read/write anyway. |
| 8352 | */ |
| 8353 | if (file->f_op == &io_uring_fops) { |
| 8354 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8355 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8356 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8357 | 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] | 8358 | } |
| 8359 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8360 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8361 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8362 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8363 | return ret; |
| 8364 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8365 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8366 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8367 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8368 | out_fput: |
| 8369 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8370 | file = io_file_from_index(ctx, i); |
| 8371 | if (file) |
| 8372 | fput(file); |
| 8373 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8374 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8375 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8376 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8377 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8378 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8379 | return ret; |
| 8380 | } |
| 8381 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8382 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8383 | int index) |
| 8384 | { |
| 8385 | #if defined(CONFIG_UNIX) |
| 8386 | struct sock *sock = ctx->ring_sock->sk; |
| 8387 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8388 | struct sk_buff *skb; |
| 8389 | |
| 8390 | /* |
| 8391 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8392 | * file set. If there's no room, fall back to allocating a new skb |
| 8393 | * and filling it in. |
| 8394 | */ |
| 8395 | spin_lock_irq(&head->lock); |
| 8396 | skb = skb_peek(head); |
| 8397 | if (skb) { |
| 8398 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8399 | |
| 8400 | if (fpl->count < SCM_MAX_FD) { |
| 8401 | __skb_unlink(skb, head); |
| 8402 | spin_unlock_irq(&head->lock); |
| 8403 | fpl->fp[fpl->count] = get_file(file); |
| 8404 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8405 | fpl->count++; |
| 8406 | spin_lock_irq(&head->lock); |
| 8407 | __skb_queue_head(head, skb); |
| 8408 | } else { |
| 8409 | skb = NULL; |
| 8410 | } |
| 8411 | } |
| 8412 | spin_unlock_irq(&head->lock); |
| 8413 | |
| 8414 | if (skb) { |
| 8415 | fput(file); |
| 8416 | return 0; |
| 8417 | } |
| 8418 | |
| 8419 | return __io_sqe_files_scm(ctx, 1, index); |
| 8420 | #else |
| 8421 | return 0; |
| 8422 | #endif |
| 8423 | } |
| 8424 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8425 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8426 | struct io_rsrc_node *node, void *rsrc) |
| 8427 | { |
| 8428 | struct io_rsrc_put *prsrc; |
| 8429 | |
| 8430 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8431 | if (!prsrc) |
| 8432 | return -ENOMEM; |
| 8433 | |
| 8434 | prsrc->tag = *io_get_tag_slot(data, idx); |
| 8435 | prsrc->rsrc = rsrc; |
| 8436 | list_add(&prsrc->list, &node->rsrc_list); |
| 8437 | return 0; |
| 8438 | } |
| 8439 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8440 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8441 | unsigned int issue_flags, u32 slot_index) |
| 8442 | { |
| 8443 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8444 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8445 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8446 | struct io_fixed_file *file_slot; |
| 8447 | int ret = -EBADF; |
| 8448 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8449 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8450 | if (file->f_op == &io_uring_fops) |
| 8451 | goto err; |
| 8452 | ret = -ENXIO; |
| 8453 | if (!ctx->file_data) |
| 8454 | goto err; |
| 8455 | ret = -EINVAL; |
| 8456 | if (slot_index >= ctx->nr_user_files) |
| 8457 | goto err; |
| 8458 | |
| 8459 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8460 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8461 | |
| 8462 | if (file_slot->file_ptr) { |
| 8463 | struct file *old_file; |
| 8464 | |
| 8465 | ret = io_rsrc_node_switch_start(ctx); |
| 8466 | if (ret) |
| 8467 | goto err; |
| 8468 | |
| 8469 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8470 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8471 | ctx->rsrc_node, old_file); |
| 8472 | if (ret) |
| 8473 | goto err; |
| 8474 | file_slot->file_ptr = 0; |
| 8475 | needs_switch = true; |
| 8476 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8477 | |
| 8478 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8479 | io_fixed_file_set(file_slot, file); |
| 8480 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8481 | if (ret) { |
| 8482 | file_slot->file_ptr = 0; |
| 8483 | goto err; |
| 8484 | } |
| 8485 | |
| 8486 | ret = 0; |
| 8487 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8488 | if (needs_switch) |
| 8489 | io_rsrc_node_switch(ctx, ctx->file_data); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8490 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8491 | if (ret) |
| 8492 | fput(file); |
| 8493 | return ret; |
| 8494 | } |
| 8495 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8496 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8497 | { |
| 8498 | unsigned int offset = req->close.file_slot - 1; |
| 8499 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8500 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8501 | struct io_fixed_file *file_slot; |
| 8502 | struct file *file; |
| 8503 | int ret, i; |
| 8504 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8505 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8506 | ret = -ENXIO; |
| 8507 | if (unlikely(!ctx->file_data)) |
| 8508 | goto out; |
| 8509 | ret = -EINVAL; |
| 8510 | if (offset >= ctx->nr_user_files) |
| 8511 | goto out; |
| 8512 | ret = io_rsrc_node_switch_start(ctx); |
| 8513 | if (ret) |
| 8514 | goto out; |
| 8515 | |
| 8516 | i = array_index_nospec(offset, ctx->nr_user_files); |
| 8517 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
| 8518 | ret = -EBADF; |
| 8519 | if (!file_slot->file_ptr) |
| 8520 | goto out; |
| 8521 | |
| 8522 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8523 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8524 | if (ret) |
| 8525 | goto out; |
| 8526 | |
| 8527 | file_slot->file_ptr = 0; |
| 8528 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8529 | ret = 0; |
| 8530 | out: |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8531 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8532 | return ret; |
| 8533 | } |
| 8534 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8535 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8536 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8537 | unsigned nr_args) |
| 8538 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8539 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8540 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8541 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8542 | struct io_fixed_file *file_slot; |
| 8543 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8544 | int fd, i, err = 0; |
| 8545 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8546 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8547 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8548 | if (!ctx->file_data) |
| 8549 | return -ENXIO; |
| 8550 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8551 | return -EINVAL; |
| 8552 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8553 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8554 | u64 tag = 0; |
| 8555 | |
| 8556 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8557 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8558 | err = -EFAULT; |
| 8559 | break; |
| 8560 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8561 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8562 | err = -EINVAL; |
| 8563 | break; |
| 8564 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8565 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8566 | continue; |
| 8567 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8568 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8569 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8570 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8571 | if (file_slot->file_ptr) { |
| 8572 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8573 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 8574 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8575 | if (err) |
| 8576 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8577 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8578 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8579 | } |
| 8580 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8581 | file = fget(fd); |
| 8582 | if (!file) { |
| 8583 | err = -EBADF; |
| 8584 | break; |
| 8585 | } |
| 8586 | /* |
| 8587 | * Don't allow io_uring instances to be registered. If |
| 8588 | * UNIX isn't enabled, then this causes a reference |
| 8589 | * cycle and this instance can never get freed. If UNIX |
| 8590 | * is enabled we'll handle it just fine, but there's |
| 8591 | * still no point in allowing a ring fd as it doesn't |
| 8592 | * support regular read/write anyway. |
| 8593 | */ |
| 8594 | if (file->f_op == &io_uring_fops) { |
| 8595 | fput(file); |
| 8596 | err = -EBADF; |
| 8597 | break; |
| 8598 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8599 | *io_get_tag_slot(data, up->offset + done) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8600 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8601 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8602 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8603 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8604 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8605 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8606 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8607 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8608 | } |
| 8609 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8610 | if (needs_switch) |
| 8611 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8612 | return done ? done : err; |
| 8613 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8614 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8615 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8616 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8617 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8618 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8619 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8620 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8621 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8622 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8623 | hash = ctx->hash_map; |
| 8624 | if (!hash) { |
| 8625 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8626 | if (!hash) { |
| 8627 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8628 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8629 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8630 | refcount_set(&hash->refs, 1); |
| 8631 | init_waitqueue_head(&hash->wait); |
| 8632 | ctx->hash_map = hash; |
| 8633 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8634 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8635 | |
| 8636 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8637 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8638 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8639 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8640 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8641 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8642 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8643 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8644 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8645 | } |
| 8646 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8647 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
| 8648 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8649 | { |
| 8650 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8651 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8652 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8653 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8654 | if (unlikely(!tctx)) |
| 8655 | return -ENOMEM; |
| 8656 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8657 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8658 | if (unlikely(ret)) { |
| 8659 | kfree(tctx); |
| 8660 | return ret; |
| 8661 | } |
| 8662 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8663 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8664 | if (IS_ERR(tctx->io_wq)) { |
| 8665 | ret = PTR_ERR(tctx->io_wq); |
| 8666 | percpu_counter_destroy(&tctx->inflight); |
| 8667 | kfree(tctx); |
| 8668 | return ret; |
| 8669 | } |
| 8670 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8671 | xa_init(&tctx->xa); |
| 8672 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8673 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8674 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8675 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8676 | spin_lock_init(&tctx->task_lock); |
| 8677 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8678 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8679 | return 0; |
| 8680 | } |
| 8681 | |
| 8682 | void __io_uring_free(struct task_struct *tsk) |
| 8683 | { |
| 8684 | struct io_uring_task *tctx = tsk->io_uring; |
| 8685 | |
| 8686 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8687 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8688 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8689 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8690 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8691 | kfree(tctx); |
| 8692 | tsk->io_uring = NULL; |
| 8693 | } |
| 8694 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8695 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8696 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8697 | { |
| 8698 | int ret; |
| 8699 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8700 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8701 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8702 | IORING_SETUP_ATTACH_WQ) { |
| 8703 | struct fd f; |
| 8704 | |
| 8705 | f = fdget(p->wq_fd); |
| 8706 | if (!f.file) |
| 8707 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8708 | if (f.file->f_op != &io_uring_fops) { |
| 8709 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8710 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8711 | } |
| 8712 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8713 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8714 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8715 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8716 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8717 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8718 | |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 8719 | ret = security_uring_sqpoll(); |
| 8720 | if (ret) |
| 8721 | return ret; |
| 8722 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8723 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8724 | if (IS_ERR(sqd)) { |
| 8725 | ret = PTR_ERR(sqd); |
| 8726 | goto err; |
| 8727 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8728 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8729 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8730 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8731 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8732 | if (!ctx->sq_thread_idle) |
| 8733 | ctx->sq_thread_idle = HZ; |
| 8734 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8735 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8736 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8737 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8738 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8739 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8740 | io_sq_thread_unpark(sqd); |
| 8741 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8742 | if (ret < 0) |
| 8743 | goto err; |
| 8744 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8745 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8746 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8747 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8748 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8749 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8750 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8751 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8752 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8753 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8754 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8755 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8756 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8757 | |
| 8758 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8759 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8760 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 8761 | if (IS_ERR(tsk)) { |
| 8762 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8763 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8764 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8765 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8766 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8767 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8768 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8769 | if (ret) |
| 8770 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8771 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8772 | /* Can't have SQ_AFF without SQPOLL */ |
| 8773 | ret = -EINVAL; |
| 8774 | goto err; |
| 8775 | } |
| 8776 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8777 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8778 | err_sqpoll: |
| 8779 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8780 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8781 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8782 | return ret; |
| 8783 | } |
| 8784 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8785 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8786 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8787 | { |
| 8788 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8789 | } |
| 8790 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8791 | static inline int __io_account_mem(struct user_struct *user, |
| 8792 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8793 | { |
| 8794 | unsigned long page_limit, cur_pages, new_pages; |
| 8795 | |
| 8796 | /* Don't allow more pages than we can safely lock */ |
| 8797 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8798 | |
| 8799 | do { |
| 8800 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8801 | new_pages = cur_pages + nr_pages; |
| 8802 | if (new_pages > page_limit) |
| 8803 | return -ENOMEM; |
| 8804 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8805 | new_pages) != cur_pages); |
| 8806 | |
| 8807 | return 0; |
| 8808 | } |
| 8809 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8810 | 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] | 8811 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8812 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8813 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8814 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8815 | if (ctx->mm_account) |
| 8816 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8817 | } |
| 8818 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8819 | 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] | 8820 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8821 | int ret; |
| 8822 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8823 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8824 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8825 | if (ret) |
| 8826 | return ret; |
| 8827 | } |
| 8828 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8829 | if (ctx->mm_account) |
| 8830 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8831 | |
| 8832 | return 0; |
| 8833 | } |
| 8834 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8835 | static void io_mem_free(void *ptr) |
| 8836 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8837 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8838 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8839 | if (!ptr) |
| 8840 | return; |
| 8841 | |
| 8842 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8843 | if (put_page_testzero(page)) |
| 8844 | free_compound_page(page); |
| 8845 | } |
| 8846 | |
| 8847 | static void *io_mem_alloc(size_t size) |
| 8848 | { |
| 8849 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8850 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8851 | |
| 8852 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8853 | } |
| 8854 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8855 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8856 | size_t *sq_offset) |
| 8857 | { |
| 8858 | struct io_rings *rings; |
| 8859 | size_t off, sq_array_size; |
| 8860 | |
| 8861 | off = struct_size(rings, cqes, cq_entries); |
| 8862 | if (off == SIZE_MAX) |
| 8863 | return SIZE_MAX; |
| 8864 | |
| 8865 | #ifdef CONFIG_SMP |
| 8866 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8867 | if (off == 0) |
| 8868 | return SIZE_MAX; |
| 8869 | #endif |
| 8870 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8871 | if (sq_offset) |
| 8872 | *sq_offset = off; |
| 8873 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8874 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8875 | if (sq_array_size == SIZE_MAX) |
| 8876 | return SIZE_MAX; |
| 8877 | |
| 8878 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8879 | return SIZE_MAX; |
| 8880 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8881 | return off; |
| 8882 | } |
| 8883 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8884 | 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] | 8885 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8886 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8887 | unsigned int i; |
| 8888 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8889 | if (imu != ctx->dummy_ubuf) { |
| 8890 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8891 | unpin_user_page(imu->bvec[i].bv_page); |
| 8892 | if (imu->acct_pages) |
| 8893 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8894 | kvfree(imu); |
| 8895 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8896 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8897 | } |
| 8898 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8899 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 8900 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 8901 | io_buffer_unmap(ctx, &prsrc->buf); |
| 8902 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8903 | } |
| 8904 | |
| 8905 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8906 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8907 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8908 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8909 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8910 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8911 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 8912 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8913 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8914 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8915 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8916 | } |
| 8917 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8918 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 8919 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8920 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8921 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8922 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8923 | return -ENXIO; |
| 8924 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8925 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
| 8926 | if (!ret) |
| 8927 | __io_sqe_buffers_unregister(ctx); |
| 8928 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8929 | } |
| 8930 | |
| 8931 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8932 | void __user *arg, unsigned index) |
| 8933 | { |
| 8934 | struct iovec __user *src; |
| 8935 | |
| 8936 | #ifdef CONFIG_COMPAT |
| 8937 | if (ctx->compat) { |
| 8938 | struct compat_iovec __user *ciovs; |
| 8939 | struct compat_iovec ciov; |
| 8940 | |
| 8941 | ciovs = (struct compat_iovec __user *) arg; |
| 8942 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8943 | return -EFAULT; |
| 8944 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8945 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8946 | dst->iov_len = ciov.iov_len; |
| 8947 | return 0; |
| 8948 | } |
| 8949 | #endif |
| 8950 | src = (struct iovec __user *) arg; |
| 8951 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8952 | return -EFAULT; |
| 8953 | return 0; |
| 8954 | } |
| 8955 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8956 | /* |
| 8957 | * Not super efficient, but this is just a registration time. And we do cache |
| 8958 | * the last compound head, so generally we'll only do a full search if we don't |
| 8959 | * match that one. |
| 8960 | * |
| 8961 | * We check if the given compound head page has already been accounted, to |
| 8962 | * avoid double accounting it. This allows us to account the full size of the |
| 8963 | * page, not just the constituent pages of a huge page. |
| 8964 | */ |
| 8965 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8966 | int nr_pages, struct page *hpage) |
| 8967 | { |
| 8968 | int i, j; |
| 8969 | |
| 8970 | /* check current page array */ |
| 8971 | for (i = 0; i < nr_pages; i++) { |
| 8972 | if (!PageCompound(pages[i])) |
| 8973 | continue; |
| 8974 | if (compound_head(pages[i]) == hpage) |
| 8975 | return true; |
| 8976 | } |
| 8977 | |
| 8978 | /* check previously registered pages */ |
| 8979 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8980 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8981 | |
| 8982 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8983 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8984 | continue; |
| 8985 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8986 | return true; |
| 8987 | } |
| 8988 | } |
| 8989 | |
| 8990 | return false; |
| 8991 | } |
| 8992 | |
| 8993 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8994 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8995 | struct page **last_hpage) |
| 8996 | { |
| 8997 | int i, ret; |
| 8998 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 8999 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9000 | for (i = 0; i < nr_pages; i++) { |
| 9001 | if (!PageCompound(pages[i])) { |
| 9002 | imu->acct_pages++; |
| 9003 | } else { |
| 9004 | struct page *hpage; |
| 9005 | |
| 9006 | hpage = compound_head(pages[i]); |
| 9007 | if (hpage == *last_hpage) |
| 9008 | continue; |
| 9009 | *last_hpage = hpage; |
| 9010 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 9011 | continue; |
| 9012 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 9013 | } |
| 9014 | } |
| 9015 | |
| 9016 | if (!imu->acct_pages) |
| 9017 | return 0; |
| 9018 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9019 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9020 | if (ret) |
| 9021 | imu->acct_pages = 0; |
| 9022 | return ret; |
| 9023 | } |
| 9024 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9025 | 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] | 9026 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9027 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9028 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9029 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9030 | struct vm_area_struct **vmas = NULL; |
| 9031 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9032 | unsigned long off, start, end, ubuf; |
| 9033 | size_t size; |
| 9034 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9035 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9036 | if (!iov->iov_base) { |
| 9037 | *pimu = ctx->dummy_ubuf; |
| 9038 | return 0; |
| 9039 | } |
| 9040 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9041 | ubuf = (unsigned long) iov->iov_base; |
| 9042 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 9043 | start = ubuf >> PAGE_SHIFT; |
| 9044 | nr_pages = end - start; |
| 9045 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9046 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9047 | ret = -ENOMEM; |
| 9048 | |
| 9049 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 9050 | if (!pages) |
| 9051 | goto done; |
| 9052 | |
| 9053 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 9054 | GFP_KERNEL); |
| 9055 | if (!vmas) |
| 9056 | goto done; |
| 9057 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9058 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 9059 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9060 | goto done; |
| 9061 | |
| 9062 | ret = 0; |
| 9063 | mmap_read_lock(current->mm); |
| 9064 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 9065 | pages, vmas); |
| 9066 | if (pret == nr_pages) { |
| 9067 | /* don't support file backed memory */ |
| 9068 | for (i = 0; i < nr_pages; i++) { |
| 9069 | struct vm_area_struct *vma = vmas[i]; |
| 9070 | |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 9071 | if (vma_is_shmem(vma)) |
| 9072 | continue; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9073 | if (vma->vm_file && |
| 9074 | !is_file_hugepages(vma->vm_file)) { |
| 9075 | ret = -EOPNOTSUPP; |
| 9076 | break; |
| 9077 | } |
| 9078 | } |
| 9079 | } else { |
| 9080 | ret = pret < 0 ? pret : -EFAULT; |
| 9081 | } |
| 9082 | mmap_read_unlock(current->mm); |
| 9083 | if (ret) { |
| 9084 | /* |
| 9085 | * if we did partial map, or found file backed vmas, |
| 9086 | * release any pages we did get |
| 9087 | */ |
| 9088 | if (pret > 0) |
| 9089 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9090 | goto done; |
| 9091 | } |
| 9092 | |
| 9093 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 9094 | if (ret) { |
| 9095 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9096 | goto done; |
| 9097 | } |
| 9098 | |
| 9099 | off = ubuf & ~PAGE_MASK; |
| 9100 | size = iov->iov_len; |
| 9101 | for (i = 0; i < nr_pages; i++) { |
| 9102 | size_t vec_len; |
| 9103 | |
| 9104 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 9105 | imu->bvec[i].bv_page = pages[i]; |
| 9106 | imu->bvec[i].bv_len = vec_len; |
| 9107 | imu->bvec[i].bv_offset = off; |
| 9108 | off = 0; |
| 9109 | size -= vec_len; |
| 9110 | } |
| 9111 | /* store original address for later verification */ |
| 9112 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9113 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9114 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9115 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9116 | ret = 0; |
| 9117 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9118 | if (ret) |
| 9119 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9120 | kvfree(pages); |
| 9121 | kvfree(vmas); |
| 9122 | return ret; |
| 9123 | } |
| 9124 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9125 | 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] | 9126 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9127 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 9128 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9129 | } |
| 9130 | |
| 9131 | static int io_buffer_validate(struct iovec *iov) |
| 9132 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9133 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 9134 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9135 | /* |
| 9136 | * Don't impose further limits on the size and buffer |
| 9137 | * constraints here, we'll -EINVAL later when IO is |
| 9138 | * submitted if they are wrong. |
| 9139 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9140 | if (!iov->iov_base) |
| 9141 | return iov->iov_len ? -EFAULT : 0; |
| 9142 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9143 | return -EFAULT; |
| 9144 | |
| 9145 | /* arbitrary limit, but we need something */ |
| 9146 | if (iov->iov_len > SZ_1G) |
| 9147 | return -EFAULT; |
| 9148 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9149 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9150 | return -EOVERFLOW; |
| 9151 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9152 | return 0; |
| 9153 | } |
| 9154 | |
| 9155 | 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] | 9156 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9157 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9158 | struct page *last_hpage = NULL; |
| 9159 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9160 | int i, ret; |
| 9161 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9162 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9163 | if (ctx->user_bufs) |
| 9164 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9165 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9166 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9167 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9168 | if (ret) |
| 9169 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9170 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9171 | if (ret) |
| 9172 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9173 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9174 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9175 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9176 | return ret; |
| 9177 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9178 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9179 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9180 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9181 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9182 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9183 | ret = io_buffer_validate(&iov); |
| 9184 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9185 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9186 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9187 | ret = -EINVAL; |
| 9188 | break; |
| 9189 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9190 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9191 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9192 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9193 | if (ret) |
| 9194 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9195 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9196 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9197 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9198 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9199 | ctx->buf_data = data; |
| 9200 | if (ret) |
| 9201 | __io_sqe_buffers_unregister(ctx); |
| 9202 | else |
| 9203 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9204 | return ret; |
| 9205 | } |
| 9206 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9207 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9208 | struct io_uring_rsrc_update2 *up, |
| 9209 | unsigned int nr_args) |
| 9210 | { |
| 9211 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9212 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9213 | struct page *last_hpage = NULL; |
| 9214 | bool needs_switch = false; |
| 9215 | __u32 done; |
| 9216 | int i, err; |
| 9217 | |
| 9218 | if (!ctx->buf_data) |
| 9219 | return -ENXIO; |
| 9220 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9221 | return -EINVAL; |
| 9222 | |
| 9223 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9224 | struct io_mapped_ubuf *imu; |
| 9225 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9226 | u64 tag = 0; |
| 9227 | |
| 9228 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9229 | if (err) |
| 9230 | break; |
| 9231 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9232 | err = -EFAULT; |
| 9233 | break; |
| 9234 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9235 | err = io_buffer_validate(&iov); |
| 9236 | if (err) |
| 9237 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9238 | if (!iov.iov_base && tag) { |
| 9239 | err = -EINVAL; |
| 9240 | break; |
| 9241 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9242 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9243 | if (err) |
| 9244 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9245 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9246 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9247 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9248 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
| 9249 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9250 | if (unlikely(err)) { |
| 9251 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9252 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9253 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9254 | ctx->user_bufs[i] = NULL; |
| 9255 | needs_switch = true; |
| 9256 | } |
| 9257 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9258 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9259 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9260 | } |
| 9261 | |
| 9262 | if (needs_switch) |
| 9263 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9264 | return done ? done : err; |
| 9265 | } |
| 9266 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9267 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9268 | { |
| 9269 | __s32 __user *fds = arg; |
| 9270 | int fd; |
| 9271 | |
| 9272 | if (ctx->cq_ev_fd) |
| 9273 | return -EBUSY; |
| 9274 | |
| 9275 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9276 | return -EFAULT; |
| 9277 | |
| 9278 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9279 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9280 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9281 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9282 | ctx->cq_ev_fd = NULL; |
| 9283 | return ret; |
| 9284 | } |
| 9285 | |
| 9286 | return 0; |
| 9287 | } |
| 9288 | |
| 9289 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9290 | { |
| 9291 | if (ctx->cq_ev_fd) { |
| 9292 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9293 | ctx->cq_ev_fd = NULL; |
| 9294 | return 0; |
| 9295 | } |
| 9296 | |
| 9297 | return -ENXIO; |
| 9298 | } |
| 9299 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9300 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9301 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9302 | struct io_buffer *buf; |
| 9303 | unsigned long index; |
| 9304 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9305 | xa_for_each(&ctx->io_buffers, index, buf) { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9306 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9307 | cond_resched(); |
| 9308 | } |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9309 | } |
| 9310 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9311 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9312 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9313 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9314 | int nr = 0; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9315 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9316 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9317 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9318 | |
| 9319 | while (state->free_list.next) { |
| 9320 | struct io_wq_work_node *node; |
| 9321 | struct io_kiocb *req; |
| 9322 | |
| 9323 | node = wq_stack_extract(&state->free_list); |
| 9324 | req = container_of(node, struct io_kiocb, comp_list); |
| 9325 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9326 | nr++; |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9327 | } |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9328 | if (nr) |
| 9329 | percpu_ref_put_many(&ctx->refs, nr); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9330 | mutex_unlock(&ctx->uring_lock); |
| 9331 | } |
| 9332 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9333 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9334 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9335 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9336 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9337 | } |
| 9338 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9339 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9340 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9341 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9342 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9343 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9344 | mmdrop(ctx->mm_account); |
| 9345 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9346 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9347 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 9348 | io_rsrc_refs_drop(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9349 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9350 | io_wait_rsrc_data(ctx->buf_data); |
| 9351 | io_wait_rsrc_data(ctx->file_data); |
| 9352 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9353 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9354 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9355 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9356 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9357 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9358 | if (ctx->rings) |
| 9359 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9360 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9361 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9362 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9363 | if (ctx->sq_creds) |
| 9364 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9365 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9366 | /* there are no registered resources left, nobody uses it */ |
| 9367 | if (ctx->rsrc_node) |
| 9368 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9369 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9370 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9371 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 756ab7c | 2021-10-06 16:06:47 +0100 | [diff] [blame] | 9372 | flush_delayed_work(&ctx->fallback_work); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9373 | |
| 9374 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9375 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9376 | |
| 9377 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9378 | if (ctx->ring_sock) { |
| 9379 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9380 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9381 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9382 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9383 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9384 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9385 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9386 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9387 | |
| 9388 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9389 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9390 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9391 | if (ctx->hash_map) |
| 9392 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9393 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9394 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9395 | kfree(ctx); |
| 9396 | } |
| 9397 | |
| 9398 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9399 | { |
| 9400 | struct io_ring_ctx *ctx = file->private_data; |
| 9401 | __poll_t mask = 0; |
| 9402 | |
Pavel Begunkov | d60aa65 | 2021-10-04 20:02:52 +0100 | [diff] [blame] | 9403 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9404 | /* |
| 9405 | * synchronizes with barrier from wq_has_sleeper call in |
| 9406 | * io_commit_cqring |
| 9407 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9408 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9409 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9410 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9411 | |
| 9412 | /* |
| 9413 | * Don't flush cqring overflow list here, just do a simple check. |
| 9414 | * Otherwise there could possible be ABBA deadlock: |
| 9415 | * CPU0 CPU1 |
| 9416 | * ---- ---- |
| 9417 | * lock(&ctx->uring_lock); |
| 9418 | * lock(&ep->mtx); |
| 9419 | * lock(&ctx->uring_lock); |
| 9420 | * lock(&ep->mtx); |
| 9421 | * |
| 9422 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9423 | * pushs them to do the flush. |
| 9424 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9425 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9426 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9427 | |
| 9428 | return mask; |
| 9429 | } |
| 9430 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9431 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9432 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9433 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9434 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9435 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9436 | if (creds) { |
| 9437 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9438 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9439 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9440 | |
| 9441 | return -EINVAL; |
| 9442 | } |
| 9443 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9444 | struct io_tctx_exit { |
| 9445 | struct callback_head task_work; |
| 9446 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9447 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9448 | }; |
| 9449 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9450 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9451 | { |
| 9452 | struct io_uring_task *tctx = current->io_uring; |
| 9453 | struct io_tctx_exit *work; |
| 9454 | |
| 9455 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9456 | /* |
| 9457 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9458 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 9459 | */ |
| 9460 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9461 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9462 | complete(&work->completion); |
| 9463 | } |
| 9464 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9465 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9466 | { |
| 9467 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9468 | |
| 9469 | return req->ctx == data; |
| 9470 | } |
| 9471 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9472 | static __cold void io_ring_exit_work(struct work_struct *work) |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9473 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9474 | 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] | 9475 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9476 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9477 | struct io_tctx_exit exit; |
| 9478 | struct io_tctx_node *node; |
| 9479 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9480 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9481 | /* |
| 9482 | * If we're doing polled IO and end up having requests being |
| 9483 | * submitted async (out-of-line), then completions can come in while |
| 9484 | * we're waiting for refs to drop. We need to reap these manually, |
| 9485 | * as nobody else will be looking for them. |
| 9486 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9487 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9488 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9489 | if (ctx->sq_data) { |
| 9490 | struct io_sq_data *sqd = ctx->sq_data; |
| 9491 | struct task_struct *tsk; |
| 9492 | |
| 9493 | io_sq_thread_park(sqd); |
| 9494 | tsk = sqd->thread; |
| 9495 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9496 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9497 | io_cancel_ctx_cb, ctx, true); |
| 9498 | io_sq_thread_unpark(sqd); |
| 9499 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9500 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9501 | io_req_caches_free(ctx); |
| 9502 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9503 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9504 | /* there is little hope left, don't run it too often */ |
| 9505 | interval = HZ * 60; |
| 9506 | } |
| 9507 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9508 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9509 | init_completion(&exit.completion); |
| 9510 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9511 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9512 | /* |
| 9513 | * Some may use context even when all refs and requests have been put, |
| 9514 | * and they are free to do so while still holding uring_lock or |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 9515 | * completion_lock, see io_req_task_submit(). Apart from other work, |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9516 | * this lock/unlock section also waits them to finish. |
| 9517 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9518 | mutex_lock(&ctx->uring_lock); |
| 9519 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9520 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9521 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9522 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9523 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9524 | /* don't spin on a single task if cancellation failed */ |
| 9525 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9526 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9527 | if (WARN_ON_ONCE(ret)) |
| 9528 | continue; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9529 | |
| 9530 | mutex_unlock(&ctx->uring_lock); |
| 9531 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9532 | mutex_lock(&ctx->uring_lock); |
| 9533 | } |
| 9534 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9535 | spin_lock(&ctx->completion_lock); |
| 9536 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9537 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9538 | io_ring_ctx_free(ctx); |
| 9539 | } |
| 9540 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9541 | /* Returns true if we found and killed one or more timeouts */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9542 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
| 9543 | struct task_struct *tsk, bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9544 | { |
| 9545 | struct io_kiocb *req, *tmp; |
| 9546 | int canceled = 0; |
| 9547 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9548 | spin_lock(&ctx->completion_lock); |
| 9549 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9550 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9551 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9552 | io_kill_timeout(req, -ECANCELED); |
| 9553 | canceled++; |
| 9554 | } |
| 9555 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9556 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9557 | if (canceled != 0) |
| 9558 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9559 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9560 | if (canceled != 0) |
| 9561 | io_cqring_ev_posted(ctx); |
| 9562 | return canceled != 0; |
| 9563 | } |
| 9564 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9565 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9566 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9567 | unsigned long index; |
| 9568 | struct creds *creds; |
| 9569 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9570 | mutex_lock(&ctx->uring_lock); |
| 9571 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9572 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9573 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9574 | xa_for_each(&ctx->personalities, index, creds) |
| 9575 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9576 | mutex_unlock(&ctx->uring_lock); |
| 9577 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9578 | io_kill_timeouts(ctx, NULL, true); |
| 9579 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9580 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9581 | /* 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] | 9582 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9583 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9584 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9585 | /* |
| 9586 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9587 | * if we're exiting a ton of rings at the same time. It just adds |
| 9588 | * noise and overhead, there's no discernable change in runtime |
| 9589 | * over using system_wq. |
| 9590 | */ |
| 9591 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9592 | } |
| 9593 | |
| 9594 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9595 | { |
| 9596 | struct io_ring_ctx *ctx = file->private_data; |
| 9597 | |
| 9598 | file->private_data = NULL; |
| 9599 | io_ring_ctx_wait_and_kill(ctx); |
| 9600 | return 0; |
| 9601 | } |
| 9602 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9603 | struct io_task_cancel { |
| 9604 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9605 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9606 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9607 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9608 | 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] | 9609 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9610 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9611 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9612 | bool ret; |
| 9613 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9614 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9615 | struct io_ring_ctx *ctx = req->ctx; |
| 9616 | |
| 9617 | /* protect against races with linked timeouts */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9618 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9619 | ret = io_match_task(req, cancel->task, cancel->all); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9620 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9621 | } else { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9622 | ret = io_match_task(req, cancel->task, cancel->all); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9623 | } |
| 9624 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9625 | } |
| 9626 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9627 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
| 9628 | struct task_struct *task, |
| 9629 | bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9630 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9631 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9632 | LIST_HEAD(list); |
| 9633 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9634 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9635 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9636 | if (io_match_task(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9637 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9638 | break; |
| 9639 | } |
| 9640 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9641 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9642 | if (list_empty(&list)) |
| 9643 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9644 | |
| 9645 | while (!list_empty(&list)) { |
| 9646 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9647 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9648 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9649 | kfree(de); |
| 9650 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9651 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9652 | } |
| 9653 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9654 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9655 | { |
| 9656 | struct io_tctx_node *node; |
| 9657 | enum io_wq_cancel cret; |
| 9658 | bool ret = false; |
| 9659 | |
| 9660 | mutex_lock(&ctx->uring_lock); |
| 9661 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9662 | struct io_uring_task *tctx = node->task->io_uring; |
| 9663 | |
| 9664 | /* |
| 9665 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9666 | * killed after ctx nodes, which requires to take the lock. |
| 9667 | */ |
| 9668 | if (!tctx || !tctx->io_wq) |
| 9669 | continue; |
| 9670 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9671 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9672 | } |
| 9673 | mutex_unlock(&ctx->uring_lock); |
| 9674 | |
| 9675 | return ret; |
| 9676 | } |
| 9677 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9678 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9679 | struct task_struct *task, |
| 9680 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9681 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9682 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9683 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9684 | |
| 9685 | while (1) { |
| 9686 | enum io_wq_cancel cret; |
| 9687 | bool ret = false; |
| 9688 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9689 | if (!task) { |
| 9690 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9691 | } else if (tctx && tctx->io_wq) { |
| 9692 | /* |
| 9693 | * Cancels requests of all rings, not only @ctx, but |
| 9694 | * it's fine as the task is in exit/exec. |
| 9695 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9696 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9697 | &cancel, true); |
| 9698 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9699 | } |
| 9700 | |
| 9701 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9702 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9703 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 9704 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9705 | io_iopoll_try_reap_events(ctx); |
| 9706 | ret = true; |
| 9707 | } |
| 9708 | } |
| 9709 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9710 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9711 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9712 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9713 | if (task) |
| 9714 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9715 | if (!ret) |
| 9716 | break; |
| 9717 | cond_resched(); |
| 9718 | } |
| 9719 | } |
| 9720 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9721 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9722 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9723 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9724 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9725 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9726 | |
| 9727 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9728 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9729 | if (unlikely(ret)) |
| 9730 | return ret; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9731 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9732 | tctx = current->io_uring; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9733 | if (ctx->iowq_limits_set) { |
| 9734 | unsigned int limits[2] = { ctx->iowq_limits[0], |
| 9735 | ctx->iowq_limits[1], }; |
| 9736 | |
| 9737 | ret = io_wq_max_workers(tctx->io_wq, limits); |
| 9738 | if (ret) |
| 9739 | return ret; |
| 9740 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9741 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9742 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 9743 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 9744 | if (!node) |
| 9745 | return -ENOMEM; |
| 9746 | node->ctx = ctx; |
| 9747 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9748 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9749 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 9750 | node, GFP_KERNEL)); |
| 9751 | if (ret) { |
| 9752 | kfree(node); |
| 9753 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9754 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9755 | |
| 9756 | mutex_lock(&ctx->uring_lock); |
| 9757 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 9758 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9759 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9760 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9761 | return 0; |
| 9762 | } |
| 9763 | |
| 9764 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9765 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9766 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9767 | 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] | 9768 | { |
| 9769 | struct io_uring_task *tctx = current->io_uring; |
| 9770 | |
| 9771 | if (likely(tctx && tctx->last == ctx)) |
| 9772 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9773 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9774 | } |
| 9775 | |
| 9776 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9777 | * Remove this io_uring_file -> task mapping. |
| 9778 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9779 | static __cold void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9780 | { |
| 9781 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9782 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9783 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 9784 | if (!tctx) |
| 9785 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9786 | node = xa_erase(&tctx->xa, index); |
| 9787 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9788 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9789 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9790 | WARN_ON_ONCE(current != node->task); |
| 9791 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 9792 | |
| 9793 | mutex_lock(&node->ctx->uring_lock); |
| 9794 | list_del(&node->ctx_node); |
| 9795 | mutex_unlock(&node->ctx->uring_lock); |
| 9796 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9797 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9798 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9799 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9800 | } |
| 9801 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9802 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9803 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9804 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9805 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9806 | unsigned long index; |
| 9807 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9808 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9809 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9810 | cond_resched(); |
| 9811 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9812 | if (wq) { |
| 9813 | /* |
| 9814 | * Must be after io_uring_del_task_file() (removes nodes under |
| 9815 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 9816 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9817 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 9818 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9819 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9820 | } |
| 9821 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9822 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9823 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9824 | if (tracked) |
| 9825 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9826 | return percpu_counter_sum(&tctx->inflight); |
| 9827 | } |
| 9828 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9829 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9830 | { |
| 9831 | struct io_uring_task *tctx = task->io_uring; |
| 9832 | unsigned int refs = tctx->cached_refs; |
| 9833 | |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9834 | if (refs) { |
| 9835 | tctx->cached_refs = 0; |
| 9836 | percpu_counter_sub(&tctx->inflight, refs); |
| 9837 | put_task_struct_many(task, refs); |
| 9838 | } |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9839 | } |
| 9840 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9841 | /* |
| 9842 | * Find any io_uring ctx that this task has registered or done IO on, and cancel |
| 9843 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. |
| 9844 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9845 | static __cold void io_uring_cancel_generic(bool cancel_all, |
| 9846 | struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9847 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9848 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 9849 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9850 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9851 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9852 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9853 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 9854 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 9855 | if (!current->io_uring) |
| 9856 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 9857 | if (tctx->io_wq) |
| 9858 | io_wq_exit_start(tctx->io_wq); |
| 9859 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9860 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9861 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9862 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9863 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9864 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9865 | if (!inflight) |
| 9866 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9867 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9868 | if (!sqd) { |
| 9869 | struct io_tctx_node *node; |
| 9870 | unsigned long index; |
| 9871 | |
| 9872 | xa_for_each(&tctx->xa, index, node) { |
| 9873 | /* sqpoll task will cancel all its requests */ |
| 9874 | if (node->ctx->sq_data) |
| 9875 | continue; |
| 9876 | io_uring_try_cancel_requests(node->ctx, current, |
| 9877 | cancel_all); |
| 9878 | } |
| 9879 | } else { |
| 9880 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 9881 | io_uring_try_cancel_requests(ctx, current, |
| 9882 | cancel_all); |
| 9883 | } |
| 9884 | |
| 9885 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9886 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9887 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9888 | * If we've seen completions, retry without waiting. This |
| 9889 | * avoids a race where a completion comes in before we did |
| 9890 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9891 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9892 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9893 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9894 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9895 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9896 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9897 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9898 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9899 | if (cancel_all) { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9900 | /* for exec all current's requests should be gone, kill tctx */ |
| 9901 | __io_uring_free(current); |
| 9902 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9903 | } |
| 9904 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9905 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9906 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9907 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9908 | } |
| 9909 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9910 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9911 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9912 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9913 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9914 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9915 | struct page *page; |
| 9916 | void *ptr; |
| 9917 | |
| 9918 | switch (offset) { |
| 9919 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9920 | case IORING_OFF_CQ_RING: |
| 9921 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9922 | break; |
| 9923 | case IORING_OFF_SQES: |
| 9924 | ptr = ctx->sq_sqes; |
| 9925 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9926 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9927 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9928 | } |
| 9929 | |
| 9930 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9931 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9932 | return ERR_PTR(-EINVAL); |
| 9933 | |
| 9934 | return ptr; |
| 9935 | } |
| 9936 | |
| 9937 | #ifdef CONFIG_MMU |
| 9938 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9939 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9940 | { |
| 9941 | size_t sz = vma->vm_end - vma->vm_start; |
| 9942 | unsigned long pfn; |
| 9943 | void *ptr; |
| 9944 | |
| 9945 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9946 | if (IS_ERR(ptr)) |
| 9947 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9948 | |
| 9949 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9950 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9951 | } |
| 9952 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9953 | #else /* !CONFIG_MMU */ |
| 9954 | |
| 9955 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9956 | { |
| 9957 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9958 | } |
| 9959 | |
| 9960 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9961 | { |
| 9962 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9963 | } |
| 9964 | |
| 9965 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9966 | unsigned long addr, unsigned long len, |
| 9967 | unsigned long pgoff, unsigned long flags) |
| 9968 | { |
| 9969 | void *ptr; |
| 9970 | |
| 9971 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9972 | if (IS_ERR(ptr)) |
| 9973 | return PTR_ERR(ptr); |
| 9974 | |
| 9975 | return (unsigned long) ptr; |
| 9976 | } |
| 9977 | |
| 9978 | #endif /* !CONFIG_MMU */ |
| 9979 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9980 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9981 | { |
| 9982 | DEFINE_WAIT(wait); |
| 9983 | |
| 9984 | do { |
| 9985 | if (!io_sqring_full(ctx)) |
| 9986 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9987 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9988 | |
| 9989 | if (!io_sqring_full(ctx)) |
| 9990 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9991 | schedule(); |
| 9992 | } while (!signal_pending(current)); |
| 9993 | |
| 9994 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9995 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9996 | } |
| 9997 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9998 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9999 | struct __kernel_timespec __user **ts, |
| 10000 | const sigset_t __user **sig) |
| 10001 | { |
| 10002 | struct io_uring_getevents_arg arg; |
| 10003 | |
| 10004 | /* |
| 10005 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 10006 | * is just a pointer to the sigset_t. |
| 10007 | */ |
| 10008 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 10009 | *sig = (const sigset_t __user *) argp; |
| 10010 | *ts = NULL; |
| 10011 | return 0; |
| 10012 | } |
| 10013 | |
| 10014 | /* |
| 10015 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 10016 | * timespec and sigset_t pointers if good. |
| 10017 | */ |
| 10018 | if (*argsz != sizeof(arg)) |
| 10019 | return -EINVAL; |
| 10020 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 10021 | return -EFAULT; |
| 10022 | *sig = u64_to_user_ptr(arg.sigmask); |
| 10023 | *argsz = arg.sigmask_sz; |
| 10024 | *ts = u64_to_user_ptr(arg.ts); |
| 10025 | return 0; |
| 10026 | } |
| 10027 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10028 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10029 | u32, min_complete, u32, flags, const void __user *, argp, |
| 10030 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10031 | { |
| 10032 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10033 | int submitted = 0; |
| 10034 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10035 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10036 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 10037 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 10038 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10039 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 10040 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10041 | return -EINVAL; |
| 10042 | |
| 10043 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10044 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10045 | return -EBADF; |
| 10046 | |
| 10047 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10048 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10049 | goto out_fput; |
| 10050 | |
| 10051 | ret = -ENXIO; |
| 10052 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10053 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10054 | goto out_fput; |
| 10055 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10056 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10057 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10058 | goto out; |
| 10059 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10060 | /* |
| 10061 | * For SQ polling, the thread will do all submissions and completions. |
| 10062 | * Just return the requested submit count, and wake the thread if |
| 10063 | * we were asked to. |
| 10064 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10065 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10066 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 10067 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 10068 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10069 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 10070 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 10071 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10072 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10073 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 10074 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10075 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 10076 | ret = io_sqpoll_wait_sq(ctx); |
| 10077 | if (ret) |
| 10078 | goto out; |
| 10079 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10080 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10081 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10082 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10083 | if (unlikely(ret)) |
| 10084 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10085 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10086 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10087 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10088 | |
| 10089 | if (submitted != to_submit) |
| 10090 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10091 | } |
| 10092 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10093 | const sigset_t __user *sig; |
| 10094 | struct __kernel_timespec __user *ts; |
| 10095 | |
| 10096 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 10097 | if (unlikely(ret)) |
| 10098 | goto out; |
| 10099 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10100 | min_complete = min(min_complete, ctx->cq_entries); |
| 10101 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 10102 | /* |
| 10103 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 10104 | * space applications don't need to do io completion events |
| 10105 | * polling again, they can rely on io_sq_thread to do polling |
| 10106 | * work, which can reduce cpu usage and uring_lock contention. |
| 10107 | */ |
| 10108 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 10109 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 10110 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10111 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10112 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10113 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10114 | } |
| 10115 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10116 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 10117 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10118 | out_fput: |
| 10119 | fdput(f); |
| 10120 | return submitted ? submitted : ret; |
| 10121 | } |
| 10122 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10123 | #ifdef CONFIG_PROC_FS |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10124 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10125 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10126 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10127 | struct user_namespace *uns = seq_user_ns(m); |
| 10128 | struct group_info *gi; |
| 10129 | kernel_cap_t cap; |
| 10130 | unsigned __capi; |
| 10131 | int g; |
| 10132 | |
| 10133 | seq_printf(m, "%5d\n", id); |
| 10134 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 10135 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 10136 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 10137 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 10138 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 10139 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 10140 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 10141 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 10142 | seq_puts(m, "\n\tGroups:\t"); |
| 10143 | gi = cred->group_info; |
| 10144 | for (g = 0; g < gi->ngroups; g++) { |
| 10145 | seq_put_decimal_ull(m, g ? " " : "", |
| 10146 | from_kgid_munged(uns, gi->gid[g])); |
| 10147 | } |
| 10148 | seq_puts(m, "\n\tCapEff:\t"); |
| 10149 | cap = cred->cap_effective; |
| 10150 | CAP_FOR_EACH_U32(__capi) |
| 10151 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 10152 | seq_putc(m, '\n'); |
| 10153 | return 0; |
| 10154 | } |
| 10155 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10156 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
| 10157 | struct seq_file *m) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10158 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10159 | struct io_sq_data *sq = NULL; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10160 | struct io_overflow_cqe *ocqe; |
| 10161 | struct io_rings *r = ctx->rings; |
| 10162 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10163 | unsigned int sq_head = READ_ONCE(r->sq.head); |
| 10164 | unsigned int sq_tail = READ_ONCE(r->sq.tail); |
| 10165 | unsigned int cq_head = READ_ONCE(r->cq.head); |
| 10166 | unsigned int cq_tail = READ_ONCE(r->cq.tail); |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10167 | unsigned int sq_entries, cq_entries; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10168 | bool has_lock; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10169 | unsigned int i; |
| 10170 | |
| 10171 | /* |
| 10172 | * we may get imprecise sqe and cqe info if uring is actively running |
| 10173 | * since we get cached_sq_head and cached_cq_tail without uring_lock |
| 10174 | * and sq_tail and cq_head are changed by userspace. But it's ok since |
| 10175 | * we usually use these info when it is stuck. |
| 10176 | */ |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10177 | seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask); |
| 10178 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
| 10179 | seq_printf(m, "SqTail:\t%u\n", sq_tail); |
| 10180 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); |
| 10181 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); |
| 10182 | seq_printf(m, "CqHead:\t%u\n", cq_head); |
| 10183 | seq_printf(m, "CqTail:\t%u\n", cq_tail); |
| 10184 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); |
| 10185 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); |
| 10186 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); |
| 10187 | for (i = 0; i < sq_entries; i++) { |
| 10188 | unsigned int entry = i + sq_head; |
| 10189 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); |
Jens Axboe | a195778 | 2021-11-05 09:31:05 -0600 | [diff] [blame] | 10190 | struct io_uring_sqe *sqe; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10191 | |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10192 | if (sq_idx > sq_mask) |
| 10193 | continue; |
| 10194 | sqe = &ctx->sq_sqes[sq_idx]; |
| 10195 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", |
| 10196 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, |
| 10197 | sqe->user_data); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10198 | } |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10199 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
| 10200 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); |
| 10201 | for (i = 0; i < cq_entries; i++) { |
| 10202 | unsigned int entry = i + cq_head; |
| 10203 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10204 | |
| 10205 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10206 | entry & cq_mask, cqe->user_data, cqe->res, |
| 10207 | cqe->flags); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10208 | } |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10209 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10210 | /* |
| 10211 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10212 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10213 | * cases. If we fail to get the lock, we just don't iterate any |
| 10214 | * structures that could be going away outside the io_uring mutex. |
| 10215 | */ |
| 10216 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10217 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10218 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10219 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10220 | if (!sq->thread) |
| 10221 | sq = NULL; |
| 10222 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10223 | |
| 10224 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 10225 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10226 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10227 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10228 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10229 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10230 | if (f) |
| 10231 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10232 | else |
| 10233 | seq_printf(m, "%5u: <none>\n", i); |
| 10234 | } |
| 10235 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10236 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10237 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10238 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10239 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10240 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10241 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10242 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10243 | unsigned long index; |
| 10244 | const struct cred *cred; |
| 10245 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10246 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10247 | xa_for_each(&ctx->personalities, index, cred) |
| 10248 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10249 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10250 | if (has_lock) |
| 10251 | mutex_unlock(&ctx->uring_lock); |
| 10252 | |
| 10253 | seq_puts(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10254 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10255 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10256 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10257 | struct io_kiocb *req; |
| 10258 | |
| 10259 | hlist_for_each_entry(req, list, hash_node) |
| 10260 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10261 | req->task->task_works != NULL); |
| 10262 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10263 | |
| 10264 | seq_puts(m, "CqOverflowList:\n"); |
| 10265 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { |
| 10266 | struct io_uring_cqe *cqe = &ocqe->cqe; |
| 10267 | |
| 10268 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", |
| 10269 | cqe->user_data, cqe->res, cqe->flags); |
| 10270 | |
| 10271 | } |
| 10272 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10273 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10274 | } |
| 10275 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10276 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10277 | { |
| 10278 | struct io_ring_ctx *ctx = f->private_data; |
| 10279 | |
| 10280 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10281 | __io_uring_show_fdinfo(ctx, m); |
| 10282 | percpu_ref_put(&ctx->refs); |
| 10283 | } |
| 10284 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10285 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10286 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10287 | static const struct file_operations io_uring_fops = { |
| 10288 | .release = io_uring_release, |
| 10289 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10290 | #ifndef CONFIG_MMU |
| 10291 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10292 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10293 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10294 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10295 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10296 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10297 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10298 | }; |
| 10299 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10300 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10301 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10302 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10303 | struct io_rings *rings; |
| 10304 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10305 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10306 | /* make sure these are sane, as we already accounted them */ |
| 10307 | ctx->sq_entries = p->sq_entries; |
| 10308 | ctx->cq_entries = p->cq_entries; |
| 10309 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10310 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10311 | if (size == SIZE_MAX) |
| 10312 | return -EOVERFLOW; |
| 10313 | |
| 10314 | rings = io_mem_alloc(size); |
| 10315 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10316 | return -ENOMEM; |
| 10317 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10318 | ctx->rings = rings; |
| 10319 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10320 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10321 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10322 | rings->sq_ring_entries = p->sq_entries; |
| 10323 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10324 | |
| 10325 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10326 | if (size == SIZE_MAX) { |
| 10327 | io_mem_free(ctx->rings); |
| 10328 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10329 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10330 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10331 | |
| 10332 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10333 | if (!ctx->sq_sqes) { |
| 10334 | io_mem_free(ctx->rings); |
| 10335 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10336 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10337 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10338 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10339 | return 0; |
| 10340 | } |
| 10341 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10342 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10343 | { |
| 10344 | int ret, fd; |
| 10345 | |
| 10346 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10347 | if (fd < 0) |
| 10348 | return fd; |
| 10349 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10350 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10351 | if (ret) { |
| 10352 | put_unused_fd(fd); |
| 10353 | return ret; |
| 10354 | } |
| 10355 | fd_install(fd, file); |
| 10356 | return fd; |
| 10357 | } |
| 10358 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10359 | /* |
| 10360 | * Allocate an anonymous fd, this is what constitutes the application |
| 10361 | * visible backing of an io_uring instance. The application mmaps this |
| 10362 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10363 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10364 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10365 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10366 | { |
| 10367 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10368 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10369 | int ret; |
| 10370 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10371 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10372 | &ctx->ring_sock); |
| 10373 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10374 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10375 | #endif |
| 10376 | |
Paul Moore | 91a9ab7 | 2021-02-01 19:33:52 -0500 | [diff] [blame] | 10377 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
| 10378 | O_RDWR | O_CLOEXEC, NULL); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10379 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10380 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10381 | sock_release(ctx->ring_sock); |
| 10382 | ctx->ring_sock = NULL; |
| 10383 | } else { |
| 10384 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10385 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10386 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10387 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10388 | } |
| 10389 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10390 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10391 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10392 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10393 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10394 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10395 | int ret; |
| 10396 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10397 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10398 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10399 | if (entries > IORING_MAX_ENTRIES) { |
| 10400 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10401 | return -EINVAL; |
| 10402 | entries = IORING_MAX_ENTRIES; |
| 10403 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10404 | |
| 10405 | /* |
| 10406 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10407 | * application to drive a higher depth than the size of the SQ ring, |
| 10408 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10409 | * some flexibility in overcommitting a bit. If the application has |
| 10410 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10411 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10412 | */ |
| 10413 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10414 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10415 | /* |
| 10416 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10417 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10418 | * any cq vs sq ring sizing. |
| 10419 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10420 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10421 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10422 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10423 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10424 | return -EINVAL; |
| 10425 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10426 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10427 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10428 | if (p->cq_entries < p->sq_entries) |
| 10429 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10430 | } else { |
| 10431 | p->cq_entries = 2 * p->sq_entries; |
| 10432 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10433 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10434 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10435 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10436 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10437 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10438 | if (!capable(CAP_IPC_LOCK)) |
| 10439 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10440 | |
| 10441 | /* |
| 10442 | * This is just grabbed for accounting purposes. When a process exits, |
| 10443 | * the mm is exited and dropped before the files, hence we need to hang |
| 10444 | * on to this mm purely for the purposes of being able to unaccount |
| 10445 | * memory (locked/pinned vm). It's not used for anything else. |
| 10446 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10447 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10448 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10449 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10450 | ret = io_allocate_scq_urings(ctx, p); |
| 10451 | if (ret) |
| 10452 | goto err; |
| 10453 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10454 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10455 | if (ret) |
| 10456 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10457 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10458 | ret = io_rsrc_node_switch_start(ctx); |
| 10459 | if (ret) |
| 10460 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10461 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10462 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10463 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10464 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10465 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10466 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10467 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10468 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10469 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10470 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10471 | |
| 10472 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10473 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10474 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10475 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10476 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10477 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10478 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10479 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10480 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10481 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10482 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10483 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10484 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10485 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 10486 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10487 | |
| 10488 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10489 | ret = -EFAULT; |
| 10490 | goto err; |
| 10491 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10492 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10493 | file = io_uring_get_file(ctx); |
| 10494 | if (IS_ERR(file)) { |
| 10495 | ret = PTR_ERR(file); |
| 10496 | goto err; |
| 10497 | } |
| 10498 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10499 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10500 | * Install ring fd as the very last thing, so we don't risk someone |
| 10501 | * having closed it before we finish setup |
| 10502 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10503 | ret = io_uring_install_fd(ctx, file); |
| 10504 | if (ret < 0) { |
| 10505 | /* fput will clean it up */ |
| 10506 | fput(file); |
| 10507 | return ret; |
| 10508 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10509 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10510 | 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] | 10511 | return ret; |
| 10512 | err: |
| 10513 | io_ring_ctx_wait_and_kill(ctx); |
| 10514 | return ret; |
| 10515 | } |
| 10516 | |
| 10517 | /* |
| 10518 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10519 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10520 | * params structure passed in. |
| 10521 | */ |
| 10522 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10523 | { |
| 10524 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10525 | int i; |
| 10526 | |
| 10527 | if (copy_from_user(&p, params, sizeof(p))) |
| 10528 | return -EFAULT; |
| 10529 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10530 | if (p.resv[i]) |
| 10531 | return -EINVAL; |
| 10532 | } |
| 10533 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10534 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10535 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10536 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10537 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10538 | return -EINVAL; |
| 10539 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10540 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10541 | } |
| 10542 | |
| 10543 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10544 | struct io_uring_params __user *, params) |
| 10545 | { |
| 10546 | return io_uring_setup(entries, params); |
| 10547 | } |
| 10548 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10549 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
| 10550 | unsigned nr_args) |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10551 | { |
| 10552 | struct io_uring_probe *p; |
| 10553 | size_t size; |
| 10554 | int i, ret; |
| 10555 | |
| 10556 | size = struct_size(p, ops, nr_args); |
| 10557 | if (size == SIZE_MAX) |
| 10558 | return -EOVERFLOW; |
| 10559 | p = kzalloc(size, GFP_KERNEL); |
| 10560 | if (!p) |
| 10561 | return -ENOMEM; |
| 10562 | |
| 10563 | ret = -EFAULT; |
| 10564 | if (copy_from_user(p, arg, size)) |
| 10565 | goto out; |
| 10566 | ret = -EINVAL; |
| 10567 | if (memchr_inv(p, 0, size)) |
| 10568 | goto out; |
| 10569 | |
| 10570 | p->last_op = IORING_OP_LAST - 1; |
| 10571 | if (nr_args > IORING_OP_LAST) |
| 10572 | nr_args = IORING_OP_LAST; |
| 10573 | |
| 10574 | for (i = 0; i < nr_args; i++) { |
| 10575 | p->ops[i].op = i; |
| 10576 | if (!io_op_defs[i].not_supported) |
| 10577 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10578 | } |
| 10579 | p->ops_len = i; |
| 10580 | |
| 10581 | ret = 0; |
| 10582 | if (copy_to_user(arg, p, size)) |
| 10583 | ret = -EFAULT; |
| 10584 | out: |
| 10585 | kfree(p); |
| 10586 | return ret; |
| 10587 | } |
| 10588 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10589 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10590 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10591 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10592 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10593 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10594 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10595 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10596 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10597 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10598 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10599 | if (ret < 0) { |
| 10600 | put_cred(creds); |
| 10601 | return ret; |
| 10602 | } |
| 10603 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10604 | } |
| 10605 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10606 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
| 10607 | void __user *arg, unsigned int nr_args) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10608 | { |
| 10609 | struct io_uring_restriction *res; |
| 10610 | size_t size; |
| 10611 | int i, ret; |
| 10612 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10613 | /* Restrictions allowed only if rings started disabled */ |
| 10614 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10615 | return -EBADFD; |
| 10616 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10617 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10618 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10619 | return -EBUSY; |
| 10620 | |
| 10621 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10622 | return -EINVAL; |
| 10623 | |
| 10624 | size = array_size(nr_args, sizeof(*res)); |
| 10625 | if (size == SIZE_MAX) |
| 10626 | return -EOVERFLOW; |
| 10627 | |
| 10628 | res = memdup_user(arg, size); |
| 10629 | if (IS_ERR(res)) |
| 10630 | return PTR_ERR(res); |
| 10631 | |
| 10632 | ret = 0; |
| 10633 | |
| 10634 | for (i = 0; i < nr_args; i++) { |
| 10635 | switch (res[i].opcode) { |
| 10636 | case IORING_RESTRICTION_REGISTER_OP: |
| 10637 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10638 | ret = -EINVAL; |
| 10639 | goto out; |
| 10640 | } |
| 10641 | |
| 10642 | __set_bit(res[i].register_op, |
| 10643 | ctx->restrictions.register_op); |
| 10644 | break; |
| 10645 | case IORING_RESTRICTION_SQE_OP: |
| 10646 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10647 | ret = -EINVAL; |
| 10648 | goto out; |
| 10649 | } |
| 10650 | |
| 10651 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10652 | break; |
| 10653 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10654 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10655 | break; |
| 10656 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10657 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10658 | break; |
| 10659 | default: |
| 10660 | ret = -EINVAL; |
| 10661 | goto out; |
| 10662 | } |
| 10663 | } |
| 10664 | |
| 10665 | out: |
| 10666 | /* Reset all restrictions if an error happened */ |
| 10667 | if (ret != 0) |
| 10668 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10669 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10670 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10671 | |
| 10672 | kfree(res); |
| 10673 | return ret; |
| 10674 | } |
| 10675 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10676 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10677 | { |
| 10678 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10679 | return -EBADFD; |
| 10680 | |
| 10681 | if (ctx->restrictions.registered) |
| 10682 | ctx->restricted = 1; |
| 10683 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10684 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10685 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10686 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10687 | return 0; |
| 10688 | } |
| 10689 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10690 | 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] | 10691 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10692 | unsigned nr_args) |
| 10693 | { |
| 10694 | __u32 tmp; |
| 10695 | int err; |
| 10696 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10697 | if (up->resv) |
| 10698 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10699 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10700 | return -EOVERFLOW; |
| 10701 | err = io_rsrc_node_switch_start(ctx); |
| 10702 | if (err) |
| 10703 | return err; |
| 10704 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10705 | switch (type) { |
| 10706 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10707 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10708 | case IORING_RSRC_BUFFER: |
| 10709 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10710 | } |
| 10711 | return -EINVAL; |
| 10712 | } |
| 10713 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10714 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10715 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10716 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10717 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10718 | |
| 10719 | if (!nr_args) |
| 10720 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10721 | memset(&up, 0, sizeof(up)); |
| 10722 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10723 | return -EFAULT; |
| 10724 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10725 | } |
| 10726 | |
| 10727 | 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] | 10728 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10729 | { |
| 10730 | struct io_uring_rsrc_update2 up; |
| 10731 | |
| 10732 | if (size != sizeof(up)) |
| 10733 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10734 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10735 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10736 | if (!up.nr || up.resv) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10737 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10738 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10739 | } |
| 10740 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10741 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10742 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10743 | { |
| 10744 | struct io_uring_rsrc_register rr; |
| 10745 | |
| 10746 | /* keep it extendible */ |
| 10747 | if (size != sizeof(rr)) |
| 10748 | return -EINVAL; |
| 10749 | |
| 10750 | memset(&rr, 0, sizeof(rr)); |
| 10751 | if (copy_from_user(&rr, arg, size)) |
| 10752 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10753 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10754 | return -EINVAL; |
| 10755 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10756 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10757 | case IORING_RSRC_FILE: |
| 10758 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10759 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10760 | case IORING_RSRC_BUFFER: |
| 10761 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10762 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10763 | } |
| 10764 | return -EINVAL; |
| 10765 | } |
| 10766 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10767 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
| 10768 | void __user *arg, unsigned len) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10769 | { |
| 10770 | struct io_uring_task *tctx = current->io_uring; |
| 10771 | cpumask_var_t new_mask; |
| 10772 | int ret; |
| 10773 | |
| 10774 | if (!tctx || !tctx->io_wq) |
| 10775 | return -EINVAL; |
| 10776 | |
| 10777 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10778 | return -ENOMEM; |
| 10779 | |
| 10780 | cpumask_clear(new_mask); |
| 10781 | if (len > cpumask_size()) |
| 10782 | len = cpumask_size(); |
| 10783 | |
| 10784 | if (copy_from_user(new_mask, arg, len)) { |
| 10785 | free_cpumask_var(new_mask); |
| 10786 | return -EFAULT; |
| 10787 | } |
| 10788 | |
| 10789 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10790 | free_cpumask_var(new_mask); |
| 10791 | return ret; |
| 10792 | } |
| 10793 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10794 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10795 | { |
| 10796 | struct io_uring_task *tctx = current->io_uring; |
| 10797 | |
| 10798 | if (!tctx || !tctx->io_wq) |
| 10799 | return -EINVAL; |
| 10800 | |
| 10801 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 10802 | } |
| 10803 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10804 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 10805 | void __user *arg) |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10806 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10807 | { |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10808 | struct io_tctx_node *node; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10809 | struct io_uring_task *tctx = NULL; |
| 10810 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10811 | __u32 new_count[2]; |
| 10812 | int i, ret; |
| 10813 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10814 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 10815 | return -EFAULT; |
| 10816 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10817 | if (new_count[i] > INT_MAX) |
| 10818 | return -EINVAL; |
| 10819 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10820 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 10821 | sqd = ctx->sq_data; |
| 10822 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10823 | /* |
| 10824 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 10825 | * ordering. Fine to drop uring_lock here, we hold |
| 10826 | * a ref to the ctx. |
| 10827 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10828 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10829 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10830 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10831 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10832 | if (sqd->thread) |
| 10833 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10834 | } |
| 10835 | } else { |
| 10836 | tctx = current->io_uring; |
| 10837 | } |
| 10838 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10839 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10840 | |
Pavel Begunkov | bad119b | 2021-11-08 15:10:03 +0000 | [diff] [blame] | 10841 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10842 | if (new_count[i]) |
| 10843 | ctx->iowq_limits[i] = new_count[i]; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10844 | ctx->iowq_limits_set = true; |
| 10845 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10846 | if (tctx && tctx->io_wq) { |
| 10847 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 10848 | if (ret) |
| 10849 | goto err; |
| 10850 | } else { |
| 10851 | memset(new_count, 0, sizeof(new_count)); |
| 10852 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10853 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10854 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10855 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10856 | io_put_sq_data(sqd); |
| 10857 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10858 | |
| 10859 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 10860 | return -EFAULT; |
| 10861 | |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10862 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
| 10863 | if (sqd) |
| 10864 | return 0; |
| 10865 | |
| 10866 | /* now propagate the restriction to all registered users */ |
| 10867 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 10868 | struct io_uring_task *tctx = node->task->io_uring; |
| 10869 | |
| 10870 | if (WARN_ON_ONCE(!tctx->io_wq)) |
| 10871 | continue; |
| 10872 | |
| 10873 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10874 | new_count[i] = ctx->iowq_limits[i]; |
| 10875 | /* ignore errors, it always returns zero anyway */ |
| 10876 | (void)io_wq_max_workers(tctx->io_wq, new_count); |
| 10877 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10878 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10879 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10880 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10881 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10882 | io_put_sq_data(sqd); |
| 10883 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10884 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10885 | } |
| 10886 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10887 | static bool io_register_op_must_quiesce(int op) |
| 10888 | { |
| 10889 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 10890 | case IORING_REGISTER_BUFFERS: |
| 10891 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 10892 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10893 | case IORING_UNREGISTER_FILES: |
| 10894 | case IORING_REGISTER_FILES_UPDATE: |
| 10895 | case IORING_REGISTER_PROBE: |
| 10896 | case IORING_REGISTER_PERSONALITY: |
| 10897 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10898 | case IORING_REGISTER_FILES2: |
| 10899 | case IORING_REGISTER_FILES_UPDATE2: |
| 10900 | case IORING_REGISTER_BUFFERS2: |
| 10901 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10902 | case IORING_REGISTER_IOWQ_AFF: |
| 10903 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10904 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10905 | return false; |
| 10906 | default: |
| 10907 | return true; |
| 10908 | } |
| 10909 | } |
| 10910 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10911 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10912 | { |
| 10913 | long ret; |
| 10914 | |
| 10915 | percpu_ref_kill(&ctx->refs); |
| 10916 | |
| 10917 | /* |
| 10918 | * Drop uring mutex before waiting for references to exit. If another |
| 10919 | * thread is currently inside io_uring_enter() it might need to grab the |
| 10920 | * uring_lock to make progress. If we hold it here across the drain |
| 10921 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10922 | * no new references will come in after we've killed the percpu ref. |
| 10923 | */ |
| 10924 | mutex_unlock(&ctx->uring_lock); |
| 10925 | do { |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10926 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
| 10927 | if (ret) { |
| 10928 | ret = min(0L, ret); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10929 | break; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10930 | } |
| 10931 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10932 | ret = io_run_task_work_sig(); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10933 | io_req_caches_free(ctx); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10934 | } while (ret >= 0); |
| 10935 | mutex_lock(&ctx->uring_lock); |
| 10936 | |
| 10937 | if (ret) |
| 10938 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 10939 | return ret; |
| 10940 | } |
| 10941 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10942 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10943 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10944 | __releases(ctx->uring_lock) |
| 10945 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10946 | { |
| 10947 | int ret; |
| 10948 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10949 | /* |
| 10950 | * We're inside the ring mutex, if the ref is already dying, then |
| 10951 | * someone else killed the ctx or is already going through |
| 10952 | * io_uring_register(). |
| 10953 | */ |
| 10954 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10955 | return -ENXIO; |
| 10956 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 10957 | if (ctx->restricted) { |
| 10958 | if (opcode >= IORING_REGISTER_LAST) |
| 10959 | return -EINVAL; |
| 10960 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 10961 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 10962 | return -EACCES; |
| 10963 | } |
| 10964 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10965 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10966 | ret = io_ctx_quiesce(ctx); |
| 10967 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 10968 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10969 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10970 | |
| 10971 | switch (opcode) { |
| 10972 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10973 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10974 | break; |
| 10975 | case IORING_UNREGISTER_BUFFERS: |
| 10976 | ret = -EINVAL; |
| 10977 | if (arg || nr_args) |
| 10978 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10979 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10980 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10981 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10982 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10983 | break; |
| 10984 | case IORING_UNREGISTER_FILES: |
| 10985 | ret = -EINVAL; |
| 10986 | if (arg || nr_args) |
| 10987 | break; |
| 10988 | ret = io_sqe_files_unregister(ctx); |
| 10989 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10990 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10991 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10992 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10993 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10994 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10995 | ret = -EINVAL; |
| 10996 | if (nr_args != 1) |
| 10997 | break; |
| 10998 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10999 | if (ret) |
| 11000 | break; |
| 11001 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 11002 | ctx->eventfd_async = 1; |
| 11003 | else |
| 11004 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11005 | break; |
| 11006 | case IORING_UNREGISTER_EVENTFD: |
| 11007 | ret = -EINVAL; |
| 11008 | if (arg || nr_args) |
| 11009 | break; |
| 11010 | ret = io_eventfd_unregister(ctx); |
| 11011 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 11012 | case IORING_REGISTER_PROBE: |
| 11013 | ret = -EINVAL; |
| 11014 | if (!arg || nr_args > 256) |
| 11015 | break; |
| 11016 | ret = io_probe(ctx, arg, nr_args); |
| 11017 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11018 | case IORING_REGISTER_PERSONALITY: |
| 11019 | ret = -EINVAL; |
| 11020 | if (arg || nr_args) |
| 11021 | break; |
| 11022 | ret = io_register_personality(ctx); |
| 11023 | break; |
| 11024 | case IORING_UNREGISTER_PERSONALITY: |
| 11025 | ret = -EINVAL; |
| 11026 | if (arg) |
| 11027 | break; |
| 11028 | ret = io_unregister_personality(ctx, nr_args); |
| 11029 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 11030 | case IORING_REGISTER_ENABLE_RINGS: |
| 11031 | ret = -EINVAL; |
| 11032 | if (arg || nr_args) |
| 11033 | break; |
| 11034 | ret = io_register_enable_rings(ctx); |
| 11035 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 11036 | case IORING_REGISTER_RESTRICTIONS: |
| 11037 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 11038 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11039 | case IORING_REGISTER_FILES2: |
| 11040 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11041 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11042 | case IORING_REGISTER_FILES_UPDATE2: |
| 11043 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11044 | IORING_RSRC_FILE); |
| 11045 | break; |
| 11046 | case IORING_REGISTER_BUFFERS2: |
| 11047 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 11048 | break; |
| 11049 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 11050 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11051 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11052 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 11053 | case IORING_REGISTER_IOWQ_AFF: |
| 11054 | ret = -EINVAL; |
| 11055 | if (!arg || !nr_args) |
| 11056 | break; |
| 11057 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 11058 | break; |
| 11059 | case IORING_UNREGISTER_IOWQ_AFF: |
| 11060 | ret = -EINVAL; |
| 11061 | if (arg || nr_args) |
| 11062 | break; |
| 11063 | ret = io_unregister_iowq_aff(ctx); |
| 11064 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11065 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 11066 | ret = -EINVAL; |
| 11067 | if (!arg || nr_args != 2) |
| 11068 | break; |
| 11069 | ret = io_register_iowq_max_workers(ctx, arg); |
| 11070 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11071 | default: |
| 11072 | ret = -EINVAL; |
| 11073 | break; |
| 11074 | } |
| 11075 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11076 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11077 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11078 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 11079 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11080 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11081 | return ret; |
| 11082 | } |
| 11083 | |
| 11084 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 11085 | void __user *, arg, unsigned int, nr_args) |
| 11086 | { |
| 11087 | struct io_ring_ctx *ctx; |
| 11088 | long ret = -EBADF; |
| 11089 | struct fd f; |
| 11090 | |
| 11091 | f = fdget(fd); |
| 11092 | if (!f.file) |
| 11093 | return -EBADF; |
| 11094 | |
| 11095 | ret = -EOPNOTSUPP; |
| 11096 | if (f.file->f_op != &io_uring_fops) |
| 11097 | goto out_fput; |
| 11098 | |
| 11099 | ctx = f.file->private_data; |
| 11100 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 11101 | io_run_task_work(); |
| 11102 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11103 | mutex_lock(&ctx->uring_lock); |
| 11104 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 11105 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 11106 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 11107 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11108 | out_fput: |
| 11109 | fdput(f); |
| 11110 | return ret; |
| 11111 | } |
| 11112 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11113 | static int __init io_uring_init(void) |
| 11114 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11115 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 11116 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 11117 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 11118 | } while (0) |
| 11119 | |
| 11120 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 11121 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 11122 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 11123 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 11124 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 11125 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 11126 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 11127 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 11128 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 11129 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11130 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11131 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 11132 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 11133 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 11134 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 11135 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 11136 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 11137 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11138 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 11139 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 11140 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 11141 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 11142 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 11143 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 11144 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 11145 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11146 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11147 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 11148 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11149 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11150 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11151 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 11152 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11153 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11154 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 11155 | sizeof(struct io_uring_rsrc_update)); |
| 11156 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 11157 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 11158 | |
| 11159 | /* ->buf_index is u16 */ |
| 11160 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 11161 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11162 | /* should fit into one byte */ |
| 11163 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 11164 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
| 11165 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11166 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 11167 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 11168 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11169 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 11170 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 11171 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11172 | return 0; |
| 11173 | }; |
| 11174 | __initcall(io_uring_init); |